Fix load order

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-10-04 16:51:48 -04:00
parent 18ddec447a
commit af8f563922
5 changed files with 209 additions and 148 deletions

View File

@ -2,7 +2,6 @@ package interpolation
import ( import (
"os" "os"
"strings" "strings"
"github.com/docker/cli/cli/compose/template" "github.com/docker/cli/cli/compose/template"
@ -11,8 +10,6 @@ import (
// Options supported by Interpolate // Options supported by Interpolate
type Options struct { type Options struct {
// SectionName of the configuration section
SectionName string
// LookupValue from a key // LookupValue from a key
LookupValue LookupValue LookupValue LookupValue
// TypeCastMapping maps key paths to functions to cast to a type // TypeCastMapping maps key paths to functions to cast to a type
@ -30,8 +27,6 @@ type Cast func(value string) (interface{}, error)
// Interpolate replaces variables in a string with the values from a mapping // Interpolate replaces variables in a string with the values from a mapping
func Interpolate(config map[string]interface{}, opts Options) (map[string]interface{}, error) { func Interpolate(config map[string]interface{}, opts Options) (map[string]interface{}, error) {
out := map[string]interface{}{}
if opts.LookupValue == nil { if opts.LookupValue == nil {
opts.LookupValue = os.LookupEnv opts.LookupValue = os.LookupEnv
} }
@ -39,43 +34,12 @@ func Interpolate(config map[string]interface{}, opts Options) (map[string]interf
opts.TypeCastMapping = make(map[Path]Cast) opts.TypeCastMapping = make(map[Path]Cast)
} }
for key, item := range config {
if item == nil {
out[key] = nil
continue
}
mapItem, ok := item.(map[string]interface{})
if !ok {
return nil, errors.Errorf("Invalid type for %s : %T instead of %T", key, item, out)
}
interpolatedItem, err := interpolateSectionItem(NewPath(key), mapItem, opts)
if err != nil {
return nil, err
}
out[key] = interpolatedItem
}
return out, nil
}
func interpolateSectionItem(
path Path,
item map[string]interface{},
opts Options,
) (map[string]interface{}, error) {
out := map[string]interface{}{} out := map[string]interface{}{}
for key, value := range item { for key, value := range config {
interpolatedValue, err := recursiveInterpolate(value, path.Next(key), opts) interpolatedValue, err := recursiveInterpolate(value, NewPath(key), opts)
switch err := err.(type) { if err != nil {
case nil: return out, err
case *template.InvalidTemplateError:
return nil, errors.Errorf(
"Invalid interpolation format for %#v option in %s %#v: %#v. You may need to escape any $ with another $.",
key, opts.SectionName, path.root(), err.Template,
)
default:
return nil, errors.Wrapf(err, "error while interpolating %s in %s %s", key, opts.SectionName, path.root())
} }
out[key] = interpolatedValue out[key] = interpolatedValue
} }
@ -89,13 +53,14 @@ func recursiveInterpolate(value interface{}, path Path, opts Options) (interface
case string: case string:
newValue, err := template.Substitute(value, template.Mapping(opts.LookupValue)) newValue, err := template.Substitute(value, template.Mapping(opts.LookupValue))
if err != nil || newValue == value { if err != nil || newValue == value {
return value, err return value, newPathError(path, err)
} }
caster, ok := opts.getCasterForPath(path) caster, ok := opts.getCasterForPath(path)
if !ok { if !ok {
return newValue, nil return newValue, nil
} }
return caster(newValue) casted, err := caster(newValue)
return casted, newPathError(path, errors.Wrap(err, "failed to cast to expected type"))
case map[string]interface{}: case map[string]interface{}:
out := map[string]interface{}{} out := map[string]interface{}{}
@ -111,7 +76,7 @@ func recursiveInterpolate(value interface{}, path Path, opts Options) (interface
case []interface{}: case []interface{}:
out := make([]interface{}, len(value)) out := make([]interface{}, len(value))
for i, elem := range value { for i, elem := range value {
interpolatedElem, err := recursiveInterpolate(elem, path, opts) interpolatedElem, err := recursiveInterpolate(elem, path.Next(PathMatchList), opts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -125,12 +90,28 @@ func recursiveInterpolate(value interface{}, path Path, opts Options) (interface
} }
} }
func newPathError(path Path, err error) error {
switch err := err.(type) {
case nil:
return nil
case *template.InvalidTemplateError:
return errors.Errorf(
"invalid interpolation format for %s: %#v. You may need to escape any $ with another $.",
path, err.Template)
default:
return errors.Wrapf(err, "error while interpolating %s", path)
}
}
const pathSeparator = "." const pathSeparator = "."
// PathMatchAll is a token used as part of a Path to match any key at that level // PathMatchAll is a token used as part of a Path to match any key at that level
// in the nested structure // in the nested structure
const PathMatchAll = "*" const PathMatchAll = "*"
// PathMatchList is a token used as part of a Path to match items in a list
const PathMatchList = "[]"
// Path is a dotted path of keys to a value in a nested mapping structure. A * // Path is a dotted path of keys to a value in a nested mapping structure. A *
// section in a path will match any key in the mapping structure. // section in a path will match any key in the mapping structure.
type Path string type Path string
@ -145,14 +126,6 @@ func (p Path) Next(part string) Path {
return Path(string(p) + pathSeparator + part) return Path(string(p) + pathSeparator + part)
} }
func (p Path) root() string {
parts := p.parts()
if len(parts) == 0 {
return ""
}
return parts[0]
}
func (p Path) parts() []string { func (p Path) parts() []string {
return strings.Split(string(p), pathSeparator) return strings.Split(string(p), pathSeparator)
} }

View File

@ -45,10 +45,7 @@ func TestInterpolate(t *testing.T) {
}, },
}, },
} }
result, err := Interpolate(services, Options{ result, err := Interpolate(services, Options{LookupValue: defaultMapping})
SectionName: "service",
LookupValue: defaultMapping,
})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, result) assert.Equal(t, expected, result)
} }
@ -59,11 +56,8 @@ func TestInvalidInterpolation(t *testing.T) {
"image": "${", "image": "${",
}, },
} }
_, err := Interpolate(services, Options{ _, err := Interpolate(services, Options{LookupValue: defaultMapping})
SectionName: "service", assert.EqualError(t, err, `invalid interpolation format for servicea.image: "${". You may need to escape any $ with another $.`)
LookupValue: defaultMapping,
})
assert.EqualError(t, err, `Invalid interpolation format for "image" option in service "servicea": "${". You may need to escape any $ with another $.`)
} }
func TestInterpolateWithDefaults(t *testing.T) { func TestInterpolateWithDefaults(t *testing.T) {
@ -95,7 +89,7 @@ func TestInterpolateWithCast(t *testing.T) {
} }
result, err := Interpolate(config, Options{ result, err := Interpolate(config, Options{
LookupValue: defaultMapping, LookupValue: defaultMapping,
TypeCastMapping: map[Path]Cast{NewPath("*", "replicas"): toInt}, TypeCastMapping: map[Path]Cast{NewPath(PathMatchAll, "replicas"): toInt},
}) })
assert.NoError(t, err) assert.NoError(t, err)
expected := map[string]interface{}{ expected := map[string]interface{}{

View File

@ -8,46 +8,40 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
var interpolateTypeCastMapping = map[string]map[interp.Path]interp.Cast{ var interpolateTypeCastMapping = map[interp.Path]interp.Cast{
"services": { servicePath("configs", interp.PathMatchList, "mode"): toInt,
iPath("configs", "mode"): toInt, servicePath("secrets", interp.PathMatchList, "mode"): toInt,
iPath("secrets", "mode"): toInt, servicePath("healthcheck", "retries"): toInt,
iPath("healthcheck", "retries"): toInt, servicePath("healthcheck", "disable"): toBoolean,
iPath("healthcheck", "disable"): toBoolean, servicePath("deploy", "replicas"): toInt,
iPath("deploy", "replicas"): toInt, servicePath("deploy", "update_config", "parallelism"): toInt,
iPath("deploy", "update_config", "parallelism:"): toInt, servicePath("deploy", "update_config", "max_failure_ratio"): toFloat,
iPath("deploy", "update_config", "max_failure_ratio"): toFloat, servicePath("deploy", "restart_policy", "max_attempts"): toInt,
iPath("deploy", "restart_policy", "max_attempts"): toInt, servicePath("ports", interp.PathMatchList, "target"): toInt,
iPath("ports", "target"): toInt, servicePath("ports", interp.PathMatchList, "published"): toInt,
iPath("ports", "published"): toInt, servicePath("ulimits", interp.PathMatchAll): toInt,
iPath("ulimits", interp.PathMatchAll): toInt, servicePath("ulimits", interp.PathMatchAll, "hard"): toInt,
iPath("ulimits", interp.PathMatchAll, "hard"): toInt, servicePath("ulimits", interp.PathMatchAll, "soft"): toInt,
iPath("ulimits", interp.PathMatchAll, "soft"): toInt, servicePath("privileged"): toBoolean,
iPath("privileged"): toBoolean, servicePath("read_only"): toBoolean,
iPath("read_only"): toBoolean, servicePath("stdin_open"): toBoolean,
iPath("stdin_open"): toBoolean, servicePath("tty"): toBoolean,
iPath("tty"): toBoolean, servicePath("volumes", interp.PathMatchList, "read_only"): toBoolean,
iPath("volumes", "read_only"): toBoolean, servicePath("volumes", interp.PathMatchList, "volume", "nocopy"): toBoolean,
iPath("volumes", "volume", "nocopy"): toBoolean, iPath("networks", interp.PathMatchAll, "external"): toBoolean,
}, iPath("networks", interp.PathMatchAll, "internal"): toBoolean,
"networks": { iPath("networks", interp.PathMatchAll, "attachable"): toBoolean,
iPath("external"): toBoolean, iPath("volumes", interp.PathMatchAll, "external"): toBoolean,
iPath("internal"): toBoolean, iPath("secrets", interp.PathMatchAll, "external"): toBoolean,
iPath("attachable"): toBoolean, iPath("configs", interp.PathMatchAll, "external"): toBoolean,
},
"volumes": {
iPath("external"): toBoolean,
},
"secrets": {
iPath("external"): toBoolean,
},
"configs": {
iPath("external"): toBoolean,
},
} }
func iPath(parts ...string) interp.Path { func iPath(parts ...string) interp.Path {
return interp.NewPath(append([]string{interp.PathMatchAll}, parts...)...) return interp.NewPath(parts...)
}
func servicePath(parts ...string) interp.Path {
return iPath(append([]string{"services", interp.PathMatchAll}, parts...)...)
} }
func toInt(value string) (interface{}, error) { func toInt(value string) (interface{}, error) {
@ -70,26 +64,11 @@ func toBoolean(value string) (interface{}, error) {
} }
} }
func interpolateConfig(configDict map[string]interface{}, lookupEnv interp.LookupValue) (map[string]map[string]interface{}, error) { func interpolateConfig(configDict map[string]interface{}, lookupEnv interp.LookupValue) (map[string]interface{}, error) {
config := make(map[string]map[string]interface{}) return interp.Interpolate(
configDict,
for _, key := range []string{"services", "networks", "volumes", "secrets", "configs"} {
section, ok := configDict[key]
if !ok {
config[key] = make(map[string]interface{})
continue
}
var err error
config[key], err = interp.Interpolate(
section.(map[string]interface{}),
interp.Options{ interp.Options{
SectionName: key,
LookupValue: lookupEnv, LookupValue: lookupEnv,
TypeCastMapping: interpolateTypeCastMapping[key], TypeCastMapping: interpolateTypeCastMapping,
}) })
if err != nil {
return nil, err
}
}
return config, nil
} }

View File

@ -54,7 +54,8 @@ func Load(configDetails types.ConfigDetails) (*types.Config, error) {
return nil, err return nil, err
} }
config, err := interpolateConfig(configDict, configDetails.LookupEnv) var err error
configDict, err = interpolateConfig(configDict, configDetails.LookupEnv)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -62,30 +63,7 @@ func Load(configDetails types.ConfigDetails) (*types.Config, error) {
if err := schema.Validate(configDict, schema.Version(configDict)); err != nil { if err := schema.Validate(configDict, schema.Version(configDict)); err != nil {
return nil, err return nil, err
} }
return loadSections(configDict, configDetails)
cfg := types.Config{}
cfg.Services, err = LoadServices(config["services"], configDetails.WorkingDir, configDetails.LookupEnv)
if err != nil {
return nil, err
}
cfg.Networks, err = LoadNetworks(config["networks"])
if err != nil {
return nil, err
}
cfg.Volumes, err = LoadVolumes(config["volumes"])
if err != nil {
return nil, err
}
cfg.Secrets, err = LoadSecrets(config["secrets"], configDetails.WorkingDir)
if err != nil {
return nil, err
}
cfg.Configs, err = LoadConfigObjs(config["configs"], configDetails.WorkingDir)
return &cfg, err
} }
func validateForbidden(configDict map[string]interface{}) error { func validateForbidden(configDict map[string]interface{}) error {
@ -100,6 +78,66 @@ func validateForbidden(configDict map[string]interface{}) error {
return nil return nil
} }
func loadSections(config map[string]interface{}, configDetails types.ConfigDetails) (*types.Config, error) {
var err error
cfg := types.Config{}
var loaders = []struct {
key string
fnc func(config map[string]interface{}) error
}{
{
key: "services",
fnc: func(config map[string]interface{}) error {
cfg.Services, err = LoadServices(config, configDetails.WorkingDir, configDetails.LookupEnv)
return err
},
},
{
key: "networks",
fnc: func(config map[string]interface{}) error {
cfg.Networks, err = LoadNetworks(config)
return err
},
},
{
key: "volumes",
fnc: func(config map[string]interface{}) error {
cfg.Volumes, err = LoadVolumes(config)
return err
},
},
{
key: "secrets",
fnc: func(config map[string]interface{}) error {
cfg.Secrets, err = LoadSecrets(config, configDetails.WorkingDir)
return err
},
},
{
key: "configs",
fnc: func(config map[string]interface{}) error {
cfg.Configs, err = LoadConfigObjs(config, configDetails.WorkingDir)
return err
},
},
}
for _, loader := range loaders {
if err := loader.fnc(getSection(config, loader.key)); err != nil {
return nil, err
}
}
return &cfg, nil
}
func getSection(config map[string]interface{}, key string) map[string]interface{} {
section, ok := config[key]
if !ok {
return make(map[string]interface{})
}
return section.(map[string]interface{})
}
// GetUnsupportedProperties returns the list of any unsupported properties that are // GetUnsupportedProperties returns the list of any unsupported properties that are
// used in the Compose files. // used in the Compose files.
func GetUnsupportedProperties(configDetails types.ConfigDetails) []string { func GetUnsupportedProperties(configDetails types.ConfigDetails) []string {

View File

@ -529,8 +529,9 @@ services:
- target: $theint - target: $theint
published: $theint published: $theint
ulimits: ulimits:
- $theint nproc: $theint
- hard: $theint nofile:
hard: $theint
soft: $theint soft: $theint
privileged: $thebool privileged: $thebool
read_only: $thebool read_only: $thebool
@ -538,6 +539,7 @@ services:
tty: $thebool tty: $thebool
volumes: volumes:
- source: data - source: data
type: volume
read_only: $thebool read_only: $thebool
volume: volume:
nocopy: $thebool nocopy: $thebool
@ -567,7 +569,78 @@ networks:
config, err := Load(buildConfigDetails(dict, env)) config, err := Load(buildConfigDetails(dict, env))
require.NoError(t, err) require.NoError(t, err)
expected := &types.Config{} expected := &types.Config{
Services: []types.ServiceConfig{
{
Name: "web",
Configs: []types.ServiceConfigObjConfig{
{
Source: "appconfig",
Mode: uint32Ptr(555),
},
},
Secrets: []types.ServiceSecretConfig{
{
Source: "super",
Mode: uint32Ptr(555),
},
},
HealthCheck: &types.HealthCheckConfig{
Retries: uint64Ptr(555),
Disable: true,
},
Deploy: types.DeployConfig{
Replicas: uint64Ptr(555),
UpdateConfig: &types.UpdateConfig{
Parallelism: uint64Ptr(555),
MaxFailureRatio: 3.14,
},
RestartPolicy: &types.RestartPolicy{
MaxAttempts: uint64Ptr(555),
},
},
Ports: []types.ServicePortConfig{
{Target: 555, Mode: "ingress", Protocol: "tcp"},
{Target: 34567, Mode: "ingress", Protocol: "tcp"},
{Target: 555, Published: 555},
},
Ulimits: map[string]*types.UlimitsConfig{
"nproc": {Single: 555},
"nofile": {Hard: 555, Soft: 555},
},
Privileged: true,
ReadOnly: true,
StdinOpen: true,
Tty: true,
Volumes: []types.ServiceVolumeConfig{
{
Source: "data",
Type: "volume",
ReadOnly: true,
Volume: &types.ServiceVolumeVolume{NoCopy: true},
},
},
Environment: types.MappingWithEquals{},
},
},
Configs: map[string]types.ConfigObjConfig{
"appconfig": {External: types.External{External: true, Name: "appconfig"}},
},
Secrets: map[string]types.SecretConfig{
"super": {External: types.External{External: true, Name: "super"}},
},
Volumes: map[string]types.VolumeConfig{
"data": {External: types.External{External: true, Name: "data"}},
},
Networks: map[string]types.NetworkConfig{
"front": {
External: types.External{External: true, Name: "front"},
Internal: true,
Attachable: true,
},
},
}
assert.Equal(t, expected, config) assert.Equal(t, expected, config)
} }
@ -748,6 +821,10 @@ func uint64Ptr(value uint64) *uint64 {
return &value return &value
} }
func uint32Ptr(value uint32) *uint32 {
return &value
}
func TestFullExample(t *testing.T) { func TestFullExample(t *testing.T) {
bytes, err := ioutil.ReadFile("full-example.yml") bytes, err := ioutil.ReadFile("full-example.yml")
require.NoError(t, err) require.NoError(t, err)