mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Simplify refvar transformer.
This commit is contained in:
@@ -9,58 +9,54 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type refvarTransformer struct {
|
type refvarTransformer struct {
|
||||||
fieldSpecs []config.FieldSpec
|
fieldSpecs []config.FieldSpec
|
||||||
vars map[string]string
|
mappingFunc func(string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRefVarTransformer returns a Trasformer that replaces $(VAR) style variables with values.
|
// NewRefVarTransformer returns a Transformer that replaces $(VAR) style
|
||||||
func NewRefVarTransformer(vars map[string]string, p []config.FieldSpec) Transformer {
|
// variables with values.
|
||||||
|
// The fieldSpecs are the places to look for occurrences of $(VAR).
|
||||||
|
func NewRefVarTransformer(
|
||||||
|
varMap map[string]string, fs []config.FieldSpec) Transformer {
|
||||||
return &refvarTransformer{
|
return &refvarTransformer{
|
||||||
vars: vars,
|
fieldSpecs: fs,
|
||||||
fieldSpecs: p,
|
mappingFunc: expansion.MappingFuncFor(varMap),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transform determines the final values of variables:
|
// replaceVars accepts as 'in' a string, or string array, which can have
|
||||||
//
|
// embedded instances of $VAR style variables, e.g. a container command string.
|
||||||
// 1. Determine the final value of each variable:
|
// The function returns the string with the variables expanded to their final
|
||||||
// a. If the variable's Value is set, expand the `$(var)` references to other
|
// values.
|
||||||
// variables in the .Value field; the sources of variables are the declared
|
func (rv *refvarTransformer) replaceVars(in interface{}) (interface{}, error) {
|
||||||
// variables of the container and the service environment variables
|
switch vt := in.(type) {
|
||||||
// b. If a source is defined for an environment variable, resolve the source
|
case []interface{}:
|
||||||
// 2. Create the container's environment in the order variables are declared
|
var xs []string
|
||||||
// 3. Add remaining service environment vars
|
for _, a := range in.([]interface{}) {
|
||||||
func (rv *refvarTransformer) Transform(resources resmap.ResMap) error {
|
xs = append(xs, expansion.Expand(a.(string), rv.mappingFunc))
|
||||||
for resId := range resources {
|
}
|
||||||
objMap := resources[resId].Map()
|
return xs, nil
|
||||||
for _, pc := range rv.fieldSpecs {
|
case interface{}:
|
||||||
if !resId.Gvk().IsSelected(&pc.Gvk) {
|
s, ok := in.(string)
|
||||||
continue
|
if !ok {
|
||||||
}
|
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
|
||||||
err := mutateField(objMap, pc.PathSlice(), false, func(in interface{}) (interface{}, error) {
|
}
|
||||||
var (
|
return expansion.Expand(s, rv.mappingFunc), nil
|
||||||
mappingFunc = expansion.MappingFuncFor(rv.vars)
|
default:
|
||||||
)
|
return "", fmt.Errorf("invalid type encountered %T", vt)
|
||||||
switch vt := in.(type) {
|
}
|
||||||
case []interface{}:
|
}
|
||||||
var xs []string
|
|
||||||
for _, a := range in.([]interface{}) {
|
// Transform replaces $(VAR) style variables with values.
|
||||||
xs = append(xs, expansion.Expand(a.(string), mappingFunc))
|
func (rv *refvarTransformer) Transform(m resmap.ResMap) error {
|
||||||
}
|
for id, res := range m {
|
||||||
return xs, nil
|
for _, fieldSpec := range rv.fieldSpecs {
|
||||||
case interface{}:
|
if id.Gvk().IsSelected(&fieldSpec.Gvk) {
|
||||||
s, ok := in.(string)
|
if err := mutateField(
|
||||||
if !ok {
|
res.Map(), fieldSpec.PathSlice(),
|
||||||
return nil, fmt.Errorf("%#v is expected to be %T", in, s)
|
false, rv.replaceVars); err != nil {
|
||||||
}
|
return err
|
||||||
runtimeVal := expansion.Expand(s, mappingFunc)
|
|
||||||
return runtimeVal, nil
|
|
||||||
default:
|
|
||||||
return "", fmt.Errorf("invalid type encountered %T", vt)
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user