-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathreconcile.go
980 lines (874 loc) · 40.7 KB
/
reconcile.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
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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
package storagecluster
import (
"context"
error1 "errors"
"fmt"
"strings"
"time"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
statusutil "github.com/red-hat-storage/ocs-operator/v4/controllers/util"
"github.com/red-hat-storage/ocs-operator/v4/version"
"github.com/blang/semver/v4"
"github.com/go-logr/logr"
conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1"
"github.com/operator-framework/operator-lib/conditions"
corev1 "k8s.io/api/core/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// ReconcileStrategy is a string representing how we want to reconcile
// (or not) a particular resource
type ReconcileStrategy string
// StorageClassProvisionerType is a string representing StorageClass Provisioner. E.g: aws-ebs
type StorageClassProvisionerType string
type resourceManager interface {
ensureCreated(*StorageClusterReconciler, *ocsv1.StorageCluster) (reconcile.Result, error)
ensureDeleted(*StorageClusterReconciler, *ocsv1.StorageCluster) (reconcile.Result, error)
}
type ocsJobTemplates struct{}
const (
// Name of MetadataPVCTemplate
metadataPVCName = "metadata"
// Name of WalPVCTemplate
walPVCName = "wal"
// ReconcileStrategyUnknown is the same as default
ReconcileStrategyUnknown ReconcileStrategy = ""
// ReconcileStrategyInit means reconcile once and ignore if it exists
ReconcileStrategyInit ReconcileStrategy = "init"
// ReconcileStrategyIgnore means never reconcile
ReconcileStrategyIgnore ReconcileStrategy = "ignore"
// ReconcileStrategyManage means always reconcile
ReconcileStrategyManage ReconcileStrategy = "manage"
// ReconcileStrategyStandalone means to renconcile the component exclusively (NooBaa)
ReconcileStrategyStandalone ReconcileStrategy = "standalone"
// DeviceTypeSSD represents the DeviceType SSD
DeviceTypeSSD = "ssd"
// DeviceTypeHDD represents the DeviceType HDD
DeviceTypeHDD = "hdd"
// DeviceTypeNVMe represents the DeviceType NVMe
DeviceTypeNVMe = "nvme"
// AzureDisk represents Azure Premium Managed Disks provisioner for StorageClass
AzureDisk StorageClassProvisionerType = "kubernetes.io/azure-disk"
// EBS represents AWS EBS provisioner for StorageClass
EBS StorageClassProvisionerType = "kubernetes.io/aws-ebs"
VirtualMachineCrdName = "virtualmachines.kubevirt.io"
StorageClientCrdName = "storageclients.ocs.openshift.io"
VolumeGroupSnapshotClassCrdName = "volumegroupsnapshotclasses.groupsnapshot.storage.k8s.io"
)
var storageClusterFinalizer = "storagecluster.ocs.openshift.io"
const labelZoneRegionWithoutBeta = "failure-domain.kubernetes.io/region"
const labelZoneFailureDomainWithoutBeta = "failure-domain.kubernetes.io/zone"
const labelRookPrefix = "topology.rook.io"
var validTopologyLabelKeys = []string{
// This is the most preferred key as kubernetes recommends zone and region
// labels under this key.
corev1.LabelZoneRegionStable,
// These two are retained only to have backward compatibility; they are
// deprecated by kubernetes. If topology.kubernetes.io key has same label we
// will skip the next two from the topologyMap.
corev1.LabelZoneRegion,
labelZoneRegionWithoutBeta,
// This is the most preferred key as kubernetes recommends zone and region
// labels under this key.
corev1.LabelZoneFailureDomainStable,
// These two are retained only to have backward compatibility; they are
// deprecated by kubernetes. If topology.kubernetes.io key has same label we
// will skip the next two from the topologyMap.
corev1.LabelZoneFailureDomain,
labelZoneFailureDomainWithoutBeta,
// This is the kubernetes recommended label to select nodes.
corev1.LabelHostname,
// This label is used to assign rack based topology.
labelRookPrefix,
}
// +kubebuilder:rbac:groups=ocs.openshift.io,resources=*,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=ceph.rook.io,resources=cephclusters;cephblockpools;cephfilesystems;cephnfses;cephobjectstores;cephobjectstoreusers;cephrbdmirrors;cephblockpoolradosnamespaces,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=noobaa.io,resources=noobaas,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=storage.k8s.io,resources=storageclasses,verbs=watch;create;update;delete;get;list
// +kubebuilder:rbac:groups=core,resources=pods;services;serviceaccounts;endpoints;persistentvolumes;persistentvolumeclaims;events;configmaps;secrets;nodes,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=core,resources=namespaces,verbs=get
// +kubebuilder:rbac:groups=apps,resources=deployments;daemonsets;replicasets;statefulsets,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors;prometheusrules,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=template.openshift.io,resources=templates,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=snapshot.storage.k8s.io,resources=volumesnapshotclasses,verbs=get;watch;create;update;delete
// +kubebuilder:rbac:groups=groupsnapshot.storage.k8s.io,resources=volumegroupsnapshotclasses,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=config.openshift.io,resources=infrastructures;networks,verbs=get;list;watch
// +kubebuilder:rbac:groups=config.openshift.io,resources=clusterversions;networks,verbs=get;list;watch
// +kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups=route.openshift.io,resources=routes,verbs=create;delete;list;watch;update
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;create;update
// +kubebuilder:rbac:groups=operators.coreos.com,resources=operatorconditions,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=quota.openshift.io,resources=clusterresourcequotas,verbs=get;list;watch;create;update;delete
// +kubebuilder:rbac:groups=batch,resources=cronjobs;jobs,verbs=get;list;create;update;watch;delete
// +kubebuilder:rbac:groups=operators.coreos.com,resources=clusterserviceversions,verbs=get;list;watch
// +kubebuilder:rbac:groups=k8s.cni.cncf.io,resources=network-attachment-definitions,verbs=get;list;watch
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings;rolebindings;clusterroles;roles,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=objectbucket.io,resources=objectbuckets;objectbucketclaims,verbs=get;list;watch
// +kubebuilder:rbac:groups=authentication.k8s.io,resources=tokenreviews,verbs=create
// +kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create
// Reconcile reads that state of the cluster for a StorageCluster object and makes changes based on the state read
// and what is in the StorageCluster.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 *StorageClusterReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
prevLogger := r.Log
defer func() { r.Log = prevLogger }()
r.Log = r.Log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
r.ctx = ctrllog.IntoContext(ctx, r.Log)
for _, crdName := range []string{VirtualMachineCrdName, StorageClientCrdName, VolumeGroupSnapshotClassCrdName} {
crd := &metav1.PartialObjectMetadata{}
crd.SetGroupVersionKind(extv1.SchemeGroupVersion.WithKind("CustomResourceDefinition"))
crd.Name = crdName
if err := r.Client.Get(ctx, client.ObjectKeyFromObject(crd), crd); client.IgnoreNotFound(err) != nil {
r.Log.Error(err, "Failed to get CRD", "CRD", crd.Name)
return reconcile.Result{}, err
}
util.AssertEqual(r.AvailableCrds[crd.Name], crd.UID != "", util.ExitCodeThatShouldRestartTheProcess)
}
// Fetch the StorageCluster instance
sc := &ocsv1.StorageCluster{}
if err := r.Client.Get(ctx, request.NamespacedName, sc); err != nil {
if errors.IsNotFound(err) {
r.Log.Info("No StorageCluster resource.", "StorageCluster", klog.KRef(sc.Namespace, sc.Name))
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
r.Log.Error(err, "Failed to retrieve StorageCluster.", "StorageCluster", klog.KRef(sc.Namespace, sc.Name))
return reconcile.Result{}, err
}
if err := r.validateStorageClusterSpec(sc); err != nil {
return reconcile.Result{}, err
}
r.IsNoobaaStandalone = sc.Spec.MultiCloudGateway != nil &&
ReconcileStrategy(sc.Spec.MultiCloudGateway.ReconcileStrategy) == ReconcileStrategyStandalone
var err error
r.clusters, err = statusutil.GetClusters(ctx, r.Client)
if err != nil {
r.Log.Error(err, "Failed to get clusters")
return reconcile.Result{}, err
}
r.IsMultipleStorageClusters = len(r.clusters.GetStorageClusters()) > 1
// Reconcile changes to the cluster
result, reconcileError := r.reconcilePhases(ctx, sc)
// Ensure that cephtoolbox is deployed as instructed by the user
err = r.ensureToolsDeployment(sc)
if err != nil {
r.Log.Error(err, "Failed to process ceph tools deployment.", "CephToolDeployment", klog.KRef(sc.Namespace, rookCephToolDeploymentName))
return reconcile.Result{}, err
}
// Apply status changes to the storagecluster
statusError := r.Client.Status().Update(ctx, sc)
if statusError != nil {
r.Log.Info("Could not update StorageCluster status.", "StorageCluster", klog.KRef(sc.Namespace, sc.Name))
}
// Reconcile errors have higher priority than status update errors
if reconcileError != nil {
return result, reconcileError
} else if statusError != nil {
return result, statusError
}
return result, nil
}
func (r *StorageClusterReconciler) initializeImagesStatus(sc *ocsv1.StorageCluster) {
images := &sc.Status.Images
if images.Ceph == nil {
images.Ceph = &ocsv1.ComponentImageStatus{}
}
images.Ceph.DesiredImage = r.images.Ceph
if images.NooBaaCore == nil {
images.NooBaaCore = &ocsv1.ComponentImageStatus{}
}
images.NooBaaCore.DesiredImage = r.images.NooBaaCore
if images.NooBaaDB == nil {
images.NooBaaDB = &ocsv1.ComponentImageStatus{}
}
images.NooBaaDB.DesiredImage = r.images.NooBaaDB
}
// validateStorageClusterSpec must be called before reconciling. Any syntactic and semantic errors in the CR must be caught here.
func (r *StorageClusterReconciler) validateStorageClusterSpec(instance *ocsv1.StorageCluster) error {
if err := versionCheck(instance, r.Log); err != nil {
r.Log.Error(err, "Failed to validate StorageCluster version.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
r.recorder.ReportIfNotPresent(instance, corev1.EventTypeWarning, statusutil.EventReasonValidationFailed, err.Error())
instance.Status.Phase = statusutil.PhaseError
reason := statusutil.EventReasonValidationFailed
message := err.Error()
statusutil.SetVersionMismatchCondition(&instance.Status.Conditions, corev1.ConditionTrue, reason, message)
if updateErr := r.Client.Status().Update(context.TODO(), instance); updateErr != nil {
r.Log.Error(updateErr, "Failed to update StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
return updateErr
}
return err
}
reason := statusutil.VersionValidReason
message := "Version check successful"
statusutil.SetVersionMismatchCondition(&instance.Status.Conditions, corev1.ConditionFalse, reason, message)
if !instance.Spec.ExternalStorage.Enable {
if err := r.validateStorageDeviceSets(instance); err != nil {
r.Log.Error(err, "Failed to validate StorageDeviceSets.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
r.recorder.ReportIfNotPresent(instance, corev1.EventTypeWarning, statusutil.EventReasonValidationFailed, err.Error())
instance.Status.Phase = statusutil.PhaseError
if updateErr := r.Client.Status().Update(context.TODO(), instance); updateErr != nil {
r.Log.Error(updateErr, "Failed to update StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
return updateErr
}
return err
}
}
if err := validateArbiterSpec(instance); err != nil {
r.Log.Error(err, "Failed to validate ArbiterSpec.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
r.recorder.ReportIfNotPresent(instance, corev1.EventTypeWarning, statusutil.EventReasonValidationFailed, err.Error())
instance.Status.Phase = statusutil.PhaseError
if updateErr := r.Client.Status().Update(context.TODO(), instance); updateErr != nil {
r.Log.Error(updateErr, "Could not update StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
return updateErr
}
return err
}
if err := validateOverprovisionControlSpec(instance); err != nil {
r.Log.Error(err, "Failed to validate OverprovisionControlSpec.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
r.recorder.ReportIfNotPresent(instance, corev1.EventTypeWarning, statusutil.EventReasonValidationFailed, err.Error())
instance.Status.Phase = statusutil.PhaseError
if updateErr := r.Client.Status().Update(context.TODO(), instance); updateErr != nil {
r.Log.Error(updateErr, "Could not update StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
return updateErr
}
return err
}
if err := validateCustomStorageClassNames(instance); err != nil {
r.Log.Error(err, "Failed to validate custom StorageClassNames.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
r.recorder.ReportIfNotPresent(instance, corev1.EventTypeWarning, statusutil.EventReasonValidationFailed, err.Error())
instance.Status.Phase = statusutil.PhaseError
if updateErr := r.Client.Status().Update(context.TODO(), instance); updateErr != nil {
r.Log.Error(updateErr, "Could not update StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
return updateErr
}
return err
}
return nil
}
func (r *StorageClusterReconciler) reconcilePhases(
ctx context.Context,
instance *ocsv1.StorageCluster) (reconcile.Result, error) {
if instance.Spec.ExternalStorage.Enable {
r.Log.Info("Reconciling external StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
} else {
r.Log.Info("Reconciling StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
}
// Initialize the StatusImages section of the storageclsuter CR
r.initializeImagesStatus(instance)
// Check for active StorageCluster only if Create request is made
// and ignore it if there's another active StorageCluster
// If Update request is made and StorageCluster is PhaseIgnored, no need to
// proceed further
if instance.Status.Phase == "" {
isActive := r.isActiveStorageCluster(instance)
if !isActive {
instance.Status.Phase = statusutil.PhaseIgnored
return reconcile.Result{}, nil
}
} else if instance.Status.Phase == statusutil.PhaseIgnored {
return reconcile.Result{}, nil
}
if instance.Status.Phase != statusutil.PhaseReady &&
instance.Status.Phase != statusutil.PhaseClusterExpanding &&
instance.Status.Phase != statusutil.PhaseDeleting &&
instance.Status.Phase != statusutil.PhaseOnboarding &&
instance.Status.Phase != statusutil.PhaseConnecting {
instance.Status.Phase = statusutil.PhaseProgressing
}
// Add conditions if there are none
if len(instance.Status.Conditions) == 1 && instance.Status.Conditions[0].Type == ocsv1.ConditionVersionMismatch {
reason := ocsv1.ReconcileInit
message := "Initializing StorageCluster"
statusutil.SetProgressingCondition(&instance.Status.Conditions, reason, message)
}
// Check GetDeletionTimestamp to determine if the object is under deletion
if instance.GetDeletionTimestamp().IsZero() {
var instanceUpdateRequired bool
// Setting an annotation on the storagecluster allows to only look at metadata for finding the configured mode rather than parsing the spec
if instance.Spec.AllowRemoteStorageConsumers && util.AddAnnotation(instance, "ocs.openshift.io/deployment-mode", "provider") {
instanceUpdateRequired = true
}
if !contains(instance.GetFinalizers(), storageClusterFinalizer) {
r.Log.Info("Finalizer not found for StorageCluster. Adding finalizer.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
instance.ObjectMeta.Finalizers = append(instance.ObjectMeta.Finalizers, storageClusterFinalizer)
instanceUpdateRequired = true
}
if instanceUpdateRequired {
if err := r.Client.Update(r.ctx, instance); err != nil {
r.Log.Info("Failed to update StorageCluster", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
return reconcile.Result{}, err
}
}
if scWasUpdated, err := r.reconcileUninstallAnnotations(instance); err != nil {
return reconcile.Result{}, err
} else if scWasUpdated {
r.Log.Info("exiting reconcile loop immediately after updating the storagecluster annotations")
return reconcile.Result{}, err
}
} else {
// The object is marked for deletion
instance.Status.Phase = statusutil.PhaseDeleting
if contains(instance.GetFinalizers(), storageClusterFinalizer) {
if res, err := r.deleteResources(instance); err != nil {
r.Log.Info("Uninstall in progress.", "Status", err)
r.recorder.ReportIfNotPresent(instance, corev1.EventTypeWarning, statusutil.EventReasonUninstallPending, err.Error())
return reconcile.Result{RequeueAfter: time.Second * time.Duration(1)}, nil
} else if !res.IsZero() {
// result is not empty
return res, nil
}
r.Log.Info("Removing finalizer from StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
// Once all finalizers have been removed, the object will be deleted
instance.ObjectMeta.Finalizers = remove(instance.ObjectMeta.Finalizers, storageClusterFinalizer)
if err := r.Client.Update(context.TODO(), instance); err != nil {
r.Log.Info("Failed to remove finalizer from StorageCluster", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
return reconcile.Result{}, err
}
}
r.Log.Info("StorageCluster is terminated, skipping reconciliation.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
// mark operator upgradeable if and only if all other storageclusters are ready or current cluster is the last cluster
if r.clusters.AreOtherStorageClustersReady(instance) {
returnErr := r.SetOperatorConditions("Skipping StorageCluster reconciliation", "Terminated", metav1.ConditionTrue, nil)
return reconcile.Result{}, returnErr
}
return reconcile.Result{}, nil
}
if res, err := r.ownStorageClusterPeersInNamespace(instance); err != nil || !res.IsZero() {
return reconcile.Result{}, err
}
if res, err := r.ownStorageConsumersInNamespace(instance); err != nil || !res.IsZero() {
return reconcile.Result{}, err
}
if instance.Spec.ExternalStorage.Enable {
if err := r.setExternalOCSResourcesData(instance); err != nil {
r.Log.Error(err, "Failed to set the externalOCSResources cache")
return reconcile.Result{}, err
}
}
// in-memory conditions should start off empty. It will only ever hold
// negative conditions (!Available, Degraded, Progressing)
r.conditions = nil
// Start with empty r.phase
r.phase = ""
var objs []resourceManager
if !instance.Spec.ExternalStorage.Enable {
if !r.IsNoobaaStandalone {
// list of default ensure functions
// preserve list order
objs = []resourceManager{
&storageClassWebhook{},
&ocsProviderServer{},
&storageClient{},
&backingStorageClasses{},
&ocsTopologyMap{},
&ocsStorageQuota{},
&ocsCephConfig{},
&ocsCephCluster{},
&ocsCephBlockPools{},
&ocsCephFilesystems{},
&ocsCephNFS{},
&ocsCephNFSService{},
&ocsCephObjectStores{},
&ocsCephObjectStoreUsers{},
&ocsCephRGWRoutes{},
&ocsStorageClass{},
&ocsNoobaaSystem{},
&ocsSnapshotClass{},
&ocsGroupSnapshotClass{},
&ocsJobTemplates{},
&ocsCephRbdMirrors{},
&odfInfoConfig{},
}
} else {
// noobaa-only ensure functions
objs = []resourceManager{
&ocsNoobaaSystem{},
}
}
} else {
// for external cluster, we have a different set of ensure functions
// preserve list order
objs = []resourceManager{
&ocsCephCluster{},
&ocsExternalResources{},
&ocsStorageQuota{},
&ocsSnapshotClass{},
&ocsGroupSnapshotClass{},
&ocsNoobaaSystem{},
&odfInfoConfig{},
}
}
for _, obj := range objs {
returnRes, returnErr := obj.ensureCreated(r, instance)
if r.phase == statusutil.PhaseClusterExpanding {
message := "StorageCluster is expanding"
reason := "Expanding"
returnErr = r.SetOperatorConditions(message, reason, metav1.ConditionFalse, returnErr)
instance.Status.Phase = statusutil.PhaseClusterExpanding
conditionsv1.SetStatusCondition(&instance.Status.Conditions, conditionsv1.Condition{
Type: conditionsv1.ConditionUpgradeable,
Status: corev1.ConditionFalse,
Reason: reason,
Message: message,
})
} else if instance.Status.Phase != statusutil.PhaseReady &&
instance.Status.Phase != statusutil.PhaseOnboarding &&
instance.Status.Phase != statusutil.PhaseConnecting {
instance.Status.Phase = statusutil.PhaseProgressing
}
if returnErr != nil {
reason := ocsv1.ReconcileFailed
message := fmt.Sprintf("Error while reconciling: %v", returnErr)
statusutil.SetErrorCondition(&instance.Status.Conditions, reason, message)
if instance.Status.Phase != statusutil.PhaseOnboarding {
// if the error was due to skipped storage classes
// instead of error phase we will set it to progressing
if strings.Contains(returnErr.Error(), storageClassSkippedError) {
instance.Status.Phase = statusutil.PhaseProgressing
return reconcile.Result{Requeue: true}, nil
}
instance.Status.Phase = statusutil.PhaseError
}
// don't want to overwrite the actual reconcile failure
return reconcile.Result{}, returnErr
} else if !returnRes.IsZero() {
// result is not empty
return returnRes, nil
}
}
// Process resource profiles only if the cluster is not external or provider mode or noobaa standalone, and if the resource profile has changed
if !(instance.Spec.ExternalStorage.Enable || instance.Spec.AllowRemoteStorageConsumers || r.IsNoobaaStandalone) &&
(instance.Spec.ResourceProfile != instance.Status.LastAppliedResourceProfile) {
err := r.ensureResourceProfileChangeApplied(instance)
if err != nil {
if err == errResourceProfileChangeApplying {
reason := ocsv1.ReconcileFailed
message := err.Error()
statusutil.SetProgressingCondition(&instance.Status.Conditions, reason, message)
instance.Status.Phase = statusutil.PhaseProgressing
return reconcile.Result{Requeue: true}, nil
} else if err == errResourceProfileChangeFailed {
reason := ocsv1.ReconcileFailed
message := err.Error()
statusutil.SetErrorCondition(&instance.Status.Conditions, reason, message)
instance.Status.Phase = statusutil.PhaseError
}
return reconcile.Result{}, err
}
}
// All component operators are in a happy state.
if r.conditions == nil {
r.Log.Info("No component operator reported negatively.")
reason := ocsv1.ReconcileCompleted
message := ocsv1.ReconcileCompletedMessage
statusutil.SetCompleteCondition(&instance.Status.Conditions, reason, message)
if instance.Spec.ExternalStorage.Enable {
statusutil.RemoveExternalCephClusterNegativeConditions(&instance.Status.Conditions)
}
// If no operator whose conditions we are watching reports an error, then it is safe
// to set upgradeable to true.
if instance.Status.Phase != statusutil.PhaseClusterExpanding {
instance.Status.Phase = statusutil.PhaseReady
var returnErr error
var notUpgradeableReasons, notUpgradeableMessages []string
// mark operator upgradeable if and only if all storageclusters are ready
if !r.clusters.AreOtherStorageClustersReady(instance) {
notUpgradeableReasons = append(notUpgradeableReasons, "NotReady")
notUpgradeableMessages = append(notUpgradeableMessages, "StorageCluster is not ready")
}
// check operator upgradeability based on connected clients
if instance.Spec.AllowRemoteStorageConsumers {
if count, err := getUnsupportedClientsCount(r, instance.Namespace); err != nil {
notUpgradeableReasons = append(notUpgradeableReasons, "ODFClients")
notUpgradeableMessages = append(notUpgradeableMessages, "Unable to determine status of connected ODF Clients")
} else if count != 0 {
notUpgradeableReasons = append(notUpgradeableReasons, "ODFClients")
notUpgradeableMessages = append(notUpgradeableMessages, fmt.Sprintf("%d connected ODF Client Operators are not up to date", count))
}
}
if len(notUpgradeableMessages) > 0 {
// we are not upgradeable
returnErr = r.SetOperatorConditions(
strings.Join(notUpgradeableMessages, ";"), strings.Join(notUpgradeableReasons, ";"),
metav1.ConditionFalse, nil)
} else {
// we are upgradeable
returnErr = r.SetOperatorConditions(message, reason, metav1.ConditionTrue, nil)
}
if returnErr != nil {
return reconcile.Result{}, returnErr
}
}
} else {
// If any component operator reports negatively we want to write that to
// the instance while preserving it's lastTransitionTime.
// For example, consider the resource has the Available condition
// type with type "False". When reconciling the resource we would
// add it to the in-memory representation of OCS's conditions (r.conditions)
// and here we are simply writing it back to the server.
// One shortcoming is that only one failure of a particular condition can be
// captured at one time (ie. if resource1 and resource2 are both reporting !Available,
// you will only see resource2q as it updates last).
for _, condition := range r.conditions {
conditionsv1.SetStatusCondition(&instance.Status.Conditions, condition)
}
reason := ocsv1.ReconcileCompleted
message := ocsv1.ReconcileCompletedMessage
conditionsv1.SetStatusCondition(&instance.Status.Conditions, conditionsv1.Condition{
Type: ocsv1.ConditionReconcileComplete,
Status: corev1.ConditionTrue,
Reason: reason,
Message: message,
})
// If for any reason we marked ourselves !upgradeable...then unset readiness
if conditionsv1.IsStatusConditionFalse(instance.Status.Conditions, conditionsv1.ConditionUpgradeable) {
returnErr := r.SetOperatorConditions("StorageCluster is not ready.", "NotReady", metav1.ConditionFalse, nil)
if returnErr != nil {
return reconcile.Result{}, returnErr
}
}
if instance.Status.Phase != statusutil.PhaseClusterExpanding &&
!instance.Spec.ExternalStorage.Enable {
if conditionsv1.IsStatusConditionTrue(instance.Status.Conditions, conditionsv1.ConditionProgressing) {
instance.Status.Phase = statusutil.PhaseProgressing
} else if conditionsv1.IsStatusConditionFalse(instance.Status.Conditions, conditionsv1.ConditionUpgradeable) {
instance.Status.Phase = statusutil.PhaseNotReady
} else {
instance.Status.Phase = statusutil.PhaseError
}
}
}
// enable metrics exporter at the end of reconcile
// this allows storagecluster to be instantiated before
// scraping metrics
if instance.Spec.Monitoring == nil || ReconcileStrategy(instance.Spec.Monitoring.ReconcileStrategy) != ReconcileStrategyIgnore {
if instance.Spec.Monitoring == nil {
instance.Spec.Monitoring = &ocsv1.MonitoringSpec{
ReconcileStrategy: string(ReconcileStrategyUnknown),
}
}
if err := r.enableMetricsExporter(ctx, instance); err != nil {
r.Log.Error(err, "Failed to reconcile metrics exporter.")
return reconcile.Result{}, err
}
if err := r.enablePrometheusRules(ctx, instance); err != nil {
r.Log.Error(err, "Failed to reconcile prometheus rules.")
return reconcile.Result{}, err
}
}
// For security of encrypted messagges, When both encryption and compression are enabled,
// compression setting will be ignored and message will not be compressed.
// Refer https://docs.ceph.com/en/quincy/rados/configuration/msgr2/#confval-ms_compress_secure
networkSpec := instance.Spec.Network
if networkSpec != nil && networkSpec.Connections != nil &&
networkSpec.Connections.Encryption != nil && networkSpec.Connections.Encryption.Enabled &&
networkSpec.Connections.Compression != nil && networkSpec.Connections.Compression.Enabled {
r.Log.Info("Both in-transit encryption & compression are enabled. " +
"To protect security of encrypted messages ceph will ignore compression")
r.recorder.ReportIfNotPresent(instance, corev1.EventTypeWarning, "EncryptionAndCompressionEnabled",
"Both in-transit encryption & compression are enabled. "+
"To protect security of encrypted messages ceph will ignore compression")
}
// Ensure that verbose logging is enabled when RBD mirroring is enabled
// TODO: This is a temporary arrangement, this is to be removed when RDR goes to GA
if !instance.Spec.ExternalStorage.Enable {
result, err := r.ensureRbdMirrorDebugLogging(instance)
if !result.IsZero() || err != nil {
r.Log.Error(err, "Failed to ensure RBD mirror debug logging.")
return result, err
}
}
return reconcile.Result{}, nil
}
// versionCheck populates the `.Status.Version` field
func versionCheck(sc *ocsv1.StorageCluster, reqLogger logr.Logger) error {
if sc.Status.Version == "" {
sc.Status.Version = version.Version
} else if sc.Status.Version != version.Version { // check anything else only if the versions mismatch
storClustSemV1, err := semver.Make(sc.Status.Version)
if err != nil {
reqLogger.Error(err, "Error while parsing Storage Cluster version")
return err
}
ocsSemV1, err := semver.Make(version.Version)
if err != nil {
reqLogger.Error(err, "Error while parsing OCS Operator version")
return err
}
// if the storage cluster version is higher than the invoking OCS Operator's version,
// return error
if storClustSemV1.GT(ocsSemV1) {
err = fmt.Errorf("Storage cluster version (%s) is higher than the OCS Operator version (%s)",
sc.Status.Version, version.Version)
reqLogger.Error(err, "Incompatible Storage cluster version")
return err
}
// if the storage cluster version is less than the OCS Operator version,
// just update.
sc.Status.Version = version.Version
}
return nil
}
func (r *StorageClusterReconciler) SetOperatorConditions(message string, reason string, isUpgradeable metav1.ConditionStatus, prevError error) error {
prevError = client.IgnoreNotFound(prevError)
operatorConditionErr := r.OperatorCondition.Set(context.TODO(), isUpgradeable, conditions.Option(conditions.WithMessage(message)), conditions.Option(conditions.WithReason(reason)))
if operatorConditionErr != nil {
r.Log.Error(operatorConditionErr, "Unable to update OperatorCondition")
}
if prevError != nil && operatorConditionErr != nil {
return error1.New(prevError.Error() + operatorConditionErr.Error())
} else if prevError != nil {
return prevError
}
return operatorConditionErr
}
// validateStorageDeviceSets checks the StorageDeviceSets of the given
// StorageCluster for completeness and correctness
func (r *StorageClusterReconciler) validateStorageDeviceSets(sc *ocsv1.StorageCluster) error {
for i, ds := range sc.Spec.StorageDeviceSets {
if ds.DataPVCTemplate.Spec.StorageClassName == nil || *ds.DataPVCTemplate.Spec.StorageClassName == "" {
return fmt.Errorf("failed to validate StorageDeviceSet %d: no StorageClass specified", i)
}
if ds.MetadataPVCTemplate != nil {
if ds.MetadataPVCTemplate.Spec.StorageClassName == nil || *ds.MetadataPVCTemplate.Spec.StorageClassName == "" {
return fmt.Errorf("failed to validate StorageDeviceSet %d: no StorageClass specified for metadataPVCTemplate", i)
}
}
if ds.WalPVCTemplate != nil {
if ds.WalPVCTemplate.Spec.StorageClassName == nil || *ds.WalPVCTemplate.Spec.StorageClassName == "" {
return fmt.Errorf("failed to validate StorageDeviceSet %d: no StorageClass specified for walPVCTemplate", i)
}
}
if ds.DeviceType != "" {
if (DeviceTypeSSD != strings.ToLower(ds.DeviceType)) && (DeviceTypeHDD != strings.ToLower(ds.DeviceType)) && (DeviceTypeNVMe != strings.ToLower(ds.DeviceType)) {
return fmt.Errorf("failed to validate DeviceType %q: no Device of this type", ds.DeviceType)
}
}
}
return nil
}
func (r *StorageClusterReconciler) isActiveStorageCluster(instance *ocsv1.StorageCluster) bool {
// instance is already marked for deletion
// do not mark it as active
if !instance.GetDeletionTimestamp().IsZero() {
return false
}
// Ensure that the internal storageCluster is only allowed in the OperatorNamespace.
if !instance.Spec.ExternalStorage.Enable && instance.Namespace != r.OperatorNamespace {
return false
}
// Do not allow Multiple Storage Clusters with same name
if r.clusters.HasMultipleStorageClustersWithSameName(instance.Name) {
return false
}
// Do not allow Multiple Storage Clusters in same namespace
if r.clusters.HasMultipleStorageClustersInNamespace(instance.Namespace) &&
!r.isStorageClusterNotIgnored(instance, r.clusters.GetStorageClustersInNamespace(instance.Namespace)) {
return false
}
var storageClusterList []ocsv1.StorageCluster
if !instance.Spec.ExternalStorage.Enable {
storageClusterList = r.clusters.GetInternalStorageClusters()
} else {
storageClusterList = r.clusters.GetExternalStorageClusters()
}
// There is only one StorageCluster i.e. instance
if len(storageClusterList) == 1 {
return true
}
return r.isStorageClusterNotIgnored(instance, storageClusterList)
}
func (r *StorageClusterReconciler) isStorageClusterNotIgnored(
instance *ocsv1.StorageCluster, storageClusters []ocsv1.StorageCluster) bool {
// There are many StorageClusters. Check if this is Active
for n, storageCluster := range storageClusters {
if storageCluster.Status.Phase != statusutil.PhaseIgnored &&
storageCluster.ObjectMeta.Name != instance.ObjectMeta.Name {
// Both StorageClusters are in creation phase
// Tiebreak using CreationTimestamp and Alphanumeric ordering
if storageCluster.Status.Phase == "" {
if storageCluster.CreationTimestamp.Before(&instance.CreationTimestamp) {
return false
} else if storageCluster.CreationTimestamp.Equal(&instance.CreationTimestamp) && storageCluster.Name < instance.Name {
return false
}
if n == len(storageClusters)-1 {
return true
}
continue
}
return false
}
}
return true
}
func (r *StorageClusterReconciler) ownStorageClusterPeersInNamespace(instance *ocsv1.StorageCluster) (reconcile.Result, error) {
storageClusterPeerList := &ocsv1.StorageClusterPeerList{}
err := r.Client.List(r.ctx, storageClusterPeerList, client.InNamespace(instance.Namespace))
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to list storageClusterPeer: %w", err)
}
for i := range storageClusterPeerList.Items {
scp := &storageClusterPeerList.Items[i]
lenOwners := len(scp.OwnerReferences)
err := controllerutil.SetOwnerReference(instance, scp, r.Scheme)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to set owner reference on StorageClusterPeer %v: %w", scp.Name, err)
}
if lenOwners != len(scp.OwnerReferences) {
err = r.Client.Update(r.ctx, scp)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to persist StorageCluster owner ref on StorageClusterPeer %v: %w", scp.Name, err)
}
}
}
return reconcile.Result{}, nil
}
func (r *StorageClusterReconciler) ownStorageConsumersInNamespace(instance *ocsv1.StorageCluster) (reconcile.Result, error) {
storageConsumerList := &ocsv1alpha1.StorageConsumerList{}
err := r.Client.List(r.ctx, storageConsumerList, client.InNamespace(instance.Namespace))
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to list storageConsumer: %w", err)
}
for i := range storageConsumerList.Items {
scp := &storageConsumerList.Items[i]
lenOwners := len(scp.OwnerReferences)
err := controllerutil.SetOwnerReference(instance, scp, r.Scheme)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to set owner reference on storageConsumer %v: %w", scp.Name, err)
}
if lenOwners != len(scp.OwnerReferences) {
err = r.Client.Update(r.ctx, scp)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to persist StorageCluster owner ref on storageConsumer %v: %w", scp.Name, err)
}
}
}
return reconcile.Result{}, nil
}
// Checks whether a string is contained within a slice
func contains(slice []string, s string) bool {
for _, item := range slice {
if item == s {
return true
}
}
return false
}
// Removes a given string from a slice and returns the new slice
func remove(slice []string, s string) (result []string) {
for _, item := range slice {
if item == s {
continue
}
result = append(result, item)
}
return
}
func validateArbiterSpec(sc *ocsv1.StorageCluster) error {
if sc.Spec.Arbiter.Enable && sc.Spec.FlexibleScaling {
return fmt.Errorf("arbiter and flexibleScaling both can't be enabled")
}
if sc.Spec.Arbiter.Enable && sc.Spec.NodeTopologies.ArbiterLocation == "" {
return fmt.Errorf("arbiter is set to enable but no arbiterLocation has been provided in the Spec.NodeTopologies.ArbiterLocation")
}
return nil
}
func validateOverprovisionControlSpec(sc *ocsv1.StorageCluster) error {
for _, opc := range sc.Spec.OverprovisionControl {
val, ok := opc.Capacity.AsInt64()
if !ok {
return fmt.Errorf("non-valid capacity %v for overprovision %s", opc.Capacity, opc.QuotaName)
}
if val <= 0 {
return fmt.Errorf("capacity can not be a zero or negative value for overprovision %s", opc.QuotaName)
}
if opc.StorageClassName == "" {
return fmt.Errorf("missing storageclassname")
}
if opc.QuotaName == "" {
return fmt.Errorf("missing quotaname")
}
}
return nil
}
func validateCustomStorageClassNames(sc *ocsv1.StorageCluster) error {
scMap := make(map[string]bool)
duplicateNames := []string{}
if sc.Spec.ManagedResources.CephBlockPools.StorageClassName != "" {
if _, ok := scMap[sc.Spec.ManagedResources.CephBlockPools.StorageClassName]; ok {
duplicateNames = append(duplicateNames, "CephBlockPools")
}
scMap[sc.Spec.ManagedResources.CephBlockPools.StorageClassName] = true
}
if sc.Spec.ManagedResources.CephFilesystems.StorageClassName != "" {
if _, ok := scMap[sc.Spec.ManagedResources.CephFilesystems.StorageClassName]; ok {
duplicateNames = append(duplicateNames, "CephFilesystems")
}
scMap[sc.Spec.ManagedResources.CephFilesystems.StorageClassName] = true
}
if sc.Spec.ManagedResources.CephNonResilientPools.StorageClassName != "" {
if _, ok := scMap[sc.Spec.ManagedResources.CephNonResilientPools.StorageClassName]; ok {
duplicateNames = append(duplicateNames, "CephNonResilientPools")
}
scMap[sc.Spec.ManagedResources.CephNonResilientPools.StorageClassName] = true
}
if sc.Spec.ManagedResources.CephObjectStores.StorageClassName != "" {
if _, ok := scMap[sc.Spec.ManagedResources.CephObjectStores.StorageClassName]; ok {
duplicateNames = append(duplicateNames, "CephObjectStores")
}
scMap[sc.Spec.ManagedResources.CephObjectStores.StorageClassName] = true
}
if sc.Spec.NFS != nil && sc.Spec.NFS.Enable && sc.Spec.NFS.StorageClassName != "" {
if _, ok := scMap[sc.Spec.NFS.StorageClassName]; ok {
duplicateNames = append(duplicateNames, "NFS")
}
scMap[sc.Spec.NFS.StorageClassName] = true
}
if sc.Spec.Encryption.StorageClass && sc.Spec.Encryption.KeyManagementService.Enable && sc.Spec.Encryption.StorageClassName != "" {
if _, ok := scMap[sc.Spec.Encryption.StorageClassName]; ok {
duplicateNames = append(duplicateNames, "Encryption")
}
scMap[sc.Spec.Encryption.StorageClassName] = true
}
if len(duplicateNames) > 0 {
return fmt.Errorf("Duplicate StorageClass name(s) provided: %v", duplicateNames)
}
return nil
}
func getUnsupportedClientsCount(r *StorageClusterReconciler, namespace string) (int, error) {
scList := &ocsv1alpha1.StorageConsumerList{}
err := r.Client.List(r.ctx, scList, client.InNamespace(namespace))
if err != nil {
r.Log.Error(err, "Failed to list StorageConsumers")
return -1, err
}
var count int
providerVersion, _ := semver.Make(version.Version)
for idx := range scList.Items {
clientVersion, err := semver.Make(scList.Items[idx].Status.Client.OperatorVersion)
if err == nil {
// provider operator and client operator should be on same version for full compatibility
if providerVersion.Major != clientVersion.Major || providerVersion.Minor != clientVersion.Minor {
count++
}
} else {
r.Log.Error(err, "Failed to parse client operator version", "StorageConsumer", scList.Items[idx].GetName())
count++
}
}
return count, nil
}