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 selection of kustomization resource from multi doc yaml #4131

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/flux/diff_kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func TestDiffKustomization(t *testing.T) {
objectFile: "./testdata/diff-kustomization/stringdata-sops-secret.yaml",
assert: assertGoldenFile("./testdata/diff-kustomization/diff-with-drifted-stringdata-sops-secret.golden"),
},
{
name: "diff where kustomization file has multiple objects with the same name",
args: "diff kustomization podinfo --path ./testdata/build-kustomization/podinfo --progress-bar=false --kustomization-file ./testdata/diff-kustomization/flux-kustomization-multiobj.yaml",
objectFile: "",
assert: assertGoldenFile("./testdata/diff-kustomization/nothing-is-deployed.golden"),
},
}

tmpl := map[string]string{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Namespace
metadata:
name: podinfo

---
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: podinfo
spec:
interval: 5m0s
path: ./kustomize
force: true
prune: true
sourceRef:
kind: GitRepository
name: podinfo
targetNamespace: default
9 changes: 8 additions & 1 deletion internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (b *Builder) unMarshallKustomization() (*kustomizev1.Kustomization, error)
k := &kustomizev1.Kustomization{}
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data))
// check for kustomization in yaml with the same name and namespace
for !(k.Name == b.name && (k.Namespace == b.namespace || k.Namespace == "")) {
for {
err = decoder.Decode(k)
if err != nil {
if err == io.EOF {
Expand All @@ -343,6 +343,13 @@ func (b *Builder) unMarshallKustomization() (*kustomizev1.Kustomization, error)
return nil, fmt.Errorf("failed to unmarshall kustomization file %s: %w", b.kustomizationFile, err)
}
}

if strings.HasPrefix(k.APIVersion, kustomizev1.GroupVersion.Group+"/") &&
k.Kind == kustomizev1.KustomizationKind &&
k.Name == b.name &&
(k.Namespace == b.namespace || k.Namespace == "") {
break
}
}
return k, nil
}
Expand Down
11 changes: 10 additions & 1 deletion internal/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ func Test_unMarshallKustomization(t *testing.T) {
wantErr: true,
errString: "failed find kustomization with name",
},
{
name: "yaml containing other resource with same name as kustomization",
localKsFile: "testdata/local-kustomization/invalid-resource.yaml",
wantErr: true,
errString: "failed find kustomization with name",
},
}

b := &Builder{
Expand Down Expand Up @@ -324,7 +330,10 @@ func Test_ResolveKustomization(t *testing.T) {
},
}

b := &Builder{}
b := &Builder{
name: "podinfo",
namespace: "flux-system",
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b.kustomizationFile = tt.localKsFile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: podinfo