|
| 1 | +/* |
| 2 | +Copyright 2021 Red Hat OpenShift Container Storage. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/md5" |
| 22 | + "encoding/hex" |
| 23 | + "encoding/json" |
| 24 | + "fmt" |
| 25 | + "strings" |
| 26 | + |
| 27 | + ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/api/v4/v1alpha1" |
| 28 | + "github.com/red-hat-storage/ocs-operator/v4/controllers/util" |
| 29 | + |
| 30 | + corev1 "k8s.io/api/core/v1" |
| 31 | + "k8s.io/apimachinery/pkg/api/errors" |
| 32 | + "k8s.io/apimachinery/pkg/runtime" |
| 33 | + ctrl "sigs.k8s.io/controller-runtime" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 36 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 37 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 38 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 39 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 40 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 41 | +) |
| 42 | + |
| 43 | +// StorageConsumerReconciler reconciles a StorageConsumer object |
| 44 | +type StorageConsumerUpgradeReconciler struct { |
| 45 | + client.Client |
| 46 | + Scheme *runtime.Scheme |
| 47 | +} |
| 48 | + |
| 49 | +// SetupWithManager sets up the controller with the Manager. |
| 50 | +func (r *StorageConsumerUpgradeReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 51 | + |
| 52 | + oldUnprocessedConsumerPredicate := predicate.TypedFuncs[ocsv1alpha1.StorageConsumer]{ |
| 53 | + CreateFunc: func(e event.TypedCreateEvent[ocsv1alpha1.StorageConsumer]) bool { |
| 54 | + _, exist := e.Object.GetAnnotations()[util.Is419AdjustedAnnotationKey] |
| 55 | + return strings.HasPrefix(e.Object.Status.Client.OperatorVersion, "4.18") && !exist |
| 56 | + }, |
| 57 | + DeleteFunc: func(e event.TypedDeleteEvent[ocsv1alpha1.StorageConsumer]) bool { |
| 58 | + return false |
| 59 | + }, |
| 60 | + UpdateFunc: func(e event.TypedUpdateEvent[ocsv1alpha1.StorageConsumer]) bool { |
| 61 | + return false |
| 62 | + }, |
| 63 | + GenericFunc: func(e event.TypedGenericEvent[ocsv1alpha1.StorageConsumer]) bool { |
| 64 | + return false |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + return ctrl.NewControllerManagedBy(mgr). |
| 69 | + For(&ocsv1alpha1.StorageConsumer{}, builder.WithPredicates(oldUnprocessedConsumerPredicate)). |
| 70 | + Complete(r) |
| 71 | +} |
| 72 | + |
| 73 | +// +kubebuilder:rbac:groups=ocs.openshift.io,resources=storageconsumers,verbs=get;watch;create;update |
| 74 | +// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;create;update |
| 75 | + |
| 76 | +func (r *StorageConsumerUpgradeReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { |
| 77 | + |
| 78 | + log := log.FromContext(ctx, "StorageConsumerUpgrade", request.NamespacedName) |
| 79 | + |
| 80 | + storageConsumer := &ocsv1alpha1.StorageConsumer{} |
| 81 | + storageConsumer.Name = request.Name |
| 82 | + storageConsumer.Namespace = request.Namespace |
| 83 | + |
| 84 | + if err := r.Client.Get(ctx, client.ObjectKeyFromObject(storageConsumer), storageConsumer); errors.IsNotFound(err) { |
| 85 | + log.Info("No StorageConsumer resource.") |
| 86 | + // Request object not found, could have been deleted after reconcile request. |
| 87 | + // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers. |
| 88 | + // Return and don't requeue |
| 89 | + return reconcile.Result{}, nil |
| 90 | + } else if err != nil { |
| 91 | + // Error reading the object - requeue the request. |
| 92 | + log.Error(err, "Failed to retrieve StorageConsumer.") |
| 93 | + return reconcile.Result{}, err |
| 94 | + } |
| 95 | + |
| 96 | + consumerConfigMapName := fmt.Sprintf("storageconsumer-%v", util.FnvHash(storageConsumer.Name)) |
| 97 | + |
| 98 | + if storageConsumer.Spec.ResourceNameMappingConfigMap.Name == consumerConfigMapName { |
| 99 | + return reconcile.Result{}, nil |
| 100 | + } |
| 101 | + |
| 102 | + storageCluster, err := util.GetStorageClusterInNamespace(ctx, r.Client, storageConsumer.Namespace) |
| 103 | + if err != nil { |
| 104 | + return reconcile.Result{}, err |
| 105 | + } |
| 106 | + |
| 107 | + availableServices, err := util.GetAvailableServices(ctx, r.Client, storageCluster) |
| 108 | + if err != nil { |
| 109 | + return ctrl.Result{}, fmt.Errorf("failed to get available services configured in StorageCluster: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + consumerConfigMap := &corev1.ConfigMap{} |
| 113 | + consumerConfigMap.Name = consumerConfigMapName |
| 114 | + consumerConfigMap.Namespace = storageConsumer.Namespace |
| 115 | + |
| 116 | + if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, consumerConfigMap, func() error { |
| 117 | + data := util.GetStorageConsumerDefaultResourceNames( |
| 118 | + storageConsumer.Name, |
| 119 | + string(storageConsumer.UID), |
| 120 | + availableServices, |
| 121 | + ) |
| 122 | + resourceMap := util.WrapStorageConsumerResourceMap(data) |
| 123 | + |
| 124 | + // For Provider Mode we supported creating one rbdClaim, generating the clientProfile, rns name, secret names |
| 125 | + // based on what we decided |
| 126 | + rbdClaimName := util.GenerateNameForCephBlockPoolSC(storageCluster) |
| 127 | + md5Sum := md5.Sum([]byte(rbdClaimName)) |
| 128 | + rbdClientProfile := hex.EncodeToString(md5Sum[:]) |
| 129 | + rbdStorageRequestHash := getStorageRequestName(string(storageConsumer.UID), rbdClaimName) |
| 130 | + rbdNodeSecretName := storageClaimCephCsiSecretName("node", rbdStorageRequestHash) |
| 131 | + rbdProvisionerSecretName := storageClaimCephCsiSecretName("provisioner", rbdStorageRequestHash) |
| 132 | + md5Sum = md5.Sum([]byte(rbdStorageRequestHash)) |
| 133 | + rnsName := fmt.Sprintf("cephradosnamespace-%s", hex.EncodeToString(md5Sum[:16])) |
| 134 | + |
| 135 | + cephFsClaimName := util.GenerateNameForCephFilesystemSC(storageCluster) |
| 136 | + md5Sum = md5.Sum([]byte(cephFsClaimName)) |
| 137 | + cephFSClientProfile := hex.EncodeToString(md5Sum[:]) |
| 138 | + cephFsStorageRequestHash := getStorageRequestName(string(storageConsumer.UID), cephFsClaimName) |
| 139 | + cephFsNodeSecretName := storageClaimCephCsiSecretName("node", cephFsStorageRequestHash) |
| 140 | + cephFsProvisionerSecretName := storageClaimCephCsiSecretName("provisioner", cephFsStorageRequestHash) |
| 141 | + md5Sum = md5.Sum([]byte(cephFsStorageRequestHash)) |
| 142 | + svgName := fmt.Sprintf("cephfilesystemsubvolumegroup-%s", hex.EncodeToString(md5Sum[:16])) |
| 143 | + |
| 144 | + resourceMap.ReplaceRbdRadosNamespaceName(rnsName) |
| 145 | + resourceMap.ReplaceSubVolumeGroupName(svgName) |
| 146 | + resourceMap.ReplaceSubVolumeGroupRadosNamespaceName("csi") |
| 147 | + resourceMap.ReplaceRbdClientProfileName(rbdClientProfile) |
| 148 | + resourceMap.ReplaceCephFsClientProfileName(cephFSClientProfile) |
| 149 | + resourceMap.ReplaceCsiRbdNodeSecretName(rbdNodeSecretName) |
| 150 | + resourceMap.ReplaceCsiRbdProvisionerSecretName(rbdProvisionerSecretName) |
| 151 | + resourceMap.ReplaceCsiCephFsNodeSecretName(cephFsNodeSecretName) |
| 152 | + resourceMap.ReplaceCsiCephFsProvisionerSecretName(cephFsProvisionerSecretName) |
| 153 | + consumerConfigMap.Data = data |
| 154 | + return nil |
| 155 | + }); err != nil { |
| 156 | + return reconcile.Result{}, err |
| 157 | + } |
| 158 | + |
| 159 | + spec := &storageConsumer.Spec |
| 160 | + spec.ResourceNameMappingConfigMap.Name = consumerConfigMap.Name |
| 161 | + spec.StorageClasses = []ocsv1alpha1.StorageClassSpec{ |
| 162 | + {Name: util.GenerateNameForCephBlockPoolSC(storageCluster)}, |
| 163 | + {Name: util.GenerateNameForCephFilesystemSC(storageCluster)}, |
| 164 | + } |
| 165 | + spec.VolumeSnapshotClasses = []ocsv1alpha1.VolumeSnapshotClassSpec{ |
| 166 | + {Name: util.GenerateNameForSnapshotClass(storageCluster.Name, util.RbdSnapshotter)}, |
| 167 | + {Name: util.GenerateNameForSnapshotClass(storageCluster.Name, util.CephfsSnapshotter)}, |
| 168 | + } |
| 169 | + spec.VolumeGroupSnapshotClasses = []ocsv1alpha1.VolumeGroupSnapshotClassSpec{ |
| 170 | + {Name: util.GenerateNameForGroupSnapshotClass(storageCluster, util.RbdGroupSnapshotter)}, |
| 171 | + {Name: util.GenerateNameForGroupSnapshotClass(storageCluster, util.CephfsGroupSnapshotter)}, |
| 172 | + } |
| 173 | + util.AddAnnotation(storageConsumer, util.Is419AdjustedAnnotationKey, "") |
| 174 | + |
| 175 | + if err := r.Client.Update(ctx, storageConsumer); err != nil { |
| 176 | + return reconcile.Result{}, err |
| 177 | + } |
| 178 | + |
| 179 | + return reconcile.Result{}, nil |
| 180 | +} |
| 181 | + |
| 182 | +// getStorageRequestHash generates a hash for a StorageRequest based |
| 183 | +// on the MD5 hash of the StorageClaim name and storageConsumer UUID. |
| 184 | +func getStorageRequestHash(consumerUUID, storageClaimName string) string { |
| 185 | + s := struct { |
| 186 | + StorageConsumerUUID string `json:"storageConsumerUUID"` |
| 187 | + StorageClaimName string `json:"storageClaimName"` |
| 188 | + }{ |
| 189 | + consumerUUID, |
| 190 | + storageClaimName, |
| 191 | + } |
| 192 | + |
| 193 | + requestName, err := json.Marshal(s) |
| 194 | + if err != nil { |
| 195 | + panic("failed to marshal storage class request name") |
| 196 | + } |
| 197 | + md5Sum := md5.Sum(requestName) |
| 198 | + return hex.EncodeToString(md5Sum[:16]) |
| 199 | +} |
| 200 | + |
| 201 | +// getStorageRequestName generates a name for a StorageRequest resource. |
| 202 | +func getStorageRequestName(consumerUUID, storageClaimName string) string { |
| 203 | + return fmt.Sprintf("storagerequest-%s", getStorageRequestHash(consumerUUID, storageClaimName)) |
| 204 | +} |
| 205 | + |
| 206 | +func getMD5Hash(text string) string { |
| 207 | + hash := md5.Sum([]byte(text)) |
| 208 | + return hex.EncodeToString(hash[:]) |
| 209 | +} |
| 210 | + |
| 211 | +func storageClaimCephCsiSecretName(secretType, suffix string) string { |
| 212 | + return fmt.Sprintf("ceph-client-%s-%s", secretType, suffix) |
| 213 | +} |
0 commit comments