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

Fix reject needing both current and previous ids #5263

Merged

Conversation

bugoverdose
Copy link
Member

@bugoverdose bugoverdose commented Aug 10, 2023

fixes #5241

The bug is really well explained in the issue, so please check it to understand the context.

Bug Explanation

  1. In applyReplacement, we apply replacement on the selected node if it is not rejected.

  2. When prefix or suffix transformer is applied on a resource, the id of the resource is changed.

Problem: We were checking both of the ids, previous and current, and if any of it was not rejected, we applied the replacement.

  • Even if previous id was rejected, current id is not.
  • Even if current id was rejected, previous id is not.
  • So we had to manually reject both previous and current ids, which is wierd and not scalable.
func applyReplacement(nodes []*yaml.RNode, value *yaml.RNode, targetSelectors []*types.TargetSelector) ([]*yaml.RNode, error) {
	for _, selector := range targetSelectors {
		...
		for _, possibleTarget := range nodes {
		        // MakeResIds returns all of an RNode's current and previous Ids
			ids, err := utils.MakeResIds(possibleTarget)
			...
			// filter targets by matching resource IDs
			for i, id := range ids {
				if id.IsSelectedBy(selector.Select.ResId) && !rejectId(selector.Reject, &ids[i]) {
					err := copyValueToTarget(possibleTarget, value, selector)
					if err != nil {
						return nil, err
					}
					break
				}
			}
		}
	}
	return nodes, nil
}

Fix

I just made it apply replacement only if none of the ids (previous and current) were rejected.

Tests

I think we need a E2E test for this. TestFilter at replacement_test.go isn't enough for this case.

Is there a way to test multiple Filters?

Another Solutions

  1. To ignore the effect of other transformations, We could only check the original id of each node instead of all the ids.
  2. Making sure replacement happens before other transformations could be a way, but I don't think that's a smart solution. Also, TransformerFactories

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 10, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @bugoverdose!

It looks like this is your first PR to kubernetes-sigs/kustomize 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kustomize has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 10, 2023
@k8s-ci-robot
Copy link
Contributor

Hi @bugoverdose. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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 the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 10, 2023
@natasha41575
Copy link
Contributor

natasha41575 commented Aug 10, 2023

I think we need a E2E test for this. TestFilter at replacement_test.go isn't enough for this case.

Is there a way to test multiple Filters?

Can you take a look at https://github.com/kubernetes-sigs/kustomize/blob/master/api/krusty/replacementtransformer_test.go and see if the test infrastructure there is sufficient for you to add a test?

For bug fixes, we usually like to have the first commit add a test, but show the incorrect behavior (so that it passes). This helps to demonstrate what the problem is.
The second commit should include the fix, and update the test. You can see an example PR here: #3931

@natasha41575
Copy link
Contributor

natasha41575 commented Aug 10, 2023

Also quick note, could you change the first line of your PR description to "fixes #5241" or "closes #5241"? This will cause the bug to autoclose when this PR merges.

@natasha41575
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 10, 2023
@k8s-ci-robot
Copy link
Contributor

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/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Aug 11, 2023
@bugoverdose
Copy link
Member Author

bugoverdose commented Aug 11, 2023

@natasha41575

Also quick note, could you change the first line of your PR description to "fixes #5241" or "closes from #5241"? This will cause the bug to autoclose when this PR merges.

I changed it from "from" to "fixes".

Can you take a look at https://github.com/kubernetes-sigs/kustomize/blob/master/api/krusty/replacementtransformer_test.go and see if the test infrastructure there is sufficient for you to add a test?

I couldn't really understand the test setup, but it worked. Thanks.

For bug fixes, we usually like to have the first commit add a test, but show the incorrect behavior (so that it passes). This helps to demonstrate what the problem is.
The second commit should include the fix, and update the test. You can see an example PR here: #3931

For a second, I thought you wanted me to commit a failing test so that it would be like Test Driven Development, but I guess it makes more sense to do it the way you explained. Basically, it shows the change in implementation. I force pushed the commits to match the order.

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
/approve

Thanks for the fix.

Would you be willing to update the replacements docs to explain the behavior? I think it would be nice to have a sentence or two under targets somewhere that explains that selection and rejection will match a resource by any of its previous ids. The source for those docs live here, and we can merge a docs update with the next release.

@@ -546,3 +546,71 @@ metadata:
name: red-dc6gc5btkc
`)
}

func TestReplacementTransformerWithSuffixTransformerAndReject(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Test looks good! Thanks for adding this.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bugoverdose, natasha41575

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 lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Aug 14, 2023
@k8s-ci-robot k8s-ci-robot merged commit 911ddcd into kubernetes-sigs:master Aug 14, 2023
5 checks passed
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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reject on replacements requires both current and all previous IDs match
3 participants