Merge pull request #5906 from raffis/fix-preserve-invalid-labels
fix: preserve invalid metadata.labels in `flux build ks`
This commit is contained in:
@@ -659,17 +659,10 @@ func kustomizationsEqual(k1 *kustomizev1.Kustomization, k2 *kustomizev1.Kustomiz
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) setOwnerLabels(res *resource.Resource) error {
|
func (b *Builder) setOwnerLabels(res *resource.Resource) error {
|
||||||
labels := res.GetLabels()
|
if err := res.PipeE(yaml.SetLabel(controllerGroup+"/name", b.kustomization.GetName())); err != nil {
|
||||||
|
|
||||||
labels[controllerGroup+"/name"] = b.kustomization.GetName()
|
|
||||||
labels[controllerGroup+"/namespace"] = b.kustomization.GetNamespace()
|
|
||||||
|
|
||||||
err := res.SetLabels(labels)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return res.PipeE(yaml.SetLabel(controllerGroup+"/namespace", b.kustomization.GetNamespace()))
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func maskSopsData(res *resource.Resource) error {
|
func maskSopsData(res *resource.Resource) error {
|
||||||
|
|||||||
@@ -840,3 +840,38 @@ resources:
|
|||||||
t.Fatal("expected error when referencing resource outside cwd, got nil")
|
t.Fatal("expected error when referencing resource outside cwd, got nil")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Build_preserveAllLabels(t *testing.T) {
|
||||||
|
b, err := NewBuilder("test-ks", "testdata/invalid-labels/resources",
|
||||||
|
WithDryRun(true),
|
||||||
|
WithNamespace("flux-system"),
|
||||||
|
WithKustomizationFile("testdata/invalid-labels/kustomization.yaml"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to create builder: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
objects, err := b.Build()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("build failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(objects) == 0 {
|
||||||
|
t.Fatal("expected at least one object, got none")
|
||||||
|
}
|
||||||
|
|
||||||
|
rawLabels, _, _ := unstructured.NestedMap(objects[0].Object, "metadata", "labels")
|
||||||
|
|
||||||
|
if got, ok := rawLabels["invalid"]; !ok || got != int64(0) {
|
||||||
|
t.Errorf("expected label invalid=0, got %#v", got)
|
||||||
|
}
|
||||||
|
if got, ok := rawLabels["valid"]; !ok || got != "yes" {
|
||||||
|
t.Errorf("expected label valid=\"yes\", got %v", got)
|
||||||
|
}
|
||||||
|
if got := rawLabels[controllerGroup+"/name"]; got != "test-ks" {
|
||||||
|
t.Errorf("expected owner name label \"test-ks\", got %v", got)
|
||||||
|
}
|
||||||
|
if got := rawLabels[controllerGroup+"/namespace"]; got != "flux-system" {
|
||||||
|
t.Errorf("expected owner namespace label \"flux-system\", got %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
apiVersion: kustomize.toolkit.fluxcd.io/v1
|
||||||
|
kind: Kustomization
|
||||||
|
metadata:
|
||||||
|
name: test-ks
|
||||||
|
namespace: flux-system
|
||||||
|
spec:
|
||||||
|
path: "./resources"
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: invalid-resource
|
||||||
|
labels:
|
||||||
|
invalid: 0
|
||||||
|
valid: yes
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||||
|
kind: Kustomization
|
||||||
|
resources:
|
||||||
|
- invalid-resource.yaml
|
||||||
Reference in New Issue
Block a user