New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to customize NamespaceTransformer overwrite behaviour #4708
Merged
k8s-ci-robot
merged 2 commits into
kubernetes-sigs:master
from
KnVerey:ns_transformer_overwrite_option
Jul 14, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ linters: | |
- gocyclo | ||
# - godot | ||
# - godox | ||
- goerr113 | ||
# - goerr113 | ||
- gofmt | ||
# - gofumpt | ||
- goheader | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is outside the scope of this PR, but I hope we can improve this error message some day |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm glad we're disabling this, it's been slightly annoying to work with
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to question the set of linters I enabled when they really don't align with the style in this codebase and/or value is low. I erred on the side of enabling more of them, without necessarily having a clear picture of the implications.