Skip to content

Commit 54d6cf7

Browse files
committed
Simplify refvar transformer.
1 parent e487e49 commit 54d6cf7

File tree

1 file changed

+41
-45
lines changed

1 file changed

+41
-45
lines changed

pkg/transformers/refvars.go

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,54 @@ import (
99
)
1010

1111
type refvarTransformer struct {
12-
fieldSpecs []config.FieldSpec
13-
vars map[string]string
12+
fieldSpecs []config.FieldSpec
13+
mappingFunc func(string) string
1414
}
1515

16-
// NewRefVarTransformer returns a Trasformer that replaces $(VAR) style variables with values.
17-
func NewRefVarTransformer(vars map[string]string, p []config.FieldSpec) Transformer {
16+
// NewRefVarTransformer returns a Transformer that replaces $(VAR) style
17+
// variables with values.
18+
// The fieldSpecs are the places to look for occurrences of $(VAR).
19+
func NewRefVarTransformer(
20+
varMap map[string]string, fs []config.FieldSpec) Transformer {
1821
return &refvarTransformer{
19-
vars: vars,
20-
fieldSpecs: p,
22+
fieldSpecs: fs,
23+
mappingFunc: expansion.MappingFuncFor(varMap),
2124
}
2225
}
2326

24-
// Transform determines the final values of variables:
25-
//
26-
// 1. Determine the final value of each variable:
27-
// a. If the variable's Value is set, expand the `$(var)` references to other
28-
// variables in the .Value field; the sources of variables are the declared
29-
// variables of the container and the service environment variables
30-
// b. If a source is defined for an environment variable, resolve the source
31-
// 2. Create the container's environment in the order variables are declared
32-
// 3. Add remaining service environment vars
33-
func (rv *refvarTransformer) Transform(resources resmap.ResMap) error {
34-
for resId := range resources {
35-
objMap := resources[resId].Map()
36-
for _, pc := range rv.fieldSpecs {
37-
if !resId.Gvk().IsSelected(&pc.Gvk) {
38-
continue
39-
}
40-
err := mutateField(objMap, pc.PathSlice(), false, func(in interface{}) (interface{}, error) {
41-
var (
42-
mappingFunc = expansion.MappingFuncFor(rv.vars)
43-
)
44-
switch vt := in.(type) {
45-
case []interface{}:
46-
var xs []string
47-
for _, a := range in.([]interface{}) {
48-
xs = append(xs, expansion.Expand(a.(string), mappingFunc))
49-
}
50-
return xs, nil
51-
case interface{}:
52-
s, ok := in.(string)
53-
if !ok {
54-
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
55-
}
56-
runtimeVal := expansion.Expand(s, mappingFunc)
57-
return runtimeVal, nil
58-
default:
59-
return "", fmt.Errorf("invalid type encountered %T", vt)
27+
// replaceVars accepts as 'in' a string, or string array, which can have
28+
// embedded instances of $VAR style variables, e.g. a container command string.
29+
// The function returns the string with the variables expanded to their final
30+
// values.
31+
func (rv *refvarTransformer) replaceVars(in interface{}) (interface{}, error) {
32+
switch vt := in.(type) {
33+
case []interface{}:
34+
var xs []string
35+
for _, a := range in.([]interface{}) {
36+
xs = append(xs, expansion.Expand(a.(string), rv.mappingFunc))
37+
}
38+
return xs, nil
39+
case interface{}:
40+
s, ok := in.(string)
41+
if !ok {
42+
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
43+
}
44+
return expansion.Expand(s, rv.mappingFunc), nil
45+
default:
46+
return "", fmt.Errorf("invalid type encountered %T", vt)
47+
}
48+
}
49+
50+
// Transform replaces $(VAR) style variables with values.
51+
func (rv *refvarTransformer) Transform(m resmap.ResMap) error {
52+
for id, res := range m {
53+
for _, fieldSpec := range rv.fieldSpecs {
54+
if id.Gvk().IsSelected(&fieldSpec.Gvk) {
55+
if err := mutateField(
56+
res.Map(), fieldSpec.PathSlice(),
57+
false, rv.replaceVars); err != nil {
58+
return err
6059
}
61-
})
62-
if err != nil {
63-
return err
6460
}
6561
}
6662
}

0 commit comments

Comments
 (0)