Skip to content

Commit

Permalink
Use common labels for created resources
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobebway committed Nov 22, 2023
1 parent 4b027b8 commit 8ff1ccf
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 28 deletions.
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 (
KeyBackend = "eventing.kyma-project.io/backend"
KeyCreatedBy = "app.kubernetes.io/created-by"
KeyInstance = "app.kubernetes.io/instance"
KeyDashboard = "kyma-project.io/dashboard"
KeyName = "app.kubernetes.io/name"

ValueEventingPublisherProxy = "eventing-publisher-proxy"
ValueEventingManager = "eventing-manager"
ValueEventing = "eventing"
)
17 changes: 6 additions & 11 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))
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
4 changes: 2 additions & 2 deletions pkg/eventing/manager.go
Original file line number Diff line number Diff line change
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

0 comments on commit 8ff1ccf

Please sign in to comment.