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(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (backport of #10430) #11948

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 10 additions & 2 deletions pkg/plugins/resources/k8s/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
return err
}

labels, annotations := splitLabelsAndAnnotations(opts.Labels, obj.GetAnnotations())
labels, annotations := SplitLabelsAndAnnotations(opts.Labels, obj.GetAnnotations())
obj.GetObjectMeta().SetLabels(labels)
obj.GetObjectMeta().SetAnnotations(annotations)
obj.SetMesh(opts.Mesh)
Expand Down Expand Up @@ -99,7 +99,15 @@
return errors.Wrapf(err, "failed to convert core model of type %s into k8s counterpart", r.Descriptor().Name)
}

<<<<<<< HEAD

Check failure on line 102 in pkg/plugins/resources/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

syntax error: unexpected <<, expected }

Check failure on line 102 in pkg/plugins/resources/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

syntax error: unexpected <<, expected }

Check failure on line 102 in pkg/plugins/resources/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

syntax error: unexpected <<, expected }
labels, annotations := splitLabelsAndAnnotations(opts.Labels, obj.GetAnnotations())
=======
updateLabels := r.GetMeta().GetLabels()
if opts.ModifyLabels {
updateLabels = opts.Labels
}
labels, annotations := SplitLabelsAndAnnotations(updateLabels, obj.GetAnnotations())
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))

Check failure on line 110 in pkg/plugins/resources/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

more than one character in rune literal

Check failure on line 110 in pkg/plugins/resources/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

invalid character U+0023 '#') (typecheck)

Check failure on line 110 in pkg/plugins/resources/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

more than one character in rune literal

Check failure on line 110 in pkg/plugins/resources/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

invalid character U+0023 '#') (typecheck)
obj.GetObjectMeta().SetLabels(labels)
obj.GetObjectMeta().SetAnnotations(annotations)
obj.SetMesh(r.GetMeta().GetMesh())
Expand Down Expand Up @@ -237,7 +245,7 @@

// Kuma resource labels are generally stored on Kubernetes as labels, except "kuma.io/display-name".
// We store it as an annotation because the resource name on k8s is limited by 253 and the label value is limited by 63.
func splitLabelsAndAnnotations(coreLabels map[string]string, currentAnnotations map[string]string) (map[string]string, map[string]string) {
func SplitLabelsAndAnnotations(coreLabels map[string]string, currentAnnotations map[string]string) (map[string]string, map[string]string) {
labels := maps.Clone(coreLabels)
annotations := maps.Clone(currentAnnotations)
if annotations == nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/plugins/runtime/k8s/webhooks/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
"github.com/kumahq/kuma/pkg/core/resources/registry"
k8s_common "github.com/kumahq/kuma/pkg/plugins/common/k8s"
<<<<<<< HEAD
"github.com/kumahq/kuma/pkg/plugins/runtime/k8s/metadata"
=======
"github.com/kumahq/kuma/pkg/plugins/resources/k8s"
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
)

type Defaulter interface {
Expand Down Expand Up @@ -72,6 +76,7 @@ func (h *defaultingHandler) Handle(ctx context.Context, req admission.Request) a
if resp := h.IsOperationAllowed(req.UserInfo, resource); !resp.Allowed {
return resp
}
<<<<<<< HEAD

if resource.Descriptor().Scope == core_model.ScopeMesh {
labels := obj.GetLabels()
Expand All @@ -94,6 +99,14 @@ func (h *defaultingHandler) Handle(ctx context.Context, req admission.Request) a
obj.SetLabels(labels)
}
}
=======
labels, annotations := k8s.SplitLabelsAndAnnotations(
core_model.ComputeLabels(resource, h.Mode, true, h.SystemNamespace),
obj.GetAnnotations(),
)
obj.SetLabels(labels)
obj.SetAnnotations(annotations)
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))

marshaled, err := json.Marshal(obj)
if err != nil {
Expand Down
69 changes: 69 additions & 0 deletions pkg/plugins/runtime/k8s/webhooks/defaulter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ var _ = Describe("Defaulter", func() {
"kind": "Mesh",
"metadata": {
"name": "empty",
<<<<<<< HEAD
"creationTimestamp": null
=======
"creationTimestamp": null,
"annotations": {
"kuma.io/display-name": "empty"
}
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
},
"spec": {
"metrics": {
Expand Down Expand Up @@ -171,7 +178,14 @@ var _ = Describe("Defaulter", func() {
"kind": "Mesh",
"metadata": {
"name": "empty",
<<<<<<< HEAD
"creationTimestamp": null
=======
"creationTimestamp": null,
"annotations": {
"kuma.io/display-name": "empty"
}
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
},
"spec": {
"metrics": {
Expand Down Expand Up @@ -220,7 +234,15 @@ var _ = Describe("Defaulter", func() {
"name": "empty",
"creationTimestamp": null,
"labels": {
<<<<<<< HEAD
"kuma.io/mesh": "my-mesh-1"
=======
"kuma.io/mesh": "my-mesh-1",
"k8s.kuma.io/namespace": "example"
},
"annotations": {
"kuma.io/display-name": "empty"
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
}
},
"spec": {}
Expand Down Expand Up @@ -257,7 +279,16 @@ var _ = Describe("Defaulter", func() {
"creationTimestamp": null,
"labels": {
"kuma.io/origin": "zone",
<<<<<<< HEAD
"kuma.io/mesh": "default"
=======
"kuma.io/mesh": "default",
"kuma.io/policy-role": "workload-owner",
"k8s.kuma.io/namespace": "example"
},
"annotations": {
"kuma.io/display-name": "empty"
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
}
},
"spec": {
Expand Down Expand Up @@ -292,8 +323,18 @@ var _ = Describe("Defaulter", func() {
"name": "empty",
"creationTimestamp": null,
"labels": {
<<<<<<< HEAD
"kuma.io/origin": "zone",
"kuma.io/mesh": "default"
=======
"k8s.kuma.io/namespace": "example",
"kuma.io/mesh": "default",
"kuma.io/origin": "zone",
"kuma.io/policy-role": "workload-owner"
},
"annotations": {
"kuma.io/display-name": "empty"
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
}
},
"spec": {
Expand Down Expand Up @@ -328,8 +369,18 @@ var _ = Describe("Defaulter", func() {
"name": "empty",
"creationTimestamp": null,
"labels": {
<<<<<<< HEAD
"kuma.io/origin": "zone",
"kuma.io/mesh": "default"
=======
"k8s.kuma.io/namespace": "example",
"kuma.io/mesh": "default",
"kuma.io/origin": "zone",
"kuma.io/policy-role": "workload-owner"
},
"annotations": {
"kuma.io/display-name": "empty"
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
}
},
"spec": {
Expand Down Expand Up @@ -375,8 +426,17 @@ var _ = Describe("Defaulter", func() {
"name":"empty",
"creationTimestamp":null,
"labels": {
<<<<<<< HEAD
"kuma.io/origin": "zone",
"kuma.io/mesh": "default"
=======
"k8s.kuma.io/namespace": "example",
"kuma.io/mesh": "default",
"kuma.io/origin": "zone"
},
"annotations": {
"kuma.io/display-name": "empty"
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
}
},
"spec":{
Expand Down Expand Up @@ -420,7 +480,16 @@ var _ = Describe("Defaulter", func() {
"name": "empty",
"creationTimestamp": null,
"labels": {
<<<<<<< HEAD
"kuma.io/mesh": "default"
=======
"k8s.kuma.io/namespace": "example",
"kuma.io/mesh": "default",
"kuma.io/policy-role": "workload-owner"
},
"annotations": {
"kuma.io/display-name": "empty"
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
}
},
"spec": {
Expand Down
Loading