@@ -35,9 +35,8 @@ import (
35
35
"google.golang.org/grpc/status"
36
36
v1 "k8s.io/api/core/v1"
37
37
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38
- "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
39
38
"k8s.io/apimachinery/pkg/fields"
40
- "k8s.io/client-go/dynamic "
39
+ "k8s.io/apimachinery/pkg/types "
41
40
api "k8s.io/kubernetes/pkg/apis/core"
42
41
"sigs.k8s.io/controller-runtime/pkg/client/config"
43
42
spv1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/storagepool/cns/v1alpha1"
@@ -399,23 +398,24 @@ func getDatastoreURLFromStoragePool(ctx context.Context, spName string) (string,
399
398
}
400
399
401
400
// create a new StoragePool client.
402
- spclient , err := dynamic . NewForConfig ( cfg )
401
+ c , err := k8s . NewClientForGroup ( ctx , cfg , spv1alpha1 . SchemeGroupVersion . Group )
403
402
if err != nil {
404
403
return "" , fmt .Errorf ("failed to create StoragePool client using config. Err: %+v" , err )
405
404
}
406
- spResource := spv1alpha1 .SchemeGroupVersion .WithResource ("storagepools" )
407
405
406
+ sp := & spv1alpha1.StoragePool {}
408
407
// Get StoragePool with spName.
409
- sp , err := spclient . Resource ( spResource ). Get (ctx , spName , metav1. GetOptions {} )
408
+ err = c . Get (ctx , types. NamespacedName { Name : spName }, sp )
410
409
if err != nil {
411
410
return "" , fmt .Errorf ("failed to get StoragePool with name %s: %+v" , spName , err )
412
411
}
413
412
414
413
// extract the datastoreUrl field.
415
- datastoreURL , found , err := unstructured . NestedString ( sp .Object , "spec" , "parameters" , " datastoreUrl")
416
- if ! found || err != nil {
414
+ datastoreURL , found := sp .Spec . Parameters [ " datastoreUrl"]
415
+ if ! found {
417
416
return "" , fmt .Errorf ("failed to find datastoreUrl in StoragePool %s" , spName )
418
417
}
418
+
419
419
return datastoreURL , nil
420
420
}
421
421
@@ -429,31 +429,30 @@ func getStoragePoolInfo(ctx context.Context, spName string) ([]string, string, e
429
429
}
430
430
431
431
// Create a new StoragePool client.
432
- spClient , err := dynamic . NewForConfig ( cfg )
432
+ c , err := k8s . NewClientForGroup ( ctx , cfg , spv1alpha1 . SchemeGroupVersion . Group )
433
433
if err != nil {
434
434
return nil , "" , fmt .Errorf ("failed to create StoragePool client using config. Err: %+v" , err )
435
435
}
436
- spResource := spv1alpha1 .SchemeGroupVersion .WithResource ("storagepools" )
437
436
438
437
// Get StoragePool with spName.
439
- sp , err := spClient .Resource (spResource ).Get (ctx , spName , metav1.GetOptions {})
438
+ sp := & spv1alpha1.StoragePool {}
439
+ err = c .Get (ctx , types.NamespacedName {Name : spName }, sp )
440
440
if err != nil {
441
441
return nil , "" , fmt .Errorf ("failed to get StoragePool with name %s: %+v" , spName , err )
442
442
}
443
443
444
444
// Extract the accessibleNodes field.
445
- accessibleNodes , found , err := unstructured .NestedStringSlice (sp .Object , "status" , "accessibleNodes" )
446
- if ! found || err != nil {
447
- return nil , "" , fmt .Errorf ("failed to find datastoreUrl in StoragePool %s" , spName )
445
+ if len (sp .Status .AccessibleNodes ) == 0 {
446
+ return nil , "" , fmt .Errorf ("failed to find accessible nodes in StoragePool %s" , spName )
448
447
}
449
448
450
449
// Get the storage pool type.
451
- poolType , found , err := unstructured . NestedString ( sp .Object , "metadata" , "labels" , spTypeKey )
452
- if ! found || err != nil {
450
+ poolType , found := sp .ObjectMeta . Labels [ spTypeKey ]
451
+ if ! found {
453
452
return nil , "" , fmt .Errorf ("failed to find pool type in StoragePool %s" , spName )
454
453
}
455
454
456
- return accessibleNodes , poolType , nil
455
+ return sp . Status . AccessibleNodes , poolType , nil
457
456
}
458
457
459
458
// isValidAccessibilityRequirements validates if the given accessibility
0 commit comments