/
authmanager.go
540 lines (489 loc) · 21.9 KB
/
authmanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"context"
"fmt"
"sync"
"time"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25/mo"
vim25types "github.com/vmware/govmomi/vim25/types"
"google.golang.org/grpc/codes"
cnsvsphere "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/cns-lib/vsphere"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger"
)
// AuthorizationService exposes interfaces to support authorization check on
// datastores and get datastores which will be used by create volume.
// It is provided for backward compatibility. For latest vSphere release,
// this service is redundant.
type AuthorizationService interface {
// GetDatastoreMapForBlockVolumes returns a map of datastore URL to datastore
// info for only those datastores the CSI VC user has Datastore.FileManagement
// privilege for.
GetDatastoreMapForBlockVolumes(ctx context.Context) map[string]*cnsvsphere.DatastoreInfo
// GetFsEnabledClusterToDsMap returns a map of cluster ID to datastores info
// objects for vSAN clusters with file services enabled. The datastores are
// those on which the CSI VC user has Host.Config.Storage privilege.
GetFsEnabledClusterToDsMap(ctx context.Context) map[string][]*cnsvsphere.DatastoreInfo
// ResetvCenterInstance sets new vCenter instance for AuthorizationService
ResetvCenterInstance(ctx context.Context, vCenter *cnsvsphere.VirtualCenter)
}
// AuthManager maintains an internal map to track the datastores that need to be
// used by create volume.
type AuthManager struct {
// Map the datastore url to datastore info which need to be used when
// invoking CNS CreateVolume API.
datastoreMapForBlockVolumes map[string]*cnsvsphere.DatastoreInfo
// Map the vSAN file services enabled cluster ID to list of datastore info
// for that cluster. The datastore info objects are to be used when invoking
// CNS CreateVolume API.
fsEnabledClusterToDsMap map[string][]*cnsvsphere.DatastoreInfo
// Make sure the update for datastoreMap is mutually exclusive.
rwMutex sync.RWMutex
// VCenter Instance.
vcenter *cnsvsphere.VirtualCenter
}
// onceForAuthorizationService is used for initializing the AuthorizationService
// singleton.
var onceForAuthorizationService sync.Once
// authManagerIntance is instance of authManager and implements interface for
// AuthorizationService.
var authManagerInstance *AuthManager
var authManagerInstances = make(map[string]*AuthManager)
// GetAuthorizationService returns the singleton AuthorizationService.
func GetAuthorizationService(ctx context.Context, vc *cnsvsphere.VirtualCenter) (AuthorizationService, error) {
log := logger.GetLogger(ctx)
onceForAuthorizationService.Do(func() {
log.Info("Initializing authorization service...")
authManagerInstance = &AuthManager{
datastoreMapForBlockVolumes: make(map[string]*cnsvsphere.DatastoreInfo),
fsEnabledClusterToDsMap: make(map[string][]*cnsvsphere.DatastoreInfo),
rwMutex: sync.RWMutex{},
vcenter: vc,
}
})
log.Info("authorization service initialized")
return authManagerInstance, nil
}
// GetAuthorizationServices returns the AuthManager instances for supplied vCenter servers
func GetAuthorizationServices(ctx context.Context, vcs []*cnsvsphere.VirtualCenter) (map[string]*AuthManager, error) {
log := logger.GetLogger(ctx)
onceForAuthorizationService.Do(func() {
log.Info("Initializing authorization service...")
for _, vc := range vcs {
authManagerInstance = &AuthManager{
datastoreMapForBlockVolumes: make(map[string]*cnsvsphere.DatastoreInfo),
fsEnabledClusterToDsMap: make(map[string][]*cnsvsphere.DatastoreInfo),
rwMutex: sync.RWMutex{},
vcenter: vc,
}
authManagerInstances[vc.Config.Host] = authManagerInstance
}
})
log.Info("authorization service initialized")
return authManagerInstances, nil
}
// GetDatastoreMapForBlockVolumes returns a DatastoreMapForBlockVolumes. This
// map maps datastore url to datastore info which need to be used when creating
// block volume.
func (authManager *AuthManager) GetDatastoreMapForBlockVolumes(
ctx context.Context) map[string]*cnsvsphere.DatastoreInfo {
datastoreMapForBlockVolumes := make(map[string]*cnsvsphere.DatastoreInfo)
authManager.rwMutex.RLock()
defer authManager.rwMutex.RUnlock()
for dsURL, dsInfo := range authManager.datastoreMapForBlockVolumes {
datastoreMapForBlockVolumes[dsURL] = dsInfo
}
return datastoreMapForBlockVolumes
}
// GetFsEnabledClusterToDsMap returns a map of cluster ID with vSAN file
// services enabled to its datastore info objects.
func (authManager *AuthManager) GetFsEnabledClusterToDsMap(
ctx context.Context) map[string][]*cnsvsphere.DatastoreInfo {
fsEnabledClusterToDsMap := make(map[string][]*cnsvsphere.DatastoreInfo)
authManager.rwMutex.RLock()
defer authManager.rwMutex.RUnlock()
for clusterID, datastores := range authManager.fsEnabledClusterToDsMap {
fsEnabledClusterToDsMap[clusterID] = datastores
}
return fsEnabledClusterToDsMap
}
// ResetvCenterInstance sets new vCenter instance for AuthorizationService.
func (authManager *AuthManager) ResetvCenterInstance(ctx context.Context, vCenter *cnsvsphere.VirtualCenter) {
log := logger.GetLogger(ctx)
log.Info("Resetting vCenter Instance in the AuthManager")
authManager.vcenter = vCenter
}
// refreshDatastoreMapForBlockVolumes scans all datastores in vCenter to check
// privileges, and compute the datastoreMapForBlockVolumes.
func (authManager *AuthManager) refreshDatastoreMapForBlockVolumes() {
ctx, log := logger.GetNewContextWithLogger()
vcenterHost := authManager.vcenter.Config.Host
log.Debugf("auth manager: refreshDatastoreMapForBlockVolumes is triggered for vCenter %q", vcenterHost)
newDatastoreMapForBlockVolumes, err := GenerateDatastoreMapForBlockVolumes(ctx, authManager.vcenter)
if err == nil {
authManager.rwMutex.Lock()
defer authManager.rwMutex.Unlock()
authManager.datastoreMapForBlockVolumes = newDatastoreMapForBlockVolumes
log.Infof("auth manager: datastoreMapForBlockVolumes is updated to %v for vCenter %q",
newDatastoreMapForBlockVolumes, vcenterHost)
} else {
log.Warnf("auth manager: failed to get updated datastoreMapForBlockVolumes for vCenter %q, Err: %v",
vcenterHost, err)
}
}
// refreshFSEnabledClustersToDsMap scans all clusters with vSAN FS enabled in
// vCenter to check privileges, and compute the fsEnabledClusterToDsMap.
func (authManager *AuthManager) refreshFSEnabledClustersToDsMap() {
ctx, log := logger.GetNewContextWithLogger()
vcenterHost := authManager.vcenter.Config.Host
log.Debugf("auth manager: refreshDatastoreMapsForFileVolumes is triggered for vCenter %q", vcenterHost)
newFsEnabledClusterToDsMap, err := GenerateFSEnabledClustersToDsMap(ctx, authManager.vcenter)
if err == nil {
authManager.rwMutex.Lock()
defer authManager.rwMutex.Unlock()
authManager.fsEnabledClusterToDsMap = newFsEnabledClusterToDsMap
log.Infof("auth manager: newFsEnabledClusterToDsMap is updated to %v for vCenter %q",
newFsEnabledClusterToDsMap, vcenterHost)
} else {
log.Warnf("auth manager: failed to get updated datastoreMapForFileVolumes for vCenter %q, Err: %v",
vcenterHost, err)
}
}
// ComputeDatastoreMapForBlockVolumes refreshes DatastoreMapForBlockVolumes
// periodically.
func ComputeDatastoreMapForBlockVolumes(authManager *AuthManager, authCheckInterval int) {
log := logger.GetLoggerWithNoContext()
log.Infof("auth manager: ComputeDatastoreMapForBlockVolumes entered for vCenter %q",
authManager.vcenter.Config.Host)
ticker := time.NewTicker(time.Duration(authCheckInterval) * time.Minute)
for ; true; <-ticker.C {
authManager.refreshDatastoreMapForBlockVolumes()
}
}
// ComputeFSEnabledClustersToDsMap refreshes fsEnabledClusterToDsMap
// periodically.
func ComputeFSEnabledClustersToDsMap(authManager *AuthManager, authCheckInterval int) {
log := logger.GetLoggerWithNoContext()
log.Infof("auth manager: ComputeFSEnabledClustersToDsMap entered for vCenter %q",
authManager.vcenter.Config.Host)
ticker := time.NewTicker(time.Duration(authCheckInterval) * time.Minute)
for ; true; <-ticker.C {
authManager.refreshFSEnabledClustersToDsMap()
}
}
// GenerateDatastoreMapForBlockVolumes scans all datastores in Vcenter and do
// privilege check on those datastoes. It will return datastores which has the
// privileges for creating block volume.
func GenerateDatastoreMapForBlockVolumes(ctx context.Context,
vc *cnsvsphere.VirtualCenter) (map[string]*cnsvsphere.DatastoreInfo, error) {
log := logger.GetLogger(ctx)
// Get all datastores from VC.
dcList, err := vc.GetDatacenters(ctx)
if err != nil {
return nil, logger.LogNewErrorCodef(log, codes.Internal,
"failed to get datacenter list for vCenter %q. err: %+v", vc.Config.Host, err)
}
var dsURLTodsInfoMap map[string]*cnsvsphere.DatastoreInfo
var dsURLs []string
var dsInfos []*cnsvsphere.DatastoreInfo
var entities []vim25types.ManagedObjectReference
for _, dc := range dcList {
dsURLTodsInfoMap, err = dc.GetAllDatastores(ctx)
if err != nil {
msg := fmt.Sprintf("failed to get dsURLTodsInfoMap for vCenter %q. err: %+v", vc.Config.Host, err)
log.Error(msg)
}
for dsURL, dsInfo := range dsURLTodsInfoMap {
dsURLs = append(dsURLs, dsURL)
dsInfos = append(dsInfos, dsInfo)
dsMoRef := dsInfo.Reference()
entities = append(entities, dsMoRef)
}
}
if len(entities) == 0 {
return map[string]*cnsvsphere.DatastoreInfo{}, nil
}
dsURLToInfoMap, err := getDatastoresWithBlockVolumePrivs(ctx, vc, dsURLs, dsInfos, entities)
if err != nil {
log.Errorf("failed to get datastores with required priv for vCenter %q. Error: %+v", vc.Config.Host, err)
return nil, err
}
return dsURLToInfoMap, nil
}
// GenerateFSEnabledClustersToDsMap scans all clusters in VC and do privilege
// check on them. It will return a map of cluster id to the list of datastore
// objects. The key is cluster moid with vSAN FS enabled and Host.Config.Storage
// privilege. The value is a list of vSAN datastoreInfo objects for the cluster.
func GenerateFSEnabledClustersToDsMap(ctx context.Context,
vc *cnsvsphere.VirtualCenter) (map[string][]*cnsvsphere.DatastoreInfo, error) {
log := logger.GetLogger(ctx)
clusterToDsInfoListMap := make(map[string][]*cnsvsphere.DatastoreInfo)
datacenters, err := vc.ListDatacenters(ctx)
if err != nil {
log.Errorf("failed to find datacenters from vCenter: %q, Error: %+v", vc.Config.Host, err)
return nil, err
}
// Get all vSAN datastores from VC.
vsanDsURLToInfoMap, err := vc.GetVsanDatastores(ctx, datacenters)
if err != nil {
log.Errorf("failed to get vSAN datastores for vCenter %q, error %+v", vc.Config.Host, err)
return nil, err
}
// Return empty map if no vSAN datastores are found.
if len(vsanDsURLToInfoMap) == 0 {
log.Infof("No vSAN datastores found for vCenter %q", vc.Config.Host)
return clusterToDsInfoListMap, nil
}
// Initialize vsan client.
err = vc.ConnectVsan(ctx)
if err != nil {
log.Errorf("error occurred while connecting to VSAN from vCenter %q, err: %+v", vc.Config.Host, err)
return nil, err
}
fsEnabledClusterToDsURLsMap, err := getFSEnabledClusterToDsURLsMap(ctx, vc, datacenters)
if err != nil {
log.Errorf("failed to get file service enabled clusters map for vCenter %q, error %+v", vc.Config.Host, err)
return nil, err
}
// Create a map of cluster to dsInfo objects. These objects are used while
// calling CNS API to create file volumes.
for clusterMoID, dsURLs := range fsEnabledClusterToDsURLsMap {
for _, dsURL := range dsURLs {
if dsInfo, ok := vsanDsURLToInfoMap[dsURL]; ok {
log.Debugf("Adding vSAN datastore %q to the list for FS enabled cluster %q for vCenter %q",
dsURL, clusterMoID, vc.Config.Host)
clusterToDsInfoListMap[clusterMoID] = append(clusterToDsInfoListMap[clusterMoID], dsInfo)
}
}
}
log.Debugf("clusterToDsInfoListMap for vCenter %q is %+v", vc.Config.Host, clusterToDsInfoListMap)
return clusterToDsInfoListMap, nil
}
// getDatastoresWithBlockVolumePrivs gets datastores with required priv for CSI
// user.
func getDatastoresWithBlockVolumePrivs(ctx context.Context, vc *cnsvsphere.VirtualCenter,
dsURLs []string, dsInfos []*cnsvsphere.DatastoreInfo,
entities []vim25types.ManagedObjectReference) (map[string]*cnsvsphere.DatastoreInfo, error) {
log := logger.GetLogger(ctx)
log.Debugf("auth manager: file - dsURLs %v, dsInfos %v, vCenter %q", dsURLs, dsInfos, vc.Config.Host)
// dsURLToInfoMap will store a list of vSAN datastores with vSAN FS enabled
// for which CSI user has privilege to.
dsURLToInfoMap := make(map[string]*cnsvsphere.DatastoreInfo)
// Get authMgr.
authMgr := object.NewAuthorizationManager(vc.Client.Client)
privIds := []string{DsPriv, SysReadPriv}
userName := vc.Config.Username
// Invoke authMgr function HasUserPrivilegeOnEntities.
result, err := authMgr.HasUserPrivilegeOnEntities(ctx, entities, userName, privIds) // entities empty -> error
if err != nil {
log.Errorf("auth manager: failed to check privilege %v on entities %v for user %s and for vCenter %q",
privIds, entities, userName, vc.Config.Host)
return nil, err
}
if len(result) == 0 {
log.Infof("auth manager: HasUserPrivilegeOnEntities returned empty result when checking privileges %v "+
"on entities %v for user %s and for vCenter %q", privIds, entities, userName, vc.Config.Host)
} else {
log.Debugf("auth manager: HasUserPrivilegeOnEntities returned %v when checking privileges %v on entities %v "+
"for user %s and for vCenter %q", result, privIds, entities, userName, vc.Config.Host)
}
for index, entityPriv := range result {
hasPriv := true
privAvails := entityPriv.PrivAvailability
for _, privAvail := range privAvails {
if !privAvail.IsGranted {
// Required privilege is not grant for this entity.
hasPriv = false
break
}
}
if hasPriv {
dsURLToInfoMap[dsURLs[index]] = dsInfos[index]
log.Debugf("auth manager: datastore with URL %s and name %s has privileges and is added to dsURLToInfoMap "+
"for vCenter %q", dsInfos[index].Info.Name, dsURLs[index], vc.Config.Host)
}
}
if len(result) != 0 && len(dsURLToInfoMap) == 0 {
log.Infof("auth manager: user %s on vCenter %q doesn't have privileges for any datastore. "+
"HasUserPrivilegeOnEntities returns %v, when checking privileges %v on entities %v."+
userName, vc.Config.Host, result, privIds, entities)
}
return dsURLToInfoMap, nil
}
// Creates a map of cluster id to datastore urls. The key is cluster moid with
// vSAN FS enabled and Host.Config.Storage privilege. The value is a list of
// vSAN datastore URLs for each cluster.
func getFSEnabledClusterToDsURLsMap(ctx context.Context, vc *cnsvsphere.VirtualCenter,
datacenters []*cnsvsphere.Datacenter) (map[string][]string, error) {
log := logger.GetLogger(ctx)
log.Debugf("Computing the map for vSAN FS enabled clusters to datastore URLS for vCenter %q.",
vc.Config.Host)
// Get clusters with vSAN FS enabled and privileges.
vSANFSClustersWithPriv, err := getFSEnabledClustersWithPriv(ctx, vc, datacenters)
if err != nil {
log.Errorf("failed to get the file service enabled clusters with privileges for vCenter %q. error: %+v",
vc.Config.Host, err)
return nil, err
}
fsEnabledClusterToDsMap := make(map[string][]string)
for _, cluster := range vSANFSClustersWithPriv {
clusterMoID := cluster.Reference().Value
dsMoList, err := getDatastoreMOsFromCluster(ctx, vc, cluster)
if err != nil {
log.Errorf("failed to get datastores for cluster %q and vCenter %q. error: %+v",
clusterMoID, vc.Config.Host, err)
return nil, err
}
for _, dsMo := range dsMoList {
if dsMo.Summary.Type == VsanDatastoreType {
fsEnabledClusterToDsMap[clusterMoID] =
append(fsEnabledClusterToDsMap[clusterMoID], dsMo.Info.GetDatastoreInfo().Url)
}
}
}
return fsEnabledClusterToDsMap, nil
}
// Returns a list of clusters with Host.Config.Storage privilege and vSAN file
// services enabled.
func getFSEnabledClustersWithPriv(ctx context.Context, vc *cnsvsphere.VirtualCenter,
datacenters []*cnsvsphere.Datacenter) ([]*object.ClusterComputeResource, error) {
log := logger.GetLogger(ctx)
log.Debugf("Computing the clusters with vSAN file services enabled and Host.Config.Storage privileges "+
"for vCenter %q", vc.Config.Host)
// Get clusters from datacenters.
clusterComputeResources := []*object.ClusterComputeResource{}
for _, datacenter := range datacenters {
finder := find.NewFinder(datacenter.Datacenter.Client(), false)
finder.SetDatacenter(datacenter.Datacenter)
clusterComputeResource, err := finder.ClusterComputeResourceList(ctx, "*")
if err != nil {
if _, ok := err.(*find.NotFoundError); ok {
log.Errorf("No clusterComputeResource found in datacenter: %+v and vCenter: %q. error: %+v",
datacenter, vc.Config.Host, err)
continue
}
log.Errorf("Error occurred while getting clusterComputeResource for vCenter %q. error: %+v",
vc.Config.Host, err)
return nil, err
}
clusterComputeResources = append(clusterComputeResources, clusterComputeResource...)
}
// Get Clusters with HostConfigStoragePriv.
authMgr := object.NewAuthorizationManager(vc.Client.Client)
privIds := []string{HostConfigStoragePriv}
userName := vc.Config.Username
var entities []vim25types.ManagedObjectReference
clusterComputeResourcesMap := make(map[string]*object.ClusterComputeResource)
for _, cluster := range clusterComputeResources {
entities = append(entities, cluster.Reference())
clusterComputeResourcesMap[cluster.Reference().Value] = cluster
}
// Invoke authMgr function HasUserPrivilegeOnEntities.
result, err := authMgr.HasUserPrivilegeOnEntities(ctx, entities, userName, privIds)
if err != nil {
log.Errorf("auth manager: failed to check privilege %v on entities %v for user %s and for vCenter %q",
privIds, entities, userName, vc.Config.Host)
return nil, err
}
if len(result) == 0 {
log.Infof("auth manager: HasUserPrivilegeOnEntities returned empty result when checking privileges %v "+
"on entities %v for user %s and for vCenter %q", privIds, entities, userName, vc.Config.Host)
} else {
log.Debugf("auth manager: HasUserPrivilegeOnEntities returned %v when checking privileges %v on entities %v "+
"for user %s and for vCenter %q", result, privIds, entities, userName, vc.Config.Host)
}
clusterComputeResourceWithPriv := []*object.ClusterComputeResource{}
for _, entityPriv := range result {
hasPriv := true
privAvails := entityPriv.PrivAvailability
for _, privAvail := range privAvails {
// Required privilege is not grant for this entity.
if !privAvail.IsGranted {
hasPriv = false
break
}
}
if hasPriv {
clusterComputeResourceWithPriv = append(clusterComputeResourceWithPriv,
clusterComputeResourcesMap[entityPriv.Entity.Value])
}
}
if len(result) != 0 && len(clusterComputeResourceWithPriv) == 0 {
log.Infof("auth manager: user %s on vCenter %q doesn't have privileges for any ClusterComputeResource. "+
"HasUserPrivilegeOnEntities returns %v, when checking privileges %v on entities %v."+
userName, vc.Config.Host, result, privIds, entities)
} else {
log.Debugf("Clusters with priv: %s and vCenter: %q are : %+v", HostConfigStoragePriv,
vc.Config.Host, clusterComputeResourceWithPriv)
}
// Get clusters which are vSAN and have vSAN FS enabled.
clusterComputeResourceWithPrivAndFS := []*object.ClusterComputeResource{}
// Add all the vsan datastores with vsan FS (from these clusters) to map.
for _, cluster := range clusterComputeResourceWithPriv {
// Get the cluster config to know if file service is enabled on it or not.
config, err := vc.VsanClient.VsanClusterGetConfig(ctx, cluster.Reference())
if err != nil {
log.Errorf("failed to get the vsan cluster config for vCenter %q. error: %+v", vc.Config.Host, err)
return nil, err
}
if !(*config.Enabled) {
log.Infof("cluster: %+v, vCenter: %q is a non-vSAN cluster. Skipping this cluster",
cluster, vc.Config.Host)
continue
} else if config.FileServiceConfig == nil {
log.Infof("VsanClusterGetConfig.FileServiceConfig is empty. Skipping this cluster: %+v with "+
"vCenter: %q and with config: %+v", cluster, vc.Config.Host, config)
continue
}
if config.FileServiceConfig.Enabled {
clusterComputeResourceWithPrivAndFS = append(clusterComputeResourceWithPrivAndFS, cluster)
log.Debugf("vSAN file service is enabled for cluster: %+v and vCenter: %q.",
cluster, vc.Config.Host)
} else {
log.Infof("vSAN file service is disabled for cluster: %+v and vCenter: %q.",
cluster, vc.Config.Host)
}
}
return clusterComputeResourceWithPrivAndFS, nil
}
// Returns datastore managed objects with info & summary properties for a given
// cluster.
func getDatastoreMOsFromCluster(ctx context.Context, vc *cnsvsphere.VirtualCenter,
cluster *object.ClusterComputeResource) ([]mo.Datastore, error) {
log := logger.GetLogger(ctx)
datastores, err := cluster.Datastores(ctx)
if err != nil {
log.Errorf("Error occurred while getting datastores from cluster %q and vCenter %q. error: %+v",
cluster.Reference().Value, vc.Config.Host, err)
return nil, err
}
// Get datastore properties.
pc := property.DefaultCollector(vc.Client.Client)
properties := []string{"info", "summary", "customValue"}
var dsList []vim25types.ManagedObjectReference
var dsMoList []mo.Datastore
for _, datastore := range datastores {
dsList = append(dsList, datastore.Reference())
}
err = pc.Retrieve(ctx, dsList, properties, &dsMoList)
if err != nil {
log.Errorf("failed to retrieve datastores. dsObjList: %+v, properties: %+v, vCenter: %q, err: %v",
dsList, properties, vc.Config.Host, err)
return nil, err
}
return dsMoList, nil
}