@@ -27,12 +27,8 @@ import (
27
27
"strings"
28
28
"time"
29
29
30
- "k8s.io/client-go/kubernetes"
31
- "k8s.io/client-go/rest"
32
30
"k8s.io/klog/v2"
33
31
"k8s.io/utils/strings/slices"
34
-
35
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36
32
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
37
33
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/deviceutils"
38
34
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
95
91
)
96
92
97
93
const (
98
- driverName = "pd.csi.storage.gke.io"
99
- dataCacheLabel = "datacache-storage-gke-io"
100
- dataCacheLabelValue = "enabled"
94
+ driverName = "pd.csi.storage.gke.io"
101
95
)
102
96
103
97
func init () {
@@ -331,37 +325,74 @@ func urlFlag(target **url.URL, name string, usage string) {
331
325
})
332
326
}
333
327
334
- func setupDataCache (ctx context.Context , nodeName string , nodeId string ) error {
328
+ func fetchLssdsForRaiding (lssdCount int ) ([]string , error ) {
329
+ allLssds , err := driver .FetchAllLssds ()
330
+ if err != nil {
331
+ return nil , fmt .Errorf ("Error listing all LSSDs %v" , err )
332
+ }
333
+
334
+ raidedLssds , err := driver .FetchRaidedLssds ()
335
+ if err != nil {
336
+ return nil , fmt .Errorf ("Error listing RAIDed LSSDs %v" , err )
337
+ }
338
+
339
+ unRaidedLssds := []string {}
340
+ for _ , l := range allLssds {
341
+ if ! slices .Contains (raidedLssds , l ) {
342
+ unRaidedLssds = append (unRaidedLssds , l )
343
+ }
344
+ if len (unRaidedLssds ) == lssdCount {
345
+ break
346
+ }
347
+ }
348
+
349
+ LSSDsWithEmptyMountPoint , err := driver .FetchLSSDsWihtEmptyMountPoint ()
350
+ if err != nil {
351
+ return nil , fmt .Errorf ("Error listing LSSDs with empty mountpoint: %v" , err )
352
+ }
353
+
354
+ // We need to ensure the disks to be used for Datacache are both unRAIDed & not containing mountpoints for ephemeral storage already
355
+ availableLssds := slices .Filter (nil , unRaidedLssds , func (e string ) bool {
356
+ return slices .Contains (LSSDsWithEmptyMountPoint , e )
357
+ })
358
+
359
+ if len (availableLssds ) == 0 {
360
+ return nil , fmt .Errorf ("No LSSDs available to set up caching" )
361
+ }
362
+
363
+ if len (availableLssds ) < lssdCount {
364
+ return nil , fmt .Errorf ("Not enough LSSDs available to set up caching. Available LSSDs: %v, wanted LSSDs: %v" , len (availableLssds ), lssdCount )
365
+ }
366
+ return availableLssds , nil
367
+ }
368
+
369
+ func setupDataCache (ctx context.Context , nodeName string ) error {
335
370
isAlreadyRaided , err := driver .IsRaided ()
336
371
if err != nil {
337
- klog .V (4 ).Infof ("Errored while scanning for available LocalSSDs err:%v; continuing Raiding" , err )
372
+ klog .V (2 ).Infof ("Errored while scanning for available LocalSSDs err:%v; continuing Raiding" , err )
338
373
} else if isAlreadyRaided {
339
- klog .V (4 ).Infof ("Local SSDs are already RAIDed. Skipping Data Cache setup." )
374
+ klog .V (2 ).Infof ("Local SSDs are already RAIDed. Skipping Datacache setup." )
340
375
return nil
341
376
}
342
377
343
378
lssdCount := common .LocalSSDCountForDataCache
344
379
if nodeName != common .TestNode {
345
- cfg , err := rest .InClusterConfig ()
346
- if err != nil {
347
- return err
348
- }
349
- kubeClient , err := kubernetes .NewForConfig (cfg )
350
- if err != nil {
351
- return err
380
+ var err error
381
+ lssdCount , err = driver .GetDataCacheCountFromNodeLabel (ctx , nodeName )
382
+ if lssdCount == 0 {
383
+ klog .Infof ("Datacache is not enabled on node %v" , nodeName )
384
+ return nil
352
385
}
353
- node , err := kubeClient .CoreV1 ().Nodes ().Get (ctx , nodeName , metav1.GetOptions {})
354
386
if err != nil {
355
- // We could retry, but this error will also crashloop the driver which may be as good a way to retry as any.
356
387
return err
357
388
}
358
- if val , found := node .GetLabels ()[dataCacheLabel ]; ! found || val != dataCacheLabelValue {
359
- klog .V (2 ).Infof ("Datacache not enabled for node %s; node label %s=%s and not %s" , nodeName , dataCacheLabel , val , dataCacheLabelValue )
360
- return nil
361
- }
362
389
}
363
- klog .V (2 ).Info ("Raiding local ssds to setup data cache" )
364
- if err := driver .RaidLocalSsds (); err != nil {
390
+ lssdNames , err := fetchLssdsForRaiding (lssdCount )
391
+ if err != nil {
392
+ klog .Fatalf ("Failed to get sufficient SSDs for Datacache's caching setup: %v" , err )
393
+ }
394
+ klog .V (2 ).Infof ("Raiding local ssds to setup data cache: %v" , lssdNames )
395
+ if err := driver .RaidLocalSsds (lssdNames ); err != nil {
365
396
return fmt .Errorf ("Failed to Raid local SSDs, unable to setup data caching, got error %v" , err )
366
397
}
367
398
0 commit comments