/
parameters.go
99 lines (90 loc) · 2.88 KB
/
parameters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package commands
import (
"fmt"
"github.com/deislabs/cnab-go/bundle"
"github.com/docker/app/internal"
"github.com/docker/app/internal/store"
"github.com/docker/app/types/parameters"
cliopts "github.com/docker/cli/opts"
"github.com/pkg/errors"
)
type mergeBundleOpt func(bndl *bundle.Bundle, params map[string]string) error
func withFileParameters(parametersFiles []string) mergeBundleOpt {
return func(bndl *bundle.Bundle, params map[string]string) error {
p, err := parameters.LoadFiles(parametersFiles)
if err != nil {
return err
}
for k, v := range p.Flatten() {
params[k] = v
}
return nil
}
}
func withCommandLineParameters(overrides []string) mergeBundleOpt {
return func(bndl *bundle.Bundle, params map[string]string) error {
d := cliopts.ConvertKVStringsToMap(overrides)
for k, v := range d {
params[k] = v
}
return nil
}
}
func withSendRegistryAuth(sendRegistryAuth bool) mergeBundleOpt {
return func(bndl *bundle.Bundle, params map[string]string) error {
if _, ok := bndl.Parameters[internal.ParameterShareRegistryCredsName]; ok {
val := "false"
if sendRegistryAuth {
val = "true"
}
params[internal.ParameterShareRegistryCredsName] = val
}
return nil
}
}
func withOrchestratorParameters(orchestrator string, kubeNamespace string) mergeBundleOpt {
return func(bndl *bundle.Bundle, params map[string]string) error {
if _, ok := bndl.Parameters[internal.ParameterOrchestratorName]; ok {
params[internal.ParameterOrchestratorName] = orchestrator
}
if _, ok := bndl.Parameters[internal.ParameterKubernetesNamespaceName]; ok {
params[internal.ParameterKubernetesNamespaceName] = kubeNamespace
}
return nil
}
}
func mergeBundleParameters(installation *store.Installation, ops ...mergeBundleOpt) error {
bndl := installation.Bundle
if installation.Parameters == nil {
installation.Parameters = make(map[string]interface{})
}
userParams := map[string]string{}
for _, op := range ops {
if err := op(bndl, userParams); err != nil {
return err
}
}
if err := matchAndMergeParametersDefinition(installation.Parameters, userParams, bndl.Parameters); err != nil {
return err
}
var err error
installation.Parameters, err = bundle.ValuesOrDefaults(installation.Parameters, bndl)
return err
}
func matchAndMergeParametersDefinition(currentValues map[string]interface{}, parameterValues map[string]string, parameterDefinitions map[string]bundle.ParameterDefinition) error {
for k, v := range parameterValues {
definition, ok := parameterDefinitions[k]
if !ok {
return fmt.Errorf("parameter %q is not defined in the bundle", k)
}
value, err := definition.ConvertValue(v)
if err != nil {
return errors.Wrapf(err, "invalid value for parameter %q", k)
}
if err := definition.ValidateParameterValue(value); err != nil {
return errors.Wrapf(err, "invalid value for parameter %q", k)
}
currentValues[k] = value
}
return nil
}