|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/deislabs/duffle/pkg/bundle" |
| 7 | + "github.com/docker/app/types/parameters" |
| 8 | + cliopts "github.com/docker/cli/opts" |
| 9 | + "github.com/pkg/errors" |
| 10 | +) |
| 11 | + |
| 12 | +type parameterOperation func(bndl *bundle.Bundle, params map[string]string) error |
| 13 | + |
| 14 | +func withFileParameters(parametersFiles []string) parameterOperation { |
| 15 | + return func(bndl *bundle.Bundle, params map[string]string) error { |
| 16 | + p, err := parameters.LoadFiles(parametersFiles) |
| 17 | + if err != nil { |
| 18 | + return err |
| 19 | + } |
| 20 | + for k, v := range p.Flatten() { |
| 21 | + params[k] = v |
| 22 | + } |
| 23 | + return nil |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func withCommandLineParameters(overrides []string) parameterOperation { |
| 28 | + return func(bndl *bundle.Bundle, params map[string]string) error { |
| 29 | + d := cliopts.ConvertKVStringsToMap(overrides) |
| 30 | + for k, v := range d { |
| 31 | + params[k] = v |
| 32 | + } |
| 33 | + return nil |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func withOrchestratorParameters(orchestrator string, kubeNamespace string) parameterOperation { |
| 38 | + return func(bndl *bundle.Bundle, params map[string]string) error { |
| 39 | + if _, ok := bndl.Parameters["docker.orchestrator"]; ok { |
| 40 | + params["docker.orchestrator"] = orchestrator |
| 41 | + } |
| 42 | + if _, ok := bndl.Parameters["docker.kubernetes-namespace"]; ok { |
| 43 | + params["docker.kubernetes-namespace"] = kubeNamespace |
| 44 | + } |
| 45 | + return nil |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func mergeBundleParameters(bndl *bundle.Bundle, ops ...parameterOperation) (map[string]interface{}, error) { |
| 50 | + userParams := map[string]string{} |
| 51 | + for _, op := range ops { |
| 52 | + if err := op(bndl, userParams); err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + } |
| 56 | + convertedParams, err := matchParametersDefinition(userParams, bndl.Parameters) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + return bundle.ValuesOrDefaults(convertedParams, bndl) |
| 61 | +} |
| 62 | + |
| 63 | +func matchParametersDefinition(parameterValues map[string]string, parameterDefinitions map[string]bundle.ParameterDefinition) (map[string]interface{}, error) { |
| 64 | + finalValues := map[string]interface{}{} |
| 65 | + for k, v := range parameterValues { |
| 66 | + definition, ok := parameterDefinitions[k] |
| 67 | + if !ok { |
| 68 | + return nil, fmt.Errorf("parameter %q is not defined in the bundle", k) |
| 69 | + } |
| 70 | + value, err := definition.ConvertValue(v) |
| 71 | + if err != nil { |
| 72 | + return nil, errors.Wrapf(err, "invalid value for parameter %q", k) |
| 73 | + } |
| 74 | + if err := definition.ValidateParameterValue(value); err != nil { |
| 75 | + return nil, errors.Wrapf(err, "invalid value for parameter %q", k) |
| 76 | + } |
| 77 | + finalValues[k] = value |
| 78 | + } |
| 79 | + return finalValues, nil |
| 80 | +} |
0 commit comments