Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.7 backport] Fix pod with owner of unknown workload kind #1126

Merged
merged 2 commits into from
Sep 6, 2022
Merged
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
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