| 1 |
// Copyright 2022 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package localizer |
| 5 |
|
| 6 |
import ( |
| 7 |
"sigs.k8s.io/kustomize/api/filters/fieldspec" |
| 8 |
"sigs.k8s.io/kustomize/api/filters/filtersutil" |
| 9 |
"sigs.k8s.io/kustomize/api/filters/fsslice" |
| 10 |
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers" |
| 11 |
"sigs.k8s.io/kustomize/api/konfig" |
| 12 |
"sigs.k8s.io/kustomize/api/types" |
| 13 |
"sigs.k8s.io/kustomize/kyaml/errors" |
| 14 |
"sigs.k8s.io/kustomize/kyaml/kio" |
| 15 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 16 |
"sigs.k8s.io/kustomize/kyaml/yaml" |
| 17 |
) |
| 18 |
|
| 19 |
// localizeBuiltinPlugins localizes built-in plugins with file paths. |
| 20 |
// Note that this excludes helm, which needs a repo. |
| 21 |
type localizeBuiltinPlugins struct { |
| 22 |
lc *localizer |
| 23 |
|
| 24 |
// locPathFn is used by localizeNode to set the localized path on the plugin. |
| 25 |
locPathFn func(string) (string, error) |
| 26 |
} |
| 27 |
|
| 28 |
var _ kio.Filter = &localizeBuiltinPlugins{} |
| 29 |
|
| 30 |
// Filter localizes the built-in plugins with file paths. |
| 31 |
func (lbp *localizeBuiltinPlugins) Filter(plugins []*yaml.RNode) ([]*yaml.RNode, error) { |
| 32 |
for _, singlePlugin := range plugins { |
| 33 |
err := singlePlugin.PipeE(fsslice.Filter{ |
| 34 |
FsSlice: types.FsSlice{ |
| 35 |
types.FieldSpec{ |
| 36 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ConfigMapGenerator.String()}, |
| 37 |
Path: "env", |
| 38 |
}, |
| 39 |
types.FieldSpec{ |
| 40 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ConfigMapGenerator.String()}, |
| 41 |
Path: "envs", |
| 42 |
}, |
| 43 |
types.FieldSpec{ |
| 44 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.SecretGenerator.String()}, |
| 45 |
Path: "env", |
| 46 |
}, |
| 47 |
types.FieldSpec{ |
| 48 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.SecretGenerator.String()}, |
| 49 |
Path: "envs", |
| 50 |
}, |
| 51 |
types.FieldSpec{ |
| 52 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.HelmChartInflationGenerator.String()}, |
| 53 |
Path: "valuesFile", |
| 54 |
}, |
| 55 |
types.FieldSpec{ |
| 56 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.HelmChartInflationGenerator.String()}, |
| 57 |
Path: "additionalValuesFiles", |
| 58 |
}, |
| 59 |
types.FieldSpec{ |
| 60 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchTransformer.String()}, |
| 61 |
Path: "path", |
| 62 |
}, |
| 63 |
types.FieldSpec{ |
| 64 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchJson6902Transformer.String()}, |
| 65 |
Path: "path", |
| 66 |
}, |
| 67 |
types.FieldSpec{ |
| 68 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ReplacementTransformer.String()}, |
| 69 |
Path: "replacements/path", |
| 70 |
}, |
| 71 |
}, |
| 72 |
SetValue: func(node *yaml.RNode) error { |
| 73 |
lbp.locPathFn = lbp.lc.localizeFile |
| 74 |
return lbp.localizeAll(node) |
| 75 |
}, |
| 76 |
}, |
| 77 |
fsslice.Filter{ |
| 78 |
FsSlice: types.FsSlice{ |
| 79 |
types.FieldSpec{ |
| 80 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ConfigMapGenerator.String()}, |
| 81 |
Path: "files", |
| 82 |
}, |
| 83 |
types.FieldSpec{ |
| 84 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.SecretGenerator.String()}, |
| 85 |
Path: "files", |
| 86 |
}, |
| 87 |
}, |
| 88 |
SetValue: func(node *yaml.RNode) error { |
| 89 |
lbp.locPathFn = lbp.lc.localizeFileSource |
| 90 |
return lbp.localizeAll(node) |
| 91 |
}, |
| 92 |
}, |
| 93 |
yaml.FilterFunc(func(node *yaml.RNode) (*yaml.RNode, error) { |
| 94 |
isHelm := node.GetApiVersion() == konfig.BuiltinPluginApiVersion && |
| 95 |
node.GetKind() == builtinhelpers.HelmChartInflationGenerator.String() |
| 96 |
if !isHelm { |
| 97 |
return node, nil |
| 98 |
} |
| 99 |
home, err := node.Pipe(yaml.Lookup("chartHome")) |
| 100 |
if err != nil { |
| 101 |
return nil, errors.Wrap(err) |
| 102 |
} |
| 103 |
if home == nil { |
| 104 |
_, err = lbp.lc.copyChartHomeEntry("") |
| 105 |
} else { |
| 106 |
lbp.locPathFn = lbp.lc.copyChartHomeEntry |
| 107 |
err = lbp.localizeScalar(home) |
| 108 |
} |
| 109 |
return node, errors.WrapPrefixf(err, "plugin %s", resid.FromRNode(node)) |
| 110 |
}), |
| 111 |
fieldspec.Filter{ |
| 112 |
FieldSpec: types.FieldSpec{ |
| 113 |
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchStrategicMergeTransformer.String()}, |
| 114 |
Path: "paths", |
| 115 |
}, |
| 116 |
SetValue: func(node *yaml.RNode) error { |
| 117 |
lbp.locPathFn = lbp.lc.localizeK8sResource |
| 118 |
return lbp.localizeAll(node) |
| 119 |
}, |
| 120 |
}) |
| 121 |
if err != nil { |
| 122 |
return nil, errors.Wrap(err) |
| 123 |
} |
| 124 |
} |
| 125 |
return plugins, nil |
| 126 |
} |
| 127 |
|
| 128 |
// localizeAll sets each entry in node to its value localized by locPathFn. |
| 129 |
// Node is a sequence or scalar value. |
| 130 |
func (lbp *localizeBuiltinPlugins) localizeAll(node *yaml.RNode) error { |
| 131 |
// We rely on the build command to throw errors for nodes in |
| 132 |
// built-in plugins that are sequences when expected to be scalar, |
| 133 |
// and vice versa. |
| 134 |
//nolint: exhaustive |
| 135 |
switch node.YNode().Kind { |
| 136 |
case yaml.SequenceNode: |
| 137 |
return errors.Wrap(node.VisitElements(lbp.localizeScalar)) |
| 138 |
case yaml.ScalarNode: |
| 139 |
return lbp.localizeScalar(node) |
| 140 |
default: |
| 141 |
return errors.Errorf("expected sequence or scalar node") |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// localizeScalar sets the scalar node to its value localized by locPathFn. |
| 146 |
func (lbp *localizeBuiltinPlugins) localizeScalar(node *yaml.RNode) error { |
| 147 |
localizedPath, err := lbp.locPathFn(node.YNode().Value) |
| 148 |
if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
return filtersutil.SetScalar(localizedPath)(node) |
| 152 |
} |