Skip to content

Commit

Permalink
fix: avoid adding the mount paths multiple times (#2432)
Browse files Browse the repository at this point in the history
In case the webhook is triggered more than once
  • Loading branch information
blumamir authored Feb 11, 2025
1 parent 30c075b commit de33e57
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions instrumentor/controllers/agentenabled/podswebhook/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand Down

0 comments on commit de33e57

Please sign in to comment.