From 3403eb9783004363108035d478983a9af6ed97c9 Mon Sep 17 00:00:00 2001 From: Ilya Lobkov Date: Thu, 17 Oct 2024 19:10:21 +0200 Subject: [PATCH 1/2] fix(kuma-cp): resources that were created on 2.7.x are missing namespace labels when synced on global Signed-off-by: Ilya Lobkov --- pkg/kds/context/context.go | 1 + pkg/kds/util/meta.go | 50 +++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/pkg/kds/context/context.go b/pkg/kds/context/context.go index 0f5301b2b110..ab38f2d34b08 100644 --- a/pkg/kds/context/context.go +++ b/pkg/kds/context/context.go @@ -103,6 +103,7 @@ func DefaultContext( util.WithLabel(mesh_proto.ResourceOriginLabel, string(mesh_proto.ZoneResourceOrigin)), util.WithLabel(mesh_proto.ZoneTag, cfg.Multizone.Zone.Name), util.WithoutLabel(mesh_proto.DeletionGracePeriodStartedLabel), + util.If(util.IsKubernetes(cfg.Store.Type), util.PopulateNamespaceLabelFromNameExtension()), ), MapInsightResourcesZeroGeneration, reconcile_v2.If( diff --git a/pkg/kds/util/meta.go b/pkg/kds/util/meta.go index 0a66c506a778..8941b322cbab 100644 --- a/pkg/kds/util/meta.go +++ b/pkg/kds/util/meta.go @@ -6,15 +6,17 @@ import ( "golang.org/x/exp/maps" mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1" + config_store "github.com/kumahq/kuma/pkg/config/core/resources/store" "github.com/kumahq/kuma/pkg/core/resources/model" ) // KDS ResourceMeta only contains name and mesh. // The rest is managed by the receiver of resources anyways. See ResourceSyncer#Sync type resourceMeta struct { - name string - mesh string - labels map[string]string + name string + mesh string + labels map[string]string + nameExtensions model.ResourceNameExtensions } type CloneResourceMetaOpt func(*resourceMeta) @@ -34,21 +36,57 @@ func WithLabel(key, value string) CloneResourceMetaOpt { } } +// PopulateNamespaceLabelFromNameExtension on Kubernetes zones adds 'k8s.kuma.io/namespace' label to the resources +// before syncing them to Global. +// +// In 2.7.x method 'GetMeta().GetLabels()' on Kubernetes returned a label map with 'k8s.kuma.io/namespace' added +// dynamically. This behaviour was changed in 2.9.x by https://github.com/kumahq/kuma/pull/11020, the namespace label is now +// supposed to be set in ComputeLabels function. But this functions is called only on Create/Update of the resources. +// This means policies that were created on 2.7.x won't have 'k8s.kuma.io/namespace' label when synced to Global. +// Even though the lack of namespace labels affects only how resource looks in GUI on Global it's still worth setting it. +func PopulateNamespaceLabelFromNameExtension() CloneResourceMetaOpt { + return func(m *resourceMeta) { + namespace := m.nameExtensions[model.K8sNamespaceComponent] + if _, ok := m.labels[mesh_proto.KubeNamespaceTag]; !ok && namespace != "" { + m.labels[mesh_proto.KubeNamespaceTag] = namespace + } + } +} + func WithoutLabel(key string) CloneResourceMetaOpt { return func(m *resourceMeta) { delete(m.labels, key) } } +func If(condition func(resource model.ResourceMeta) bool, fn CloneResourceMetaOpt) CloneResourceMetaOpt { + return func(meta *resourceMeta) { + if condition(meta) { + fn(meta) + } + } +} + +func IsKubernetes(storeType config_store.StoreType) func(model.ResourceMeta) bool { + return func(_ model.ResourceMeta) bool { + return storeType == config_store.KubernetesStore + } +} + func CloneResourceMeta(m model.ResourceMeta, fs ...CloneResourceMetaOpt) model.ResourceMeta { labels := maps.Clone(m.GetLabels()) if labels == nil { labels = map[string]string{} } + ne := maps.Clone(m.GetNameExtensions()) + if ne == nil { + ne = model.ResourceNameExtensions{} + } meta := &resourceMeta{ - name: m.GetName(), - mesh: m.GetMesh(), - labels: labels, + name: m.GetName(), + mesh: m.GetMesh(), + labels: labels, + nameExtensions: ne, } for _, f := range fs { f(meta) From d387e6e85195f23001128852f554a6ff9a2097ff Mon Sep 17 00:00:00 2001 From: Ilya Lobkov Date: Fri, 18 Oct 2024 00:24:33 +0200 Subject: [PATCH 2/2] make check Signed-off-by: Ilya Lobkov --- pkg/kds/util/meta.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kds/util/meta.go b/pkg/kds/util/meta.go index 8941b322cbab..3f61761ee239 100644 --- a/pkg/kds/util/meta.go +++ b/pkg/kds/util/meta.go @@ -40,7 +40,7 @@ func WithLabel(key, value string) CloneResourceMetaOpt { // before syncing them to Global. // // In 2.7.x method 'GetMeta().GetLabels()' on Kubernetes returned a label map with 'k8s.kuma.io/namespace' added -// dynamically. This behaviour was changed in 2.9.x by https://github.com/kumahq/kuma/pull/11020, the namespace label is now +// dynamically. This behavior was changed in 2.9.x by https://github.com/kumahq/kuma/pull/11020, the namespace label is now // supposed to be set in ComputeLabels function. But this functions is called only on Create/Update of the resources. // This means policies that were created on 2.7.x won't have 'k8s.kuma.io/namespace' label when synced to Global. // Even though the lack of namespace labels affects only how resource looks in GUI on Global it's still worth setting it.