-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathcnsfileaccessconfig_controller.go
More file actions
901 lines (817 loc) · 35.7 KB
/
cnsfileaccessconfig_controller.go
File metadata and controls
901 lines (817 loc) · 35.7 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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*
Copyright 2021-2025 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 cnsfileaccessconfig
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"time"
vmoperatortypes "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
cnstypes "github.com/vmware/govmomi/cns/types"
vsanfstypes "github.com/vmware/govmomi/vsan/vsanfs/types"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
cnsoperatorapis "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator"
v1a1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator/cnsfileaccessconfig/v1alpha1"
volumes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/cns-lib/volume"
commonconfig "sigs.k8s.io/vsphere-csi-driver/v3/pkg/common/config"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common/commonco"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/logger"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/internalapis/cnsoperator/cnsfilevolumeclient"
k8s "sigs.k8s.io/vsphere-csi-driver/v3/pkg/kubernetes"
cnsoperatortypes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/types"
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/util"
)
const (
workerThreadsEnvVar = "WORKER_THREADS_CNS_FILE_ACCESS_CONFIG"
defaultMaxWorkerThreads = 10
capvVmLabelKey = "capv.vmware.com"
devopsUserLabelKey = "cns.vmware.com/user-created"
)
// backOffDuration is a map of cnsfileaccessconfig name's to the time after
// which a request for this instance will be requeued. Initialized to 1 second
// for new instances and for instances whose latest reconcile operation
// succeeded. If the reconcile fails, backoff is incremented exponentially.
var (
backOffDuration map[types.NamespacedName]time.Duration
backOffDurationMapMutex = sync.Mutex{}
volumePermissionLockMap *sync.Map
)
// Add creates a new CnsFileAccessConfig Controller and adds it to the Manager.
// The Manager will set fields on the Controller and Start it when the Manager
// is Started.
func Add(mgr manager.Manager, clusterFlavor cnstypes.CnsClusterFlavor,
configInfo *commonconfig.ConfigurationInfo, volumeManager volumes.Manager) error {
ctx, log := logger.GetNewContextWithLogger()
if clusterFlavor != cnstypes.CnsClusterFlavorWorkload {
log.Debug("Not initializing the CnsFileAccessConfig Controller as its a non-WCP CSI deployment")
return nil
}
if commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.TKGsHA) {
clusterComputeResourceMoIds, _, err := common.GetClusterComputeResourceMoIds(ctx)
if err != nil {
log.Errorf("failed to get clusterComputeResourceMoIds. err: %v", err)
return err
}
if len(clusterComputeResourceMoIds) > 1 &&
!commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.WorkloadDomainIsolation) {
log.Infof("Not initializing the CnsFileAccessConfig Controller as stretched supervisor is detected.")
return nil
}
}
volumePermissionLockMap = &sync.Map{}
// Initializes kubernetes client.
k8sclient, err := k8s.NewClient(ctx)
if err != nil {
log.Errorf("Creating Kubernetes client failed. Err: %v", err)
return err
}
// eventBroadcaster broadcasts events on cnsfileaccessconfig instances to
// the event sink.
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(
&typedcorev1.EventSinkImpl{
Interface: k8sclient.CoreV1().Events(""),
},
)
restClientConfig, err := k8s.GetKubeConfig(ctx)
if err != nil {
msg := fmt.Sprintf("Failed to initialize rest clientconfig. Error: %+v", err)
log.Error(msg)
return err
}
vmOperatorClient, err := k8s.NewClientForGroup(ctx, restClientConfig, vmoperatortypes.GroupName)
if err != nil {
msg := fmt.Sprintf("Failed to initialize vmOperatorClient. Error: %+v", err)
log.Error(msg)
return err
}
cfg, err := config.GetConfig()
if err != nil {
msg := fmt.Sprintf("Failed to get config. Err: %+v", err)
log.Error(msg)
return err
}
// create a new dynamic client for config.
dynamicClient, err := dynamic.NewForConfig(cfg)
if err != nil {
msg := fmt.Sprintf("Failed to create client using config. Err: %+v", err)
log.Error(msg)
return err
}
recorder := eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: cnsoperatorapis.GroupName})
return add(mgr, newReconciler(mgr, configInfo, volumeManager, vmOperatorClient, dynamicClient, recorder))
}
// newReconciler returns a new reconcile.Reconciler.
func newReconciler(mgr manager.Manager, configInfo *commonconfig.ConfigurationInfo,
volumeManager volumes.Manager, vmOperatorClient client.Client, dynamicClient dynamic.Interface,
recorder record.EventRecorder) reconcile.Reconciler {
return &ReconcileCnsFileAccessConfig{
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
configInfo: configInfo,
volumeManager: volumeManager,
vmOperatorClient: vmOperatorClient,
dynamicClient: dynamicClient,
recorder: recorder,
}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler.
func add(mgr manager.Manager, r reconcile.Reconciler) error {
ctx, log := logger.GetNewContextWithLogger()
maxWorkerThreads := util.GetMaxWorkerThreads(ctx,
workerThreadsEnvVar, defaultMaxWorkerThreads)
// Create a new controller.
err := ctrl.NewControllerManagedBy(mgr).Named("cnsfileaccessconfig-controller").
For(&v1a1.CnsFileAccessConfig{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
WithOptions(controller.Options{MaxConcurrentReconciles: maxWorkerThreads}).
Complete(r)
if err != nil {
log.Errorf("Failed to build application controller. Err: %v", err)
return err
}
backOffDuration = make(map[types.NamespacedName]time.Duration)
return nil
}
// Blank assignment to verify that ReconcileCnsFileAccessConfig implements
// reconcile.Reconciler.
var _ reconcile.Reconciler = &ReconcileCnsFileAccessConfig{}
// ReconcileCnsFileAccessConfig reconciles a CnsFileAccessConfig object.
type ReconcileCnsFileAccessConfig struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver.
client client.Client
scheme *runtime.Scheme
configInfo *commonconfig.ConfigurationInfo
volumeManager volumes.Manager
vmOperatorClient client.Client
dynamicClient dynamic.Interface
recorder record.EventRecorder
}
// Reconcile reads that cluster state for a CnsFileAccessConfig object and makes
// changes based on the state read and what is in the CnsFileAccessConfig.Spec.
// Note:
// The Controller will requeue the Request to be processed again if the returned
// error is non-nil or Result.Requeue is true, otherwise upon completion it will
// remove the work from the queue.
func (r *ReconcileCnsFileAccessConfig) Reconcile(ctx context.Context,
request reconcile.Request) (reconcile.Result, error) {
log := logger.GetLogger(ctx)
// Fetch the CnsFileAccessConfig instance.
instance := &v1a1.CnsFileAccessConfig{}
err := r.client.Get(ctx, request.NamespacedName, instance)
if err != nil {
if apierrors.IsNotFound(err) {
log.Infof("CnsFileAccessConfig resource not found. Ignoring since object must be deleted.")
return reconcile.Result{}, nil
}
log.Errorf("Error reading the CnsFileAccessConfig with name: %q on namespace: %q. Err: %+v",
request.Name, request.Namespace, err)
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
// Initialize backOffDuration for the instance, if required.
backOffDurationMapMutex.Lock()
var timeout time.Duration
if _, exists := backOffDuration[request.NamespacedName]; !exists {
backOffDuration[request.NamespacedName] = time.Second
}
timeout = backOffDuration[request.NamespacedName]
backOffDurationMapMutex.Unlock()
// Get the virtualmachine instance
vm, apiVersion, err := getVirtualMachine(ctx, r.vmOperatorClient, instance.Spec.VMName, instance.Namespace)
if err != nil {
msg := fmt.Sprintf("Failed to get virtualmachine instance for the VM with name: %q. Error: %+v",
instance.Spec.VMName, err)
log.Error(msg)
// If virtualmachine instance is NotFound and if deletion timestamp is set on CnsFileAccessConfig instance,
// then proceed with the deletion of CnsFileAccessConfig instance.
if apierrors.IsNotFound(err) && instance.DeletionTimestamp != nil {
log.Infof("CnsFileAccessConfig instance %q has deletion timestamp set, but VM instance with "+
"name %q is not found. Processing the deletion of CnsFileAccessConfig instance.",
instance.Name, instance.Spec.VMName)
// Fetch the PVC and PV instance and get volume ID
skipConfigureVolumeACL := false
volumeID, err := util.GetVolumeID(ctx, r.client, instance.Spec.PvcName, instance.Namespace)
if err != nil {
if apierrors.IsNotFound(err) {
// If PVC instance is NotFound (deleted), then there is no need to configure ACL on file volume.
// TODO: When we support PV with retain policy on supervisor, then we need to configure ACL
// in cases where PVC is deleted but PV is not deleted.
skipConfigureVolumeACL = true
} else {
msg := fmt.Sprintf("Failed to get volumeID from pvcName: %q. Error: %+v", instance.Spec.PvcName, err)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
}
err = r.removePermissionsForFileVolume(ctx, volumeID, instance, skipConfigureVolumeACL)
if err != nil {
msg := fmt.Sprintf("Failed to remove file volume permissions with error: %+v", err)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
// Remove PVC protection finalizer from PVC
err = removeFinalizerFromPVC(ctx, r.client, instance)
if err != nil {
msg := fmt.Sprintf("failed to remove finalizer from PVC CnsFileAccessConfig "+
"instance: %q on namespace: %q. Error: %+v",
instance.Name, instance.Namespace, err)
recordEvent(ctx, r, instance, v1.EventTypeWarning, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
// Remove finalizer from CnsFileAccessConfig CRD
err = k8s.RemoveFinalizer(ctx, r.client, instance, cnsoperatortypes.CNSFinalizer)
if err != nil {
msg := fmt.Sprintf("failed to remove finalizer from CnsFileAccessConfig "+
"instance: %q on namespace: %q. Error: %+v", instance.Name, instance.Namespace, err)
recordEvent(ctx, r, instance, v1.EventTypeWarning, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
// Cleanup instance entry from backOffDuration map.
backOffDurationMapMutex.Lock()
delete(backOffDuration, request.NamespacedName)
backOffDurationMapMutex.Unlock()
return reconcile.Result{}, nil
}
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
log.Debugf("Found virtualMachine instance for VM: %q/%q: %+v", instance.Namespace, instance.Spec.VMName, vm)
ifFileVolumesWithVmserviceVmsSupported := commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx,
common.FileVolumesWithVmService)
if ifFileVolumesWithVmserviceVmsSupported {
err = validateVmAndPvc(ctx, instance.Labels, instance.Name, instance.Spec.PvcName,
instance.Namespace, r.client, vm)
if err != nil {
log.Errorf("failed to validate VM %s and PVC %s", vm.Name, instance.Spec.PvcName)
setInstanceError(ctx, r, instance, err.Error())
return reconcile.Result{RequeueAfter: timeout}, nil
}
}
if instance.DeletionTimestamp != nil {
log.Infof("CnsFileAccessConfig instance %q has deletion timestamp set", instance.Name)
volumeExists := true
volumeID, err := util.GetVolumeID(ctx, r.client, instance.Spec.PvcName, instance.Namespace)
if err != nil {
if ifFileVolumesWithVmserviceVmsSupported &&
apierrors.IsNotFound(err) {
log.Infof("Volume ID for PVC %s in namespace %s not found. No need to configure ACL",
instance.Spec.PvcName, instance.Namespace)
// It is possible that finalizer from PVC was removed successfully but
// finalizer from CnsFileAcessConfig CR was not removed.
// If volume does not exist in such a case, reconciler should go ahead and remove
// finalizer from the CR instance also instead of erroing out.
volumeExists = false
} else {
msg := fmt.Sprintf("Failed to get volumeID from pvcName: %q. Error: %+v", instance.Spec.PvcName, err)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
}
if volumeExists {
err = r.configureNetPermissionsForFileVolume(ctx, volumeID, vm, instance, true)
if err != nil {
msg := fmt.Sprintf("Failed to configure CnsFileAccessConfig instance with error: %+v", err)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
// Remove PVC protection finalizer from PVC
err = removeFinalizerFromPVC(ctx, r.client, instance)
if err != nil {
msg := fmt.Sprintf("failed to remove finalizer from PVC CnsFileAccessConfig "+
"instance: %q on namespace: %q. Error: %+v",
instance.Name, instance.Namespace, err)
recordEvent(ctx, r, instance, v1.EventTypeWarning, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
}
err = k8s.RemoveFinalizer(ctx, r.client, instance, cnsoperatortypes.CNSFinalizer)
if err != nil {
msg := fmt.Sprintf("failed to update CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v",
instance.Name, instance.Namespace, err)
recordEvent(ctx, r, instance, v1.EventTypeWarning, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
// Cleanup instance entry from backOffDuration map.
backOffDurationMapMutex.Lock()
delete(backOffDuration, request.NamespacedName)
backOffDurationMapMutex.Unlock()
return reconcile.Result{}, nil
}
// If the CnsFileAccessConfig instance is already successful,
// and not deleted by the user, remove the instance from the queue.
if instance.Status.Done {
// Cleanup instance entry from backOffDuration map.
log.Infof("CnsFileAccessConfig instance: %q on namespace: %q has status marked as done. Skipping reconcile.",
instance.Name, instance.Namespace)
backOffDurationMapMutex.Lock()
delete(backOffDuration, request.NamespacedName)
backOffDurationMapMutex.Unlock()
return reconcile.Result{}, nil
}
// Add finalizer to CnsFileAccessConfig instance if it does not already exist.
err = k8s.AddFinalizer(ctx, r.client, instance, cnsoperatortypes.CNSFinalizer)
if err != nil {
msg := fmt.Sprintf("failed to add finalizer on CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v",
instance.Name, instance.Namespace, err)
recordEvent(ctx, r, instance, v1.EventTypeWarning, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
vmOwnerRefExists := false
if len(instance.OwnerReferences) != 0 {
for _, ownerRef := range instance.OwnerReferences {
if ownerRef.Kind == reflect.TypeOf(vmoperatortypes.VirtualMachine{}).Name() &&
ownerRef.Name == instance.Spec.VMName && ownerRef.UID == vm.UID {
vmOwnerRefExists = true
break
}
}
}
if !vmOwnerRefExists {
// Set ownerRef on CnsFileAccessConfig instance (in-memory) to VM instance.
setInstanceOwnerRef(instance, instance.Spec.VMName, vm.UID, apiVersion)
err = updateCnsFileAccessConfig(ctx, r.client, instance)
if err != nil {
msg := fmt.Sprintf("failed to update CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v",
instance.Name, instance.Namespace, err)
recordEvent(ctx, r, instance, v1.EventTypeWarning, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
}
// Add CNS pvc protection finalizer if it does not already exist.
err = addPvcFinalizer(ctx, instance, r.client)
if err != nil {
msg := fmt.Sprintf("failed to add finalizer to PVC %s for instance: %s on namespace: %q. Error: %+v",
instance.Spec.PvcName, instance.Name, instance.Namespace, err)
recordEvent(ctx, r, instance, v1.EventTypeWarning, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
log.Infof("Reconciling CnsFileAccessConfig with instance: %q from namespace: %q. timeout %q seconds",
instance.Name, instance.Namespace, timeout)
if !instance.Status.Done {
volumeID, err := util.GetVolumeID(ctx, r.client, instance.Spec.PvcName, instance.Namespace)
if err != nil {
msg := fmt.Sprintf("Failed to get volumeID from pvcName: %q. Error: %+v", instance.Spec.PvcName, err)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
// Query volume.
log.Debugf("Querying volume: %s for CnsFileAccessConfig request with name: %q on namespace: %q",
volumeID, instance.Name, instance.Namespace)
querySelection := cnstypes.CnsQuerySelection{
Names: []string{
string(cnstypes.QuerySelectionNameTypeVolumeType),
string(cnstypes.QuerySelectionNameTypeBackingObjectDetails),
},
}
volume, err := common.QueryVolumeByID(ctx, r.volumeManager, volumeID, &querySelection)
if err != nil {
if err.Error() == common.ErrNotFound.Error() {
msg := fmt.Sprintf("CNS Volume: %s not found", volumeID)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
msg := fmt.Sprintf("Failed to query CNS volume: %s with error: %+v", volumeID, err)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
if volume.VolumeType != string(cnstypes.CnsVolumeTypeFile) {
msg := fmt.Sprintf("CNS Volume: %s is not RWX volume", volumeID)
err = logger.LogNewError(log, msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, reconcile.TerminalError(err)
}
vSANFileBackingDetails := volume.BackingObjectDetails.(*cnstypes.CnsVsanFileShareBackingDetails)
accessPoints := make(map[string]string)
for _, kv := range vSANFileBackingDetails.AccessPoints {
accessPoints[kv.Key] = kv.Value
}
if len(accessPoints) == 0 {
msg := fmt.Sprintf("No access points found for volume: %q", volumeID)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
err = r.configureNetPermissionsForFileVolume(ctx, volumeID, vm, instance, false)
if err != nil {
msg := fmt.Sprintf("Failed to configure CnsFileAccessConfig instance with error: %+v", err)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
// Update the instance to indicate the volume registration is successful.
msg := fmt.Sprintf("Successfully configured access points of VM: %q on the volume: %q",
instance.Spec.VMName, instance.Spec.PvcName)
instance.Status.AccessPoints = accessPoints
err = setInstanceSuccess(ctx, r, instance, msg)
if err != nil {
msg := fmt.Sprintf("Failed to update CnsFileAccessConfig instance with error: %+v", err)
log.Error(msg)
setInstanceError(ctx, r, instance, msg)
return reconcile.Result{RequeueAfter: timeout}, nil
}
log.Info(msg)
}
backOffDurationMapMutex.Lock()
delete(backOffDuration, request.NamespacedName)
backOffDurationMapMutex.Unlock()
return reconcile.Result{}, nil
}
// addPvcFinalizer checks if CnsPvcFinalizer exists on PVC.
// If it does not exist, it updates the PVC with it.
func addPvcFinalizer(ctx context.Context,
instance *v1a1.CnsFileAccessConfig, client client.Client) error {
log := logger.GetLogger(ctx)
if !commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.FileVolumesWithVmService) {
return nil
}
pvc := &v1.PersistentVolumeClaim{}
err := client.Get(ctx, types.NamespacedName{Name: instance.Spec.PvcName, Namespace: instance.Namespace}, pvc)
if err != nil {
return err
}
if controllerutil.ContainsFinalizer(pvc, cnsoperatortypes.CNSPvcFinalizer) {
// Finalizer already present on PVC
return nil
}
original := pvc.DeepCopy()
if !controllerutil.AddFinalizer(pvc, cnsoperatortypes.CNSPvcFinalizer) {
return fmt.Errorf("failed to add CNS finalizer to PVC %s in namespace %s", pvc.Name,
pvc.Namespace)
}
err = k8s.PatchObject(ctx, client, original, pvc)
if err != nil {
return fmt.Errorf("failed to add finalizer %s on PVC %s in namespace %s", cnsoperatortypes.CNSPvcFinalizer,
instance.Spec.PvcName, instance.Namespace)
}
log.Infof("Successfully added finalizer %s to PVC %s in namespace %s", cnsoperatortypes.CNSPvcFinalizer,
pvc.Name, pvc.Namespace)
return nil
}
// isPvcInUse returns true if there is at least 1 VM which is using the given PVC.
func isPvcInUse(ctx context.Context, pvcName string,
instance *v1a1.CnsFileAccessConfig) (bool, error) {
log := logger.GetLogger(ctx)
cnsFileVolumeClientInstance, err := cnsfilevolumeclient.GetFileVolumeClientInstance(ctx)
if err != nil {
return true, logger.LogNewErrorf(log, "Failed to get CNSFileVolumeClient instance. Error: %+v", err)
}
// Find out of CnsFileVolumeClient instance exists
return cnsFileVolumeClientInstance.CnsFileVolumeClientExistsForPvc(ctx, instance.Namespace+"/"+instance.Spec.PvcName)
}
// removeFinalizerFromPVC will remove the CNS Finalizer, cns.vmware.com/pvc-protection,
// from a given PersistentVolumeClaim.
func removeFinalizerFromPVC(ctx context.Context, client client.Client,
instance *v1a1.CnsFileAccessConfig) error {
log := logger.GetLogger(ctx)
if !commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.FileVolumesWithVmService) {
return nil
}
pvcName := instance.Spec.PvcName
namespace := instance.Namespace
isPvcInUse, err := isPvcInUse(ctx, pvcName, instance)
if err != nil {
log.Errorf("failed to find if PVC %s is being used any VM or not. Err: %+v", pvcName, err)
return err
}
if isPvcInUse {
log.Infof("Cannot remove finalizer from PVC %s as it is in use by at least 1 VM.", pvcName)
return nil
}
pvc := &v1.PersistentVolumeClaim{}
err = client.Get(ctx, types.NamespacedName{Name: pvcName, Namespace: namespace}, pvc)
if err != nil {
if apierrors.IsNotFound(err) {
log.Debugf("PVC %s not found", pvcName)
return nil
}
return err
}
if !controllerutil.ContainsFinalizer(pvc, cnsoperatortypes.CNSPvcFinalizer) {
log.Debugf("Finalizer: %q not found on PersistentVolumeClaim: %q on namespace: %q not found. Returning nil",
cnsoperatortypes.CNSPvcFinalizer, pvcName, namespace)
return nil
}
original := pvc.DeepCopy()
if !controllerutil.RemoveFinalizer(pvc, cnsoperatortypes.CNSPvcFinalizer) {
err := fmt.Errorf("failed to remove finalizer %s from PVC %s in namespace %s",
cnsoperatortypes.CNSPvcFinalizer, pvcName, pvc.Namespace)
log.Errorf("failed to remove finalizer from PVC. Err: %+v", err)
return err
}
err = k8s.PatchObject(ctx, client, original, pvc)
if err != nil {
return fmt.Errorf("failed to remove finalizer %s from PVC %s in namespace %s",
cnsoperatortypes.CNSPvcFinalizer, pvcName, pvc.Namespace)
}
log.Infof("Successfully removed finalizer %s from PVC %s in namespace %s", cnsoperatortypes.CNSPvcFinalizer,
pvcName, namespace)
return nil
}
// removePermissionsForFileVolume helps to remove net permissions for a given file volume.
// This method is used when we don't have VM instance. It fetches the VM IP from CNSFileVolumeClient
// instance for the VM name associated with CnsFileAccessConfig.
func (r *ReconcileCnsFileAccessConfig) removePermissionsForFileVolume(ctx context.Context, volumeID string,
instance *v1a1.CnsFileAccessConfig, skipConfigureVolumeACL bool) error {
log := logger.GetLogger(ctx)
volumePermissionLock, _ := volumePermissionLockMap.LoadOrStore(volumeID, &sync.Mutex{})
instanceLock, _ := volumePermissionLock.(*sync.Mutex)
instanceLock.Lock()
defer instanceLock.Unlock()
cnsFileVolumeClientInstance, err := cnsfilevolumeclient.GetFileVolumeClientInstance(ctx)
if err != nil {
return logger.LogNewErrorf(log, "Failed to get CNSFileVolumeClient instance. Error: %+v", err)
}
vmIP, vmsAssociatedWithIP, err := cnsFileVolumeClientInstance.GetVMIPFromVMName(ctx,
instance.Namespace+"/"+instance.Spec.PvcName, instance.Spec.VMName)
if err != nil {
return logger.LogNewErrorf(log, "Failed to get VM IP from VM name in CNSFileVolumeClient instance. "+
"Error: %+v", err)
}
// If PVC is not found, then skipConfigureVolumeACL will be true.
// There is no need to configure volume ACL on volume if PVC is already deleted.
if !skipConfigureVolumeACL && (vmsAssociatedWithIP == 1) {
// In case of VDS setup, we will always have 1 VM associated with IP. But, in case of
// SNAT setup, we may have multiple VMs associated with same IP. We will remove ACL
// permission only when it is the last VM associated with IP.
err = r.configureVolumeACLs(ctx, volumeID, vmIP, true)
if err != nil {
return logger.LogNewErrorf(log, "Failed to remove net permissions for file volume %q. Error: %+v",
volumeID, err)
}
}
err = cnsFileVolumeClientInstance.RemoveClientVMFromIPList(ctx,
instance.Namespace+"/"+instance.Spec.PvcName, instance.Spec.VMName, vmIP)
if err != nil {
return logger.LogNewErrorf(log, "Failed to remove VM %q with IP %q from IPList. Error: %+v",
instance.Spec.VMName, vmIP, err)
}
log.Infof("Successfully removed VM IP %q from IPList for CnsFileAccessConfig request with name: %q on "+
"namespace: %q", vmIP, instance.Name, instance.Namespace)
return nil
}
// configureNetPermissionsForFileVolume helps to add or remove net permissions
// for a given file volume. The callers of this method can remove or add net
// permissions by setting the parameter removePermission to true or false
// respectively. Returns error if any operation fails.
func (r *ReconcileCnsFileAccessConfig) configureNetPermissionsForFileVolume(ctx context.Context,
volumeID string, vm *vmoperatortypes.VirtualMachine, instance *v1a1.CnsFileAccessConfig,
removePermission bool) error {
log := logger.GetLogger(ctx)
volumePermissionLock, _ := volumePermissionLockMap.LoadOrStore(volumeID, &sync.Mutex{})
instanceLock, _ := volumePermissionLock.(*sync.Mutex)
instanceLock.Lock()
defer instanceLock.Unlock()
tkgVMIP, err := r.getVMExternalIP(ctx, vm)
if err != nil {
return logger.LogNewErrorf(log, "Failed to get external facing IP address for VM: %s/%s instance. Error: %+v",
vm.Namespace, vm.Name, err)
}
cnsFileVolumeClientInstance, err := cnsfilevolumeclient.GetFileVolumeClientInstance(ctx)
if err != nil {
return logger.LogNewErrorf(log, "Failed to get CNSFileVolumeClient instance. Error: %+v", err)
}
clientVms, err := cnsFileVolumeClientInstance.GetClientVMsFromIPList(ctx,
instance.Namespace+"/"+instance.Spec.PvcName, tkgVMIP)
if err != nil {
return logger.LogNewErrorf(log, "Failed to get the list of clients VMs for IP %q. Error: %+v", tkgVMIP, err)
}
log.Infof("CNSFileVolumeClient for PVC %s/%s has the following ClientVMs registered: %v for IP: %q",
instance.Namespace, instance.Spec.PvcName, clientVms, tkgVMIP)
if !removePermission {
if len(clientVms) == 0 {
err = r.configureVolumeACLs(ctx, volumeID, tkgVMIP, false)
if err != nil {
return logger.LogNewErrorf(log, "Failed to add net permissions for file volume %q. Error: %+v",
volumeID, err)
}
}
err = cnsFileVolumeClientInstance.AddClientVMToIPList(ctx,
instance.Namespace+"/"+instance.Spec.PvcName, instance.Spec.VMName, tkgVMIP)
if err != nil {
return logger.LogNewErrorf(log, "Failed to add VM %q with IP %q to IPList. Error: %+v",
vm.Name, tkgVMIP, err)
}
log.Infof("Successfully added VM IP %q to IPList for CnsFileAccessConfig request with name: %q on namespace: %q",
tkgVMIP, instance.Name, instance.Namespace)
return nil
}
// RemovePermission is set to true.
if len(clientVms) == 1 && clientVms[0] == vm.Name {
err = r.configureVolumeACLs(ctx, volumeID, tkgVMIP, true)
if err != nil {
return logger.LogNewErrorf(log, "Failed to remove net permissions for file volume %q. Error: %+v",
volumeID, err)
}
}
err = cnsFileVolumeClientInstance.RemoveClientVMFromIPList(ctx,
instance.Namespace+"/"+instance.Spec.PvcName, instance.Spec.VMName, tkgVMIP)
if err != nil {
return logger.LogNewErrorf(log, "Failed to remove VM %q with IP %q to IPList. Error: %+v", vm.Name, tkgVMIP, err)
}
log.Infof("Successfully removed VM IP %q from IPList for CnsFileAccessConfig request with name: %q on namespace: %q",
tkgVMIP, instance.Name, instance.Namespace)
return nil
}
// configureVolumeACLs helps to prepare the CnsVolumeACLConfigureSpec
// for a given TKG VM IP address and volumeID and invoke CNS API.
func (r *ReconcileCnsFileAccessConfig) configureVolumeACLs(ctx context.Context,
volumeID string, tkgVMIP string, delete bool) error {
log := logger.GetLogger(ctx)
cnsVolumeID := cnstypes.CnsVolumeId{
Id: volumeID,
}
vSanFileShareNetPermissions := make([]vsanfstypes.VsanFileShareNetPermission, 0)
vsanFileShareAccessType := vsanfstypes.VsanFileShareAccessTypeREAD_WRITE
vSanFileShareNetPermissions = append(vSanFileShareNetPermissions, vsanfstypes.VsanFileShareNetPermission{
Ips: tkgVMIP,
Permissions: vsanFileShareAccessType,
AllowRoot: true,
})
cnsNFSAccessControlSpecList := make([]cnstypes.CnsNFSAccessControlSpec, 0)
cnsNFSAccessControlSpecList = append(cnsNFSAccessControlSpecList, cnstypes.CnsNFSAccessControlSpec{
Permission: vSanFileShareNetPermissions,
Delete: delete,
})
cnsVolumeACLConfigSpec := cnstypes.CnsVolumeACLConfigureSpec{
VolumeId: cnsVolumeID,
AccessControlSpecList: cnsNFSAccessControlSpecList,
}
log.Debugf("CnsVolumeACLConfigSpec : %v", cnsVolumeACLConfigSpec)
err := r.volumeManager.ConfigureVolumeACLs(ctx, cnsVolumeACLConfigSpec)
if err != nil {
return logger.LogNewErrorf(log, "Failed to configure ACLs for volume: %q. Error: %+v", volumeID, err)
}
log.Infof("Successfully configured ACLs for volume %q", volumeID)
return nil
}
// getVMExternalIP helps to fetch the external facing IP for a given TKG VM.
func (r *ReconcileCnsFileAccessConfig) getVMExternalIP(ctx context.Context,
vm *vmoperatortypes.VirtualMachine) (string, error) {
log := logger.GetLogger(ctx)
networkProvider, err := util.GetNetworkProvider(ctx)
if err != nil {
return "", logger.LogNewErrorf(log, "Failed to identify the network provider. Error: %+v", err)
}
if networkProvider == "" {
return "", logger.LogNewError(log, "unable to find network provider information")
}
networkTypes := []string{util.NSXTNetworkProvider, util.
VDSNetworkProvider}
if commonco.ContainerOrchestratorUtility.IsFSSEnabled(ctx, common.VPCCapabilitySupervisor) {
networkTypes = append(networkTypes, util.VPCNetworkProvider)
}
supported_found := false
for _, networkType := range networkTypes {
if networkType == networkProvider {
supported_found = true
break
}
}
if !supported_found {
return "", logger.LogNewErrorf(log, "Unknown network provider. Error: %+v", err)
}
tkgVMIP, err := util.GetTKGVMIP(ctx, r.vmOperatorClient,
r.dynamicClient, vm.Namespace, vm.Name, networkProvider)
if err != nil {
return "", logger.LogNewErrorf(log, "Failed to get external facing IP address for VM %q/%q. Err: %+v",
vm.Namespace, vm.Name, err)
}
log.Infof("Found tkg VMIP %q for VM %q in namespace %q", tkgVMIP, vm.Name, vm.Namespace)
return tkgVMIP, nil
}
// validateVmAndPvc validates if the VM and PVC combination given in the instance is correct or not.
// CnsFileAccessConfig CRs created by devpos users will have "cns.vmware.com/user-created" label.
// When this labels are present, the VM must not belong to TKG cluster.
// This is verified by ensuring that:
// The VM does not have a label applied by CAPV - example capv.vmware.com/cluster.name.
func validateVmAndPvc(ctx context.Context, instanceLabels map[string]string, instanceName string, pvcName string,
namespace string, client client.Client, vm *vmoperatortypes.VirtualMachine) error {
log := logger.GetLogger(ctx)
if instanceLabels == nil {
log.Infof("No labels found on the instance %s. Nothing to validate.", instanceName)
return nil
}
isUserCreatedCnsFileAccessConfig := false
for key := range instanceLabels {
if key == devopsUserLabelKey {
isUserCreatedCnsFileAccessConfig = true
break
}
}
if !isUserCreatedCnsFileAccessConfig {
log.Infof("Instance %s is not created by devops user. Nothing to validate", instanceName)
return nil
}
for key := range vm.Labels {
if strings.Contains(key, capvVmLabelKey) {
msg := fmt.Sprintf("CnsFileAccessConfig is created by devops user and has TKG VM %s. "+
"Invalid combination.", vm.Name)
log.Errorf(msg)
err := errors.New(msg)
return err
}
}
log.Infof("Successfully verified instance %s for VM/PVC combination", instanceName)
return nil
}
// setInstanceSuccess sets instance to success and records an event on the
// CnsFileAccessConfig instance.
func setInstanceSuccess(ctx context.Context, r *ReconcileCnsFileAccessConfig,
instance *v1a1.CnsFileAccessConfig, msg string) error {
instance.Status.Done = true
instance.Status.Error = ""
err := k8s.UpdateStatus(ctx, r.client, instance)
if err != nil {
return err
}
recordEvent(ctx, r, instance, v1.EventTypeNormal, msg)
return nil
}
// setInstanceError sets error and records an event on the CnsFileAccessConfig
// instance.
func setInstanceError(ctx context.Context, r *ReconcileCnsFileAccessConfig,
instance *v1a1.CnsFileAccessConfig, errMsg string) {
log := logger.GetLogger(ctx)
instance.Status.Error = errMsg
err := k8s.UpdateStatus(ctx, r.client, instance)
if err != nil {
log.Errorf("updateCnsFileAccessConfig failed. err: %v", err)
}
recordEvent(ctx, r, instance, v1.EventTypeWarning, errMsg)
}
func updateCnsFileAccessConfig(ctx context.Context, client client.Client,
instance *v1a1.CnsFileAccessConfig) error {
log := logger.GetLogger(ctx)
original := instance.DeepCopy()
err := k8s.PatchObject(ctx, client, original, instance)
if err != nil {
log.Errorf("failed to patch CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v",
instance.Name, instance.Namespace, err)
}
return err
}
// recordEvent records the event, sets the backOffDuration for the instance
// appropriately and logs the message.
// backOffDuration is reset to 1 second on success and doubled on failure.
func recordEvent(ctx context.Context, r *ReconcileCnsFileAccessConfig,
instance *v1a1.CnsFileAccessConfig, eventtype string, msg string) {
log := logger.GetLogger(ctx)
log.Debugf("Event type is %s", eventtype)
namespacedName := types.NamespacedName{
Name: instance.Name,
Namespace: instance.Namespace,
}
switch eventtype {
case v1.EventTypeWarning:
// Double backOff duration.
backOffDurationMapMutex.Lock()
backOffDuration[namespacedName] = min(backOffDuration[namespacedName]*2,
cnsoperatortypes.MaxBackOffDurationForReconciler)
r.recorder.Event(instance, v1.EventTypeWarning, "CnsFileAccessConfigFailed", msg)
backOffDurationMapMutex.Unlock()
case v1.EventTypeNormal:
// Reset backOff duration to one second.
backOffDurationMapMutex.Lock()
backOffDuration[namespacedName] = time.Second
r.recorder.Event(instance, v1.EventTypeNormal, "CnsFileAccessConfigSucceeded", msg)
backOffDurationMapMutex.Unlock()
}
}