From de33e5774f2dc2f31b79187b7bcae2201ca81caa Mon Sep 17 00:00:00 2001 From: Amir Blum Date: Tue, 11 Feb 2025 21:48:17 +0200 Subject: [PATCH] fix: avoid adding the mount paths multiple times (#2432) In case the webhook is triggered more than once --- .../agentenabled/podswebhook/mount.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/instrumentor/controllers/agentenabled/podswebhook/mount.go b/instrumentor/controllers/agentenabled/podswebhook/mount.go index 011875f68..2312c1904 100644 --- a/instrumentor/controllers/agentenabled/podswebhook/mount.go +++ b/instrumentor/controllers/agentenabled/podswebhook/mount.go @@ -12,6 +12,15 @@ func MountDirectory(containerSpec *corev1.Container, dir string) { // TODO: assuming the directory always starts with {{ODIGOS_AGENTS_DIR}}. This should be validated. // Should we return errors here to validate static values? relativePath := strings.TrimPrefix(dir, distro.AgentPlaceholderDirectory+"/") + + // make sure we are idempotent, not adding ourselves multiple times + for _, volumeMount := range containerSpec.VolumeMounts { + if volumeMount.SubPath == dir { + // the volume is already mounted, do not add it again + return + } + } + absolutePath := strings.ReplaceAll(dir, distro.AgentPlaceholderDirectory, k8sconsts.OdigosAgentsDirectory) containerSpec.VolumeMounts = append(containerSpec.VolumeMounts, corev1.VolumeMount{ Name: k8sconsts.OdigosAgentMountVolumeName, @@ -22,6 +31,15 @@ func MountDirectory(containerSpec *corev1.Container, dir string) { } func MountPodVolume(pod *corev1.Pod) { + + // make sure we are idempotent, not adding ourselves multiple times + for _, volume := range pod.Spec.Volumes { + if volume.Name == k8sconsts.OdigosAgentMountVolumeName { + // the volume is already mounted, do not add it again + return + } + } + pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{ Name: k8sconsts.OdigosAgentMountVolumeName, VolumeSource: corev1.VolumeSource{