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

add edit fix for patchesStrategicMerge to patches #4733

Conversation

koba1t
Copy link
Member

@koba1t koba1t commented Jul 29, 2022

fix #4706

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 29, 2022
@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Jul 29, 2022
@koba1t koba1t force-pushed the feat/add_edit-fix_for_patchesStrategicMerge_to_patches branch from ee59de2 to ab23987 Compare July 29, 2022 17:55
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jul 29, 2022
@koba1t
Copy link
Member Author

koba1t commented Jul 29, 2022

/assign @natasha41575

@k8s-ci-robot
Copy link
Contributor

@koba1t: 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.

@koba1t koba1t force-pushed the feat/add_edit-fix_for_patchesStrategicMerge_to_patches branch from 17f2f48 to f1b15e3 Compare August 6, 2022 20:29
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.

So sorry for the long delay. Thank you for this change! Some minor comments

for _, patchStrategicMerge := range k.PatchesStrategicMerge {
// check this patch is file path select.
if strings.Count(string(patchStrategicMerge), "\n") < 1 &&
(patchStrategicMerge[len(patchStrategicMerge)-5:] == ".yaml" || patchStrategicMerge[len(patchStrategicMerge)-4:] == ".yml") {
Copy link
Contributor

@natasha41575 natasha41575 Aug 25, 2022

Choose a reason for hiding this comment

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

An easier and more reliable way to check if this is a filepath or not is to try to open it (i.e. os.ReadFile), and check for error

Copy link
Member Author

@koba1t koba1t Aug 28, 2022

Choose a reason for hiding this comment

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

I rewrite at 032bf33.

kustomize/commands/edit/fix/fix_test.go Show resolved Hide resolved
kustomize/commands/edit/fix/fix_test.go Outdated Show resolved Hide resolved
@koba1t koba1t force-pushed the feat/add_edit-fix_for_patchesStrategicMerge_to_patches branch from a01afa9 to 7081b22 Compare August 28, 2022 17:14
@koba1t koba1t force-pushed the feat/add_edit-fix_for_patchesStrategicMerge_to_patches branch from 7081b22 to 032bf33 Compare August 28, 2022 17:20
@koba1t
Copy link
Member Author

koba1t commented Aug 28, 2022

hi @natasha41575
I fixed a few points from the comments.
Could you check it if you have time?

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. Very small nit about some formatting in the tests

kustomize/commands/edit/fix/fix_test.go Show resolved Hide resolved
spec:
template:
spec:
containers:
Copy link
Contributor

Choose a reason for hiding this comment

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

same here (and a few other places)

I know it doesn't really matter for these tests, but it's good to make sure our tests have valid input

Copy link
Member Author

Choose a reason for hiding this comment

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

I know it doesn't really matter for these tests, but it's good to make sure our tests have valid input

Sorry, I couldn't find that before committing.
I completely agree with your opinion.

I fixed everything.

@koba1t
Copy link
Member Author

koba1t commented Sep 1, 2022

hi @natasha41575
I fixed a few points.
Please recheck it in your free time.

@natasha41575
Copy link
Contributor

natasha41575 commented Sep 9, 2022

@annasong20 Do you mind giving this PR a review? And as a final check, it would be helpful if you could pull down and build the PR and verify that it does the right thing. Btw, when you are testing, it is expected that it won't work unless the patch file exists.

Copy link
Contributor

@annasong20 annasong20 left a comment

Choose a reason for hiding this comment

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

@koba1t I know you didn't change this line, but could you add a newline to the end of it?

if k.PatchesStrategicMerge != nil {
for _, patchStrategicMerge := range k.PatchesStrategicMerge {
// check this patch is file path select.
if _, err := fSys.ReadFile(string(patchStrategicMerge)); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to change anything, but just as an FYI, the existing PatchesStrategicMerge code differentiates between inline and files like this: https://github.com/kubernetes-sigs/kustomize/blob/master/plugin/builtin/patchstrategicmergetransformer/PatchStrategicMergeTransformer.go#L63
Checking for inlines this way doesn't require the file to exist.

`,
},
}
for _, test := range tests {
Copy link
Contributor

@annasong20 annasong20 Sep 12, 2022

Choose a reason for hiding this comment

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

Can we move the loop body into the body of a function passed to t.Run()? The Run method runs its parameter function as a sub-test, which will display the results as such and isolate failures between sub-tests. We can also pass in test.name.

Very thorough testing!

Copy link
Member Author

@koba1t koba1t Sep 12, 2022

Choose a reason for hiding this comment

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

I fixed it at 7c2e884

Comment on lines 385 to 387
if diff := cmp.Diff([]byte(test.expected), content); diff != "" {
t.Errorf("%s: Mismatch (-expected, +actual):\n%s", test.name, diff)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional nit: We can replace these lines with require.Empty(t, cmp.Diff([]byte(test.expected), content)) since t.Run() will print the sub-test name and require.Empty() will print diff if it isn't empty.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@annasong20
Copy link
Contributor

@natasha41575 The PR binary works as expected on the following setup:

kustomization.yaml

patchesStrategicMerge:
- deploy.yaml
- |-
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: nginx
  spec:
    template:
      spec:
        containers:
          - name: nginx
            env:
              - name: CONFIG_FILE_PATH
                value: home.yaml
patchesJson6902:
- path: patch1.yaml
  target:
    kind: Deployment
patches:
- path: patch2.yaml
- patch: |-
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      template:
        spec:
          containers:
            - name: nginx
              env:
                - name: CONFIG_FILE_PATH_2
                  value: home.yaml

deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  template:
    spec:
      containers:
      - name: nginx
        env:
        - name: CONFIG_FILE_PATH_3
          value: home.yaml

On a different note, I think it'd be helpful for the current code in fix_test.go to check for more than just an error. We should care about warnings that the build failed or that the build output is different. I know they're currently printed because we don't provide resources that match the patch targets, but the warnings could technically be the result of us "fixing" the kustomization incorrectly. It doesn't seem safe to ignore them.

Something else that I noticed while testing the binary, but of lesser importance, is that we don't inform users if their initial build was faulty. I found this very confusing because when I saw this warning, I'd assume edit fix introduced an inconsistency to my kustomization, but it turns out that it was the original kustomization that had issues.

Not saying we need to address these concerns in this PR, since the code wasn't introduced here.

@koba1t
Copy link
Member Author

koba1t commented Sep 12, 2022

I know you didn't change this line, but could you add a newline to the end of it?

Do you think this commit fixes that?

@koba1t
Copy link
Member Author

koba1t commented Sep 12, 2022

Hi, @annasong20
Thanks for your review.
I think I completed fixing the problems from your comment. Please recheck it when you have time.

@annasong20
Copy link
Contributor

I know you didn't change this line, but could you add a newline to the end of it?

Do you think this commit fixes that?

Yes, perfect! Looks good to me. Will defer to @natasha41575 to approve.

@koba1t
Copy link
Member Author

koba1t commented Oct 12, 2022

@natasha41575
Could you recheck it?

Hi, @annasong20
Thanks for your review.

@annasong20
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 12, 2022
@natasha41575
Copy link
Contributor

So sorry for the delay! Reviewing this now

@natasha41575
Copy link
Contributor

Thanks so much for the fix! This means we can get #4723 in soon as well. This is great. Please feel free to reach out if you are looking for more issues to pick up.

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: koba1t, 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 the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 21, 2022
@k8s-ci-robot k8s-ci-robot merged commit 7ee6dd5 into kubernetes-sigs:master Oct 21, 2022
@koba1t koba1t deleted the feat/add_edit-fix_for_patchesStrategicMerge_to_patches branch October 21, 2022 22:11
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/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emit a deprecation warning when deprecated fields are used
4 participants