Skip to content

Commit

Permalink
[0.7 backport] Fix pod with owner of unknown workload kind (#1126)
Browse files Browse the repository at this point in the history
* Fix pod with owner of unknown workload kind

* Fix linting
  • Loading branch information
chrismuellner authored Sep 6, 2022
1 parent e73d728 commit 199c15e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/config/enrichment_injection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package config

const EnrichmentUnknownWorkload = "UNKNOWN"
13 changes: 11 additions & 2 deletions src/standalone/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/Dynatrace/dynatrace-operator/src/arch"
"github.com/Dynatrace/dynatrace-operator/src/config"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -233,7 +234,11 @@ func (env *environment) addWorkloadKind() error {
if err != nil {
return err
}
env.WorkloadKind = workloadKind
if workloadKind == config.EnrichmentUnknownWorkload {
env.WorkloadKind = ""
} else {
env.WorkloadKind = workloadKind
}
return nil
}

Expand All @@ -242,7 +247,11 @@ func (env *environment) addWorkloadName() error {
if err != nil {
return err
}
env.WorkloadName = workloadName
if workloadName == config.EnrichmentUnknownWorkload {
env.WorkloadName = ""
} else {
env.WorkloadName = workloadName
}
return nil
}

Expand Down
16 changes: 11 additions & 5 deletions src/webhook/mutation/pod_mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

dynatracev1beta1 "github.com/Dynatrace/dynatrace-operator/src/api/v1beta1"
"github.com/Dynatrace/dynatrace-operator/src/config"
dtcsi "github.com/Dynatrace/dynatrace-operator/src/controllers/csi"
csivolumes "github.com/Dynatrace/dynatrace-operator/src/controllers/csi/driver/volumes"
appvolumes "github.com/Dynatrace/dynatrace-operator/src/controllers/csi/driver/volumes/app"
Expand Down Expand Up @@ -135,16 +136,21 @@ func findRootOwnerOfPod(ctx context.Context, clt client.Client, pod *corev1.Pod,

func findRootOwner(ctx context.Context, clt client.Client, o *metav1.PartialObjectMetadata) (string, string, error) {
if len(o.ObjectMeta.OwnerReferences) == 0 {
kind := o.Kind
if kind == "Pod" {
kind = ""
if o.ObjectMeta.Name == "" {
// pod is not created directly and does not have an owner reference set
return config.EnrichmentUnknownWorkload, config.EnrichmentUnknownWorkload, nil
}
return o.ObjectMeta.Name, kind, nil
return o.ObjectMeta.Name, o.Kind, nil
}

om := o.ObjectMeta
for _, owner := range om.OwnerReferences {
if owner.Controller != nil && *owner.Controller && isWellKnownWorkload(owner) {
if owner.Controller != nil && *owner.Controller {
if !isWellKnownWorkload(owner) {
// pod is created by workload of kind that is not well known
return config.EnrichmentUnknownWorkload, config.EnrichmentUnknownWorkload, nil
}

obj := &metav1.PartialObjectMetadata{
TypeMeta: metav1.TypeMeta{
APIVersion: owner.APIVersion,
Expand Down
2 changes: 1 addition & 1 deletion src/webhook/mutation/pod_mutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,7 @@ func buildResultPod(_ *testing.T, oneAgentFf FeatureFlag, dataIngestFf FeatureFl

pod.Spec.InitContainers[0].Env = append(pod.Spec.InitContainers[0].Env,
corev1.EnvVar{Name: dataIngestInjectedEnvVarName, Value: "true"},
corev1.EnvVar{Name: workloadKindEnvVarName, Value: ""},
corev1.EnvVar{Name: workloadKindEnvVarName, Value: "Pod"},
corev1.EnvVar{Name: workloadNameEnvVarName, Value: "test-pod-12345"},
)

Expand Down

0 comments on commit 199c15e

Please sign in to comment.