From b6bbe495fec57a1b0e85391385907c07baa22cb5 Mon Sep 17 00:00:00 2001 From: Friedrich Wilken Date: Wed, 3 Jan 2024 15:35:11 +0100 Subject: [PATCH] add managed-by label --- internal/controller/cache/cache.go | 18 +++++----- internal/controller/cache/cache_test.go | 4 +-- pkg/labels/labels.go | 38 ++++++++++++++++++++ pkg/labels/labels_test.go | 47 +++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 pkg/labels/labels.go create mode 100644 pkg/labels/labels_test.go diff --git a/internal/controller/cache/cache.go b/internal/controller/cache/cache.go index 9e04da62..80add700 100644 --- a/internal/controller/cache/cache.go +++ b/internal/controller/cache/cache.go @@ -4,13 +4,14 @@ import ( appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" corev1 "k8s.io/api/core/v1" + policyv1 "k8s.io/api/policy/v1" rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/kyma-project/nats-manager/pkg/label" + natslabels "github.com/kyma-project/nats-manager/pkg/labels" ) // New returns a cache with the cache-options applied, generade form the rest-config. @@ -19,16 +20,17 @@ func New(config *rest.Config, options cache.Options) (cache.Cache, error) { } func applySelectors(options cache.Options) cache.Options { - // The only objects we allow are the ones with the 'created-by: nats-manager' label applied. - createdByNATSManager := fromLabelSelector(label.SelectorCreatedByNATS()) + // The only objects we allow are the ones with the 'managed-by: nats-manager' label applied. + managedByNATS := fromLabelSelector(natslabels.SelectorManagedByNATS()) // Apply the label selector to all relevant objects. options.ByObject = map[client.Object]cache.ByObject{ - &appsv1.Deployment{}: createdByNATSManager, - &autoscalingv1.HorizontalPodAutoscaler{}: createdByNATSManager, - &corev1.ServiceAccount{}: createdByNATSManager, - &rbacv1.ClusterRole{}: createdByNATSManager, - &rbacv1.ClusterRoleBinding{}: createdByNATSManager, + &appsv1.Deployment{}: managedByNATS, + &autoscalingv1.HorizontalPodAutoscaler{}: managedByNATS, + &corev1.ServiceAccount{}: managedByNATS, + &rbacv1.ClusterRole{}: managedByNATS, + &rbacv1.ClusterRoleBinding{}: managedByNATS, + &policyv1.PodDisruptionBudget{}: managedByNATS, } return options } diff --git a/internal/controller/cache/cache_test.go b/internal/controller/cache/cache_test.go index 416b9cec..00f406c0 100644 --- a/internal/controller/cache/cache_test.go +++ b/internal/controller/cache/cache_test.go @@ -6,7 +6,7 @@ import ( "testing" "time" - "github.com/kyma-project/nats-manager/pkg/label" + natslabels "github.com/kyma-project/nats-manager/pkg/labels" "github.com/stretchr/testify/require" appsv1 "k8s.io/api/apps/v1" autoscalingv1 "k8s.io/api/autoscaling/v1" @@ -23,7 +23,7 @@ func Test_applySelectors(t *testing.T) { selector := cache.ByObject{ Label: labels.SelectorFromSet( map[string]string{ - label.KeyCreatedBy: label.ValueNATSManager, + natslabels.KeyCreatedBy: natslabels.ValueNATSManager, }, ), } diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go new file mode 100644 index 00000000..80d4eacd --- /dev/null +++ b/pkg/labels/labels.go @@ -0,0 +1,38 @@ +package labels + +import ( + "k8s.io/apimachinery/pkg/labels" +) + +const ( + // Kubernetes label keys used by nats-manager. + KeyComponent = "app.kubernetes.io/component" + KeyCreatedBy = "app.kubernetes.io/created-by" + KeyInstance = "app.kubernetes.io/instance" + KeyManagedBy = "app.kubernetes.io/managed-by" + KeyName = "app.kubernetes.io/name" + KeyPartOf = "app.kubernetes.io/part-of" + KeyDashboard = "kyma-project.io/dashboard" + + // Kubernetes label values used by nats-manager. + ValueNATS = "nats" + ValueNATSManager = "nats-manager" +) + +// SelectorInstanceNATS returns a labelselector for instance ("app.kubernetes.io/instance") as used +// by the nats-manager. +func SelectorInstanceNATS() labels.Selector { + return labels.SelectorFromSet(map[string]string{KeyInstance: ValueNATSManager}) +} + +// SelectorCreatedByNATS returns a labelselector for created-by ("app.kubernetes.io/created-by") as used +// by the nats-manager. +func SelectorCreatedByNATS() labels.Selector { + return labels.SelectorFromSet(map[string]string{KeyCreatedBy: ValueNATSManager}) +} + +// SelectorCreatedByNATS returns a labelselector for created-by ("app.kubernetes.io/created-by") as used +// by the nats-manager. +func SelectorManagedByNATS() labels.Selector { + return labels.SelectorFromSet(map[string]string{KeyCreatedBy: ValueNATSManager}) +} diff --git a/pkg/labels/labels_test.go b/pkg/labels/labels_test.go new file mode 100644 index 00000000..b3642261 --- /dev/null +++ b/pkg/labels/labels_test.go @@ -0,0 +1,47 @@ +package labels + +import ( + "reflect" + "testing" + + "k8s.io/apimachinery/pkg/labels" +) + +func TestSelectorInstanceNATS(t *testing.T) { + // arrange + wantedSelector := labels.SelectorFromSet(map[string]string{"app.kubernetes.io/instance": "nats-manager"}) + + // act + actualSelector := SelectorInstanceNATS() + + // assert + if !reflect.DeepEqual(wantedSelector, actualSelector) { + t.Errorf("Expected %v, but got %v", wantedSelector, actualSelector) + } +} + +func TestSelectorCreatedByNATS(t *testing.T) { + // arrange + wantedSelector := labels.SelectorFromSet(map[string]string{"app.kubernetes.io/created-by": "nats-manager"}) + + // act + actualSelector := SelectorCreatedByNATS() + + // assert + if !reflect.DeepEqual(wantedSelector, actualSelector) { + t.Errorf("Expected %v, but got %v", wantedSelector, actualSelector) + } +} + +func TestSelectorManagedByNATS(t *testing.T) { + // arrange + wantedSelector := labels.SelectorFromSet(map[string]string{"app.kubernetes.io/managed-by": "nats-manager"}) + + // act + actualSelector := SelectorManagedByNATS() + + // assert + if !reflect.DeepEqual(wantedSelector, actualSelector) { + t.Errorf("Expected %v, but got %v", wantedSelector, actualSelector) + } +}