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): resources that were created on 2.7.x are missing namespace labels when synced on global #11794

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions pkg/kds/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
50 changes: 44 additions & 6 deletions pkg/kds/util/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
"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)
Expand All @@ -34,21 +36,57 @@
}
}

// 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

Check failure on line 43 in pkg/kds/util/meta.go

View workflow job for this annotation

GitHub Actions / check

`behaviour` is a misspelling of `behavior` (misspell)
// 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.
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
Loading