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) #11946

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
37 changes: 37 additions & 0 deletions pkg/plugins/resources/k8s/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
if err != nil {
return err
}
<<<<<<< HEAD

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

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected <<, expected }
=======

labels, annotations := SplitLabelsAndAnnotations(opts.Labels, obj.GetAnnotations())
obj.GetObjectMeta().SetLabels(labels)
obj.GetObjectMeta().SetAnnotations(annotations)
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))

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

View workflow job for this annotation

GitHub Actions / lint

more than one character in rune literal

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

View workflow job for this annotation

GitHub Actions / lint

invalid character U+0023 '#'
obj.SetMesh(opts.Mesh)
obj.GetObjectMeta().SetName(name)
obj.GetObjectMeta().SetNamespace(namespace)
Expand Down Expand Up @@ -91,6 +98,18 @@
return errors.Wrapf(err, "failed to convert core model of type %s into k8s counterpart", r.Descriptor().Name)
}

<<<<<<< HEAD

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

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected <<, expected }
=======
updateLabels := r.GetMeta().GetLabels()
if opts.ModifyLabels {
updateLabels = opts.Labels
}
labels, annotations := SplitLabelsAndAnnotations(updateLabels, obj.GetAnnotations())
obj.GetObjectMeta().SetLabels(labels)
obj.GetObjectMeta().SetAnnotations(annotations)
obj.SetMesh(r.GetMeta().GetMesh())

>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))

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

View workflow job for this annotation

GitHub Actions / lint

more than one character in rune literal

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

View workflow job for this annotation

GitHub Actions / lint

invalid character U+0023 '#'
if err := s.Client.Update(ctx, obj); err != nil {
if kube_apierrs.IsConflict(err) {
return store.ErrorResourceConflict(r.Descriptor().Name, r.GetMeta().GetName(), r.GetMeta().GetMesh())
Expand Down Expand Up @@ -222,6 +241,24 @@
}
}

<<<<<<< HEAD

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

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body
=======
// 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) {
labels := maps.Clone(coreLabels)
annotations := maps.Clone(currentAnnotations)
if annotations == nil {
annotations = map[string]string{}
}
if v, ok := labels[v1alpha1.DisplayName]; ok {
annotations[v1alpha1.DisplayName] = v
delete(labels, v1alpha1.DisplayName)
}
return labels, annotations
}

>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))

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

View workflow job for this annotation

GitHub Actions / lint

syntax error: non-declaration statement outside function body

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

View workflow job for this annotation

GitHub Actions / lint

more than one character in rune literal
var _ core_model.ResourceMeta = &KubernetesMetaAdapter{}

type KubernetesMetaAdapter struct {
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 @@ -11,7 +11,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 @@ -74,6 +78,15 @@ func (h *defaultingHandler) Handle(ctx context.Context, req admission.Request) a
obj.SetLabels(labels)
}
}
<<<<<<< HEAD
=======
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
251 changes: 251 additions & 0 deletions pkg/plugins/runtime/k8s/webhooks/defaulter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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 @@ -179,7 +186,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 @@ -227,12 +241,249 @@ 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": {}
}
`,
}),
<<<<<<< HEAD
=======
Entry("should set mesh label when apply new policy on Zone", testCase{
checker: zoneChecker(true, true),
kind: string(v1alpha1.MeshTrafficPermissionType),
inputObject: `
{
"apiVersion": "kuma.io/v1alpha1",
"kind": "MeshTrafficPermission",
"metadata": {
"namespace": "example",
"name": "empty",
"creationTimestamp": null,
"labels": {
"kuma.io/origin": "zone"
}
},
"spec": {
"targetRef": {}
}
}
`,
expected: `
{
"apiVersion": "kuma.io/v1alpha1",
"kind": "MeshTrafficPermission",
"metadata": {
"namespace": "example",
"name": "empty",
"creationTimestamp": null,
"labels": {
"kuma.io/origin": "zone",
"kuma.io/mesh": "default",
"kuma.io/policy-role": "workload-owner",
"k8s.kuma.io/namespace": "example"
},
"annotations": {
"kuma.io/display-name": "empty"
}
},
"spec": {
"targetRef": {}
}
}
`,
}),
Entry("should set mesh and origin label when origin validation is disabled, federated zone", testCase{
checker: zoneChecker(true, false),
kind: string(v1alpha1.MeshTrafficPermissionType),
inputObject: `
{
"apiVersion": "kuma.io/v1alpha1",
"kind": "MeshTrafficPermission",
"metadata": {
"namespace": "example",
"name": "empty",
"creationTimestamp": null
},
"spec": {
"targetRef": {}
}
}
`,
expected: `
{
"apiVersion": "kuma.io/v1alpha1",
"kind": "MeshTrafficPermission",
"metadata": {
"namespace": "example",
"name": "empty",
"creationTimestamp": null,
"labels": {
"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"
}
},
"spec": {
"targetRef": {}
}
}
`,
}),
Entry("should set mesh and origin label when origin validation is disabled, non-federated zone", testCase{
checker: zoneChecker(false, false),
kind: string(v1alpha1.MeshTrafficPermissionType),
inputObject: `
{
"apiVersion": "kuma.io/v1alpha1",
"kind": "MeshTrafficPermission",
"metadata": {
"namespace": "example",
"name": "empty",
"creationTimestamp": null
},
"spec": {
"targetRef": {}
}
}
`,
expected: `
{
"apiVersion": "kuma.io/v1alpha1",
"kind": "MeshTrafficPermission",
"metadata": {
"namespace": "example",
"name": "empty",
"creationTimestamp": null,
"labels": {
"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"
}
},
"spec": {
"targetRef": {}
}
}
`,
}),
Entry("should set mesh and origin label on DPP", testCase{
checker: zoneChecker(true, true),
kind: string(mesh.DataplaneType),
inputObject: `
{
"apiVersion":"kuma.io/v1alpha1",
"kind":"Dataplane",
"mesh":"demo",
"metadata":{
"namespace":"example",
"name":"empty",
"creationTimestamp":null
},
"spec":{
"networking": {
"address": "127.0.0.1",
"inbound": [
{
"port": 11011,
"tags": {
"kuma.io/service": "backend"
}
}
]
}
}
}`,
expected: `
{
"apiVersion":"kuma.io/v1alpha1",
"kind":"Dataplane",
"mesh":"demo",
"metadata":{
"namespace":"example",
"name":"empty",
"creationTimestamp":null,
"labels": {
"k8s.kuma.io/namespace": "example",
"kuma.io/mesh": "default",
"kuma.io/origin": "zone"
},
"annotations": {
"kuma.io/display-name": "empty"
}
},
"spec":{
"networking": {
"address": "127.0.0.1",
"inbound": [
{
"port": 11011,
"tags": {
"kuma.io/service": "backend"
}
}
]
}
}
}`,
}),
Entry("should not add origin label on Global", testCase{
checker: globalChecker(),
kind: string(v1alpha1.MeshTrafficPermissionType),
inputObject: `
{
"apiVersion": "kuma.io/v1alpha1",
"kind": "MeshTrafficPermission",
"metadata": {
"namespace": "example",
"name": "empty",
"creationTimestamp": null
},
"spec": {
"targetRef": {}
}
}
`,
expected: `
{
"apiVersion": "kuma.io/v1alpha1",
"kind": "MeshTrafficPermission",
"metadata": {
"namespace": "example",
"name": "empty",
"creationTimestamp": null,
"labels": {
"k8s.kuma.io/namespace": "example",
"kuma.io/mesh": "default",
"kuma.io/policy-role": "workload-owner"
},
"annotations": {
"kuma.io/display-name": "empty"
}
},
"spec": {
"targetRef": {}
}
}
`,
}),
>>>>>>> da824ce57 (fix(kuma-cp): mistakenly setting 'kuma.io/display-name' as label (#10430))
)
})
Loading