diff --git a/pkg/csi_driver/node.go b/pkg/csi_driver/node.go index 8e3ae0fa0..d2866acf5 100644 --- a/pkg/csi_driver/node.go +++ b/pkg/csi_driver/node.go @@ -44,7 +44,6 @@ const ( VolumeContextKeyPodNamespace = "csi.storage.k8s.io/pod.namespace" VolumeContextKeyEphemeral = "csi.storage.k8s.io/ephemeral" VolumeContextKeyBucketName = "bucketName" - VolumeContextKeyMountOptions = "mountOptions" UmountTimeout = time.Second * 5 @@ -100,9 +99,8 @@ func (s *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublish } fuseMountOptions = joinMountOptions(fuseMountOptions, capMount.GetMountFlags()) } - if mountOptions, ok := vc[VolumeContextKeyMountOptions]; ok { - fuseMountOptions = joinMountOptions(fuseMountOptions, strings.Split(mountOptions, ",")) - } + + fuseMountOptions = parseVolumeAttributes(fuseMountOptions, vc) if vc[VolumeContextKeyEphemeral] == "true" { bucketName = vc[VolumeContextKeyBucketName] diff --git a/pkg/csi_driver/utils.go b/pkg/csi_driver/utils.go index 9a3dc83bc..3e8adfc9f 100644 --- a/pkg/csi_driver/utils.go +++ b/pkg/csi_driver/utils.go @@ -21,9 +21,11 @@ import ( "fmt" "os" "path/filepath" + "strconv" "strings" csi "github.com/container-storage-interface/spec/lib/go/csi" + "github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/util" "github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/webhook" pbSanitizer "github.com/kubernetes-csi/csi-lib-utils/protosanitizer" "golang.org/x/net/context" @@ -37,6 +39,9 @@ const ( CreateVolumeCSIFullMethod = "/csi.v1.Controller/CreateVolume" DeleteVolumeCSIFullMethod = "/csi.v1.Controller/DeleteVolume" NodePublishVolumeCSIFullMethod = "/csi.v1.Node/NodePublishVolume" + + VolumeContextKeyMountOptions = "mountOptions" + VolumeContextKeyDisableFileCache = "disableFileCache" ) func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode { @@ -144,6 +149,21 @@ func joinMountOptions(existingOptions []string, newOptions []string) []string { return allMountOptions.List() } +// parseVolumeAttributes parses volume attributes and convert them to gcsfuse mount options. +func parseVolumeAttributes(fuseMountOptions []string, volumeContext map[string]string) []string { + if mountOptions, ok := volumeContext[VolumeContextKeyMountOptions]; ok { + fuseMountOptions = joinMountOptions(fuseMountOptions, strings.Split(mountOptions, ",")) + } + + if disableFileCache, ok := volumeContext[VolumeContextKeyDisableFileCache]; ok { + if boolVal, err := strconv.ParseBool(disableFileCache); err == nil && boolVal { + fuseMountOptions = joinMountOptions(fuseMountOptions, []string{util.DisableFileCacheKey}) + } + } + + return fuseMountOptions +} + func putExitFile(pod *v1.Pod, emptyDirBasePath string) error { podIsTerminating := pod.DeletionTimestamp != nil podRestartPolicyIsNever := pod.Spec.RestartPolicy == v1.RestartPolicyNever diff --git a/pkg/sidecar_mounter/sidecar_mounter.go b/pkg/sidecar_mounter/sidecar_mounter.go index 8eb1e7270..0b676bcf6 100644 --- a/pkg/sidecar_mounter/sidecar_mounter.go +++ b/pkg/sidecar_mounter/sidecar_mounter.go @@ -236,7 +236,8 @@ func (mc *MountConfig) prepareMountArgs() (map[string]string, map[string]string) value = argPair[1] } - if boolFlags[flag] && value != "" { + switch { + case boolFlags[flag] && value != "": flag = flag + "=" + value if value == "true" || value == "false" { value = "" @@ -245,10 +246,11 @@ func (mc *MountConfig) prepareMountArgs() (map[string]string, map[string]string) continue } - } - - if flag == "app-name" { + case flag == "app-name": value = GCSFuseAppName + "-" + value + case flag == util.DisableFileCacheKey: + configFileFlagMap["cache-dir"] = "" + continue } flagMap[flag] = value diff --git a/pkg/sidecar_mounter/sidecar_mounter_test.go b/pkg/sidecar_mounter/sidecar_mounter_test.go index 152fe0a7b..2a11f2a8c 100644 --- a/pkg/sidecar_mounter/sidecar_mounter_test.go +++ b/pkg/sidecar_mounter/sidecar_mounter_test.go @@ -22,6 +22,7 @@ import ( "reflect" "testing" + "github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/util" "gopkg.in/yaml.v3" ) @@ -162,6 +163,22 @@ func TestPrepareMountArgs(t *testing.T) { }, expectedConfigMapArgs: defaultConfigFileFlagMap, }, + { + name: "should return valid args when file cache is disabled", + mc: &MountConfig{ + BucketName: "test-bucket", + BufferDir: "test-buffer-dir", + CacheDir: "test-cache-dir", + ConfigFile: "test-config-file", + Options: []string{util.DisableFileCacheKey}, + }, + expectedArgs: defaultFlagMap, + expectedConfigMapArgs: map[string]string{ + "logging:file-path": "/dev/fd/1", + "logging:format": "text", + "cache-dir": "", + }, + }, } for _, tc := range testCases { diff --git a/pkg/util/util.go b/pkg/util/util.go index fc149fcc5..8fb70e6e3 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -35,6 +35,8 @@ const ( var ( targetPathRegexp = regexp.MustCompile(`/var/lib/kubelet/pods/(.*)/volumes/kubernetes\.io~csi/(.*)/mount`) emptyReplacementRegexp = regexp.MustCompile(`kubernetes\.io~csi/(.*)/mount`) + + DisableFileCacheKey = "disable-file-cache" ) // ConvertLabelsStringToMap converts the labels from string to map