-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathutils.go
More file actions
578 lines (529 loc) · 21.9 KB
/
utils.go
File metadata and controls
578 lines (529 loc) · 21.9 KB
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
package vsphere
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/vmware/govmomi/cns"
cnstypes "github.com/vmware/govmomi/cns/types"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/config"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger"
)
const (
vsanDType = "vsanD"
cnsMgrDatastoreSuspended = "cns.vmware.com/datastoreSuspended"
// VSphere70u3Version is a 3 digit value to indicate the minimum vSphere
// version to use query volume async API.
VSphere70u3Version int = 703
// VSphere80u3Version is a 3 digit value to indicate the minimum vSphere
// version to ensure calling supported 8.0u3 APIs
VSphere80u3Version int = 803
// VSphere91VersionInt is a 3 digit value to indicate the minimum vSphere
// version to ensure calling supported 9.1.0 APIs
VSphere91VersionInt = 910
)
var (
// ErrNotSupported represents not supported error.
ErrNotSupported = errors.New("not supported")
)
// IsNotFoundError checks if err is the NotFound fault.
func IsNotFoundError(err error) bool {
isNotFoundError := false
if soap.IsSoapFault(err) {
_, isNotFoundError = soap.ToSoapFault(err).VimFault().(types.NotFound)
}
return isNotFoundError
}
// IsAlreadyExists checks if err is the AlreadyExists fault.
// If the error is AlreadyExists fault, the method returns true along with the
// name of the managed object. Otherwise, returns false.
func IsAlreadyExists(err error) (bool, string) {
isAlreadyExistsError := false
objectName := ""
if soap.IsSoapFault(err) {
_, isAlreadyExistsError = soap.ToSoapFault(err).VimFault().(types.AlreadyExists)
if isAlreadyExistsError {
objectName = soap.ToSoapFault(err).VimFault().(types.AlreadyExists).Name
}
}
return isAlreadyExistsError, objectName
}
// IsManagedObjectNotFound checks if err is the ManagedObjectNotFound fault.
// Returns true, if 'err' is a MnagedObjectNotFound fault for the intended
// 'moRef' object. Otherwise, return false.
func IsManagedObjectNotFound(err error, moRef types.ManagedObjectReference) bool {
if soap.IsSoapFault(err) {
fault, isNotFoundError := soap.ToSoapFault(err).VimFault().(types.ManagedObjectNotFound)
return isNotFoundError && fault.Obj.Type == moRef.Type && fault.Obj.Value == moRef.Value
}
return false
}
func IsInvalidArgumentError(err error) bool {
isInvalidArgumentError := false
if soap.IsVimFault(err) {
_, isInvalidArgumentError = soap.ToVimFault(err).(*types.InvalidArgument)
}
return isInvalidArgumentError
}
func IsVimFaultNotFoundError(err error) bool {
isNotFoundError := false
if soap.IsVimFault(err) {
_, isNotFoundError = soap.ToVimFault(err).(*types.NotFound)
}
return isNotFoundError
}
// IsCnsSnapshotCreatedFaultError checks if err is the CnsSnapshotCreatedFault fault returned by
// CNS CreateSnapshots API. This fault is returned by CNS in case snapshot creation is successful,
// but post-processing failed (like update db failed).
func IsCnsSnapshotCreatedFaultError(err error) bool {
isCnsSnapshotCreatedFaultError := false
if soap.IsVimFault(err) {
_, isCnsSnapshotCreatedFaultError = soap.ToVimFault(err).(*cnstypes.CnsSnapshotCreatedFault)
}
return isCnsSnapshotCreatedFaultError
}
// IsCnsSnapshotNotFoundError checks if err is the CnsSnapshotNotFoundFault fault returned by CNS QuerySnapshots API
func IsCnsSnapshotNotFoundError(err error) bool {
isCnsSnapshotNotFoundError := false
if soap.IsVimFault(err) {
_, isCnsSnapshotNotFoundError = soap.ToVimFault(err).(*cnstypes.CnsSnapshotNotFoundFault)
}
return isCnsSnapshotNotFoundError
}
// GetCnsKubernetesEntityMetaData creates a CnsKubernetesEntityMetadataObject
// object from given parameters.
func GetCnsKubernetesEntityMetaData(entityName string, labels map[string]string,
deleteFlag bool, entityType string, namespace string, clusterID string,
referredEntity []cnstypes.CnsKubernetesEntityReference) *cnstypes.CnsKubernetesEntityMetadata {
// Create new metadata spec.
var newLabels []types.KeyValue
for labelKey, labelVal := range labels {
newLabels = append(newLabels, types.KeyValue{
Key: labelKey,
Value: labelVal,
})
}
entityMetadata := &cnstypes.CnsKubernetesEntityMetadata{}
entityMetadata.EntityName = entityName
entityMetadata.Delete = deleteFlag
if labels != nil {
entityMetadata.Labels = newLabels
}
entityMetadata.EntityType = entityType
entityMetadata.Namespace = namespace
entityMetadata.ClusterID = clusterID
entityMetadata.ReferredEntity = referredEntity
return entityMetadata
}
// GetContainerCluster creates ContainerCluster object from given parameters.
func GetContainerCluster(clusterid string, username string, clusterflavor cnstypes.CnsClusterFlavor,
clusterdistribution string) cnstypes.CnsContainerCluster {
return cnstypes.CnsContainerCluster{
ClusterType: string(cnstypes.CnsClusterTypeKubernetes),
ClusterId: clusterid,
VSphereUser: username,
ClusterFlavor: string(clusterflavor),
ClusterDistribution: clusterdistribution,
}
}
// CreateCnsKuberenetesEntityReference returns an EntityReference object to
// which the given entity refers to.
func CreateCnsKuberenetesEntityReference(entityType string, entityName string,
namespace string, clusterid string) cnstypes.CnsKubernetesEntityReference {
return cnstypes.CnsKubernetesEntityReference{
EntityType: entityType,
EntityName: entityName,
Namespace: namespace,
ClusterID: clusterid,
}
}
// GetVirtualCenterConfig returns VirtualCenterConfig Object created using
// vSphere Configuration specified in the argument.
func GetVirtualCenterConfig(ctx context.Context, cfg *config.Config) (*VirtualCenterConfig, error) {
log := logger.GetLogger(ctx)
var err error
vCenterIPs, err := GetVcenterIPs(cfg) // make([]string, 0)
if err != nil {
return nil, err
}
host := vCenterIPs[0]
port, err := strconv.Atoi(cfg.VirtualCenter[host].VCenterPort)
if err != nil {
return nil, err
}
var targetvSANClustersForFile []string
if strings.TrimSpace(cfg.VirtualCenter[host].TargetvSANFileShareClusters) != "" {
targetvSANClustersForFile = strings.Split(cfg.VirtualCenter[host].TargetvSANFileShareClusters, ",")
}
vcCAFile := cfg.Global.CAFile
vcThumbprint := cfg.Global.Thumbprint
vcConfig := &VirtualCenterConfig{
Host: host,
Port: port,
CAFile: vcCAFile,
Thumbprint: vcThumbprint,
Username: cfg.VirtualCenter[host].User,
Password: cfg.VirtualCenter[host].Password,
Insecure: cfg.VirtualCenter[host].InsecureFlag,
TargetvSANFileShareClusters: targetvSANClustersForFile,
QueryLimit: cfg.Global.QueryLimit,
ListVolumeThreshold: cfg.Global.ListVolumeThreshold,
MigrationDataStoreURL: cfg.VirtualCenter[host].MigrationDataStoreURL,
FileVolumeActivated: cfg.VirtualCenter[host].FileVolumeActivated,
VCSessionManagerURL: cfg.VirtualCenter[host].VCSessionManagerURL,
VCSessionManagerToken: cfg.VirtualCenter[host].VCSessionManagerToken,
}
if vcConfig.VCSessionManagerURL != "" {
log.Infof("Using Shared Session Manager: %s", vcConfig.VCSessionManagerURL)
}
log.Debugf("Setting the queryLimit = %v, ListVolumeThreshold = %v", vcConfig.QueryLimit, vcConfig.ListVolumeThreshold)
if strings.TrimSpace(cfg.VirtualCenter[host].Datacenters) != "" {
vcConfig.DatacenterPaths = strings.Split(cfg.VirtualCenter[host].Datacenters, ",")
for idx := range vcConfig.DatacenterPaths {
vcConfig.DatacenterPaths[idx] = strings.TrimSpace(vcConfig.DatacenterPaths[idx])
}
}
return vcConfig, nil
}
// GetVirtualCenterConfigs returns VirtualCenterConfig Objects created using
// vSphere Configuration specified in the argument.
func GetVirtualCenterConfigs(ctx context.Context, cfg *config.Config) ([]*VirtualCenterConfig, error) {
log := logger.GetLogger(ctx)
var err error
VirtualCenterConfigs := make([]*VirtualCenterConfig, 0)
vCenterIPs, err := GetVcenterIPs(cfg)
if err != nil {
return nil, err
}
for _, vCenterIP := range vCenterIPs {
port, err := strconv.Atoi(cfg.VirtualCenter[vCenterIP].VCenterPort)
if err != nil {
return nil, err
}
var targetvSANClustersForFile []string
if strings.TrimSpace(cfg.VirtualCenter[vCenterIP].TargetvSANFileShareClusters) != "" {
targetvSANClustersForFile = strings.Split(cfg.VirtualCenter[vCenterIP].TargetvSANFileShareClusters, ",")
}
vcConfig := &VirtualCenterConfig{
Host: vCenterIP,
Port: port,
CAFile: cfg.VirtualCenter[vCenterIP].CAFile,
Thumbprint: cfg.VirtualCenter[vCenterIP].Thumbprint,
Username: cfg.VirtualCenter[vCenterIP].User,
Password: cfg.VirtualCenter[vCenterIP].Password,
Insecure: cfg.VirtualCenter[vCenterIP].InsecureFlag,
TargetvSANFileShareClusters: targetvSANClustersForFile,
QueryLimit: cfg.Global.QueryLimit,
ListVolumeThreshold: cfg.Global.ListVolumeThreshold,
FileVolumeActivated: cfg.VirtualCenter[vCenterIP].FileVolumeActivated,
VCSessionManagerURL: cfg.VirtualCenter[vCenterIP].VCSessionManagerURL,
VCSessionManagerToken: cfg.VirtualCenter[vCenterIP].VCSessionManagerToken,
}
if vcConfig.CAFile == "" {
vcConfig.CAFile = cfg.Global.CAFile
}
if vcConfig.Thumbprint == "" {
vcConfig.Thumbprint = cfg.Global.Thumbprint
}
log.Debugf("Setting the queryLimit = %v, ListVolumeThreshold = %v", vcConfig.QueryLimit, vcConfig.ListVolumeThreshold)
if strings.TrimSpace(cfg.VirtualCenter[vCenterIP].Datacenters) != "" {
vcConfig.DatacenterPaths = strings.Split(cfg.VirtualCenter[vCenterIP].Datacenters, ",")
for idx := range vcConfig.DatacenterPaths {
vcConfig.DatacenterPaths[idx] = strings.TrimSpace(vcConfig.DatacenterPaths[idx])
}
}
VirtualCenterConfigs = append(VirtualCenterConfigs, vcConfig)
}
return VirtualCenterConfigs, nil
}
// GetVcenterIPs returns list of vCenter IPs from VSphereConfig.
func GetVcenterIPs(cfg *config.Config) ([]string, error) {
var err error
vCenterIPs := make([]string, 0)
for key := range cfg.VirtualCenter {
vCenterIPs = append(vCenterIPs, key)
}
if len(vCenterIPs) == 0 {
err = errors.New("unable get vCenter Hosts from VSphereConfig")
}
return vCenterIPs, err
}
// GetLabelsMapFromKeyValue creates a map object from given parameter.
func GetLabelsMapFromKeyValue(labels []types.KeyValue) map[string]string {
labelsMap := make(map[string]string)
for _, label := range labels {
labelsMap[label.Key] = label.Value
}
return labelsMap
}
// CompareKubernetesMetadata compares the whole CnsKubernetesEntityMetadata
// from two given parameters.
func CompareKubernetesMetadata(ctx context.Context, k8sMetaData *cnstypes.CnsKubernetesEntityMetadata,
cnsMetaData *cnstypes.CnsKubernetesEntityMetadata) bool {
log := logger.GetLogger(ctx)
log.Debugf("CompareKubernetesMetadata called with k8spvMetaData: %+v\n and cnsMetaData: %+v\n",
spew.Sdump(k8sMetaData), spew.Sdump(cnsMetaData))
if (k8sMetaData.EntityName != cnsMetaData.EntityName) || (k8sMetaData.Delete != cnsMetaData.Delete) ||
(k8sMetaData.Namespace != cnsMetaData.Namespace) {
return false
}
labelsMatch := reflect.DeepEqual(GetLabelsMapFromKeyValue(k8sMetaData.Labels),
GetLabelsMapFromKeyValue(cnsMetaData.Labels))
log.Debugf("CompareKubernetesMetadata - labelsMatch returned: %v for k8spvMetaData: %+v\n and cnsMetaData: %+v\n",
labelsMatch, spew.Sdump(GetLabelsMapFromKeyValue(k8sMetaData.Labels)),
spew.Sdump(GetLabelsMapFromKeyValue(cnsMetaData.Labels)))
return labelsMatch
}
// GetCandidateDatastoresInClusters gets the shared datastores and vSAN-direct
// managed datastores of given VC clusters from GetCandidateDatastoresInCluster and
// returns a map of clusterID -> array of datastores
func GetCandidateDatastoresInClusters(ctx context.Context, vc *VirtualCenter, clusterIDs []string,
includevSANDirectDatastores bool) map[string][]*DatastoreInfo {
log := logger.GetLogger(ctx)
clusterIDToDSs := make(map[string][]*DatastoreInfo)
for _, clusterID := range clusterIDs {
sharedDSs, vsanDirectDSs, err := GetCandidateDatastoresInCluster(ctx, vc, clusterID, includevSANDirectDatastores)
if err != nil {
log.Warnf("Getting datastores for the cluster %s failed - err: %s", clusterID, err)
continue
}
clusterIDToDSs[clusterID] = append(sharedDSs, vsanDirectDSs...)
}
return clusterIDToDSs
}
// GetCandidateDatastoresInCluster gets the shared datastores and vSAN-direct
// managed datastores of given VC cluster.
// The 1st output parameter will be shared datastores.
// The 2nd output parameter will be vSAN-direct managed datastores.
// NOTE: The second output will be an empty list if `includevSANDirectDatastores` is set to false.
func GetCandidateDatastoresInCluster(ctx context.Context, vc *VirtualCenter, clusterID string,
includevSANDirectDatastores bool) ([]*DatastoreInfo, []*DatastoreInfo, error) {
log := logger.GetLogger(ctx)
// Find datastores shared across all hosts in given cluster.
hosts, err := vc.GetHostsByCluster(ctx, clusterID)
if err != nil {
return nil, nil, fmt.Errorf("failed to get hosts from VC. Err: %+v", err)
}
if len(hosts) == 0 {
return nil, nil, fmt.Errorf("empty List of hosts returned from VC")
}
sharedDatastores := make([]*DatastoreInfo, 0)
vsanDirectDatastores := make([]*DatastoreInfo, 0)
for index, host := range hosts {
accessibleDatastores, err := host.GetAllAccessibleDatastores(ctx)
if err != nil {
return nil, nil, err
}
if index == 0 {
for _, accessibleDs := range accessibleDatastores {
var dsType string
if includevSANDirectDatastores {
_, dsType, err = accessibleDs.GetDatastoreURLAndType(ctx)
if err != nil {
return nil, nil, logger.LogNewErrorf(log,
"Unable to find datastore type and URL for %q. Error: %+v",
accessibleDs.Reference().Value, err)
}
}
if dsType == vsanDType {
vsanDirectDatastores = append(vsanDirectDatastores, accessibleDs)
} else {
sharedDatastores = append(sharedDatastores, accessibleDs)
}
}
} else {
var sharedAccessibleDatastores []*DatastoreInfo
for _, accessibleDs := range accessibleDatastores {
var dsType string
if includevSANDirectDatastores {
_, dsType, err = accessibleDs.GetDatastoreURLAndType(ctx)
if err != nil {
return nil, nil, logger.LogNewErrorf(log,
"Unable to find datastore type and URL for %q. Error: %+v",
accessibleDs.Reference().Value, err)
}
}
if dsType == vsanDType {
vsanDirectDatastores = append(vsanDirectDatastores, accessibleDs)
continue
}
// Intersect sharedDatastores with accessibleDatastores.
for _, sharedDs := range sharedDatastores {
// Intersection is performed based on the datastoreUrl as this
// uniquely identifies the datastore.
if sharedDs.Info.Url == accessibleDs.Info.Url {
sharedAccessibleDatastores = append(sharedAccessibleDatastores, sharedDs)
break
}
}
}
sharedDatastores = sharedAccessibleDatastores
}
}
if len(sharedDatastores) == 0 && len(vsanDirectDatastores) == 0 {
return nil, nil, fmt.Errorf("no candidates datastores found in the Kubernetes cluster")
}
log.Debugf("Found shared datastores: %+v and vSAN Direct datastores: %+v", sharedDatastores,
vsanDirectDatastores)
return sharedDatastores, vsanDirectDatastores, nil
}
// GetDatastoreInfoByURL returns info of a datastore found in given cluster
// whose URL matches the specified datastore URL.
// TODO: optimise this by caching the datastore URL to clusterID mapping or
// adding the clusterID as part of the CRD.
func GetDatastoreInfoByURL(ctx context.Context, vc *VirtualCenter,
clusterIDs []string, dsURL string) (*DatastoreInfo, error) {
log := logger.GetLogger(ctx)
for _, clusterID := range clusterIDs {
// Get all datastores in this cluster.
datastoreInfos, err := vc.GetDatastoresByCluster(ctx, clusterID)
if err != nil {
log.Warnf("Not able to fetch datastores in cluster %q. Err: %v", clusterID, err)
continue
}
for _, dsInfo := range datastoreInfos {
if dsInfo.Info.Url == dsURL {
return dsInfo, nil
}
}
log.Debugf("datastore corresponding to URL %s not found in cluster %s", dsURL, clusterID)
}
return nil, fmt.Errorf("datastore corresponding to URL %v not found in any cluster", dsURL)
}
// isVsan67u3Release returns true if it is vSAN 67u3 Release of vCenter.
func isVsan67u3Release(ctx context.Context, m *defaultVirtualCenterManager, host string) (bool, error) {
log := logger.GetLogger(ctx)
log.Debug("Checking if vCenter version is of vsan 67u3 release")
vc, err := m.GetVirtualCenter(ctx, host)
if err != nil || vc == nil {
log.Errorf("failed to get vcenter version. Err: %v", err)
return false, err
}
log.Debugf("vCenter version is :%q", vc.Client.Version)
return vc.Client.Version == cns.ReleaseVSAN67u3, nil
}
// IsvSphereVersion70U3orAbove checks if specified version is 7.0 Update 3 or
// higher. The method takes aboutInfo as input which contains details about
// VC version, build number and so on. If the version is 7.0 Update 3 or higher,
// returns true, else returns false along with appropriate errors for the failure.
func IsvSphereVersion70U3orAbove(ctx context.Context, aboutInfo types.AboutInfo) (bool, error) {
log := logger.GetLogger(ctx)
items := strings.Split(aboutInfo.Version, ".")
version := strings.Join(items[:], "")
// Convert version string to int: e.g. "7.0.3" to 703, "7.0.3.1" to 703.
if len(version) >= 3 {
vSphereVersionInt, err := strconv.Atoi(version[0:3])
if err != nil {
return false, logger.LogNewErrorf(log, "error while converting version %q to integer, err %+v", version, err)
}
// Check if the current vSphere version is 7.0.3 or higher.
if vSphereVersionInt >= VSphere70u3Version {
return true, nil
}
}
// For all other versions.
return false, nil
}
// IsvSphereVersion80U3orAbove checks if specified version is 8.0 Update 3 or
// higher. The method takes aboutInfo as input which contains details about
// VC version, build number and so on. If the version is 8.0 Update 3 or higher,
// returns true, else returns false along with appropriate errors for the failure.
func IsvSphereVersion80U3orAbove(ctx context.Context, aboutInfo types.AboutInfo) (bool, error) {
log := logger.GetLogger(ctx)
items := strings.Split(aboutInfo.Version, ".")
version := strings.Join(items[:], "")
// Convert version string to int: e.g. "8.0.3" to 803, "8.0.3.1" to 803.
if len(version) >= 3 {
vSphereVersionInt, err := strconv.Atoi(version[0:3])
if err != nil {
return false, logger.LogNewErrorf(log, "error while converting version %q to integer, err %+v", version, err)
}
// Check if the current vSphere version is 8.0.3 or higher.
if vSphereVersionInt >= VSphere80u3Version {
return true, nil
}
}
// For all other versions.
return false, nil
}
// IsvSphereVersion91orAbove checks if specified version is 9.1 or higher
// The method takes aboutInfo{} as input which contains details about
// VC version, build number and so on.
// If the version is 9.1 higher, the method returns true, else returns false
// along with appropriate errors during failure cases
func IsvSphereVersion91orAbove(ctx context.Context, aboutInfo types.AboutInfo) (bool, error) {
log := logger.GetLogger(ctx)
items := strings.Split(aboutInfo.Version, ".")
version := strings.Join(items[:], "")
if len(version) >= 3 {
vSphereVersionInt, err := strconv.Atoi(version[0:3])
if err != nil {
return false, logger.LogNewErrorf(log, "error while converting version %q to integer, err %+v", version, err)
}
// Check if the current vSphere version is 9.1.0 or higher
if vSphereVersionInt >= VSphere91VersionInt {
return true, nil
}
}
// For all other versions
return false, nil
}
// IsVolumeCreationSuspended checks whether a given Datastore has cns.vmware.com/datastoreSuspended customValue
func IsVolumeCreationSuspended(ctx context.Context, datastoreInfo *DatastoreInfo) bool {
log := logger.GetLogger(ctx)
for _, customValField := range datastoreInfo.CustomValues {
customVal := customValField.(*types.CustomFieldStringValue)
if customVal.Value == cnsMgrDatastoreSuspended {
log.Infof("Ignoring datastore %v as it is suspended. Datastore moref: %v", datastoreInfo.Info.Name,
datastoreInfo.Datastore.Reference())
return true
}
}
return false
}
// FilterSuspendedDatastores filters out datastores which cns.vmware.com/datastoreSuspended customValue
func FilterSuspendedDatastores(ctx context.Context, datastoreInfoList []*DatastoreInfo) ([]*DatastoreInfo, error) {
log := logger.GetLogger(ctx)
var filteredList []*DatastoreInfo
for _, ds := range datastoreInfoList {
if !IsVolumeCreationSuspended(ctx, ds) {
filteredList = append(filteredList, ds)
}
}
if len(filteredList) == 0 {
return filteredList, logger.LogNewErrorf(log,
"No datastores are available after filtering suspended datastores")
}
log.Infof("Filtered list of datastores after removing suspended ones are: %+v", filteredList)
return filteredList, nil
}
// IsInvalidLoginError checks if the error is due to invalid credentials (*types.InvalidLogin).
//
// The govmomi client returns a soap.soapFaultError containing *types.InvalidLogin as the
// underlying VimFault. We also check the error message as a fallback.
func IsInvalidLoginError(ctx context.Context, err error) bool {
if err == nil {
return false
}
// Check if it's a soap.soapFaultError containing InvalidLogin
// This is the primary check that catches the actual error from vCenter
if soap.IsSoapFault(err) {
soapFault := soap.ToSoapFault(err)
if soapFault != nil && soapFault.VimFault() != nil {
if _, ok := soapFault.VimFault().(*types.InvalidLogin); ok {
return true
}
}
}
// Fallback: check if the error message contains the InvalidLogin text
// This handles edge cases where type checking doesn't work
if strings.Contains(err.Error(), "Cannot complete login due to an incorrect user name or password") {
return true
}
return false
}