From 513d52382360012e0a880e30c2abd919ca28fe8e Mon Sep 17 00:00:00 2001 From: marcobebway Date: Wed, 22 Nov 2023 17:54:24 +0100 Subject: [PATCH] Add created by label --- Dockerfile | 1 + config/manager/service.yaml | 2 + .../ui-extensions/eventing/kustomization.yaml | 1 + .../subscriptions/subscriptions.yaml | 1 + .../eventing/controller.go | 1 - .../eventing/eventmesh.go | 3 +- .../eventing/eventmesh_test.go | 13 ++-- internal/label/label.go | 13 ++++ pkg/eventing/deployment.go | 19 ++--- pkg/eventing/deployment_test.go | 10 +-- pkg/eventing/manager.go | 6 +- pkg/eventing/utils.go | 4 +- pkg/object/equality_test.go | 74 +++++++++++++++++++ 13 files changed, 118 insertions(+), 30 deletions(-) create mode 100644 internal/label/label.go diff --git a/Dockerfile b/Dockerfile index dd11fe283..61c8d74b2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,6 +15,7 @@ RUN go mod download COPY cmd/main.go cmd/main.go COPY api/ api/ COPY internal/controller/ internal/controller/ +COPY internal/label/ internal/label/ COPY pkg/ pkg/ COPY testing/ testing/ COPY options/ options/ diff --git a/config/manager/service.yaml b/config/manager/service.yaml index e5561ac56..db6375361 100644 --- a/config/manager/service.yaml +++ b/config/manager/service.yaml @@ -4,6 +4,7 @@ metadata: name: eventing-manager-metrics labels: control-plane: eventing-manager + app.kubernetes.io/created-by: eventing-manager app.kubernetes.io/name: eventing-manager app.kubernetes.io/instance: eventing-manager app.kubernetes.io/component: eventing-manager @@ -30,6 +31,7 @@ metadata: name: eventing-manager-health labels: control-plane: eventing-manager + app.kubernetes.io/created-by: eventing-manager app.kubernetes.io/name: eventing-manager app.kubernetes.io/instance: eventing-manager app.kubernetes.io/component: eventing-manager diff --git a/config/ui-extensions/eventing/kustomization.yaml b/config/ui-extensions/eventing/kustomization.yaml index a5f043f89..9175e1a19 100644 --- a/config/ui-extensions/eventing/kustomization.yaml +++ b/config/ui-extensions/eventing/kustomization.yaml @@ -9,6 +9,7 @@ configMapGenerator: options: disableNameSuffixHash: true labels: + app.kubernetes.io/created-by: eventing-manager app.kubernetes.io/name: eventings.operator.kyma-project.io busola.io/extension: resource busola.io/extension-version: "0.5" diff --git a/config/ui-extensions/subscriptions/subscriptions.yaml b/config/ui-extensions/subscriptions/subscriptions.yaml index f0bb09d58..2ac13b629 100644 --- a/config/ui-extensions/subscriptions/subscriptions.yaml +++ b/config/ui-extensions/subscriptions/subscriptions.yaml @@ -4,6 +4,7 @@ metadata: name: subscriptions namespace: kube-public labels: + app.kubernetes.io/created-by: eventing-manager app.kubernetes.io/name: subscriptions busola.io/extension: resource busola.io/extension-version: '0.5' diff --git a/internal/controller/operator.kyma-project.io/eventing/controller.go b/internal/controller/operator.kyma-project.io/eventing/controller.go index eb5b676d3..96fc645e1 100644 --- a/internal/controller/operator.kyma-project.io/eventing/controller.go +++ b/internal/controller/operator.kyma-project.io/eventing/controller.go @@ -61,7 +61,6 @@ const ( NatsServerNotAvailableMsg = "NATS server is not available" natsClientPort = 4222 - AppLabelValue = eventing.PublisherName PublisherSecretEMSHostKey = "ems-publish-host" TokenEndpointFormat = "%s?grant_type=%s&response_type=token" diff --git a/internal/controller/operator.kyma-project.io/eventing/eventmesh.go b/internal/controller/operator.kyma-project.io/eventing/eventmesh.go index 8c6bbbd16..a6770875c 100644 --- a/internal/controller/operator.kyma-project.io/eventing/eventmesh.go +++ b/internal/controller/operator.kyma-project.io/eventing/eventmesh.go @@ -15,6 +15,7 @@ import ( "k8s.io/apimachinery/pkg/types" "github.com/kyma-project/eventing-manager/api/operator.kyma-project.io/v1alpha1" + "github.com/kyma-project/eventing-manager/internal/label" "github.com/kyma-project/eventing-manager/pkg/env" "github.com/kyma-project/eventing-manager/pkg/eventing" subscriptionmanager "github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/manager" @@ -297,7 +298,7 @@ func getSecretForPublisher(eventMeshSecret *corev1.Secret) (*corev1.Secret, erro secret := newSecret(eventing.PublisherName, eventMeshSecret.Namespace) secret.Labels = map[string]string{ - eventing.AppLabelKey: AppLabelValue, + label.KeyName: label.ValueEventingPublisherProxy, } if _, ok := eventMeshSecret.Data["messaging"]; !ok { diff --git a/internal/controller/operator.kyma-project.io/eventing/eventmesh_test.go b/internal/controller/operator.kyma-project.io/eventing/eventmesh_test.go index 9aa3f9161..28ed9cdf4 100644 --- a/internal/controller/operator.kyma-project.io/eventing/eventmesh_test.go +++ b/internal/controller/operator.kyma-project.io/eventing/eventmesh_test.go @@ -5,12 +5,6 @@ import ( "errors" "testing" - "github.com/kyma-project/eventing-manager/pkg/eventing" - - "github.com/kyma-project/eventing-manager/api/operator.kyma-project.io/v1alpha1" - "github.com/kyma-project/eventing-manager/pkg/k8s" - k8smocks "github.com/kyma-project/eventing-manager/pkg/k8s/mocks" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -19,8 +13,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" + "github.com/kyma-project/eventing-manager/api/operator.kyma-project.io/v1alpha1" + "github.com/kyma-project/eventing-manager/internal/label" "github.com/kyma-project/eventing-manager/pkg/env" + "github.com/kyma-project/eventing-manager/pkg/eventing" managermocks "github.com/kyma-project/eventing-manager/pkg/eventing/mocks" + "github.com/kyma-project/eventing-manager/pkg/k8s" + k8smocks "github.com/kyma-project/eventing-manager/pkg/k8s/mocks" "github.com/kyma-project/eventing-manager/pkg/logger" submanagermocks "github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/manager/mocks" subscriptionmanagermocks "github.com/kyma-project/eventing-manager/pkg/subscriptionmanager/mocks" @@ -628,7 +627,7 @@ func Test_GetSecretForPublisher(t *testing.T) { Name: eventing.PublisherName, Namespace: "test-namespace", Labels: map[string]string{ - eventing.AppLabelKey: eventing.PublisherName, + label.KeyName: label.ValueEventingPublisherProxy, }, }, StringData: map[string]string{ diff --git a/internal/label/label.go b/internal/label/label.go new file mode 100644 index 000000000..9c38439a9 --- /dev/null +++ b/internal/label/label.go @@ -0,0 +1,13 @@ +package label + +const ( + KeyCreatedBy = "app.kubernetes.io/created-by" + KeyInstance = "app.kubernetes.io/instance" + KeyName = "app.kubernetes.io/name" + KeyBackend = "eventing.kyma-project.io/backend" + KeyDashboard = "kyma-project.io/dashboard" + + ValueEventingPublisherProxy = "eventing-publisher-proxy" + ValueEventingManager = "eventing-manager" + ValueEventing = "eventing" +) diff --git a/pkg/eventing/deployment.go b/pkg/eventing/deployment.go index d3a183162..046057fba 100644 --- a/pkg/eventing/deployment.go +++ b/pkg/eventing/deployment.go @@ -12,6 +12,7 @@ import ( "k8s.io/apimachinery/pkg/util/intstr" "github.com/kyma-project/eventing-manager/api/operator.kyma-project.io/v1alpha1" + "github.com/kyma-project/eventing-manager/internal/label" "github.com/kyma-project/eventing-manager/pkg/env" "github.com/kyma-project/eventing-manager/pkg/utils" ) @@ -21,18 +22,12 @@ const ( livenessTimeoutSecs = int32(1) livenessPeriodSecs = int32(2) eventMeshNamespacePrefix = "/" - InstanceLabelKey = "app.kubernetes.io/instance" - InstanceLabelValue = "eventing" - DashboardLabelKey = "kyma-project.io/dashboard" - DashboardLabelValue = "eventing" - BackendLabelKey = "eventing.kyma-project.io/backend" publisherPortName = "http" publisherPortNum = int32(8080) publisherMetricsPortName = "http-metrics" publisherMetricsPortNum = int32(9090) PublisherName = "eventing-publisher-proxy" - AppLabelKey = "app.kubernetes.io/name" PublisherSecretClientIDKey = "client-id" PublisherSecretClientSecretKey = "client-secret" PublisherSecretTokenEndpointKey = "token-endpoint" @@ -128,16 +123,16 @@ func getPodSecurityContext() *v1.PodSecurityContext { func WithLabels(publisherName string, backendType v1alpha1.BackendType) DeployOpt { labels := map[string]string{ - AppLabelKey: publisherName, - InstanceLabelKey: InstanceLabelValue, - DashboardLabelKey: DashboardLabelValue, + label.KeyName: publisherName, + label.KeyInstance: label.ValueEventing, + label.KeyDashboard: label.ValueEventing, } return func(d *appsv1.Deployment) { d.Spec.Selector = metav1.SetAsLabelSelector(labels) d.Spec.Template.ObjectMeta.Labels = labels - // label the event-publisher proxy with the backendType label - labels[BackendLabelKey] = fmt.Sprint(getECBackendType(backendType)) + labels[label.KeyBackend] = fmt.Sprint(getECBackendType(backendType)) + labels[label.KeyCreatedBy] = label.ValueEventingManager d.ObjectMeta.Labels = labels } } @@ -157,7 +152,7 @@ func WithAffinity(publisherName string) DeployOpt { Weight: 100, PodAffinityTerm: v1.PodAffinityTerm{ LabelSelector: &metav1.LabelSelector{ - MatchLabels: map[string]string{AppLabelKey: publisherName}, + MatchLabels: map[string]string{label.KeyName: publisherName}, }, TopologyKey: "kubernetes.io/hostname", }, diff --git a/pkg/eventing/deployment_test.go b/pkg/eventing/deployment_test.go index 1d0ff82af..1fd0decf3 100644 --- a/pkg/eventing/deployment_test.go +++ b/pkg/eventing/deployment_test.go @@ -5,13 +5,13 @@ import ( "strings" "testing" - appsv1 "k8s.io/api/apps/v1" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" "github.com/kyma-project/eventing-manager/api/operator.kyma-project.io/v1alpha1" + "github.com/kyma-project/eventing-manager/internal/label" "github.com/kyma-project/eventing-manager/pkg/env" "github.com/kyma-project/eventing-manager/test" testutils "github.com/kyma-project/eventing-manager/test/utils" @@ -77,9 +77,9 @@ func TestNewDeployment(t *testing.T) { t.Errorf("Invalid backend!") } - // the tight backenType should be set - assert.Equal(t, deployment.ObjectMeta.Labels[BackendLabelKey], string(getECBackendType(tc.givenBackendType))) - assert.Equal(t, deployment.ObjectMeta.Labels[AppLabelKey], publisherName) + // the right backendType should be set + assert.Equal(t, deployment.ObjectMeta.Labels[label.KeyBackend], string(getECBackendType(tc.givenBackendType))) + assert.Equal(t, deployment.ObjectMeta.Labels[label.KeyName], publisherName) // check the container properties were set properly container := findPublisherContainer(publisherName, *deployment) diff --git a/pkg/eventing/manager.go b/pkg/eventing/manager.go index 7ed46c425..0a344adb1 100644 --- a/pkg/eventing/manager.go +++ b/pkg/eventing/manager.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - ecv1alpha1 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha1" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/record" @@ -16,6 +15,7 @@ import ( "github.com/kyma-project/eventing-manager/pkg/k8s" "github.com/kyma-project/eventing-manager/pkg/logger" "github.com/kyma-project/eventing-manager/pkg/object" + ecv1alpha1 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha1" ) const ( @@ -210,7 +210,7 @@ func (em EventingManager) DeployPublisherProxyResources( publisherDeployment.Spec.Template.Labels), // HPA to auto-scale publisher proxy. newHorizontalPodAutoscaler(publisherDeployment.Name, publisherDeployment.Namespace, int32(eventing.Spec.Publisher.Min), - int32(eventing.Spec.Publisher.Max), cpuUtilization, memoryUtilization), + int32(eventing.Spec.Publisher.Max), cpuUtilization, memoryUtilization, publisherDeployment.Labels), } // create the resources on k8s. @@ -254,7 +254,7 @@ func (em EventingManager) DeletePublisherProxyResources(ctx context.Context, eve // Service to expose health endpoint of EPP. newPublisherProxyHealthService(GetPublisherHealthServiceName(*eventing), eventing.Namespace, map[string]string{}, map[string]string{}), // HPA to auto-scale publisher proxy. - newHorizontalPodAutoscaler(publisherDeployment.Name, eventing.Namespace, 0, 0, 0, 0), + newHorizontalPodAutoscaler(publisherDeployment.Name, eventing.Namespace, 0, 0, 0, 0, map[string]string{}), } // delete the resources on k8s. diff --git a/pkg/eventing/utils.go b/pkg/eventing/utils.go index d8421bfbd..2a63cd079 100644 --- a/pkg/eventing/utils.go +++ b/pkg/eventing/utils.go @@ -42,7 +42,8 @@ func GetPublisherClusterRoleBindingName(eventing v1alpha1.Eventing) string { return fmt.Sprintf("%s-%s", eventing.GetName(), publisherProxySuffix) } -func newHorizontalPodAutoscaler(name, namespace string, min int32, max int32, cpuUtilization, memoryUtilization int32) *autoscalingv2.HorizontalPodAutoscaler { +func newHorizontalPodAutoscaler(name, namespace string, min, max, cpuUtilization, memoryUtilization int32, + labels map[string]string) *autoscalingv2.HorizontalPodAutoscaler { return &autoscalingv2.HorizontalPodAutoscaler{ TypeMeta: metav1.TypeMeta{ Kind: "HorizontalPodAutoscaler", @@ -51,6 +52,7 @@ func newHorizontalPodAutoscaler(name, namespace string, min int32, max int32, cp ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, + Labels: labels, }, Spec: autoscalingv2.HorizontalPodAutoscalerSpec{ ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{ diff --git a/pkg/object/equality_test.go b/pkg/object/equality_test.go index 056aafe80..ed2acda6b 100644 --- a/pkg/object/equality_test.go +++ b/pkg/object/equality_test.go @@ -605,6 +605,80 @@ func TestPublisherProxyDeploymentEqual(t *testing.T) { }, expectedResult: true, }, + "should be equal if spec Labels are equal with same order": { + getPublisher1: func() *appsv1.Deployment { + p := defaultNATSPublisher.DeepCopy() + p.Spec.Template.Labels = map[string]string{ + "key1": "value1", + "key2": "value2", + } + return p + }, + getPublisher2: func() *appsv1.Deployment { + p := defaultNATSPublisher.DeepCopy() + p.Spec.Template.Labels = map[string]string{ + "key1": "value1", + "key2": "value2", + } + return p + }, + expectedResult: true, + }, + "should be equal if spec Labels are equal with different order": { + getPublisher1: func() *appsv1.Deployment { + p := defaultNATSPublisher.DeepCopy() + p.Spec.Template.Labels = map[string]string{ + "key1": "value1", + "key2": "value2", + } + return p + }, + getPublisher2: func() *appsv1.Deployment { + p := defaultNATSPublisher.DeepCopy() + p.Spec.Template.Labels = map[string]string{ + "key2": "value2", + "key1": "value1", + } + return p + }, + expectedResult: true, + }, + "should not be equal if spec Labels were added": { + getPublisher1: func() *appsv1.Deployment { + p := defaultNATSPublisher.DeepCopy() + p.Spec.Template.Labels = map[string]string{ + "key1": "value1", + } + return p + }, + getPublisher2: func() *appsv1.Deployment { + p := defaultNATSPublisher.DeepCopy() + p.Spec.Template.Labels = map[string]string{ + "key1": "value1", + "key2": "value2", + } + return p + }, + expectedResult: false, + }, + "should not be equal if spec Labels were removed": { + getPublisher1: func() *appsv1.Deployment { + p := defaultNATSPublisher.DeepCopy() + p.Spec.Template.Labels = map[string]string{ + "key1": "value1", + "key2": "value2", + } + return p + }, + getPublisher2: func() *appsv1.Deployment { + p := defaultNATSPublisher.DeepCopy() + p.Spec.Template.Labels = map[string]string{ + "key1": "value1", + } + return p + }, + expectedResult: false, + }, "should be unequal if spec Labels changes": { getPublisher1: func() *appsv1.Deployment { p := defaultNATSPublisher.DeepCopy()