Skip to content

Commit

Permalink
Unify construction of EEC service fully qualified domain name (#3711)
Browse files Browse the repository at this point in the history
  • Loading branch information
wepudt authored Sep 11, 2024
1 parent 174dbbd commit 2116eb5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/dynakube/extension/eec/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestEnvironmentVariables(t *testing.T) {
assert.Equal(t, corev1.EnvVar{Name: envExtensionsModuleExecPathName, Value: envExtensionsModuleExecPath}, statefulSet.Spec.Template.Spec.Containers[0].Env[4])
assert.Equal(t, corev1.EnvVar{Name: envDsInstallDirName, Value: envDsInstallDir}, statefulSet.Spec.Template.Spec.Containers[0].Env[5])
assert.Equal(t, corev1.EnvVar{Name: envK8sClusterId, Value: dk.Status.KubeSystemUUID}, statefulSet.Spec.Template.Spec.Containers[0].Env[6])
assert.Equal(t, corev1.EnvVar{Name: envK8sExtServiceUrl, Value: serviceAccountName}, statefulSet.Spec.Template.Spec.Containers[0].Env[7])
assert.Equal(t, corev1.EnvVar{Name: envK8sExtServiceUrl, Value: dk.Name + consts.ExtensionsControllerSuffix + "." + dk.Namespace}, statefulSet.Spec.Template.Spec.Containers[0].Env[7])
assert.Equal(t, corev1.EnvVar{Name: envDSTokenPath, Value: eecTokenMountPath + "/" + consts.OtelcTokenSecretKey}, statefulSet.Spec.Template.Spec.Containers[0].Env[8])
assert.Equal(t, corev1.EnvVar{Name: envHttpsCertPathPem, Value: envEecHttpsCertPathPem}, statefulSet.Spec.Template.Spec.Containers[0].Env[9])
assert.Equal(t, corev1.EnvVar{Name: envHttpsPrivKeyPathPem, Value: envEecHttpsPrivKeyPathPem}, statefulSet.Spec.Template.Spec.Containers[0].Env[10])
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/dynakube/extension/eec/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/activegate/capability"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/consts"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/hash"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/servicename"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/utils"
"github.com/Dynatrace/dynatrace-operator/pkg/util/address"
"github.com/Dynatrace/dynatrace-operator/pkg/util/conditions"
Expand Down Expand Up @@ -209,7 +210,7 @@ func buildContainerEnvs(dk *dynakube.DynaKube) []corev1.EnvVar {
{Name: envExtensionsModuleExecPathName, Value: envExtensionsModuleExecPath},
{Name: envDsInstallDirName, Value: envDsInstallDir},
{Name: envK8sClusterId, Value: dk.Status.KubeSystemUUID},
{Name: envK8sExtServiceUrl, Value: serviceAccountName},
{Name: envK8sExtServiceUrl, Value: servicename.BuildFQDN(dk)},
{Name: envDSTokenPath, Value: eecTokenMountPath + "/" + consts.OtelcTokenSecretKey},
{Name: envHttpsCertPathPem, Value: envEecHttpsCertPathPem},
{Name: envHttpsPrivKeyPathPem, Value: envEecHttpsPrivKeyPathPem},
Expand Down
3 changes: 2 additions & 1 deletion pkg/controllers/dynakube/extension/otel/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta3/dynakube"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/consts"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/hash"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/servicename"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/utils"
"github.com/Dynatrace/dynatrace-operator/pkg/util/address"
"github.com/Dynatrace/dynatrace-operator/pkg/util/conditions"
Expand Down Expand Up @@ -131,7 +132,7 @@ func buildContainer(dk *dynakube.DynaKube) corev1.Container {
SecurityContext: buildSecurityContext(),
Env: buildContainerEnvs(dk),
Resources: dk.Spec.Templates.OpenTelemetryCollector.Resources,
Args: []string{fmt.Sprintf("--config=eec://%s.%s.svc.cluster.local:%d/otcconfig/prometheusMetrics#refresh-interval=5s&auth-file=%s", dk.Name+consts.ExtensionsControllerSuffix, dk.Namespace, consts.ExtensionsCollectorComPort, otelcSecretTokenFilePath)},
Args: []string{fmt.Sprintf("--config=eec://%s:%d/otcconfig/prometheusMetrics#refresh-interval=5s&auth-file=%s", servicename.BuildFQDN(dk), consts.ExtensionsCollectorComPort, otelcSecretTokenFilePath)},
VolumeMounts: buildContainerVolumeMounts(dk),
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/controllers/dynakube/extension/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Dynatrace/dynatrace-operator/pkg/api/scheme/fake"
"github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta3/dynakube"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/consts"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/servicename"
"github.com/Dynatrace/dynatrace-operator/pkg/util/conditions"
"github.com/Dynatrace/dynatrace-operator/pkg/util/dttoken"
k8ssecret "github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/secret"
Expand Down Expand Up @@ -133,7 +134,7 @@ func TestReconciler_Reconcile(t *testing.T) {
require.NoError(t, err)

var svc corev1.Service
err = mockK8sClient.Get(context.Background(), client.ObjectKey{Name: r.buildServiceName(), Namespace: testNamespace}, &svc)
err = mockK8sClient.Get(context.Background(), client.ObjectKey{Name: servicename.Build(r.dk), Namespace: testNamespace}, &svc)
require.NoError(t, err)
assert.NotNil(t, svc)

Expand All @@ -158,7 +159,7 @@ func TestReconciler_Reconcile(t *testing.T) {
require.NoError(t, err)

var svc corev1.Service
err = mockK8sClient.Get(context.Background(), client.ObjectKey{Name: r.buildServiceName(), Namespace: testNamespace}, &svc)
err = mockK8sClient.Get(context.Background(), client.ObjectKey{Name: servicename.Build(r.dk), Namespace: testNamespace}, &svc)
require.Error(t, err)
assert.True(t, k8serrors.IsNotFound(err))
})
Expand Down
15 changes: 4 additions & 11 deletions pkg/controllers/dynakube/extension/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/consts"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/servicename"
"github.com/Dynatrace/dynatrace-operator/pkg/util/conditions"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/labels"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/service"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (r *reconciler) createOrUpdateService(ctx context.Context) error {
return err
}

conditions.SetServiceCreated(r.dk.Conditions(), consts.ExtensionsServiceConditionType, r.buildServiceName())
conditions.SetServiceCreated(r.dk.Conditions(), consts.ExtensionsServiceConditionType, servicename.Build(r.dk))

return nil
}
Expand All @@ -67,25 +68,17 @@ func (r *reconciler) buildService() (*corev1.Service, error) {
appLabels := labels.NewAppLabels(labels.ExtensionComponentLabel, r.dk.Name, labels.ExtensionComponentLabel, "")

svcPort := corev1.ServicePort{
Name: r.buildPortsName(),
Name: servicename.BuildPortName(),
Port: consts.ExtensionsCollectorComPort,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.IntOrString{Type: intstr.String, StrVal: consts.ExtensionsCollectorTargetPortName},
}

return service.Build(r.dk,
r.buildServiceName(),
servicename.Build(r.dk),
appLabels.BuildMatchLabels(),
svcPort,
service.SetLabels(coreLabels.BuildLabels()),
service.SetType(corev1.ServiceTypeClusterIP),
)
}

func (r *reconciler) buildServiceName() string {
return r.dk.Name + consts.ExtensionsControllerSuffix
}

func (r *reconciler) buildPortsName() string {
return "dynatrace" + consts.ExtensionsControllerSuffix + "-" + consts.ExtensionsCollectorTargetPortName
}
8 changes: 8 additions & 0 deletions pkg/controllers/dynakube/extension/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/Dynatrace/dynatrace-operator/pkg/api/scheme/fake"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/servicename"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/labels"
"github.com/Dynatrace/dynatrace-operator/pkg/util/timeprovider"
"github.com/Dynatrace/dynatrace-operator/pkg/version"
Expand Down Expand Up @@ -33,3 +34,10 @@ func TestReconciler_prepareService(t *testing.T) {
}, svc.Spec.Selector)
})
}

func TestFQDNNameGeneration(t *testing.T) {
t.Run(`Check FQDN name generation`, func(t *testing.T) {
dk := createDynakube()
assert.Equal(t, "test-name-extensions-controller.test-namespace", servicename.BuildFQDN(dk))
})
}
18 changes: 18 additions & 0 deletions pkg/controllers/dynakube/extension/servicename/naming.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package servicename

import (
"github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta3/dynakube"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/dynakube/extension/consts"
)

func BuildPortName() string {
return "dynatrace" + consts.ExtensionsControllerSuffix + "-" + consts.ExtensionsCollectorTargetPortName
}

func BuildFQDN(dk *dynakube.DynaKube) string {
return Build(dk) + "." + dk.Namespace
}

func Build(dk *dynakube.DynaKube) string {
return dk.Name + consts.ExtensionsControllerSuffix
}

0 comments on commit 2116eb5

Please sign in to comment.