Skip to content

Commit

Permalink
Merge pull request #4176 from m-Bilal/fix-4123
Browse files Browse the repository at this point in the history
Fixes 4123; Length check on originalFields of kustomizationFile to prevent panic
  • Loading branch information
k8s-ci-robot committed Sep 16, 2021
2 parents c1ae234 + 12c177a commit 53577a5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kustomize/commands/internal/kustfile/kustomizationfile.go
Expand Up @@ -212,7 +212,7 @@ func (mf *kustomizationFile) parseCommentedFields(content []byte) error {
if matched {
mf.originalFields = append(mf.originalFields, &commentedField{field: field, comment: squash(comments)})
comments = [][]byte{}
} else if len(comments) > 0 {
} else if len(comments) > 0 && len(mf.originalFields) > 0 {
mf.originalFields[len(mf.originalFields)-1].appendComment(squash(comments))
comments = [][]byte{}
}
Expand Down
47 changes: 47 additions & 0 deletions kustomize/commands/internal/kustfile/kustomizationfile_test.go
Expand Up @@ -356,6 +356,53 @@ kind: Kustomization
}
}

func TestCommentsWithDocumentSeperatorAtBeginning(t *testing.T) {
kustomizationContentWithComments := []byte(`
# Some comments
# This is some comment we should preserve
# don't delete it
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: mynamespace
`)

expected := []byte(`
# Some comments
# This is some comment we should preserve
# don't delete it
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: mynamespace
`)
fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(
fSys, kustomizationContentWithComments)
mf, err := NewKustomizationFile(fSys)
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}

kustomization, err := mf.Read()
if err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
if err = mf.Write(kustomization); err != nil {
t.Fatalf("Unexpected Error: %v", err)
}
bytes, _ := fSys.ReadFile(mf.path)

if diff := cmp.Diff(expected, bytes); diff != "" {
t.Errorf("Mismatch (-expected, +actual):\n%s", diff)
}
}

func TestUnknownFieldInKustomization(t *testing.T) {
kContent := []byte(`
foo:
Expand Down

0 comments on commit 53577a5

Please sign in to comment.