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

Kustomize edit panics when Kustomization file begins with document separator #4123

Closed
avisiedo opened this issue Aug 17, 2021 · 6 comments · Fixed by #4176
Closed

Kustomize edit panics when Kustomization file begins with document separator #4123

avisiedo opened this issue Aug 17, 2021 · 6 comments · Fixed by #4176
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@avisiedo
Copy link

Describe the bug

I get a panic when running kustomize edit set namespace mynamespace.

Files that can reproduce the issue

kustomization.yaml

---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: freeipa

Expected output

No panic message and the namespace field updated to mynamespace.

Actual output

panic: runtime error: index out of range [-1]

goroutine 1 [running]:
sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile.(*kustomizationFile).parseCommentedFields(0xc0002e9140, 0xc000030c60, 0x5a, 0x60, 0x0, 0x0)
        /workspace/myClone/kustomize/commands/internal/kustfile/kustomizationfile.go:216 +0x7c5
sigs.k8s.io/kustomize/kustomize/v4/commands/internal/kustfile.(*kustomizationFile).Read(0xc0002e9140, 0xf66538, 0xc0002e9140, 0x0)
        /workspace/myClone/kustomize/commands/internal/kustfile/kustomizationfile.go:174 +0x14a
sigs.k8s.io/kustomize/kustomize/v4/commands/edit/set.(*setNamespaceOptions).RunSetNamespace(0xc000275940, 0xbcb760, 0xf66538, 0x1, 0x0)
        /workspace/myClone/kustomize/commands/edit/set/setnamespace.go:66 +0x65
sigs.k8s.io/kustomize/kustomize/v4/commands/edit/set.newCmdSetNamespace.func1(0xc000315080, 0xc000320420, 0x1, 0x1, 0x0, 0x0)
        /workspace/myClone/kustomize/commands/edit/set/setnamespace.go:41 +0xd0
github.com/spf13/cobra.(*Command).execute(0xc000315080, 0xc0003203e0, 0x1, 0x1, 0xc000315080, 0xc0003203e0)
        /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:842 +0x472
github.com/spf13/cobra.(*Command).ExecuteC(0xc000308000, 0x0, 0xa12400, 0xc000030118)
        /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:950 +0x375
github.com/spf13/cobra.(*Command).Execute(...)
        /go/pkg/mod/github.com/spf13/cobra@v1.0.0/command.go:887
main.main()
        /workspace/myClone/kustomize/main.go:14 +0x2a

Kustomize version

kustomize version
{Version:kustomize/v4.2.0 GitCommit:d53a2ad45d04b0264bcee9e19879437d851cb778 BuildDate:2021-06-30T22:49:26Z GoOs:linux GoArch:amd64}

Platform

Linux

Additional context

Installed by:

curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"  | bash
@avisiedo avisiedo added the kind/bug Categorizes issue or PR as related to a bug. label Aug 17, 2021
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Aug 17, 2021
@avisiedo
Copy link
Author

I have just realized I had a blank line at the beginning of the file. Removing that blank line into the kustomization.yaml file, make the command run as expected.

@KnVerey
Copy link
Contributor

KnVerey commented Sep 1, 2021

/assign @KnVerey
/triage accepted
/retitle Kustomize panics when Kustomization file begins with document separator

@k8s-ci-robot k8s-ci-robot changed the title panic when running kustomize edit set namespace Kustomize panics when Kustomization file begins with document separator Sep 1, 2021
@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Sep 1, 2021
@KnVerey KnVerey changed the title Kustomize panics when Kustomization file begins with document separator Kustomize edit panics when Kustomization file begins with document separator Sep 1, 2021
@KnVerey
Copy link
Contributor

KnVerey commented Sep 8, 2021

/assign @m-Bilal

@k8s-ci-robot
Copy link
Contributor

@KnVerey: GitHub didn't allow me to assign the following users: m-Bilal.

Note that only kubernetes-sigs members, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

In response to this:

/assign @m-Bilal

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.

@m-Bilal
Copy link
Member

m-Bilal commented Sep 9, 2021

/assign

@m-Bilal
Copy link
Member

m-Bilal commented Sep 12, 2021

@KnVerey the panic happens because in this line

mf.originalFields[len(mf.originalFields)-1].appendComment(squash(comments))
len(mf.originalFields) returns 0.
The reason for this is when the file begins with a blank line (or a comment) and a document separator immediately follows it, len(comments) is > 0 but len(mf.originalFields) is still 0, and since document separator does not match any field, it goes into the following else if condition

So, the kustomization files


---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: freeipa

and

# This is a comment
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: freeipa

result in exactly same errors.

I was able to fix this by changing

to else if len(comments) > 0 && len(mf.originalFields) > 0. Then kustomize edit set namespace mynamespace simply ignores the document separator and results are as follows:


---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: freeipa

becomes:


apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: mynamespace

(the blank line is still there), and

# This is a comment
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: freeipa

becomes

# This is a comment
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: mynamespace

I'll raise a PR with this fix, if there's anything I missed or if it should be a different fix, please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants