From 237b58bf5bb006fc40cb9ffdca5aab545660bfc5 Mon Sep 17 00:00:00 2001 From: Brendan Shephard Date: Fri, 7 Mar 2025 14:48:38 +1000 Subject: [PATCH] Add check for ExtraMount duplicate checking If an ExtraMount is provided with two mountPaths that are the same, the creation of Kubernetes objects will be blocked. This function will allow us to verify early before we try to create the Kubernetes objects. Such as in webhook validations. Signed-off-by: Brendan Shephard --- modules/storage/storage.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/storage/storage.go b/modules/storage/storage.go index ff654d05..3dd79084 100644 --- a/modules/storage/storage.go +++ b/modules/storage/storage.go @@ -20,6 +20,7 @@ package storage import ( "encoding/json" "fmt" + "slices" corev1 "k8s.io/api/core/v1" ) @@ -175,3 +176,23 @@ func (s *Volume) ToCoreVolume() (*corev1.Volume, error) { } return coreVolume, nil } + +// DuplicateExtraMountCheck - This function takes an ExtraMount as input and validates that there are no duplicate +// mounts that could cause conflicts when we try to define Kubernetes objects. +// If any duplcates are found, we will return an error so that this function can be used in a webhook. +func DuplicateExtraMountCheck(volMounts []VolMounts) error { + + var errors error + var existingMountPaths []string + + for _, vol := range volMounts { + for _, mount := range vol.Mounts { + if slices.Contains(existingMountPaths, mount.MountPath) { + return fmt.Errorf("mountPaths must be unique for each extraMount. Duplicate found: %s", mount.MountPath) + } + existingMountPaths = append(existingMountPaths, mount.MountPath) + } + } + + return errors +}