kubernetes-sigs / kustomize

Customization of kubernetes YAML configurations
Apache License 2.0
10.91k stars 2.24k forks source link

"kustomize edit set image" is too opinionated about the formatting and moves comments around #4481

Open MichaelSp opened 2 years ago

MichaelSp commented 2 years ago

Describe the bug

After running kustomize edit set image. The comments

Files that can reproduce the issue

before

resources:
  # some important comment to below file:
  - https://raw.githubusercontent.com/argoproj/argo-cd/v2.2.5/manifests/ha/install.yaml
  - 
images:
- name: argo
  newName: argo
  newTag: "123"

running command kustomize edit set image argo=argo:234 Expected output

resources:
  # some important comments to below file:
  - https://raw.githubusercontent.com/argoproj/argo-cd/v2.2.5/manifests/ha/install.yaml

images:
- name: argo
  newName: argo
  newTag: "234"

I could also live with this output:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  # some important comments to below file:
  - https://raw.githubusercontent.com/argoproj/argo-cd/v2.2.5/manifests/ha/install.yaml

images:
- name: argo
  newName: argo
  newTag: "234"

Actual output

  # some important comments to below file:
resources:
- https://raw.githubusercontent.com/argoproj/argo-cd/v2.2.5/manifests/ha/install.yaml
images:
- name: argo
  newName: argo
  newTag: "234"
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

Especially the comment has to stay in the line above the resource!

Kustomize version {Version:kustomize/v4.5.2 GitCommit:9091919699baf1c5a5bf71b32ca73a993e98088b BuildDate:2022-02-09T23:19:28Z GoOs:darwin GoArch:amd64}

k8s-triage-robot commented 2 years ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

pietervincken commented 2 years ago

/remove-lifecycle stale

Hit us today. This hurts our automation and made us revert to YQ instead (which is far from ideal)

annasong20 commented 2 years ago

I can reproduce this issue. Same thing happens on v4.5.7 as well. My output is mostly the same, except the second resource is an empty string instead of being removed. Please see below.

   # some important comment to below file:
resources:
- https://raw.githubusercontent.com/argoproj/argo-cd/v2.2.5/manifests/ha/install.yaml
- ""
images:
- name: argo
  newName: argo
  newTag: "234"
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
natasha41575 commented 2 years ago

/triage accepted

If anyone is interested in picking this up, I think one potential solution could be to use kyaml's CopyComments and SyncOrder functions to maintain the comments and field order of the kustomization.

This probably isn't specific to set image, I imagine all the kustomize edit commands have similar problems.

/help /label "good first issue"

k8s-ci-robot commented 2 years ago

@natasha41575: This request has been marked as needing help from a contributor.

Guidelines

Please ensure that the issue body includes answers to the following questions:

For more details on the requirements of such an issue, please see here and ensure that they are met.

If this request no longer meets these requirements, the label can be removed by commenting with the /remove-help command.

In response to [this](https://github.com/kubernetes-sigs/kustomize/issues/4481): >/triage accepted > >If anyone is interested in picking this up, I think one potential solution could be to use kyaml's `CopyComments` and `SyncOrder` functions to maintain the comments and field order of the kustomization. > >/help >/label "good first issue" Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
natasha41575 commented 2 years ago

Thanks @annasong20 for confirming the issue!

kanekoh commented 2 years ago

I'm new to contribute this repo. If it is ok, I'll try this issue.

kanekoh commented 2 years ago

Is here right place to discuss how I fix this issue? Or should I discuss a question on slack?

sigs.k8s.io/kustomize/kyaml/yaml.Parse(string) removes some blank lines in kustomization file and it causes breaking some test cases.

For example, break lines in a Yaml file are removed when the Parse was called. Here is a failed test case I added.

--- FAIL: TestCommentOrder (0.13s)
    /home/cloud-user/go/src/k8s-sig.io/kustomize/kustomize/commands/internal/kustfile/kustomizationfile_test.go:239: Mismatch (-expected, +actual):
          []uint8(
            """
        -   
        -       
            apiVersion: kustomize.config.k8s.io/v1beta1
            kind: Kustomization
            ... // 3 identical lines
            # some comments to below kubernetes file:
            - https://example.com/kubernetes.yaml # comment
        -   
            images:
            - name: example
            ... // 3 identical lines
            """

Is there a way to avoid above issue?

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

m00lecule commented 1 year ago

Hi,

Today I have also faced this issue. Probably we have similar case, when pipeline triggered by new repo tag has to commit new dockerimage version to separate repository. Also I have found out that this issue has been for a while without sufficient solution.

{Version:kustomize/v4.4.0 GitCommit:63ec6bdb3d737a7c66901828c5743656c49b60e1 BuildDate:2021-09-27T16:24:12Z GoOs:linux GoArch:amd64}

I have managed to bypass this issue using yq:

TAG=$TAG yq -i '(.images[] | select( .name == "image") | .newTag) = strenv(TAG)' kustomization.yaml

ref: https://github.com/kubernetes-sigs/kustomize/issues/3323

gxwilkerson33 commented 1 year ago

/assign

vaibhav2107 commented 11 months ago

/remove-lifecycle stale

stormqueen1990 commented 2 months ago

/lifecycle frozen