Skip to content

Commit

Permalink
add managed-by label
Browse files Browse the repository at this point in the history
  • Loading branch information
friedrichwilken committed Jan 3, 2024
1 parent 0aa71fa commit b6bbe49
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
18 changes: 10 additions & 8 deletions internal/controller/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
},
),
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
@@ -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})
}
47 changes: 47 additions & 0 deletions pkg/labels/labels_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit b6bbe49

Please sign in to comment.