Skip to content

Commit

Permalink
Use common labels
Browse files Browse the repository at this point in the history
  • Loading branch information
marcobebway committed Nov 23, 2023
1 parent 4b027b8 commit 73113c9
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 39 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
14 changes: 10 additions & 4 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ namespace: kyma-system
#namePrefix: eventing-

# Labels to add to all resources and selectors.
#labels:
#- includeSelectors: true
# pairs:
# someName: someValue
labels:
- includeSelectors: false
pairs:
app.kubernetes.io/component: eventing-manager
app.kubernetes.io/created-by: eventing-manager
app.kubernetes.io/instance: eventing-manager
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: eventing-manager
app.kubernetes.io/part-of: Kyma
control-plane: eventing-manager

resources:
- ../crd
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
16 changes: 16 additions & 0 deletions internal/label/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package label

const (
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"
KeyBackend = "eventing.kyma-project.io/backend"
KeyDashboard = "kyma-project.io/dashboard"

ValueEventingPublisherProxy = "eventing-publisher-proxy"
ValueEventingManager = "eventing-manager"
ValueEventing = "eventing"
)
49 changes: 33 additions & 16 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 All @@ -56,6 +51,7 @@ func newNATSPublisherDeployment(
eventing,
publisherConfig,
WithLabels(GetPublisherDeploymentName(*eventing), v1alpha1.NatsBackendType),
WithSelector(GetPublisherDeploymentName(*eventing)),
WithContainers(publisherConfig, eventing),
WithNATSEnvVars(natsConfig, publisherConfig, eventing),
WithLogEnvVars(publisherConfig, eventing),
Expand All @@ -72,6 +68,7 @@ func newEventMeshPublisherDeployment(
eventing,
publisherConfig,
WithLabels(GetPublisherDeploymentName(*eventing), v1alpha1.EventMeshBackendType),
WithSelector(GetPublisherDeploymentName(*eventing)),
WithContainers(publisherConfig, eventing),
WithBEBEnvVars(GetPublisherDeploymentName(*eventing), publisherConfig, eventing),
WithLogEnvVars(publisherConfig, eventing),
Expand Down Expand Up @@ -126,19 +123,39 @@ func getPodSecurityContext() *v1.PodSecurityContext {
}
}

func WithLabels(publisherName string, backendType v1alpha1.BackendType) DeployOpt {
labels := map[string]string{
AppLabelKey: publisherName,
InstanceLabelKey: InstanceLabelValue,
DashboardLabelKey: DashboardLabelValue,
func getLabels(publisherName string, backendType v1alpha1.BackendType) map[string]string {
return map[string]string{
label.KeyComponent: label.ValueEventingManager,
label.KeyCreatedBy: label.ValueEventingManager,
label.KeyInstance: label.ValueEventing,
label.KeyManagedBy: label.ValueEventingManager,
label.KeyName: publisherName,
label.KeyPartOf: label.ValueEventingManager,
label.KeyBackend: fmt.Sprint(getECBackendType(backendType)),
label.KeyDashboard: label.ValueEventing,
}
}

func WithLabels(publisherName string, backendType v1alpha1.BackendType) DeployOpt {
return func(d *appsv1.Deployment) {
d.Spec.Selector = metav1.SetAsLabelSelector(labels)
labels := getLabels(publisherName, backendType)
d.ObjectMeta.Labels = labels
d.Spec.Template.ObjectMeta.Labels = labels
}
}

// label the event-publisher proxy with the backendType label
labels[BackendLabelKey] = fmt.Sprint(getECBackendType(backendType))
d.ObjectMeta.Labels = labels
func getSelector(publisherName string) *metav1.LabelSelector {
labels := map[string]string{
label.KeyInstance: label.ValueEventing,
label.KeyName: publisherName,
label.KeyDashboard: label.ValueEventing,
}
return metav1.SetAsLabelSelector(labels)
}

func WithSelector(publisherName string) DeployOpt {
return func(d *appsv1.Deployment) {
d.Spec.Selector = getSelector(publisherName)
}
}

Expand All @@ -157,7 +174,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
129 changes: 124 additions & 5 deletions pkg/eventing/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package eventing

import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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 +78,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 Expand Up @@ -327,3 +328,121 @@ func findPublisherContainer(publisherName string, deployment appsv1.Deployment)
}
return container
}

func Test_getLabels(t *testing.T) {
// given
const (
publisherName = "test-publisher"
backendTypeUnsupported = "Unsupported"
backendTypeEventMesh = "EventMesh"
backendTypeNATS = "NATS"
)
type args struct {
publisherName string
backendType v1alpha1.BackendType
}
tests := []struct {
name string
args args
want map[string]string
}{
{
name: "should return the correct labels for backend NATS",
args: args{
publisherName: publisherName,
backendType: backendTypeNATS,
},
want: map[string]string{
label.KeyComponent: label.ValueEventingManager,
label.KeyCreatedBy: label.ValueEventingManager,
label.KeyInstance: label.ValueEventing,
label.KeyManagedBy: label.ValueEventingManager,
label.KeyName: publisherName,
label.KeyPartOf: label.ValueEventingManager,
label.KeyBackend: "NATS",
label.KeyDashboard: label.ValueEventing,
},
},
{
name: "should return the correct labels for backend EventMesh",
args: args{
publisherName: publisherName,
backendType: backendTypeEventMesh,
},
want: map[string]string{
label.KeyComponent: label.ValueEventingManager,
label.KeyCreatedBy: label.ValueEventingManager,
label.KeyInstance: label.ValueEventing,
label.KeyManagedBy: label.ValueEventingManager,
label.KeyName: publisherName,
label.KeyPartOf: label.ValueEventingManager,
label.KeyBackend: "BEB",
label.KeyDashboard: label.ValueEventing,
},
},
{
name: "should return the correct labels for unsupported backend",
args: args{
publisherName: publisherName,
backendType: backendTypeUnsupported,
},
want: map[string]string{
label.KeyComponent: label.ValueEventingManager,
label.KeyCreatedBy: label.ValueEventingManager,
label.KeyInstance: label.ValueEventing,
label.KeyManagedBy: label.ValueEventingManager,
label.KeyName: publisherName,
label.KeyPartOf: label.ValueEventingManager,
label.KeyBackend: "NATS",
label.KeyDashboard: label.ValueEventing,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// when
got := getLabels(tt.args.publisherName, tt.args.backendType)

// then
require.Equal(t, tt.want, got)
})
}
}

func Test_getSelector(t *testing.T) {
// given
const (
publisherName = "test-publisher"
)
type args struct {
publisherName string
}
tests := []struct {
name string
args args
want *metav1.LabelSelector
}{
{
name: "should return the correct selector",
args: args{
publisherName: publisherName,
},
want: &metav1.LabelSelector{
MatchLabels: map[string]string{
label.KeyInstance: label.ValueEventing,
label.KeyName: publisherName,
label.KeyDashboard: label.ValueEventing,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// when
got := getSelector(tt.args.publisherName)

// then
require.Equal(t, tt.want, got)
})
}
}
Loading

0 comments on commit 73113c9

Please sign in to comment.