Skip to content

Commit

Permalink
add disableFileCache volume attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
songjiaxun committed Feb 17, 2024
1 parent 357efb0 commit d8f8bcf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
6 changes: 2 additions & 4 deletions pkg/csi_driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand Down
20 changes: 20 additions & 0 deletions pkg/csi_driver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions pkg/sidecar_mounter/sidecar_mounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions pkg/sidecar_mounter/sidecar_mounter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"reflect"
"testing"

"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/util"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d8f8bcf

Please sign in to comment.