mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Option to customize NamespaceTransformer overwrite behaviour (#4708)
* Option to customize NamespaceTransformer overwrite behaviour * Code review feedback
This commit is contained in:
@@ -12,13 +12,13 @@ type SetFn func(*yaml.RNode) error
|
||||
|
||||
// SetScalar returns a SetFn to set a scalar value
|
||||
func SetScalar(value string) SetFn {
|
||||
return func(node *yaml.RNode) error {
|
||||
return node.PipeE(yaml.FieldSetter{StringValue: value})
|
||||
}
|
||||
return SetEntry("", value, yaml.NodeTagEmpty)
|
||||
}
|
||||
|
||||
// SetEntry returns a SetFn to set an entry in a map
|
||||
func SetEntry(key, value, tag string) SetFn {
|
||||
// SetEntry returns a SetFn to set a field or a map entry to a value.
|
||||
// It can be used with an empty name to set both a value and a tag on a scalar node.
|
||||
// When setting only a value on a scalar node, use SetScalar instead.
|
||||
func SetEntry(name, value, tag string) SetFn {
|
||||
n := &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Value: value,
|
||||
@@ -26,7 +26,7 @@ func SetEntry(key, value, tag string) SetFn {
|
||||
}
|
||||
return func(node *yaml.RNode) error {
|
||||
return node.PipeE(yaml.FieldSetter{
|
||||
Name: key,
|
||||
Name: name,
|
||||
Value: yaml.NewRNode(n),
|
||||
})
|
||||
}
|
||||
@@ -34,36 +34,72 @@ func SetEntry(key, value, tag string) SetFn {
|
||||
|
||||
type TrackableSetter struct {
|
||||
// SetValueCallback will be invoked each time a field is set
|
||||
setValueCallback func(key, value, tag string, node *yaml.RNode)
|
||||
setValueCallback func(name, value, tag string, node *yaml.RNode)
|
||||
}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (s *TrackableSetter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
|
||||
func (s *TrackableSetter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) *TrackableSetter {
|
||||
s.setValueCallback = callback
|
||||
return s
|
||||
}
|
||||
|
||||
// SetScalar returns a SetFn to set a scalar value
|
||||
// SetScalar returns a SetFn to set a scalar value.
|
||||
// if a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time a scalar is set
|
||||
func (s TrackableSetter) SetScalar(value string) SetFn {
|
||||
origSetScalar := SetScalar(value)
|
||||
return s.SetEntry("", value, yaml.NodeTagEmpty)
|
||||
}
|
||||
|
||||
// SetScalarIfEmpty returns a SetFn to set a scalar value only if it isn't already set.
|
||||
// If a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time a scalar is actually set.
|
||||
func (s TrackableSetter) SetScalarIfEmpty(value string) SetFn {
|
||||
return s.SetEntryIfEmpty("", value, yaml.NodeTagEmpty)
|
||||
}
|
||||
|
||||
// SetEntry returns a SetFn to set a field or a map entry to a value.
|
||||
// It can be used with an empty name to set both a value and a tag on a scalar node.
|
||||
// When setting only a value on a scalar node, use SetScalar instead.
|
||||
// If a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time an entry is set.
|
||||
func (s TrackableSetter) SetEntry(name, value, tag string) SetFn {
|
||||
origSetEntry := SetEntry(name, value, tag)
|
||||
return func(node *yaml.RNode) error {
|
||||
if s.setValueCallback != nil {
|
||||
s.setValueCallback("", value, "", node)
|
||||
s.setValueCallback(name, value, tag, node)
|
||||
}
|
||||
return origSetScalar(node)
|
||||
return origSetEntry(node)
|
||||
}
|
||||
}
|
||||
|
||||
// SetEntry returns a SetFn to set an entry in a map
|
||||
// if a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time an entry is set
|
||||
func (s TrackableSetter) SetEntry(key, value, tag string) SetFn {
|
||||
// SetEntryIfEmpty returns a SetFn to set a field or a map entry to a value only if it isn't already set.
|
||||
// It can be used with an empty name to set both a value and a tag on a scalar node.
|
||||
// When setting only a value on a scalar node, use SetScalar instead.
|
||||
// If a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time an entry is actually set.
|
||||
func (s TrackableSetter) SetEntryIfEmpty(key, value, tag string) SetFn {
|
||||
origSetEntry := SetEntry(key, value, tag)
|
||||
return func(node *yaml.RNode) error {
|
||||
if hasExistingValue(node, key) {
|
||||
return nil
|
||||
}
|
||||
if s.setValueCallback != nil {
|
||||
s.setValueCallback(key, value, tag, node)
|
||||
}
|
||||
return origSetEntry(node)
|
||||
}
|
||||
}
|
||||
|
||||
func hasExistingValue(node *yaml.RNode, key string) bool {
|
||||
if node.IsNilOrEmpty() {
|
||||
return false
|
||||
}
|
||||
if err := yaml.ErrorIfInvalid(node, yaml.ScalarNode); err == nil {
|
||||
return yaml.GetValue(node) != ""
|
||||
}
|
||||
entry := node.Field(key)
|
||||
if entry.IsNilOrEmpty() {
|
||||
return false
|
||||
}
|
||||
return yaml.GetValue(entry.Value) != ""
|
||||
}
|
||||
|
||||
106
api/filters/filtersutil/setters_test.go
Normal file
106
api/filters/filtersutil/setters_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package filtersutil_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
func TestTrackableSetter_SetScalarIfEmpty(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *yaml.RNode
|
||||
value string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "sets null values",
|
||||
input: yaml.MakeNullNode(),
|
||||
value: "foo",
|
||||
want: "foo",
|
||||
},
|
||||
{
|
||||
name: "sets empty values",
|
||||
input: yaml.NewScalarRNode(""),
|
||||
value: "foo",
|
||||
want: "foo",
|
||||
},
|
||||
{
|
||||
name: "does not overwrite values",
|
||||
input: yaml.NewStringRNode("a"),
|
||||
value: "foo",
|
||||
want: "a",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
wasSet := false
|
||||
s := (&filtersutil.TrackableSetter{}).WithMutationTracker(func(_, _, _ string, _ *yaml.RNode) {
|
||||
wasSet = true
|
||||
})
|
||||
wantSet := tt.value == tt.want
|
||||
fn := s.SetScalarIfEmpty(tt.value)
|
||||
require.NoError(t, fn(tt.input))
|
||||
assert.Equal(t, tt.want, yaml.GetValue(tt.input))
|
||||
assert.Equal(t, wantSet, wasSet, "tracker invoked even though value was not changed")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackableSetter_SetEntryIfEmpty(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *yaml.RNode
|
||||
key string
|
||||
value string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "sets empty values",
|
||||
input: yaml.NewMapRNode(&map[string]string{"setMe": ""}),
|
||||
key: "setMe",
|
||||
value: "foo",
|
||||
want: "foo",
|
||||
},
|
||||
{
|
||||
name: "sets missing keys",
|
||||
input: yaml.NewMapRNode(&map[string]string{}),
|
||||
key: "setMe",
|
||||
value: "foo",
|
||||
want: "foo",
|
||||
},
|
||||
{
|
||||
name: "does not overwrite values",
|
||||
input: yaml.NewMapRNode(&map[string]string{"existing": "original"}),
|
||||
key: "existing",
|
||||
value: "foo",
|
||||
want: "original",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
wasSet := false
|
||||
s := (&filtersutil.TrackableSetter{}).WithMutationTracker(func(_, _, _ string, _ *yaml.RNode) {
|
||||
wasSet = true
|
||||
})
|
||||
wantSet := tt.value == tt.want
|
||||
fn := s.SetEntryIfEmpty(tt.key, tt.value, "")
|
||||
require.NoError(t, fn(tt.input))
|
||||
assert.Equal(t, tt.want, yaml.GetValue(tt.input.Field(tt.key).Value))
|
||||
assert.Equal(t, wantSet, wasSet, "tracker invoked even though value was not changed")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackableSetter_SetEntryIfEmpty_BadInputNodeKind(t *testing.T) {
|
||||
fn := filtersutil.TrackableSetter{}.SetEntryIfEmpty("foo", "false", yaml.NodeTagBool)
|
||||
rn := yaml.NewListRNode("nope")
|
||||
rn.AppendToFieldPath("dummy", "path")
|
||||
assert.EqualError(t, fn(rn), "wrong Node Kind for dummy.path expected: MappingNode was SequenceNode: value: {- nope}")
|
||||
}
|
||||
Reference in New Issue
Block a user