kyaml setters: support full and partial field replacement

This commit is contained in:
Phillip Wittrock
2020-01-02 14:00:43 -08:00
parent 3577a7e174
commit ac9b0a3e9e
6 changed files with 76 additions and 34 deletions

View File

@@ -11,10 +11,10 @@ import (
"sigs.k8s.io/kustomize/kyaml/yaml"
)
var _ yaml.Filter = &partialFieldSetter{}
var _ yaml.Filter = &fieldSetter{}
// partialFieldSetter sets part of a field value.
type partialFieldSetter struct {
// fieldSetter sets part or all of a field value.
type fieldSetter struct {
// Name is the name of the setter to perform.
Name string
@@ -32,7 +32,7 @@ type partialFieldSetter struct {
}
// Filter implements yaml.Filter
func (fs *partialFieldSetter) Filter(object *yaml.RNode) (*yaml.RNode, error) {
func (fs *fieldSetter) Filter(object *yaml.RNode) (*yaml.RNode, error) {
switch object.YNode().Kind {
case yaml.DocumentNode:
// Document is the root of the object and always contains 1 node
@@ -49,7 +49,7 @@ func (fs *partialFieldSetter) Filter(object *yaml.RNode) (*yaml.RNode, error) {
})
case yaml.ScalarNode:
// Check if there is a setter matching the name
s, f, err := fs.findPartialSetter(object)
s, f, partial, err := fs.findSetter(object)
if err != nil {
return nil, err
}
@@ -58,19 +58,19 @@ func (fs *partialFieldSetter) Filter(object *yaml.RNode) (*yaml.RNode, error) {
return object, nil
}
// set the field value
return object, fs.set(object, s, f)
return object, fs.set(object, s, f, partial)
default:
return object, nil
}
}
// findPartialSetter finds the setter matching the name if one exists
func (fs *partialFieldSetter) findPartialSetter(field *yaml.RNode) (
*fieldmeta.PartialFieldSetter, *fieldmeta.FieldMeta, error) {
func (fs *fieldSetter) findSetter(field *yaml.RNode) (
*fieldmeta.PartialFieldSetter, *fieldmeta.FieldMeta, bool, error) {
// check if there are any substitutions for this field
var fm = &fieldmeta.FieldMeta{}
if err := fm.Read(field); err != nil {
return nil, nil, err
return nil, nil, false, err
}
if fs.SetBy != "" {
fm.Extensions.SetBy = fs.SetBy
@@ -79,18 +79,23 @@ func (fs *partialFieldSetter) findPartialSetter(field *yaml.RNode) (
fm.Schema.Description = fs.Description
}
if fm.Extensions.FieldSetter != nil && fm.Extensions.FieldSetter.Name == fs.Name {
return fm.Extensions.FieldSetter, fm, false, nil
}
// check if there is a matching substitution
for i := range fm.Extensions.PartialFieldSetters {
if fm.Extensions.PartialFieldSetters[i].Name == fs.Name {
return &fm.Extensions.PartialFieldSetters[i], fm, nil
return &fm.Extensions.PartialFieldSetters[i], fm, true, nil
}
}
return nil, nil, nil
return nil, nil, false, nil
}
// set performs the substitution for the given field, substitution, and metadata
func (fs *partialFieldSetter) set(
field *yaml.RNode, s *fieldmeta.PartialFieldSetter, f *fieldmeta.FieldMeta) error {
func (fs *fieldSetter) set(
field *yaml.RNode, s *fieldmeta.PartialFieldSetter,
f *fieldmeta.FieldMeta, partial bool) error {
if s.Value == fs.Value || !strings.Contains(field.YNode().Value, s.Value) {
// no substitutions necessary -- already substituted or doesn't have the set value
// which acts as a marker
@@ -100,8 +105,13 @@ func (fs *partialFieldSetter) set(
// record that the config has been modified
fs.Count++
// replace the current value with the new value
field.YNode().Value = strings.ReplaceAll(field.YNode().Value, s.Value, fs.Value)
if !partial {
// full setter
field.YNode().Value = fs.Value
} else {
// replace the current value with the new value
field.YNode().Value = strings.ReplaceAll(field.YNode().Value, s.Value, fs.Value)
}
// be sure to set the tag to the matching type so the yaml doesn't incorrectly quote
//integers or booleans as strings