Skip to content

Commit 05a3d7c

Browse files
author
玖宇
committed
support unique hostpath for fuse sidecar mode in webhook
Signed-off-by: 玖宇 <guotongyu.gty@alibaba-inc.com>
1 parent 3c08916 commit 05a3d7c

4 files changed

Lines changed: 66 additions & 3 deletions

File tree

pkg/application/inject/fuse/mutator/mutating_context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ type mutatingContext struct {
9292
appendedVolumeNames map[string]string
9393
datasetUsedInContainers *bool
9494
datasetUsedInInitContainers *bool
95+
generateUniqueHostMountPath string
9596
}
9697

9798
func (ctx *mutatingContext) GetAppendedVolumeNames() (nameMapping map[string]string, err error) {

pkg/application/inject/fuse/mutator/mutator_default.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ package mutator
1818

1919
import (
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

397441
func (helper *defaultMutatorHelper) removeFuseMetricsContainerPort() {
398442
if len(helper.template.FuseContainer.Ports) == 0 {

pkg/common/constants.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ const (
143143
VolumeTypeVolumeTemplate VolumeType = "volumeTemplate"
144144
)
145145

146+
type HostPathMode string
147+
148+
const (
149+
HostPathModeFixed HostPathMode = "fixed"
150+
HostPathModeRandomSuffix HostPathMode = "random-suffix"
151+
)
152+
146153
// GetDefaultTieredStoreOrder get the TieredStoreOrder from the default Map
147154
// because the crd has validated the value, It's not possible to meet unknown MediumType
148155
func GetDefaultTieredStoreOrder(MediumType MediumType) (order int) {
@@ -215,5 +222,6 @@ const (
215222
)
216223

217224
const (
218-
SkipPrecheckAnnotationKey = "sidecar.fluid.io/skip-precheck"
225+
SkipPrecheckAnnotationKey = "sidecar.fluid.io/skip-precheck"
226+
HostMountPathModeOnDefaultPlatformKey = "default.fuse-sidecar.fluid.io/host-mount-path-mode"
219227
)

pkg/utils/volumes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ outer:
5959
return
6060
}
6161

62+
func IsVolumeNameHasPrefixes(input corev1.Volume, prefixes []string) bool {
63+
for _, prefix := range prefixes {
64+
if strings.HasPrefix(input.Name, prefix) {
65+
return true
66+
}
67+
}
68+
69+
return false
70+
}
71+
6272
func FindVolumeByVolumeMount(volumeMount corev1.VolumeMount, volumes []corev1.Volume) *corev1.Volume {
6373
for _, vol := range volumes {
6474
if vol.Name == volumeMount.Name {

0 commit comments

Comments
 (0)