Skip to content
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

Wildcard support for creation in ReplacementTransformer #4886

Merged
merged 8 commits into from Dec 6, 2022

Conversation

KnVerey
Copy link
Contributor

@KnVerey KnVerey commented Nov 24, 2022

⚠️ Behaviour change: in kustomize <v4.5.7, #4761 silently ignored targets with out of range indexes (wrongly, IMO!). Kustomize = v4.5.7 panics as described in that issue (also very bad!). My previous PR turned the panic into an error (fine). This one will turn the error into doing what they asked for: error if create is disabled, or creating the fields one by one if create is enabled. The latter would be an output change for a user coming from v4.5.6 or earlier, but I would argue this is fixing something that's clearly a bug.


  • Makes the Replacements transformer support the use of wildcards in conjunction with the Create option.
  • Also makes the Replacements transformer support appending list elements (both "primitive" and map-based elements).
  • Does both by implementing these features on PathMatcher, and switching to it. I tried several other things first, which you can still see in the earlier commits if interested.
    • In working on this, I discovered an existing bug where the Create option cannot work with multiple matches, which is possible without a wildcard (e.g. if you match on container image, that may not be unique in a valid object). That bug is demonstrated in the regression test in this commit: 273fdf0. Currently, it silently uses the first one. The other approaches I tried would at best not be able to fix this, which is what led me to conclude modifying and using PathMatcher was necessary.
  • Initially, I also added support for - meaning "append" without knowing the index in advance. However, the example in replacements: Unable to add/create a new array element #4469 made me think the semantics deserve separate consideration (if you use hyphens in that example, you'll append two new things, which is not the intent). Plus PathGetter uses - to mean "last", with untested/unclear behaviour on create. We can still do something like this if there's demand for it; I just decided it shouldn't be part of this PR.

Fixes #4561
Fixes #4469
Fixes #1493

@k8s-ci-robot
Copy link
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 24, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: KnVerey

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 24, 2022
@k8s-ci-robot
Copy link
Contributor

@KnVerey: This PR has multiple commits, and the default merge method is: merge.
You can request commits to be squashed using the label: tide/merge-method-squash

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 25, 2022
val *RNode
field string
matchRegex string
indexNumber int
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field is internal and was set in one place but unused.

@KnVerey KnVerey marked this pull request as ready for review November 25, 2022 23:35
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 25, 2022
@KnVerey KnVerey added this to the v5.0.0 milestone Dec 1, 2022
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 6, 2022
This approach doesn't work when multiple existing sequence elements
should match, i.e. because the sequence contains maps and we're
searching on a key they all contain (target all containers with a certain
image would be one use case for this). PathGetter just takes the first
match in that case, which is not what we want.
PathGetter treats it as meaning 'last' not 'append' and does not have test coverage for its handling of this when create is set. Semantics are dubious given that multiple Replacement fieldPaths may be specified, which would cause successive appends.
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 6, 2022
@KnVerey
Copy link
Contributor Author

KnVerey commented Dec 6, 2022

rebased and ready for review @natasha41575

Copy link
Contributor

@natasha41575 natasha41575 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to PathMatcher is very well done, thanks!

lgtm, couple of minor comments


func TestPathMatcher_Filter_Create(t *testing.T) {
testCases := map[string]struct {
name string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: looks like this name field doesn't get used anywhere, do we need it?

}
func fieldRetrievalError(fieldPath string, isCreate bool) string {
if isCreate {
return "unable to find or create field " + fieldPath + " in replacement target"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: suggest quoting the fieldPath, since it's a user input value

@KnVerey
Copy link
Contributor Author

KnVerey commented Dec 6, 2022

Updated! I also realized the "find or create" error wasn't showing up in any tests, so I added another one.

The change to PathMatcher is very well done, thanks!

Thanks! It took me a while to get there, as the commit history still shows. 😅

/label tide/merge-method-squash

@k8s-ci-robot k8s-ci-robot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Dec 6, 2022
Copy link
Contributor

@natasha41575 natasha41575 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 6, 2022
@k8s-ci-robot k8s-ci-robot merged commit 903fbb6 into kubernetes-sigs:master Dec 6, 2022
@KnVerey KnVerey deleted the replacements_wildcard branch December 6, 2022 21:08
elisshafer pushed a commit to elisshafer/kustomize that referenced this pull request Dec 8, 2022
…igs#4886)

* Ahead-of-time wildcard path expansion solution

* Wrapped PathGetter solution

This approach doesn't work when multiple existing sequence elements
should match, i.e. because the sequence contains maps and we're
searching on a key they all contain (target all containers with a certain
image would be one use case for this). PathGetter just takes the first
match in that case, which is not what we want.

* Add creation support to PathMatcher

* Regression test for existing bug when creation is enabled and sequence query should match multiple elements

* PathMatcher Create tests and support for sequence appending

* revert hyphen append support

PathGetter treats it as meaning 'last' not 'append' and does not have test coverage for its handling of this when create is set. Semantics are dubious given that multiple Replacement fieldPaths may be specified, which would cause successive appends.

* This also provides a solution to issue 1493

* Review feedback
cailynse pushed a commit to cailynse/kustomize that referenced this pull request Jan 12, 2023
…igs#4886)

* Ahead-of-time wildcard path expansion solution

* Wrapped PathGetter solution

This approach doesn't work when multiple existing sequence elements
should match, i.e. because the sequence contains maps and we're
searching on a key they all contain (target all containers with a certain
image would be one use case for this). PathGetter just takes the first
match in that case, which is not what we want.

* Add creation support to PathMatcher

* Regression test for existing bug when creation is enabled and sequence query should match multiple elements

* PathMatcher Create tests and support for sequence appending

* revert hyphen append support

PathGetter treats it as meaning 'last' not 'append' and does not have test coverage for its handling of this when create is set. Semantics are dubious given that multiple Replacement fieldPaths may be specified, which would cause successive appends.

* This also provides a solution to issue 1493

* Review feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
3 participants