mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 09:51:23 +00:00
Localize patch, ReplacementTransformer (#4943)
* Localize PatchStrategicMergeTransformer, ReplacementTransformer * Improve readability
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
package localizer
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/filters/fsslice"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
||||
@@ -19,36 +20,62 @@ import (
|
||||
// Note that this excludes helm, which needs a repo.
|
||||
type localizeBuiltinPlugins struct {
|
||||
lc *localizer
|
||||
|
||||
// locPathFn is used by localizeNode to set the localized path on the plugin.
|
||||
locPathFn func(string) (string, error)
|
||||
}
|
||||
|
||||
var _ kio.Filter = &localizeBuiltinPlugins{}
|
||||
|
||||
// Filter localizes the built-in plugins with file paths.
|
||||
func (lbp *localizeBuiltinPlugins) Filter(plugins []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
localizedPlugins, err := kio.FilterAll(fsslice.Filter{
|
||||
FsSlice: types.FsSlice{
|
||||
types.FieldSpec{
|
||||
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchTransformer.String()},
|
||||
Path: "path",
|
||||
for _, singlePlugin := range plugins {
|
||||
err := singlePlugin.PipeE(fsslice.Filter{
|
||||
FsSlice: types.FsSlice{
|
||||
types.FieldSpec{
|
||||
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchTransformer.String()},
|
||||
Path: "path",
|
||||
},
|
||||
types.FieldSpec{
|
||||
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchJson6902Transformer.String()},
|
||||
Path: "path",
|
||||
},
|
||||
types.FieldSpec{
|
||||
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.ReplacementTransformer.String()},
|
||||
Path: "replacements/path",
|
||||
},
|
||||
},
|
||||
types.FieldSpec{
|
||||
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchJson6902Transformer.String()},
|
||||
Path: "path",
|
||||
SetValue: func(node *yaml.RNode) error {
|
||||
lbp.locPathFn = lbp.lc.localizeFile
|
||||
return lbp.localizeNode(node)
|
||||
},
|
||||
},
|
||||
SetValue: lbp.localizeNode,
|
||||
}).Filter(plugins)
|
||||
|
||||
// TODO(annasong): localize ReplacementTransformer, PatchStrategicMergeTransformer, ConfigMapGenerator, SecretGenerator
|
||||
|
||||
return localizedPlugins, errors.Wrap(err)
|
||||
}
|
||||
|
||||
// localizeNode sets the scalar node to its value localized as a file path.
|
||||
func (lbp *localizeBuiltinPlugins) localizeNode(node *yaml.RNode) error {
|
||||
localizedPath, err := lbp.lc.localizeFile(node.YNode().Value)
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "unable to localize built-in plugin path")
|
||||
}, fieldspec.Filter{
|
||||
FieldSpec: types.FieldSpec{
|
||||
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchStrategicMergeTransformer.String()},
|
||||
Path: "paths",
|
||||
},
|
||||
SetValue: func(node *yaml.RNode) error {
|
||||
lbp.locPathFn = lbp.lc.localizeK8sResource
|
||||
return errors.Wrap(node.VisitElements(lbp.localizeNode))
|
||||
},
|
||||
})
|
||||
// TODO(annasong): localize ConfigMapGenerator, SecretGenerator,
|
||||
// HelmChartInflationGenerator
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
}
|
||||
return filtersutil.SetScalar(localizedPath)(node)
|
||||
return plugins, nil
|
||||
}
|
||||
|
||||
// localizeNode sets the scalar node to its value localized by locPathFn.
|
||||
func (lbp *localizeBuiltinPlugins) localizeNode(node *yaml.RNode) error {
|
||||
localizedPath, err := lbp.locPathFn(node.YNode().Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if localizedPath != "" {
|
||||
err = filtersutil.SetScalar(localizedPath)(node)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user