Skip to content

Commit f5f5624

Browse files
committed
use node UID for VG creation instead of name
1 parent 0be5a10 commit f5f5624

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

cmd/gce-pd-csi-driver/main.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"strings"
2828
"time"
2929

30+
v1 "k8s.io/api/core/v1"
3031
"k8s.io/klog/v2"
3132
"k8s.io/utils/strings/slices"
3233
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
@@ -245,7 +246,11 @@ func handle() {
245246
if err != nil {
246247
klog.Fatalf("Failed to set up metadata service: %v", err.Error())
247248
}
248-
isDataCacheEnabledNodePool, err := isDataCacheEnabledNodePool(ctx, *nodeName)
249+
node, err := driver.FetchNodeWithRetry(ctx, *nodeName)
250+
if err != nil {
251+
klog.Fatalf("Failed to get node info from API server: %v", err.Error())
252+
}
253+
isDataCacheEnabledNodePool, err := isDataCacheEnabledNodePool(ctx, node)
249254
if err != nil {
250255
klog.Fatalf("Failed to get node info from API server: %v", err.Error())
251256
}
@@ -264,7 +269,7 @@ func handle() {
264269
klog.Errorf("Data Cache enabled, but --node-name not passed")
265270
}
266271
if nsArgs.DataCacheEnabledNodePool {
267-
if err := setupDataCache(ctx, *nodeName, nodeServer.MetadataService.GetName()); err != nil {
272+
if err := setupDataCache(ctx, node); err != nil {
268273
klog.Errorf("Data Cache setup failed: %v", err)
269274
}
270275
go driver.StartWatcher(*nodeName)
@@ -351,12 +356,13 @@ func urlFlag(target **url.URL, name string, usage string) {
351356
})
352357
}
353358

354-
func isDataCacheEnabledNodePool(ctx context.Context, nodeName string) (bool, error) {
359+
func isDataCacheEnabledNodePool(ctx context.Context, node *v1.Node) (bool, error) {
355360
if !*enableDataCacheFlag {
356361
return false, nil
357362
}
363+
nodeName := node.Name
358364
if len(nodeName) > 0 && nodeName != common.TestNode { // disregard logic below when E2E testing.
359-
dataCacheLSSDCount, err := driver.GetDataCacheCountFromNodeLabel(ctx, nodeName)
365+
dataCacheLSSDCount, err := driver.GetDataCacheCountFromNodeLabel(ctx, node)
360366
return dataCacheLSSDCount != 0, err
361367
}
362368
return true, nil
@@ -394,7 +400,8 @@ func fetchLssdsForRaiding(lssdCount int) ([]string, error) {
394400
return availableLssds[:lssdCount], nil
395401
}
396402

397-
func setupDataCache(ctx context.Context, nodeName string, nodeId string) error {
403+
func setupDataCache(ctx context.Context, node *v1.Node) error {
404+
nodeName := node.Name
398405
isAlreadyRaided, err := driver.IsRaided()
399406
if err != nil {
400407
klog.V(4).Infof("Errored while scanning for available LocalSSDs err:%v; continuing Raiding", err)
@@ -404,9 +411,10 @@ func setupDataCache(ctx context.Context, nodeName string, nodeId string) error {
404411
}
405412

406413
lssdCount := common.LocalSSDCountForDataCache
414+
nodeUid := nodeName
407415
if nodeName != common.TestNode {
408-
var err error
409-
lssdCount, err = driver.GetDataCacheCountFromNodeLabel(ctx, nodeName)
416+
nodeUid = string(node.ObjectMeta.UID)
417+
lssdCount, err = driver.GetDataCacheCountFromNodeLabel(ctx, node)
410418
if err != nil {
411419
return err
412420
}
@@ -425,7 +433,7 @@ func setupDataCache(ctx context.Context, nodeName string, nodeId string) error {
425433
}
426434

427435
// Initializing data cache node (VG checks w/ raided lssd)
428-
if err := driver.InitializeDataCacheNode(nodeId); err != nil {
436+
if err := driver.InitializeDataCacheNode(nodeUid); err != nil {
429437
return err
430438
}
431439

pkg/gce-pd-csi-driver/cache.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,7 @@ func ValidateDataCacheConfig(dataCacheMode string, dataCacheSize string, ctx con
248248
return fmt.Errorf("Data Cache is not enabled for PVC (data-cache-size: %v, data-cache-mode: %v). Please set both parameters in StorageClass to enable caching", dataCacheSize, dataCacheMode)
249249
}
250250

251-
func GetDataCacheCountFromNodeLabel(ctx context.Context, nodeName string) (int, error) {
252-
cfg, err := rest.InClusterConfig()
253-
if err != nil {
254-
return 0, err
255-
}
256-
kubeClient, err := kubernetes.NewForConfig(cfg)
257-
if err != nil {
258-
return 0, err
259-
}
260-
node, err := getNodeWithRetry(ctx, kubeClient, nodeName)
261-
if err != nil {
262-
return 0, err
263-
}
251+
func GetDataCacheCountFromNodeLabel(ctx context.Context, node *v1.Node) (int, error) {
264252
if val, found := node.GetLabels()[fmt.Sprintf(common.NodeLabelPrefix, common.DataCacheLssdCountLabel)]; found {
265253
dataCacheCount, err := strconv.Atoi(val)
266254
if err != nil {
@@ -272,14 +260,22 @@ func GetDataCacheCountFromNodeLabel(ctx context.Context, nodeName string) (int,
272260
return 0, nil
273261
}
274262

275-
func getNodeWithRetry(ctx context.Context, kubeClient *kubernetes.Clientset, nodeName string) (*v1.Node, error) {
263+
func FetchNodeWithRetry(ctx context.Context, nodeName string) (*v1.Node, error) {
276264
var nodeObj *v1.Node
265+
cfg, err := rest.InClusterConfig()
266+
if err != nil {
267+
return nil, err
268+
}
269+
kubeClient, err := kubernetes.NewForConfig(cfg)
270+
if err != nil {
271+
return nil, err
272+
}
277273
backoff := wait.Backoff{
278274
Duration: 1 * time.Second,
279275
Factor: 2.0,
280276
Steps: 5,
281277
}
282-
err := wait.ExponentialBackoffWithContext(ctx, backoff, func(_ context.Context) (bool, error) {
278+
err = wait.ExponentialBackoffWithContext(ctx, backoff, func(_ context.Context) (bool, error) {
283279
node, err := kubeClient.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
284280
if err != nil {
285281
klog.Warningf("Error getting node %s: %v, retrying...\n", nodeName, err)

0 commit comments

Comments
 (0)