From d6e553af263397e914b95feef9955897cfe11158 Mon Sep 17 00:00:00 2001 From: Sneha Aradhey Date: Thu, 12 Jun 2025 18:16:48 +0000 Subject: [PATCH] dafult data cache enabled to false --- cmd/gce-pd-csi-driver/main.go | 13 +------ pkg/gce-pd-csi-driver/node.go | 2 +- pkg/gce-pd-csi-driver/utils.go | 14 ++++++++ pkg/gce-pd-csi-driver/utils_test.go | 53 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/cmd/gce-pd-csi-driver/main.go b/cmd/gce-pd-csi-driver/main.go index fee76338a..07e883d92 100644 --- a/cmd/gce-pd-csi-driver/main.go +++ b/cmd/gce-pd-csi-driver/main.go @@ -270,7 +270,7 @@ func handle() { if err != nil { klog.Fatalf("Failed to set up metadata service: %v", err.Error()) } - isDataCacheEnabledNodePool, err := isDataCacheEnabledNodePool(ctx, *nodeName) + isDataCacheEnabledNodePool, err := driver.IsDataCacheEnabledNodePool(ctx, *nodeName, *enableDataCacheFlag) if err != nil { klog.Fatalf("Failed to get node info from API server: %v", err.Error()) } @@ -381,17 +381,6 @@ func urlFlag(target **url.URL, name string, usage string) { }) } -func isDataCacheEnabledNodePool(ctx context.Context, nodeName string) (bool, error) { - if !*enableDataCacheFlag { - return false, nil - } - if len(nodeName) > 0 && nodeName != common.TestNode { // disregard logic below when E2E testing. - dataCacheLSSDCount, err := driver.GetDataCacheCountFromNodeLabel(ctx, nodeName) - return dataCacheLSSDCount != 0, err - } - return true, nil -} - func fetchLssdsForRaiding(lssdCount int) ([]string, error) { allLssds, err := driver.FetchAllLssds() if err != nil { diff --git a/pkg/gce-pd-csi-driver/node.go b/pkg/gce-pd-csi-driver/node.go index c31be82c9..13ed97715 100644 --- a/pkg/gce-pd-csi-driver/node.go +++ b/pkg/gce-pd-csi-driver/node.go @@ -613,7 +613,7 @@ func (ns *GCENodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUns // The NodeUnstageVolume does not have any volume or publish context, we need to get the info from LVM locally // Check if cache group cache-{volumeID} exist in LVM - if ns.EnableDataCache { + if ns.EnableDataCache && ns.DataCacheEnabledNodePool { nodeId := ns.MetadataService.GetName() err := cleanupCache(volumeID, nodeId) if err != nil { diff --git a/pkg/gce-pd-csi-driver/utils.go b/pkg/gce-pd-csi-driver/utils.go index 9e64778b8..f8a4d0461 100644 --- a/pkg/gce-pd-csi-driver/utils.go +++ b/pkg/gce-pd-csi-driver/utils.go @@ -339,3 +339,17 @@ func containsZone(zones []string, zone string) bool { return false } + +func IsDataCacheEnabledNodePool(ctx context.Context, nodeName string, enableDataCacheFlag bool) (bool, error) { + if !enableDataCacheFlag { + return false, nil + } + if nodeName == common.TestNode { // disregard logic below when E2E testing. + return true, nil + } + if len(nodeName) > 0 { + dataCacheLSSDCount, err := GetDataCacheCountFromNodeLabel(ctx, nodeName) + return dataCacheLSSDCount != 0, err + } + return false, nil +} diff --git a/pkg/gce-pd-csi-driver/utils_test.go b/pkg/gce-pd-csi-driver/utils_test.go index f187cc9d8..263c0143f 100644 --- a/pkg/gce-pd-csi-driver/utils_test.go +++ b/pkg/gce-pd-csi-driver/utils_test.go @@ -18,8 +18,10 @@ limitations under the License. package gceGCEDriver import ( + "context" "fmt" "testing" + "time" csi "github.com/container-storage-interface/spec/lib/go/csi" "github.com/google/go-cmp/cmp" @@ -857,3 +859,54 @@ func TestGetHyperdiskAccessModeFromCapabilities(t *testing.T) { } } } + +func TestIsDataCacheEnabledNodePool(t *testing.T) { + for _, tc := range []struct { + name string + nodeName string + wantDataCacheEnabled bool + dataCacheFlag bool + wantErr bool + }{ + { + // Valid nod ename tries to fetch the data cache count from node labels resulting in an error + name: "node name is provided", + nodeName: "gke-node-some-name", + dataCacheFlag: true, + wantDataCacheEnabled: true, + wantErr: true, + }, + { + name: "no node name provided", + nodeName: "", + dataCacheFlag: true, + wantDataCacheEnabled: false, + }, + { + name: "test node", + nodeName: common.TestNode, + dataCacheFlag: true, + wantDataCacheEnabled: true, + }, + { + name: "node name provided but data cache feature disabled", + nodeName: "", + dataCacheFlag: false, + wantDataCacheEnabled: false, + }, + } { + t.Logf("Running test: %v", tc.name) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + gotDataCacheEnabled, err := IsDataCacheEnabledNodePool(ctx, tc.nodeName, tc.dataCacheFlag) + if err != nil { + if !tc.wantErr { + t.Errorf("unexpected error, got %v", err) + } + continue + } + if gotDataCacheEnabled != tc.wantDataCacheEnabled { + t.Errorf("want %t, got %t", tc.wantDataCacheEnabled, gotDataCacheEnabled) + } + } +}