Skip to content

Commit

Permalink
refactor: inline helper function with comment
Browse files Browse the repository at this point in the history
  • Loading branch information
blumamir committed Feb 11, 2025
1 parent b92f380 commit b5af379
Showing 1 changed file with 12 additions and 23 deletions.
35 changes: 12 additions & 23 deletions instrumentor/controllers/agentenabled/podswebhook/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@ import (
corev1 "k8s.io/api/core/v1"
)

func checkIfMountDirectoryExists(containerSpec *corev1.Container, dir string) bool {
for _, volumeMount := range containerSpec.VolumeMounts {
if volumeMount.SubPath == dir {
return true
}
}
return false
}

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+"/")
if checkIfMountDirectoryExists(containerSpec, dir) {
// avoid adding the directory volume twice to the container
return

// 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)
Expand All @@ -35,21 +30,15 @@ func MountDirectory(containerSpec *corev1.Container, dir string) {
})
}

func checkIfVolumExists(pod *corev1.Pod) bool {
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 {
return true
// the volume is already mounted, do not add it again
return
}
}
return false
}

func MountPodVolume(pod *corev1.Pod) {

if checkIfVolumExists(pod) {
// avoid adding the volume twice to the pod
return
}

pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: k8sconsts.OdigosAgentMountVolumeName,
Expand Down

0 comments on commit b5af379

Please sign in to comment.