Skip to content

Commit

Permalink
Add created by label
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobebway committed Nov 22, 2023
1 parent 4b027b8 commit 513d523
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 30 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
2 changes: 2 additions & 0 deletions config/manager/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/ui-extensions/eventing/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions config/ui-extensions/subscriptions/subscriptions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
13 changes: 13 additions & 0 deletions internal/label/label.go
Original file line number Diff line number Diff line change
@@ -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"
)
19 changes: 7 additions & 12 deletions pkg/eventing/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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",
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/eventing/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pkg/eventing/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion pkg/eventing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{
Expand Down
74 changes: 74 additions & 0 deletions pkg/object/equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 513d523

Please sign in to comment.