Skip to content

Commit

Permalink
More excessively check if the oneagent injection happened (#3955) (#3960
Browse files Browse the repository at this point in the history
)
  • Loading branch information
0sewa0 authored Oct 22, 2024
1 parent 04c3a63 commit 255dc3f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
10 changes: 10 additions & 0 deletions pkg/util/kubeobjects/volumes/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ func GetVolumeMountByName(mounts []corev1.VolumeMount, volumeName string) (*core
volumeName, len(mounts),
)
}

func IsIn(mounts []corev1.VolumeMount, volumeName string) bool {
for _, vm := range mounts {
if vm.Name == volumeName {
return true
}
}

return false
}
2 changes: 1 addition & 1 deletion pkg/webhook/mutation/pod/init_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func addSeccompProfile(ctx *corev1.SecurityContext, dk dynakube.DynaKube) {
}
}

func updateContainerInfo(request *dtwebhook.ReinvocationRequest, installContainer *corev1.Container) bool {
func updateContainerInfo(request *dtwebhook.BaseRequest, installContainer *corev1.Container) bool {
pod := request.Pod
if installContainer == nil {
installContainer = findInstallContainer(pod.Spec.InitContainers)
Expand Down
13 changes: 6 additions & 7 deletions pkg/webhook/mutation/pod/oneagent/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/Dynatrace/dynatrace-operator/pkg/consts"
"github.com/Dynatrace/dynatrace-operator/pkg/injection/namespace/initgeneration"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/env"
"github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/volumes"
maputils "github.com/Dynatrace/dynatrace-operator/pkg/util/map"
dtwebhook "github.com/Dynatrace/dynatrace-operator/pkg/webhook"
"github.com/pkg/errors"
Expand Down Expand Up @@ -113,11 +115,8 @@ func (mut *Mutator) ensureInitSecret(request *dtwebhook.MutationRequest) error {
}

func ContainerIsInjected(container corev1.Container) bool {
for _, e := range container.Env {
if e.Name == dynatraceMetadataEnv {
return true
}
}

return false
return env.IsIn(container.Env, dynatraceMetadataEnv) &&
env.IsIn(container.Env, preloadEnv) &&
volumes.IsIn(container.VolumeMounts, OneAgentBinVolumeName) &&
volumes.IsIn(container.VolumeMounts, oneAgentShareVolumeName)
}
4 changes: 2 additions & 2 deletions pkg/webhook/mutation/pod/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (wh *webhook) handlePodMutation(ctx context.Context, mutationRequest *dtweb

mutationRequest.InstallContainer = createInstallInitContainerBase(wh.webhookImage, wh.clusterID, mutationRequest.Pod, mutationRequest.DynaKube)

_ = updateContainerInfo(mutationRequest.ToReinvocationRequest(), mutationRequest.InstallContainer)
_ = updateContainerInfo(mutationRequest.BaseRequest, mutationRequest.InstallContainer)

var isMutated bool

Expand Down Expand Up @@ -193,7 +193,7 @@ func (wh *webhook) handlePodReinvocation(mutationRequest *dtwebhook.MutationRequ

reinvocationRequest := mutationRequest.ToReinvocationRequest()

isMutated := updateContainerInfo(reinvocationRequest, nil)
isMutated := updateContainerInfo(reinvocationRequest.BaseRequest, nil)

if !isMutated { // == no new containers were detected, we only mutate new containers during reinvoke
return false
Expand Down

0 comments on commit 255dc3f

Please sign in to comment.