@@ -18,8 +18,12 @@ package mutator
1818
1919import (
2020 "context"
21+ "crypto/rand"
22+ "encoding/hex"
2123 "fmt"
24+ "path/filepath"
2225 "strings"
26+ "time"
2327
2428 "github.com/go-logr/logr"
2529 "github.com/pkg/errors"
@@ -180,10 +184,16 @@ func (helper *defaultMutatorHelper) Mutate() (*MutatingPodSpecs, error) {
180184 return helper .Specs , nil
181185}
182186
183- func (helper * defaultMutatorHelper ) mutateDatasetVolumes () error {
187+ func (helper * defaultMutatorHelper ) mutateDatasetVolumes () ( err error ) {
184188 volumes := helper .Specs .Volumes
185-
186189 mountPath := helper .template .FuseMountInfo .HostMountPath
190+ if common .HostPathMode (helper .Specs .MetaObj .Annotations [common .HostMountPathModeOnDefaultPlatformKey ]) == common .HostPathModeRandomSuffix {
191+ mountPath , err = helper .generateUniqueHostPath (mountPath )
192+ if err != nil {
193+ return err
194+ }
195+ }
196+
187197 if helper .template .FuseMountInfo .SubPath != "" {
188198 mountPath = mountPath + "/" + helper .template .FuseMountInfo .SubPath
189199 }
@@ -243,6 +253,17 @@ func (helper *defaultMutatorHelper) appendFuseContainerVolumes() (err error) {
243253 volumeNames = append (volumeNames , volume .Name )
244254 }
245255
256+ if common .HostPathMode (helper .Specs .MetaObj .Annotations [common .HostMountPathModeOnDefaultPlatformKey ]) == common .HostPathModeRandomSuffix {
257+ for index , volume := range volumesToAdd {
258+ if utils .IsVolumeNameHasPrefixes (volume , hostMountNames ) {
259+ if volume .HostPath != nil {
260+ volume .HostPath .Path = fmt .Sprintf ("%s/%s" , volume .HostPath .Path , helper .ctx .generateUniqueHostMountPath )
261+ volumesToAdd [index ] = volume
262+ }
263+ }
264+ }
265+ }
266+
246267 // Append volumes
247268 ctxAppenedVolumeNames , err := helper .ctx .GetAppendedVolumeNames ()
248269 if err != nil {
@@ -393,6 +414,29 @@ func (helper *defaultMutatorHelper) enablePrometheusMetricsScrape() {
393414 helper .Specs .MetaObj .Annotations [common .AnnotationPrometheusFuseMetricsScrapeKey ] = "true"
394415 }
395416}
417+ func (helper * defaultMutatorHelper ) generateUniqueHostPath (originalMountPath string ) (string , error ) {
418+ bytes := make ([]byte , 8 )
419+ if _ , err := rand .Read (bytes ); err != nil {
420+ return "" , err
421+ }
422+ uniqueDirName := hex .EncodeToString (bytes )[:8 ]
423+
424+ name := helper .Specs .MetaObj .Name
425+ if helper .Specs .MetaObj .Name == "" {
426+ name = helper .Specs .MetaObj .GenerateName + "--generate-name"
427+ }
428+ generateUniqueHostMountPath := fmt .Sprintf ("%s/%d-%s" , name , time .Now ().UnixMicro (), uniqueDirName )
429+ helper .ctx .generateUniqueHostMountPath = generateUniqueHostMountPath
430+
431+ mountPathParts := strings .Split (originalMountPath , string (filepath .Separator ))
432+ if len (mountPathParts ) < 2 {
433+ return "" , fmt .Errorf ("unsupported host mount path format: %s" , originalMountPath )
434+ }
435+ baseComponents := mountPathParts [:len (mountPathParts )- 1 ]
436+ lastComponent := mountPathParts [len (mountPathParts )- 1 ]
437+ newPathComponents := append (baseComponents , generateUniqueHostMountPath , lastComponent )
438+ return fmt .Sprintf ("/%s" , filepath .Join (newPathComponents ... )), nil
439+ }
396440
397441func (helper * defaultMutatorHelper ) removeFuseMetricsContainerPort () {
398442 if len (helper .template .FuseContainer .Ports ) == 0 {
0 commit comments