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(k8s): set annotation kuma.io/display-name for Secrets and Configs (backport of #11923) #11941

Merged
merged 4 commits into from
Nov 8, 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
32 changes: 32 additions & 0 deletions pkg/plugins/config/k8s/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"maps"
"time"

"github.com/pkg/errors"
Expand All @@ -12,11 +13,13 @@
kube_client "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/kumahq/kuma/api/mesh/v1alpha1"
system_proto "github.com/kumahq/kuma/api/system/v1alpha1"
config_model "github.com/kumahq/kuma/pkg/core/resources/apis/system"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
core_store "github.com/kumahq/kuma/pkg/core/resources/store"
common_k8s "github.com/kumahq/kuma/pkg/plugins/common/k8s"
"github.com/kumahq/kuma/pkg/plugins/resources/k8s"
)

var _ core_store.ResourceStore = &KubernetesStore{}
Expand Down Expand Up @@ -62,6 +65,11 @@
configMapKey: configRes.Spec.Config,
},
}

labels, annotations := k8s.SplitLabelsAndAnnotations(opts.Labels, cm.GetAnnotations())

Check failure on line 69 in pkg/plugins/config/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

undefined: k8s.SplitLabelsAndAnnotations
cm.GetObjectMeta().SetLabels(labels)
cm.GetObjectMeta().SetAnnotations(annotations)

if opts.Owner != nil {
k8sOwner, err := s.converter.ToKubernetesObject(opts.Owner)
if err != nil {
Expand All @@ -83,6 +91,7 @@
if !ok {
return newInvalidTypeError()
}
opts := core_store.NewUpdateOptions(fs...)
cm := &kube_core.ConfigMap{
TypeMeta: kube_meta.TypeMeta{
Kind: "ConfigMap",
Expand All @@ -94,6 +103,16 @@
configMapKey: configRes.Spec.Config,
},
}

updateLabels := cm.GetLabels()
if opts.ModifyLabels {

Check failure on line 108 in pkg/plugins/config/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

opts.ModifyLabels undefined (type *"github.com/kumahq/kuma/pkg/core/resources/store".UpdateOptions has no field or method ModifyLabels)
updateLabels = opts.Labels
}

labels, annotations := k8s.SplitLabelsAndAnnotations(updateLabels, cm.GetAnnotations())

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

View workflow job for this annotation

GitHub Actions / check

undefined: k8s.SplitLabelsAndAnnotations (typecheck)
cm.GetObjectMeta().SetLabels(labels)
cm.GetObjectMeta().SetAnnotations(annotations)

if err := s.client.Update(ctx, cm); err != nil {
if kube_apierrs.IsConflict(err) {
return core_store.ErrorResourceConflict(r.Descriptor().Name, r.GetMeta().GetName(), r.GetMeta().GetMesh())
Expand Down Expand Up @@ -192,6 +211,19 @@
return m.GetObjectMeta().GetCreationTimestamp().Time
}

func (m *KubernetesMetaAdapter) GetLabels() map[string]string {
labels := maps.Clone(m.GetObjectMeta().GetLabels())
if labels == nil {
labels = map[string]string{}
}
if displayName, ok := m.GetObjectMeta().GetAnnotations()[v1alpha1.DisplayName]; ok {
labels[v1alpha1.DisplayName] = displayName
} else {
labels[v1alpha1.DisplayName] = m.GetObjectMeta().GetName()
}
return labels
}

func newInvalidTypeError() error {
return errors.New("resource has a wrong type")
}
17 changes: 14 additions & 3 deletions pkg/plugins/config/k8s/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/controller-runtime/pkg/client"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
system_proto "github.com/kumahq/kuma/api/system/v1alpha1"
"github.com/kumahq/kuma/pkg/core"
system_model "github.com/kumahq/kuma/pkg/core/resources/apis/system"
Expand Down Expand Up @@ -91,13 +92,15 @@ var _ = Describe("KubernetesStore", func() {
kind: ConfigMap
metadata:
name: "kuma-internal-config"
namespace : "kuma-system"
namespace: "kuma-system"
data:
config: "test"
`).(*kube_core.ConfigMap)

// when
err := s.Create(context.Background(), config, core_store.CreateByKey("kuma-internal-config", ""))
err := s.Create(context.Background(), config, core_store.CreateByKey("kuma-internal-config", ""), core_store.CreateWithLabels(map[string]string{
mesh_proto.DisplayName: "kuma-internal-config",
}))

// then
Expect(err).ToNot(HaveOccurred())
Expand All @@ -108,6 +111,8 @@ var _ = Describe("KubernetesStore", func() {

// then
Expect(actual.Data).To(Equal(expected.Data))
Expect(actual.GetLabels()).NotTo(HaveKey(mesh_proto.DisplayName))
Expect(actual.GetAnnotations()).To(HaveKeyWithValue(mesh_proto.DisplayName, "kuma-internal-config"))
})
})

Expand All @@ -120,6 +125,8 @@ var _ = Describe("KubernetesStore", func() {
metadata:
name: "kuma-internal-config"
namespace : %s
annotations:
kuma.io/display-name: "kuma-internal-config"
data:
config: "test"
`, ns))
Expand All @@ -130,7 +137,9 @@ var _ = Describe("KubernetesStore", func() {
kind: ConfigMap
metadata:
name: "kuma-internal-config"
namespace : %s
namespace: %s
annotations:
kuma.io/display-name: "kuma-internal-config"
data:
config: "next test"
`, ns)).(*kube_core.ConfigMap)
Expand All @@ -156,6 +165,8 @@ var _ = Describe("KubernetesStore", func() {

// then
Expect(actual.Data).To(Equal(expected.Data))
Expect(actual.GetLabels()).NotTo(HaveKey(mesh_proto.DisplayName))
Expect(actual.GetAnnotations()).To(HaveKeyWithValue(mesh_proto.DisplayName, "kuma-internal-config"))
})

It("should return error in case of resource conflict", func() {
Expand Down
40 changes: 32 additions & 8 deletions pkg/plugins/secrets/k8s/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"fmt"
"maps"
"time"

"github.com/pkg/errors"
Expand All @@ -13,6 +14,7 @@
kube_client "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/kumahq/kuma/api/mesh/v1alpha1"
system_proto "github.com/kumahq/kuma/api/system/v1alpha1"
secret_model "github.com/kumahq/kuma/pkg/core/resources/apis/system"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
Expand Down Expand Up @@ -56,9 +58,8 @@
}
secret.Namespace = s.namespace
secret.Name = opts.Name
if r.Descriptor().Scope == core_model.ScopeMesh {
setMesh(secret, opts.Mesh, opts.Labels)
}

setLabelsAnnotationsAndMesh(secret, opts.Mesh, opts.Labels)

if opts.Owner != nil {
k8sOwner, err := s.resourcesConverter.ToKubernetesObject(opts.Owner)
Expand Down Expand Up @@ -91,9 +92,14 @@
return errors.Wrap(err, "failed to convert core Secret into k8s counterpart")
}
secret.Namespace = s.namespace
if r.Descriptor().Scope == core_model.ScopeMesh {
setMesh(secret, r.GetMeta().GetMesh(), opts.Labels)

updateLabels := r.GetMeta().GetLabels()
if opts.ModifyLabels {

Check failure on line 97 in pkg/plugins/secrets/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

opts.ModifyLabels undefined (type *"github.com/kumahq/kuma/pkg/core/resources/store".UpdateOptions has no field or method ModifyLabels)

Check failure on line 97 in pkg/plugins/secrets/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

opts.ModifyLabels undefined (type *"github.com/kumahq/kuma/pkg/core/resources/store".UpdateOptions has no field or method ModifyLabels)
updateLabels = opts.Labels
}

setLabelsAnnotationsAndMesh(secret, r.GetMeta().GetMesh(), updateLabels)
michaelbeaumont marked this conversation as resolved.
Show resolved Hide resolved

if err := s.writer.Update(ctx, secret); err != nil {
if kube_apierrs.IsConflict(err) {
return core_store.ErrorResourceConflict(r.Descriptor().Name, secret.Name, r.GetMeta().GetMesh())
Expand All @@ -107,12 +113,17 @@
return nil
}

func setMesh(s *kube_core.Secret, mesh string, labels map[string]string) {
func setLabelsAnnotationsAndMesh(s *kube_core.Secret, mesh string, labels map[string]string) {
if labels == nil {
labels = map[string]string{}
}
labels[metadata.KumaMeshLabel] = mesh
s.SetLabels(labels)
if mesh != "" {
labels[metadata.KumaMeshLabel] = mesh
}

labels, annotations := k8s.SplitLabelsAndAnnotations(labels, s.GetAnnotations())

Check failure on line 124 in pkg/plugins/secrets/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

undefined: k8s.SplitLabelsAndAnnotations (typecheck)

Check failure on line 124 in pkg/plugins/secrets/k8s/store.go

View workflow job for this annotation

GitHub Actions / check

undefined: k8s.SplitLabelsAndAnnotations) (typecheck)
s.GetObjectMeta().SetLabels(labels)
s.GetObjectMeta().SetAnnotations(annotations)
}

func (s *KubernetesStore) Delete(ctx context.Context, r core_model.Resource, fs ...core_store.DeleteOptionsFunc) error {
Expand Down Expand Up @@ -236,6 +247,19 @@
return m.GetObjectMeta().GetCreationTimestamp().Time
}

func (m *KubernetesMetaAdapter) GetLabels() map[string]string {
labels := maps.Clone(m.GetObjectMeta().GetLabels())
if labels == nil {
labels = map[string]string{}
}
if displayName, ok := m.GetObjectMeta().GetAnnotations()[v1alpha1.DisplayName]; ok {
labels[v1alpha1.DisplayName] = displayName
} else {
labels[v1alpha1.DisplayName] = m.GetObjectMeta().GetName()
}
return labels
}

type Converter interface {
ToKubernetesObject(resource core_model.Resource) (*kube_core.Secret, error)
ToCoreResource(secret *kube_core.Secret, out core_model.Resource) error
Expand Down
14 changes: 12 additions & 2 deletions pkg/plugins/secrets/k8s/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/controller-runtime/pkg/client"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
system_proto "github.com/kumahq/kuma/api/system/v1alpha1"
"github.com/kumahq/kuma/pkg/core"
core_system "github.com/kumahq/kuma/pkg/core/resources/apis/system"
Expand Down Expand Up @@ -106,7 +107,9 @@ var _ = Describe("KubernetesStore", func() {
`).(*kube_core.Secret)

// when
err := rs.Create(context.Background(), secret, store.CreateByKey(name, "demo"))
err := rs.Create(context.Background(), secret, store.CreateByKey(name, "demo"), store.CreateWithLabels(map[string]string{
mesh_proto.DisplayName: name,
}))

// then
Expect(err).ToNot(HaveOccurred())
Expand All @@ -122,6 +125,9 @@ var _ = Describe("KubernetesStore", func() {
// then
Expect(actual.Data).To(Equal(expected.Data))
Expect(actual.Type).To(Equal(expected.Type))
Expect(actual.GetObjectMeta().GetLabels()).NotTo(HaveKey(mesh_proto.DisplayName))
Expect(actual.GetObjectMeta().GetAnnotations()).To(HaveKeyWithValue(mesh_proto.DisplayName, name))

// and
Expect(actual.ObjectMeta.ResourceVersion).To(Equal(secret.Meta.GetVersion()))
})
Expand Down Expand Up @@ -192,9 +198,11 @@ var _ = Describe("KubernetesStore", func() {
name: %s
labels:
kuma.io/mesh: demo
annotations:
kuma.io/display-name: %s
data:
value: ZXhhbXBsZQ== # base64(example)
`, ns, name))
`, ns, name, name))
backend.Create(initial)
// and
expected := backend.ParseYAML(`
Expand Down Expand Up @@ -231,6 +239,8 @@ var _ = Describe("KubernetesStore", func() {
// then
Expect(actual.Data).To(Equal(expected.Data))
Expect(actual.Type).To(Equal(expected.Type))
Expect(actual.GetObjectMeta().GetLabels()).NotTo(HaveKey(mesh_proto.DisplayName))
Expect(actual.GetObjectMeta().GetAnnotations()).To(HaveKeyWithValue(mesh_proto.DisplayName, name))
// and
Expect(actual.ObjectMeta.ResourceVersion).To(Equal(secret.Meta.GetVersion()))
})
Expand Down
Loading