@@ -28,8 +28,10 @@ import (
28
28
29
29
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
30
30
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1"
31
+ "github.com/red-hat-storage/ocs-operator/v4/controllers/defaults"
31
32
"github.com/red-hat-storage/ocs-operator/v4/controllers/util"
32
33
34
+ ocsclientv1a1 "github.com/red-hat-storage/ocs-client-operator/api/v1alpha1"
33
35
rookCephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
34
36
corev1 "k8s.io/api/core/v1"
35
37
"k8s.io/apimachinery/pkg/api/errors"
@@ -38,6 +40,7 @@ import (
38
40
ctrl "sigs.k8s.io/controller-runtime"
39
41
"sigs.k8s.io/controller-runtime/pkg/builder"
40
42
"sigs.k8s.io/controller-runtime/pkg/client"
43
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
41
44
"sigs.k8s.io/controller-runtime/pkg/event"
42
45
"sigs.k8s.io/controller-runtime/pkg/predicate"
43
46
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -114,6 +117,19 @@ func (r *StorageConsumerUpgradeReconciler) Reconcile(ctx context.Context, reques
114
117
}
115
118
116
119
consumerConfigMapName := fmt .Sprintf ("storageconsumer-%v" , util .FnvHash (storageConsumer .Name ))
120
+ consumerName := storageConsumer .Name
121
+
122
+ clusterID := util .GetClusterID (ctx , r .Client , & log )
123
+ if clusterID == "" {
124
+ return reconcile.Result {}, fmt .Errorf ("failed to get openshift cluster ID" )
125
+ }
126
+
127
+ internalConsumer := clusterID == storageConsumer .Status .Client .ClusterID
128
+
129
+ if internalConsumer {
130
+ consumerConfigMapName = defaults .LocalStorageConsumerConfigMapName
131
+ consumerName = defaults .LocalStorageConsumerName
132
+ }
117
133
118
134
if err = r .reconcileConsumerConfigMap (ctx , storageCluster , storageConsumer , consumerConfigMapName ); err != nil {
119
135
return reconcile.Result {}, err
@@ -123,10 +139,20 @@ func (r *StorageConsumerUpgradeReconciler) Reconcile(ctx context.Context, reques
123
139
return reconcile.Result {}, err
124
140
}
125
141
126
- if err = r .reconcileStorageConsumer (ctx , storageCluster , storageConsumer , consumerConfigMapName ); err != nil {
142
+ if err = r .reconcileStorageConsumer (ctx , storageCluster , consumerName , consumerConfigMapName ); err != nil {
127
143
return reconcile.Result {}, err
128
144
}
129
145
146
+ if internalConsumer {
147
+ if err = r .reconcileStorageClient (ctx , storageCluster , consumerName ); err != nil {
148
+ return reconcile.Result {}, err
149
+ }
150
+ if err = r .Client .Delete (ctx , storageConsumer ); err != nil {
151
+ return reconcile.Result {}, err
152
+ }
153
+ util .RestartPod (ctx , r .Client , & log , "ocs-provider-server" , storageCluster .Namespace )
154
+ }
155
+
130
156
return reconcile.Result {}, nil
131
157
}
132
158
@@ -269,24 +295,58 @@ func (r *StorageConsumerUpgradeReconciler) removeStorageRequestOwner(ctx context
269
295
return nil
270
296
}
271
297
272
- func (r * StorageConsumerUpgradeReconciler ) reconcileStorageConsumer (ctx context.Context , storageCluster * ocsv1.StorageCluster , storageConsumer * ocsv1alpha1.StorageConsumer , consumerConfigMapName string ) error {
273
- spec := & storageConsumer .Spec
274
- spec .ResourceNameMappingConfigMap .Name = consumerConfigMapName
275
- spec .StorageClasses = []ocsv1alpha1.StorageClassSpec {
276
- {Name : util .GenerateNameForCephBlockPoolStorageClass (storageCluster )},
277
- {Name : util .GenerateNameForCephFilesystemStorageClass (storageCluster )},
298
+ func (r * StorageConsumerUpgradeReconciler ) reconcileStorageConsumer (
299
+ ctx context.Context ,
300
+ storageCluster * ocsv1.StorageCluster ,
301
+ consumerName , consumerConfigMapName string ,
302
+ ) error {
303
+
304
+ storageConsumer := & ocsv1alpha1.StorageConsumer {}
305
+ storageConsumer .Name = consumerName
306
+ storageConsumer .Namespace = storageCluster .Namespace
307
+
308
+ if _ , err := controllerutil .CreateOrUpdate (ctx , r .Client , storageConsumer , func () error {
309
+ spec := & storageConsumer .Spec
310
+ spec .Enable = true
311
+ spec .ResourceNameMappingConfigMap .Name = consumerConfigMapName
312
+ spec .StorageClasses = []ocsv1alpha1.StorageClassSpec {
313
+ {Name : util .GenerateNameForCephBlockPoolStorageClass (storageCluster )},
314
+ {Name : util .GenerateNameForCephFilesystemStorageClass (storageCluster )},
315
+ }
316
+ spec .VolumeSnapshotClasses = []ocsv1alpha1.VolumeSnapshotClassSpec {
317
+ {Name : util .GenerateNameForSnapshotClass (storageCluster .Name , util .RbdSnapshotter )},
318
+ {Name : util .GenerateNameForSnapshotClass (storageCluster .Name , util .CephfsSnapshotter )},
319
+ }
320
+ spec .VolumeGroupSnapshotClasses = []ocsv1alpha1.VolumeGroupSnapshotClassSpec {
321
+ {Name : util .GenerateNameForGroupSnapshotClass (storageCluster , util .RbdGroupSnapshotter )},
322
+ {Name : util .GenerateNameForGroupSnapshotClass (storageCluster , util .CephfsGroupSnapshotter )},
323
+ }
324
+ util .AddAnnotation (storageConsumer , util .Is419AdjustedAnnotationKey , strconv .FormatBool (true ))
325
+ return nil
326
+ }); err != nil {
327
+ return err
278
328
}
279
- spec .VolumeSnapshotClasses = []ocsv1alpha1.VolumeSnapshotClassSpec {
280
- {Name : util .GenerateNameForSnapshotClass (storageCluster .Name , util .RbdSnapshotter )},
281
- {Name : util .GenerateNameForSnapshotClass (storageCluster .Name , util .CephfsSnapshotter )},
329
+
330
+ return nil
331
+ }
332
+
333
+ // reconcileStorageClient Updates the status of the internal client to point to the new consumer
334
+ func (r * StorageConsumerUpgradeReconciler ) reconcileStorageClient (ctx context.Context , storageCluster * ocsv1.StorageCluster , consumerName string ) error {
335
+ storageConsumer := & ocsv1alpha1.StorageConsumer {}
336
+ storageConsumer .Name = consumerName
337
+ storageConsumer .Namespace = storageCluster .Namespace
338
+ if err := r .Client .Get (ctx , client .ObjectKeyFromObject (storageConsumer ), storageConsumer ); err != nil {
339
+ return err
282
340
}
283
- spec .VolumeGroupSnapshotClasses = []ocsv1alpha1.VolumeGroupSnapshotClassSpec {
284
- {Name : util .GenerateNameForGroupSnapshotClass (storageCluster , util .RbdGroupSnapshotter )},
285
- {Name : util .GenerateNameForGroupSnapshotClass (storageCluster , util .CephfsGroupSnapshotter )},
341
+
342
+ storageClient := & ocsclientv1a1.StorageClient {}
343
+ storageClient .Name = storageCluster .Name
344
+ if err := r .Client .Get (ctx , client .ObjectKeyFromObject (storageClient ), storageClient ); err != nil {
345
+ return err
286
346
}
287
- util .AddAnnotation (storageConsumer , util .Is419AdjustedAnnotationKey , strconv .FormatBool (true ))
288
347
289
- if err := r .Client .Update (ctx , storageConsumer ); client .IgnoreNotFound (err ) != nil {
348
+ storageClient .Status .ConsumerID = string (storageConsumer .UID )
349
+ if err := r .Client .Status ().Update (ctx , storageClient ); err != nil {
290
350
return err
291
351
}
292
352
return nil
0 commit comments