forked from red-hat-storage/ocs-operator
/
storageclasses.go
282 lines (261 loc) · 11.7 KB
/
storageclasses.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
package storagecluster
import (
"context"
"fmt"
"reflect"
"strings"
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v1"
"github.com/red-hat-storage/ocs-operator/controllers/util"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
)
// StorageClassConfiguration provides configuration options for a StorageClass.
type StorageClassConfiguration struct {
storageClass *storagev1.StorageClass
reconcileStrategy ReconcileStrategy
disable bool
isClusterExternal bool
}
type ocsStorageClass struct{}
// ensureCreated ensures that StorageClass resources exist in the desired
// state.
func (obj *ocsStorageClass) ensureCreated(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) error {
scs, err := r.newStorageClassConfigurations(instance)
if err != nil {
return err
}
err = r.createStorageClasses(scs)
if err != nil {
return err
}
return nil
}
// ensureDeleted deletes the storageClasses that the ocs-operator created
func (obj *ocsStorageClass) ensureDeleted(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) error {
sccs, err := r.newStorageClassConfigurations(instance)
if err != nil {
r.Log.Error(err, "Uninstall: Unable to determine the StorageClass names.") //nolint:gosimple
return nil
}
for _, scc := range sccs {
sc := scc.storageClass
existing := storagev1.StorageClass{}
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: sc.Name, Namespace: sc.Namespace}, &existing)
switch {
case err == nil:
if existing.DeletionTimestamp != nil {
r.Log.Info("Uninstall: StorageClass is already marked for deletion.", "StorageClass", klog.KRef(sc.Namespace, existing.Name))
break
}
r.Log.Info("Uninstall: Deleting StorageClass.", "StorageClass", klog.KRef(sc.Namespace, existing.Name))
existing.ObjectMeta.OwnerReferences = sc.ObjectMeta.OwnerReferences
sc.ObjectMeta = existing.ObjectMeta
err = r.Client.Delete(context.TODO(), sc)
if err != nil {
r.Log.Error(err, "Uninstall: Ignoring error deleting the StorageClass.", "StorageClass", klog.KRef(sc.Namespace, existing.Name))
}
case errors.IsNotFound(err):
r.Log.Info("Uninstall: StorageClass not found, nothing to do.", "StorageClass", klog.KRef(sc.Namespace, existing.Name))
default:
r.Log.Error(err, "Uninstall: Error while getting StorageClass.", "StorageClass", klog.KRef(sc.Namespace, existing.Name))
}
}
return nil
}
func (r *StorageClusterReconciler) createStorageClasses(sccs []StorageClassConfiguration) error {
var skippedSC []string
for _, scc := range sccs {
if scc.reconcileStrategy == ReconcileStrategyIgnore || scc.disable {
continue
}
sc := scc.storageClass
if strings.Contains(sc.Name, "-ceph-rbd") && !scc.isClusterExternal {
// wait for CephBlockPool to be ready
cephBlockPool := cephv1.CephBlockPool{}
key := types.NamespacedName{Name: sc.Parameters["pool"], Namespace: sc.Parameters["clusterID"]}
err := r.Client.Get(context.TODO(), key, &cephBlockPool)
if err != nil || cephBlockPool.Status == nil || cephBlockPool.Status.Phase != cephv1.ConditionType(util.PhaseReady) {
r.Log.Info("Waiting for CephBlockPool to be Ready. Skip reconciling StorageClass",
"CephBlockPool", klog.KRef(key.Name, key.Namespace),
"StorageClass", klog.KRef("", sc.Name),
)
skippedSC = append(skippedSC, sc.Name)
continue
}
} else if strings.Contains(sc.Name, "-cephfs") && !scc.isClusterExternal {
// wait for CephFilesystem to be ready
cephFilesystem := cephv1.CephFilesystem{}
key := types.NamespacedName{Name: sc.Parameters["fsName"], Namespace: sc.Parameters["clusterID"]}
err := r.Client.Get(context.TODO(), key, &cephFilesystem)
if err != nil || cephFilesystem.Status == nil || cephFilesystem.Status.Phase != cephv1.ConditionType(util.PhaseReady) {
r.Log.Info("Waiting for CephFilesystem to be Ready. Skip reconciling StorageClass",
"CephFilesystem", klog.KRef(key.Name, key.Namespace),
"StorageClass", klog.KRef("", sc.Name),
)
skippedSC = append(skippedSC, sc.Name)
continue
}
}
existing := &storagev1.StorageClass{}
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: sc.Name, Namespace: sc.Namespace}, existing)
if errors.IsNotFound(err) {
// Since the StorageClass is not found, we will create a new one
r.Log.Info("Creating StorageClass.", "StorageClass", klog.KRef(sc.Namespace, existing.Name))
err = r.Client.Create(context.TODO(), sc)
if err != nil {
return err
}
} else if err != nil {
return err
} else {
if scc.reconcileStrategy == ReconcileStrategyInit {
continue
}
if existing.DeletionTimestamp != nil {
return fmt.Errorf("failed to restore StorageClass %s because it is marked for deletion", existing.Name)
}
if !reflect.DeepEqual(sc.Parameters, existing.Parameters) {
// Since we have to update the existing StorageClass
// So, we will delete the existing storageclass and create a new one
r.Log.Info("StorageClass needs to be updated, deleting it.", "StorageClass", klog.KRef(sc.Namespace, existing.Name))
err = r.Client.Delete(context.TODO(), existing)
if err != nil {
r.Log.Error(err, "Failed to delete StorageClass.", "StorageClass", klog.KRef(sc.Namespace, existing.Name))
return err
}
r.Log.Info("Creating StorageClass.", "StorageClass", klog.KRef(sc.Namespace, sc.Name))
err = r.Client.Create(context.TODO(), sc)
if err != nil {
r.Log.Info("Failed to create StorageClass.", "StorageClass", klog.KRef(sc.Namespace, sc.Name))
return err
}
}
}
}
if len(skippedSC) > 0 {
return fmt.Errorf("some StorageClasses [%s] were skipped while waiting for pre-requisites to be met", strings.Join(skippedSC, ","))
}
return nil
}
// newCephFilesystemStorageClassConfiguration generates configuration options for a Ceph Filesystem StorageClass.
func newCephFilesystemStorageClassConfiguration(initData *ocsv1.StorageCluster) StorageClassConfiguration {
persistentVolumeReclaimDelete := corev1.PersistentVolumeReclaimDelete
allowVolumeExpansion := true
managementSpec := initData.Spec.ManagedResources.CephFilesystems
return StorageClassConfiguration{
storageClass: &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: generateNameForCephFilesystemSC(initData),
Annotations: map[string]string{
"description": "Provides RWO and RWX Filesystem volumes",
},
},
Provisioner: fmt.Sprintf("%s.cephfs.csi.ceph.com", initData.Namespace),
ReclaimPolicy: &persistentVolumeReclaimDelete,
// AllowVolumeExpansion is set to true to enable expansion of OCS backed Volumes
AllowVolumeExpansion: &allowVolumeExpansion,
Parameters: map[string]string{
"clusterID": initData.Namespace,
"fsName": fmt.Sprintf("%s-cephfilesystem", initData.Name),
"csi.storage.k8s.io/provisioner-secret-name": "rook-csi-cephfs-provisioner",
"csi.storage.k8s.io/provisioner-secret-namespace": initData.Namespace,
"csi.storage.k8s.io/node-stage-secret-name": "rook-csi-cephfs-node",
"csi.storage.k8s.io/node-stage-secret-namespace": initData.Namespace,
"csi.storage.k8s.io/controller-expand-secret-name": "rook-csi-cephfs-provisioner",
"csi.storage.k8s.io/controller-expand-secret-namespace": initData.Namespace,
},
},
reconcileStrategy: ReconcileStrategy(managementSpec.ReconcileStrategy),
disable: managementSpec.DisableStorageClass,
isClusterExternal: initData.Spec.ExternalStorage.Enable,
}
}
// newCephBlockPoolStorageClassConfiguration generates configuration options for a Ceph Block Pool StorageClass.
func newCephBlockPoolStorageClassConfiguration(initData *ocsv1.StorageCluster, data map[string]string) StorageClassConfiguration {
persistentVolumeReclaimDelete := corev1.PersistentVolumeReclaimDelete
allowVolumeExpansion := true
managementSpec := initData.Spec.ManagedResources.CephBlockPools
sc := StorageClassConfiguration{
storageClass: &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: generateNameForCephBlockPoolSC(initData),
Annotations: map[string]string{
"description": "Provides RWO Filesystem volumes, and RWO and RWX Block volumes",
},
},
Provisioner: fmt.Sprintf("%s.rbd.csi.ceph.com", initData.Namespace),
ReclaimPolicy: &persistentVolumeReclaimDelete,
// AllowVolumeExpansion is set to true to enable expansion of OCS backed Volumes
AllowVolumeExpansion: &allowVolumeExpansion,
Parameters: map[string]string{
"clusterID": initData.Namespace,
"pool": generateNameForCephBlockPool(initData),
"imageFeatures": "layering",
"csi.storage.k8s.io/fstype": "ext4",
"imageFormat": "2",
"csi.storage.k8s.io/provisioner-secret-name": "rook-csi-rbd-provisioner",
"csi.storage.k8s.io/provisioner-secret-namespace": initData.Namespace,
"csi.storage.k8s.io/node-stage-secret-name": "rook-csi-rbd-node",
"csi.storage.k8s.io/node-stage-secret-namespace": initData.Namespace,
"csi.storage.k8s.io/controller-expand-secret-name": "rook-csi-rbd-provisioner",
"csi.storage.k8s.io/controller-expand-secret-namespace": initData.Namespace,
},
},
reconcileStrategy: ReconcileStrategy(managementSpec.ReconcileStrategy),
disable: managementSpec.DisableStorageClass,
isClusterExternal: initData.Spec.ExternalStorage.Enable,
}
// In case of erasure coded, parameter `dataPool` is not empty. The parameter`dataPool` is the name of pool and `pool` is the name erasure coded pool
if data["dataPool"] != "" {
sc.storageClass.Parameters["pool"] = generateNameForErasureCodedCephBlockPool(initData)
sc.storageClass.Parameters["dataPool"] = generateNameForCephBlockPool(initData)
}
return sc
}
// newCephOBCStorageClassConfiguration generates configuration options for a Ceph Object Store StorageClass.
func newCephOBCStorageClassConfiguration(initData *ocsv1.StorageCluster) StorageClassConfiguration {
reclaimPolicy := corev1.PersistentVolumeReclaimDelete
managementSpec := initData.Spec.ManagedResources.CephObjectStores
return StorageClassConfiguration{
storageClass: &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: generateNameForCephRgwSC(initData),
Annotations: map[string]string{
"description": "Provides Object Bucket Claims (OBCs)",
},
},
Provisioner: fmt.Sprintf("%s.ceph.rook.io/bucket", initData.Namespace),
ReclaimPolicy: &reclaimPolicy,
Parameters: map[string]string{
"objectStoreNamespace": initData.Namespace,
"region": "us-east-1",
"objectStoreName": generateNameForCephObjectStore(initData),
},
},
reconcileStrategy: ReconcileStrategy(managementSpec.ReconcileStrategy),
disable: managementSpec.DisableStorageClass,
isClusterExternal: initData.Spec.ExternalStorage.Enable,
}
}
// newStorageClassConfigurations returns the StorageClassConfiguration instances that should be created
// on first run.
func (r *StorageClusterReconciler) newStorageClassConfigurations(initData *ocsv1.StorageCluster) ([]StorageClassConfiguration, error) {
ret := []StorageClassConfiguration{
newCephFilesystemStorageClassConfiguration(initData),
newCephBlockPoolStorageClassConfiguration(initData, map[string]string{}),
}
// OBC storageclass will be returned only in TWO conditions,
// a. either 'externalStorage' is enabled
// OR
// b. current platform is not a cloud-based platform
skip, err := r.PlatformsShouldSkipObjectStore()
if initData.Spec.ExternalStorage.Enable || err == nil && !skip {
ret = append(ret, newCephOBCStorageClassConfiguration(initData))
}
return ret, nil
}