Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/api/exp/enrichment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package exp

const (
EnrichmentEnableAttributesDtKubernetes = FFPrefix + "enable-attributes-dt.kubernetes"
)

func (ff *FeatureFlags) EnableAttributesDtKubernetes() bool {
return ff.getBoolWithDefault(EnrichmentEnableAttributesDtKubernetes, false)
}
55 changes: 55 additions & 0 deletions pkg/api/exp/enrichment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package exp

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestEnableAttributesDtKubernetes(t *testing.T) {
type testCase struct {
title string
in string
out bool
}

cases := []testCase{
{
title: "default",
in: "",
out: false,
},
{
title: "overrule to true",
in: "true",
out: true,
},
{
title: "overrule to false",
in: "false",
out: false,
},
{
title: "uppercase TRUE",
in: "TRUE",
out: true,
},
{
title: "uppercase FALSE",
in: "FALSE",
out: false,
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
ff := FeatureFlags{annotations: map[string]string{
EnrichmentEnableAttributesDtKubernetes: c.in,
}}

out := ff.EnableAttributesDtKubernetes()

assert.Equal(t, c.out, out)
})
}
}
4 changes: 3 additions & 1 deletion pkg/webhook/mutation/pod/handler/injection/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func addPodAttributes(request *dtwebhook.MutationRequest) error {
UserDefined: map[string]string{},
}

setDeprecatedAttributes(&attrs)
if request.DynaKube.FF().EnableAttributesDtKubernetes() {
setDeprecatedAttributes(&attrs)
}

envs := []corev1.EnvVar{
{Name: K8sPodNameEnv, ValueFrom: k8senv.NewSourceForField("metadata.name")},
Expand Down
90 changes: 58 additions & 32 deletions pkg/webhook/mutation/pod/handler/injection/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

containerattr "github.com/Dynatrace/dynatrace-bootstrapper/cmd/configure/attributes/container"
podattr "github.com/Dynatrace/dynatrace-bootstrapper/cmd/configure/attributes/pod"
"github.com/Dynatrace/dynatrace-operator/pkg/api/exp"
"github.com/Dynatrace/dynatrace-operator/pkg/api/latest/dynakube"
"github.com/Dynatrace/dynatrace-operator/pkg/api/latest/dynakube/metadataenrichment"
"github.com/Dynatrace/dynatrace-operator/pkg/api/latest/dynakube/oneagent"
Expand Down Expand Up @@ -47,8 +48,6 @@ func TestAddPodAttributes(t *testing.T) {
assert.Contains(t, attr.NodeName, K8sNodeNameEnv)
assert.Equal(t, request.Pod.Namespace, attr.NamespaceName)

assertDeprecatedAttributes(t, attr)

require.Len(t, request.InstallContainer.Env, 3)
assert.NotNil(t, k8senv.Find(request.InstallContainer.Env, K8sPodNameEnv))
assert.NotNil(t, k8senv.Find(request.InstallContainer.Env, K8sPodUIDEnv))
Expand All @@ -58,40 +57,67 @@ func TestAddPodAttributes(t *testing.T) {
}

t.Run("args and envs added", func(t *testing.T) {
initContainer := corev1.Container{
Args: []string{},
}
pod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test"},
}

expectedPod := pod.DeepCopy()

request := dtwebhook.MutationRequest{
BaseRequest: &dtwebhook.BaseRequest{
Pod: &pod,
DynaKube: dynakube.DynaKube{
Spec: dynakube.DynaKubeSpec{
MetadataEnrichment: metadataenrichment.Spec{
Enabled: ptr.To(true),
type testCase struct {
name string
annotations map[string]string
assertDeprecatedAnnotations func(t *testing.T, attrs podattr.Attributes)
}

testCases := []testCase{
{
name: "without deprecated annotations",
annotations: map[string]string{},
assertDeprecatedAnnotations: assertNoDeprecatedAttributes,
},
{
name: "with deprecated annotations",
annotations: map[string]string{exp.EnrichmentEnableAttributesDtKubernetes: "true"},
assertDeprecatedAnnotations: assertDeprecatedAttributes,
},
}

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
initContainer := corev1.Container{
Args: []string{},
}
pod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test"},
}

expectedPod := pod.DeepCopy()

request := dtwebhook.MutationRequest{
BaseRequest: &dtwebhook.BaseRequest{
Pod: &pod,
DynaKube: dynakube.DynaKube{
ObjectMeta: metav1.ObjectMeta{
Annotations: test.annotations,
},
Spec: dynakube.DynaKubeSpec{
MetadataEnrichment: metadataenrichment.Spec{
Enabled: ptr.To(true),
},
},
Status: dynakube.DynaKubeStatus{
KubernetesClusterMEID: "meid",
KubeSystemUUID: "systemuuid",
KubernetesClusterName: "meidname",
},
},
},
Status: dynakube.DynaKubeStatus{
KubernetesClusterMEID: "meid",
KubeSystemUUID: "systemuuid",
KubernetesClusterName: "meidname",
},
},
},
InstallContainer: &initContainer,
}
InstallContainer: &initContainer,
}

err := addPodAttributes(&request)
require.NoError(t, err)
require.Equal(t, *expectedPod, *request.Pod)
err := addPodAttributes(&request)
require.NoError(t, err)
require.Equal(t, *expectedPod, *request.Pod)

validateAttributes(t, request)
attr := validateAttributes(t, request)
test.assertDeprecatedAnnotations(t, attr)
})
}
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ func assertDeprecatedAttributes(t *testing.T, attrs podattr.Attributes) {
require.True(t, ok)
assert.Equal(t, attrs.ClusterUID, depValue)
}

func assertNoDeprecatedAttributes(t *testing.T, attrs podattr.Attributes) {
t.Helper()

_, ok := attrs.UserDefined[deprecatedClusterIDKey]
require.False(t, ok)
}
4 changes: 3 additions & 1 deletion pkg/webhook/mutation/pod/mutator/metadata/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func (mut *Mutator) Mutate(request *dtwebhook.MutationRequest) error {
WorkloadName: workloadInfo.Name,
}

SetDeprecatedAttributes(&attrs)
if request.DynaKube.FF().EnableAttributesDtKubernetes() {
SetDeprecatedAttributes(&attrs)
}

addMetadataToInitArgs(request, &attrs)
setInjectedAnnotation(request.Pod)
Expand Down
Loading