/
cephobjectstores.go
251 lines (232 loc) · 10.3 KB
/
cephobjectstores.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
package storagecluster
import (
"context"
"fmt"
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
"github.com/red-hat-storage/ocs-operator/v4/controllers/defaults"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
type ocsCephObjectStores struct{}
// ensureCreated ensures that CephObjectStore resources exist in the desired
// state.
func (obj *ocsCephObjectStores) ensureCreated(r *StorageClusterReconciler, instance *ocsv1.StorageCluster) (reconcile.Result, error) {
reconcileStrategy := ReconcileStrategy(instance.Spec.ManagedResources.CephObjectStores.ReconcileStrategy)
if reconcileStrategy == ReconcileStrategyIgnore {
return reconcile.Result{}, nil
}
skip, err := r.PlatformsShouldSkipObjectStore()
if err != nil {
return reconcile.Result{}, err
}
if skip {
platform, err := r.platform.GetPlatform(r.Client)
if err != nil {
return reconcile.Result{}, err
}
r.Log.Info("Platform is set to skip object store. Not creating a CephObjectStore.", "Platform", platform)
return reconcile.Result{}, nil
}
var cephObjectStores []*cephv1.CephObjectStore
// Add KMS details to cephObjectStores spec, only if
// cluster-wide encryption is enabled
// ie, sc.Spec.Encryption.ClusterWide/sc.Spec.Encryption.Enable is True
// and KMS ConfigMap is available
if instance.Spec.Encryption.Enable || instance.Spec.Encryption.ClusterWide {
kmsConfigMap, err := getKMSConfigMap(KMSConfigMapName, instance, r.Client)
if err != nil {
r.Log.Error(err, "Failed to procure KMS ConfigMap.", "KMSConfigMap", klog.KRef(instance.Namespace, KMSConfigMapName))
return reconcile.Result{}, err
}
if kmsConfigMap != nil {
if err = reachKMSProvider(kmsConfigMap); err != nil {
r.Log.Error(err, "Address provided in KMS ConfigMap is not reachable.", "KMSConfigMap", klog.KRef(kmsConfigMap.Namespace, kmsConfigMap.Name))
return reconcile.Result{}, err
}
}
cephObjectStores, err = r.newCephObjectStoreInstances(instance, kmsConfigMap)
if err != nil {
return reconcile.Result{}, err
}
} else {
cephObjectStores, err = r.newCephObjectStoreInstances(instance, nil)
if err != nil {
return reconcile.Result{}, err
}
}
err = r.createCephObjectStores(cephObjectStores, instance)
if err != nil {
r.Log.Error(err, "Unable to create CephObjectStores for StorageCluster.", "StorageCluster", klog.KRef(instance.Namespace, instance.Name))
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
// ensureDeleted deletes the CephObjectStores owned by the StorageCluster
func (obj *ocsCephObjectStores) ensureDeleted(r *StorageClusterReconciler, sc *ocsv1.StorageCluster) (reconcile.Result, error) {
foundCephObjectStore := &cephv1.CephObjectStore{}
cephObjectStores, err := r.newCephObjectStoreInstances(sc, nil)
if err != nil {
return reconcile.Result{}, err
}
for _, cephObjectStore := range cephObjectStores {
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: cephObjectStore.Name, Namespace: sc.Namespace}, foundCephObjectStore)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Uninstall: CephObjectStore not found.", "CephObjectStore", klog.KRef(cephObjectStore.Namespace, cephObjectStore.Name))
continue
}
return reconcile.Result{}, fmt.Errorf("Uninstall: Unable to retrieve CephObjectStore %v: %v", cephObjectStore.Name, err)
}
if cephObjectStore.GetDeletionTimestamp().IsZero() {
r.Log.Info("Uninstall: Deleting CephObjectStore.", "CephObjectStore", klog.KRef(cephObjectStore.Namespace, cephObjectStore.Name))
err = r.Client.Delete(context.TODO(), foundCephObjectStore)
if err != nil {
r.Log.Error(err, "Uninstall: Failed to delete CephObjectStore.", klog.KRef(foundCephObjectStore.Namespace, foundCephObjectStore.Name))
return reconcile.Result{}, fmt.Errorf("uninstall: Failed to delete CephObjectStore %v: %v", foundCephObjectStore.Name, err)
}
}
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: cephObjectStore.Name, Namespace: sc.Namespace}, foundCephObjectStore)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Info("Uninstall: CephObjectStore is deleted.", "CephObjectStore", klog.KRef(cephObjectStore.Namespace, cephObjectStore.Name))
continue
}
}
r.Log.Error(err, "Uninstall: Waiting for CephObjectStore to be deleted.", "CephObjectStore", klog.KRef(cephObjectStore.Namespace, cephObjectStore.Name))
return reconcile.Result{}, fmt.Errorf("uninstall: Waiting for CephObjectStore %v to be deleted", cephObjectStore.Name)
}
return reconcile.Result{}, nil
}
// createCephObjectStore creates CephObjectStore in the desired state
func (r *StorageClusterReconciler) createCephObjectStores(cephObjectStores []*cephv1.CephObjectStore, instance *ocsv1.StorageCluster) error {
for _, cephObjectStore := range cephObjectStores {
existing := cephv1.CephObjectStore{}
err := r.Client.Get(context.TODO(), types.NamespacedName{Name: cephObjectStore.Name, Namespace: cephObjectStore.Namespace}, &existing)
switch {
case err == nil:
reconcileStrategy := ReconcileStrategy(instance.Spec.ManagedResources.CephObjectStores.ReconcileStrategy)
if reconcileStrategy == ReconcileStrategyInit {
return nil
}
if existing.DeletionTimestamp != nil {
err := fmt.Errorf("failed to restore CephObjectStore object %s because it is marked for deletion", existing.Name)
r.Log.Info("Failed to restore CephObjectStore because it is marked for deletion.", "CephObjectStore", klog.KRef(existing.Namespace, existing.Name))
return err
}
r.Log.Info("Restoring original CephObjectStore.", "CephObjectStore", klog.KRef(cephObjectStore.Namespace, cephObjectStore.Name))
existing.ObjectMeta.OwnerReferences = cephObjectStore.ObjectMeta.OwnerReferences
cephObjectStore.ObjectMeta = existing.ObjectMeta
err = r.Client.Update(context.TODO(), cephObjectStore)
if err != nil {
r.Log.Error(err, "Failed to update CephObjectStore.", "CephObjectStore", klog.KRef(cephObjectStore.Namespace, cephObjectStore.Name))
return err
}
case errors.IsNotFound(err):
r.Log.Info("Creating CephObjectStore.", "CephObjectStore", klog.KRef(cephObjectStore.Namespace, cephObjectStore.Name))
err = r.Client.Create(context.TODO(), cephObjectStore)
if err != nil {
r.Log.Error(err, "Failed to create CephObjectStore.", "CephObjectStore", klog.KRef(cephObjectStore.Namespace, cephObjectStore.Name))
return err
}
}
}
return nil
}
// newCephObjectStoreInstances returns the cephObjectStore instances that should be created
// on first run.
func (r *StorageClusterReconciler) newCephObjectStoreInstances(initData *ocsv1.StorageCluster, kmsConfigMap *corev1.ConfigMap) ([]*cephv1.CephObjectStore, error) {
ret := []*cephv1.CephObjectStore{
{
ObjectMeta: metav1.ObjectMeta{
Name: generateNameForCephObjectStore(initData),
Namespace: initData.Namespace,
},
Spec: cephv1.ObjectStoreSpec{
PreservePoolsOnDelete: false,
DataPool: cephv1.PoolSpec{
DeviceClass: generateDeviceClass(initData),
FailureDomain: initData.Status.FailureDomain,
Replicated: generateCephReplicatedSpec(initData, "data"),
},
MetadataPool: cephv1.PoolSpec{
FailureDomain: initData.Status.FailureDomain,
Replicated: generateCephReplicatedSpec(initData, "metadata"),
},
Gateway: cephv1.GatewaySpec{
Port: 80,
SecurePort: 443,
Service: &cephv1.RGWServiceSpec{
Annotations: map[string]string{
"service.beta.openshift.io/serving-cert-secret-name": fmt.Sprintf("%s-%s-%s", initData.Name, "cos", cephRgwTLSSecretKey),
},
},
Instances: int32(getCephObjectStoreGatewayInstances(initData)),
Placement: getPlacement(initData, "rgw"),
Resources: defaults.GetProfileDaemonResources("rgw", initData),
// set PriorityClassName for the rgw pods
PriorityClassName: openshiftUserCritical,
Labels: cephv1.Labels{defaults.ODFResourceProfileKey: initData.Spec.ResourceProfile},
},
},
},
}
for _, obj := range ret {
err := controllerutil.SetControllerReference(initData, obj, r.Scheme)
if err != nil {
r.Log.Error(err, "Failed to set ControllerReference for CephObjectStore.", "CephObjectStore", klog.KRef(obj.Namespace, obj.Name))
return nil, err
}
if initData.Spec.ManagedResources.CephObjectStores.HostNetwork != nil {
obj.Spec.Gateway.HostNetwork = initData.Spec.ManagedResources.CephObjectStores.HostNetwork
}
// if kmsConfig is not 'nil', add the KMS details to ObjectStore spec
if kmsConfigMap != nil {
// skip if KMS_PROVIDER is ibmkeyprotect or VAULT_AUTH_METHOD is kubernetes, not supported for RGW
if kmsConfigMap.Data["KMS_PROVIDER"] == IbmKeyProtectKMSProvider ||
kmsConfigMap.Data["KMS_PROVIDER"] == ThalesKMSProvider ||
kmsConfigMap.Data["VAULT_AUTH_METHOD"] == VaultSAAuthMethod {
r.Log.Info("IBMKeyProtect/Thales as KMS provider or Vault authentication via Service Account is unsupported configuration for RGW KMS, hence skipping")
continue
}
// Set default KMS_PROVIDER and VAULT_SECRET_ENGINE values, refer https://issues.redhat.com/browse/RHSTOR-1963
if _, ok := kmsConfigMap.Data["KMS_PROVIDER"]; !ok {
kmsConfigMap.Data["KMS_PROVIDER"] = VaultKMSProvider
}
rgwConnDetails := make(map[string]string)
for key, value := range kmsConfigMap.Data {
// ignore kv engine related options
if key == "VAULT_BACKEND_PATH" || key == "VAULT_BACKEND" {
continue
}
rgwConnDetails[key] = value
}
// overwrite SecretEngine value to transit
rgwConnDetails["VAULT_SECRET_ENGINE"] = "transit"
obj.Spec.Security = &cephv1.ObjectStoreSecuritySpec{
SecuritySpec: cephv1.SecuritySpec{
KeyManagementService: cephv1.KeyManagementServiceSpec{
ConnectionDetails: rgwConnDetails,
TokenSecretName: KMSTokenSecretName,
},
},
}
}
}
return ret, nil
}
func getCephObjectStoreGatewayInstances(sc *ocsv1.StorageCluster) int {
if arbiterEnabled(sc) {
return defaults.ArbiterCephObjectStoreGatewayInstances
}
customGatewayInstances := sc.Spec.ManagedResources.CephObjectStores.GatewayInstances
if customGatewayInstances != 0 {
return customGatewayInstances
}
return defaults.CephObjectStoreGatewayInstances
}