Skip to content

Commit

Permalink
Localize PatchTransformer, PatchJson6902Transformer (#4920)
Browse files Browse the repository at this point in the history
* Localize patches, patchesJson6902 custom transformers

* Improve readability
  • Loading branch information
annasong20 committed Dec 16, 2022
1 parent ef60d5f commit a1bfab3
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 90 deletions.
60 changes: 38 additions & 22 deletions api/internal/localizer/builtinplugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,51 @@
package localizer

import (
"sigs.k8s.io/kustomize/api/filters/filtersutil"
"sigs.k8s.io/kustomize/api/filters/fsslice"
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/resid"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

// localizeBuiltinGenerators localizes built-in generators with file paths.
// localizeBuiltinPlugins localizes built-in plugins with file paths.
// Note that this excludes helm, which needs a repo.
type localizeBuiltinGenerators struct {
type localizeBuiltinPlugins struct {
lc *localizer
}

var _ kio.Filter = &localizeBuiltinGenerators{}

// Filter localizes the built-in generators with file paths. Filter returns an error if
// generators contains a resource that is not a built-in generator, cannot contain a file path,
// needs more than a file path like helm, or is not localizable.
// TODO(annasong): implement
func (lbg *localizeBuiltinGenerators) Filter(generators []*yaml.RNode) ([]*yaml.RNode, error) {
return generators, nil
}

// localizeBuiltinTransformers localizes built-in transformers with file paths.
type localizeBuiltinTransformers struct {
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",
},
types.FieldSpec{
Gvk: resid.Gvk{Version: konfig.BuiltinPluginApiVersion, Kind: builtinhelpers.PatchJson6902Transformer.String()},
Path: "path",
},
},
SetValue: lbp.localizeNode,
}).Filter(plugins)

// TODO(annasong): localize ReplacementTransformer, PatchStrategicMergeTransformer, ConfigMapGenerator, SecretGenerator

return localizedPlugins, errors.Wrap(err)
}

var _ kio.Filter = &localizeBuiltinTransformers{}

// Filter localizes the built-in transformers with file paths. Filter returns an error if
// transformers contains a resource that is not a built-in transformer, cannot contain a file path,
// or is not localizable.
// TODO(annasong): implement
func (lbt *localizeBuiltinTransformers) Filter(transformers []*yaml.RNode) ([]*yaml.RNode, error) {
return transformers, nil
// 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")
}
return filtersutil.SetScalar(localizedPath)(node)
}
45 changes: 19 additions & 26 deletions api/internal/localizer/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -224,7 +223,11 @@ func (lc *localizer) localizeFile(path string) (string, error) {
if err != nil {
return "", errors.Wrap(err)
}
return lc.localizeFileWithContent(path, content)
}

// localizeFileWithContent writes content to the localized file path and returns the localized path.
func (lc *localizer) localizeFileWithContent(path string, content []byte) (string, error) {
var locPath string
if loader.IsRemoteFile(path) {
// TODO(annasong): You need to check if you can add a localize directory here to store
Expand All @@ -243,10 +246,10 @@ func (lc *localizer) localizeFile(path string) (string, error) {
locPath = cleanFilePath(lc.fSys, lc.root, path)
}
absPath := filepath.Join(lc.dst, locPath)
if err = lc.fSys.MkdirAll(filepath.Dir(absPath)); err != nil {
if err := lc.fSys.MkdirAll(filepath.Dir(absPath)); err != nil {
return "", errors.WrapPrefixf(err, "unable to create directories to localize file %q", path)
}
if err = lc.fSys.WriteFile(absPath, content); err != nil {
if err := lc.fSys.WriteFile(absPath, content); err != nil {
return "", errors.WrapPrefixf(err, "unable to localize file %q", path)
}
return locPath, nil
Expand Down Expand Up @@ -297,44 +300,34 @@ func (lc *localizer) localizeDir(path string) (string, error) {
//
// Note that the localization in this function has not been implemented yet.
func (lc *localizer) localizeBuiltinPlugins(kust *types.Kustomization) error {
for fieldName, plugins := range map[string]struct {
entries []string
localizer kio.Filter
}{
"generators": {
kust.Generators,
&localizeBuiltinGenerators{},
},
"transformers": {
kust.Transformers,
&localizeBuiltinTransformers{},
},
"validators": {
kust.Validators,
&localizeBuiltinTransformers{},
},
for fieldName, entries := range map[string][]string{
"generators": kust.Generators,
"transformers": kust.Transformers,
"validators": kust.Validators,
} {
for i, entry := range plugins.entries {
for i, entry := range entries {
rm, isPath, err := lc.loadResource(entry)
if err != nil {
return errors.WrapPrefixf(err, "unable to load %s entry", fieldName)
}
err = rm.ApplyFilter(plugins.localizer)
err = rm.ApplyFilter(&localizeBuiltinPlugins{lc})
if err != nil {
return errors.Wrap(err)
}
localizedPlugin, err := rm.AsYaml()
if err != nil {
return errors.WrapPrefixf(err, "unable to serialize localized %s entry %q", fieldName, entry)
}
var newEntry string
var localizedEntry string
if isPath {
// TODO(annasong): write localizedPlugin to dst
newEntry = entry
localizedEntry, err = lc.localizeFileWithContent(entry, localizedPlugin)
if err != nil {
return errors.WrapPrefixf(err, "unable to localize %s entry", fieldName)
}
} else {
newEntry = string(localizedPlugin)
localizedEntry = string(localizedPlugin)
}
plugins.entries[i] = newEntry
entries[i] = localizedEntry
}
}
return nil
Expand Down
Loading

0 comments on commit a1bfab3

Please sign in to comment.