Skip to content

Commit

Permalink
Transfers EC.Labels and EC.Annotations to the POD spec
Browse files Browse the repository at this point in the history
  • Loading branch information
aorcholski committed Dec 4, 2024
1 parent 0df8665 commit f037d58
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
6 changes: 5 additions & 1 deletion pkg/controllers/edgeconnect/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,11 @@ func (controller *Controller) createOrUpdateEdgeConnectDeploymentAndSettings(ctx

desiredDeployment := deployment.New(ec)

desiredDeployment.Spec.Template.Annotations = map[string]string{consts.EdgeConnectAnnotationSecretHash: secretHash}
if desiredDeployment.Spec.Template.Annotations == nil {
desiredDeployment.Spec.Template.Annotations = map[string]string{}
}

desiredDeployment.Spec.Template.Annotations[consts.EdgeConnectAnnotationSecretHash] = secretHash
_log = _log.WithValues("deploymentName", desiredDeployment.Name)

if err := controllerutil.SetControllerReference(ec, desiredDeployment, scheme.Scheme); err != nil {
Expand Down
22 changes: 11 additions & 11 deletions pkg/controllers/edgeconnect/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/labels"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/resources"
maputils "github.com/Dynatrace/dynatrace-operator/pkg/util/map"
"github.com/Dynatrace/dynatrace-operator/pkg/util/prioritymap"
"github.com/Dynatrace/dynatrace-operator/pkg/webhook"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -15,10 +14,8 @@ import (
)

const (
customEnvPriority = prioritymap.HighPriority
defaultEnvPriority = prioritymap.DefaultPriority
unprivilegedUser = int64(1000)
unprivilegedGroup = int64(1000)
unprivilegedUser = int64(1000)
unprivilegedGroup = int64(1000)
)

func New(ec *edgeconnect.EdgeConnect) *appsv1.Deployment {
Expand All @@ -27,18 +24,20 @@ func New(ec *edgeconnect.EdgeConnect) *appsv1.Deployment {

func create(ec *edgeconnect.EdgeConnect) *appsv1.Deployment {
appLabels := buildAppLabels(ec)
labels := maputils.MergeMap(
ec.Labels,
appLabels.BuildLabels(),
buildLabels := appLabels.BuildLabels()

customPodLabels := maputils.MergeMap(
ec.Spec.Labels,
buildLabels, // higher priority
)

log.Debug("EdgeConnect deployment app labels", "labels", labels)
log.Debug("EdgeConnect deployment app labels", "labels", buildLabels)

return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: ec.Name,
Namespace: ec.Namespace,
Labels: labels,
Labels: buildLabels,
Annotations: buildAnnotations(ec),
},
Spec: appsv1.DeploymentSpec{
Expand All @@ -48,7 +47,8 @@ func create(ec *edgeconnect.EdgeConnect) *appsv1.Deployment {
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Annotations: ec.Spec.Annotations,
Labels: customPodLabels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{edgeConnectContainer(ec)},
Expand Down
82 changes: 82 additions & 0 deletions pkg/controllers/edgeconnect/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,88 @@ func Test_buildAppLabels(t *testing.T) {
})
}

func TestLabels(t *testing.T) {
testLabelKey := "test-label-key"
testLabelValue := "test-label-value"

t.Run("Check empty custom labels", func(t *testing.T) {
ec := &edgeconnect.EdgeConnect{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
},
Spec: edgeconnect.EdgeConnectSpec{},
}

deployment := New(ec)

assert.Len(t, deployment.Spec.Template.Labels, 5)
})

t.Run("Check custom label set correctly", func(t *testing.T) {
ec := &edgeconnect.EdgeConnect{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
},
Spec: edgeconnect.EdgeConnectSpec{
Labels: map[string]string{
testLabelKey: testLabelValue,
},
},
}

deployment := New(ec)

assert.Len(t, deployment.Spec.Template.Labels, 6)
assert.Contains(t, deployment.Spec.Template.ObjectMeta.Labels, testLabelKey)
assert.Equal(t, testLabelValue, deployment.Spec.Template.ObjectMeta.Labels[testLabelKey])

assert.NotContains(t, deployment.ObjectMeta.Labels, testLabelKey)
})
}

func TestAnnotations(t *testing.T) {
testAnnotationKey := "test-annotation-key"
testAnnotationValue := "test-annotation-value"

t.Run("Check empty annotations", func(t *testing.T) {
ec := &edgeconnect.EdgeConnect{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
},
Spec: edgeconnect.EdgeConnectSpec{},
}

deployment := New(ec)

assert.Nil(t, deployment.Spec.Template.ObjectMeta.Annotations)
})

t.Run("Check custom annotations set correctly", func(t *testing.T) {
ec := &edgeconnect.EdgeConnect{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Namespace: testNamespace,
},
Spec: edgeconnect.EdgeConnectSpec{
Annotations: map[string]string{
testAnnotationKey: testAnnotationValue,
},
},
}

deployment := New(ec)

assert.Len(t, deployment.Spec.Template.Annotations, 1)
assert.Contains(t, deployment.Spec.Template.ObjectMeta.Annotations, testAnnotationKey)
assert.Equal(t, testAnnotationValue, deployment.Spec.Template.ObjectMeta.Annotations[testAnnotationKey])

assert.NotContains(t, deployment.ObjectMeta.Annotations, testAnnotationKey)
})
}

func Test_prepareResourceRequirements(t *testing.T) {
ec := &edgeconnect.EdgeConnect{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit f037d58

Please sign in to comment.