Skip to content

Commit e795a3b

Browse files
committed
update main.go
1 parent 68e324e commit e795a3b

File tree

1 file changed

+62
-20
lines changed

1 file changed

+62
-20
lines changed

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

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929

3030
"k8s.io/klog/v2"
3131
"k8s.io/utils/strings/slices"
32-
3332
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
3433
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/deviceutils"
3534
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
@@ -99,9 +98,7 @@ var (
9998
)
10099

101100
const (
102-
driverName = "pd.csi.storage.gke.io"
103-
dataCacheLabel = "datacache-storage-gke-io"
104-
dataCacheLabelValue = "enabled"
101+
driverName = "pd.csi.storage.gke.io"
105102
)
106103

107104
func init() {
@@ -347,29 +344,74 @@ func urlFlag(target **url.URL, name string, usage string) {
347344
})
348345
}
349346

347+
func fetchLssdsForRaiding(lssdCount int) ([]string, error) {
348+
allLssds, err := driver.FetchAllLssds()
349+
if err != nil {
350+
return nil, fmt.Errorf("Error listing all LSSDs %v", err)
351+
}
352+
353+
raidedLssds, err := driver.FetchRaidedLssds()
354+
if err != nil {
355+
return nil, fmt.Errorf("Error listing RAIDed LSSDs %v", err)
356+
}
357+
358+
unRaidedLssds := []string{}
359+
for _, l := range allLssds {
360+
if !slices.Contains(raidedLssds, l) {
361+
unRaidedLssds = append(unRaidedLssds, l)
362+
}
363+
if len(unRaidedLssds) == lssdCount {
364+
break
365+
}
366+
}
367+
368+
LSSDsWithEmptyMountPoint, err := driver.FetchLSSDsWihtEmptyMountPoint()
369+
if err != nil {
370+
return nil, fmt.Errorf("Error listing LSSDs with empty mountpoint: %v", err)
371+
}
372+
373+
// We need to ensure the disks to be used for Datacache are both unRAIDed & not containing mountpoints for ephemeral storage already
374+
availableLssds := slices.Filter(nil, unRaidedLssds, func(e string) bool {
375+
return slices.Contains(LSSDsWithEmptyMountPoint, e)
376+
})
377+
378+
if len(availableLssds) == 0 {
379+
return nil, fmt.Errorf("No LSSDs available to set up caching")
380+
}
381+
382+
if len(availableLssds) < lssdCount {
383+
return nil, fmt.Errorf("Not enough LSSDs available to set up caching. Available LSSDs: %v, wanted LSSDs: %v", len(availableLssds), lssdCount)
384+
}
385+
return availableLssds, nil
386+
}
387+
350388
func setupDataCache(ctx context.Context, nodeName string) error {
351-
klog.V(2).Infof("Setting up data cache for node %s", nodeName)
389+
isAlreadyRaided, err := driver.IsRaided()
390+
if err != nil {
391+
klog.V(2).Infof("Errored while scanning for available LocalSSDs err:%v; continuing Raiding", err)
392+
} else if isAlreadyRaided {
393+
klog.V(2).Infof("Local SSDs are already RAIDed. Skipping Datacache setup.")
394+
return nil
395+
}
396+
397+
lssdCount := common.LocalSSDCountForDataCache
352398
if nodeName != common.TestNode {
353-
cfg, err := rest.InClusterConfig()
354-
if err != nil {
355-
return err
356-
}
357-
kubeClient, err := kubernetes.NewForConfig(cfg)
358-
if err != nil {
359-
return err
399+
var err error
400+
lssdCount, err = driver.GetDataCacheCountFromNodeLabel(ctx, nodeName)
401+
if lssdCount == 0 {
402+
klog.Infof("Datacache is not enabled on node %v", nodeName)
403+
return nil
360404
}
361-
node, err := kubeClient.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
362405
if err != nil {
363-
// We could retry, but this error will also crashloop the driver which may be as good a way to retry as any.
364406
return err
365407
}
366-
if val, found := node.GetLabels()[dataCacheLabel]; !found || val != dataCacheLabelValue {
367-
klog.V(2).Infof("Datacache not enabled for node %s; node label %s=%s and not %s", nodeName, dataCacheLabel, val, dataCacheLabelValue)
368-
return nil
369-
}
370408
}
371-
klog.V(2).Info("Raiding local ssds to setup data cache")
372-
if err := driver.RaidLocalSsds(); err != nil {
409+
lssdNames, err := fetchLssdsForRaiding(lssdCount)
410+
if err != nil {
411+
klog.Fatalf("Failed to get sufficient SSDs for Datacache's caching setup: %v", err)
412+
}
413+
klog.V(2).Infof("Raiding local ssds to setup data cache: %v", lssdNames)
414+
if err := driver.RaidLocalSsds(lssdNames); err != nil {
373415
return fmt.Errorf("Failed to Raid local SSDs, unable to setup data caching, got error %v", err)
374416
}
375417

0 commit comments

Comments
 (0)