/
controller.go
428 lines (367 loc) · 17.3 KB
/
controller.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
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package pool to manage a rook pool.
package pool
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/coreos/pkg/capnslog"
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
"github.com/pkg/errors"
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
"github.com/rook/rook/pkg/operator/ceph/config"
opcontroller "github.com/rook/rook/pkg/operator/ceph/controller"
"github.com/rook/rook/pkg/operator/ceph/csi/peermap"
"github.com/rook/rook/pkg/operator/k8sutil"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
poolApplicationNameRBD = "rbd"
controllerName = "ceph-block-pool-controller"
)
var logger = capnslog.NewPackageLogger("github.com/rook/rook", controllerName)
var cephBlockPoolKind = reflect.TypeOf(cephv1.CephBlockPool{}).Name()
// Sets the type meta for the controller main object
var controllerTypeMeta = metav1.TypeMeta{
Kind: cephBlockPoolKind,
APIVersion: fmt.Sprintf("%s/%s", cephv1.CustomResourceGroup, cephv1.Version),
}
var _ reconcile.Reconciler = &ReconcileCephBlockPool{}
// ReconcileCephBlockPool reconciles a CephBlockPool object
type ReconcileCephBlockPool struct {
client client.Client
scheme *runtime.Scheme
context *clusterd.Context
clusterInfo *cephclient.ClusterInfo
blockPoolContexts map[string]*blockPoolHealth
opManagerContext context.Context
}
type blockPoolHealth struct {
internalCtx context.Context
internalCancel context.CancelFunc
started bool
}
// Add creates a new CephBlockPool Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager, context *clusterd.Context, opManagerContext context.Context, opConfig opcontroller.OperatorConfig) error {
return add(opManagerContext, mgr, newReconciler(mgr, context, opManagerContext))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, context *clusterd.Context, opManagerContext context.Context) reconcile.Reconciler {
return &ReconcileCephBlockPool{
client: mgr.GetClient(),
scheme: mgr.GetScheme(),
context: context,
blockPoolContexts: make(map[string]*blockPoolHealth),
opManagerContext: opManagerContext,
}
}
func add(opManagerContext context.Context, mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
logger.Info("successfully started")
// Watch for changes on the CephBlockPool CRD object
err = c.Watch(&source.Kind{Type: &cephv1.CephBlockPool{TypeMeta: controllerTypeMeta}}, &handler.EnqueueRequestForObject{}, opcontroller.WatchControllerPredicate())
if err != nil {
return err
}
// Build Handler function to return the list of ceph block pool
// This is used by the watchers below
handlerFunc, err := opcontroller.ObjectToCRMapper(opManagerContext, mgr.GetClient(), &cephv1.CephBlockPoolList{}, mgr.GetScheme())
if err != nil {
return err
}
// Watch for ConfigMap "rook-ceph-mon-endpoints" update and reconcile, which will reconcile update the bootstrap peer token
err = c.Watch(&source.Kind{Type: &corev1.ConfigMap{TypeMeta: metav1.TypeMeta{Kind: "ConfigMap", APIVersion: corev1.SchemeGroupVersion.String()}}}, handler.EnqueueRequestsFromMapFunc(handlerFunc), mon.PredicateMonEndpointChanges())
if err != nil {
return err
}
return nil
}
// Reconcile reads that state of the cluster for a CephBlockPool object and makes changes based on the state read
// and what is in the CephBlockPool.Spec
// 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 *ReconcileCephBlockPool) Reconcile(context context.Context, request reconcile.Request) (reconcile.Result, error) {
// workaround because the rook logging mechanism is not compatible with the controller-runtime logging interface
reconcileResponse, err := r.reconcile(request)
if err != nil {
logger.Errorf("failed to reconcile. %v", err)
}
return reconcileResponse, err
}
func (r *ReconcileCephBlockPool) reconcile(request reconcile.Request) (reconcile.Result, error) {
// Fetch the CephBlockPool instance
cephBlockPool := &cephv1.CephBlockPool{}
err := r.client.Get(r.opManagerContext, request.NamespacedName, cephBlockPool)
if err != nil {
if kerrors.IsNotFound(err) {
logger.Debug("CephBlockPool resource not found. Ignoring since object must be deleted.")
// If there was a previous error or if a user removed this resource's finalizer, it's
// possible Rook didn't clean up the monitoring routine for this resource. Ensure the
// routine is stopped when we see the resource is gone.
r.cancelMirrorMonitoring(cephBlockPool)
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return opcontroller.ImmediateRetryResult, errors.Wrap(err, "failed to get CephBlockPool")
}
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephBlockPool)
if err != nil {
return opcontroller.ImmediateRetryResult, errors.Wrap(err, "failed to add finalizer")
}
// The CR was just created, initializing status fields
if cephBlockPool.Status == nil {
updateStatus(r.client, request.NamespacedName, cephv1.ConditionProgressing, nil)
}
// Make sure a CephCluster is present otherwise do nothing
cephCluster, isReadyToReconcile, cephClusterExists, reconcileResponse := opcontroller.IsReadyToReconcile(r.opManagerContext, r.client, request.NamespacedName, controllerName)
if !isReadyToReconcile {
// This handles the case where the Ceph Cluster is gone and we want to delete that CR
// We skip the deletePool() function since everything is gone already
//
// Also, only remove the finalizer if the CephCluster is gone
// If not, we should wait for it to be ready
// This handles the case where the operator is not ready to accept Ceph command but the cluster exists
if !cephBlockPool.GetDeletionTimestamp().IsZero() && !cephClusterExists {
// don't leak the health checker routine if we are force-deleting
r.cancelMirrorMonitoring(cephBlockPool)
// Remove finalizer
err = opcontroller.RemoveFinalizer(r.opManagerContext, r.client, cephBlockPool)
if err != nil {
return opcontroller.ImmediateRetryResult, errors.Wrap(err, "failed to remove finalizer")
}
// Return and do not requeue. Successful deletion.
return reconcile.Result{}, nil
}
return reconcileResponse, nil
}
// Populate clusterInfo during each reconcile
clusterInfo, _, _, err := mon.LoadClusterInfo(r.context, r.opManagerContext, request.NamespacedName.Namespace)
if err != nil {
return opcontroller.ImmediateRetryResult, errors.Wrap(err, "failed to populate cluster info")
}
r.clusterInfo = clusterInfo
r.clusterInfo.NetworkSpec = cephCluster.Spec.Network
// Initialize the channel for this pool
// This allows us to track multiple CephBlockPool in the same namespace
blockPoolChannelKey := fmt.Sprintf("%s-%s", cephBlockPool.Namespace, cephBlockPool.Name)
_, blockPoolContextsExists := r.blockPoolContexts[blockPoolChannelKey]
if !blockPoolContextsExists {
internalCtx, internalCancel := context.WithCancel(r.opManagerContext)
r.blockPoolContexts[blockPoolChannelKey] = &blockPoolHealth{
internalCtx: internalCtx,
internalCancel: internalCancel,
}
}
// DELETE: the CR was deleted
if !cephBlockPool.GetDeletionTimestamp().IsZero() {
// If the ceph block pool is still in the map, we must remove it during CR deletion
// We must remove it first otherwise the checker will panic since the status/info will be nil
r.cancelMirrorMonitoring(cephBlockPool)
logger.Infof("deleting pool %q", cephBlockPool.Name)
err := deletePool(r.context, clusterInfo, cephBlockPool)
if err != nil {
return opcontroller.ImmediateRetryResult, errors.Wrapf(err, "failed to delete pool %q. ", cephBlockPool.Name)
}
// disable RBD stats collection if cephBlockPool was deleted
if err := configureRBDStats(r.context, clusterInfo); err != nil {
logger.Errorf("failed to disable stats collection for pool(s). %v", err)
}
// Remove finalizer
err = opcontroller.RemoveFinalizer(r.opManagerContext, r.client, cephBlockPool)
if err != nil {
return opcontroller.ImmediateRetryResult, errors.Wrap(err, "failed to remove finalizer")
}
// Return and do not requeue. Successful deletion.
return reconcile.Result{}, nil
}
// validate the pool settings
if err := ValidatePool(r.context, clusterInfo, &cephCluster.Spec, cephBlockPool); err != nil {
if strings.Contains(err.Error(), opcontroller.UninitializedCephConfigError) {
logger.Info(opcontroller.OperatorNotInitializedMessage)
return opcontroller.WaitForRequeueIfOperatorNotInitialized, nil
}
return opcontroller.ImmediateRetryResult, errors.Wrapf(err, "invalid pool CR %q spec", cephBlockPool.Name)
}
// Get CephCluster version
cephVersion, err := opcontroller.GetImageVersion(cephCluster)
if err != nil {
return opcontroller.ImmediateRetryResult, errors.Wrapf(err, "failed to fetch ceph version from cephcluster %q", cephCluster.Name)
}
r.clusterInfo.CephVersion = *cephVersion
// CREATE/UPDATE
reconcileResponse, err = r.reconcileCreatePool(clusterInfo, &cephCluster.Spec, cephBlockPool)
if err != nil {
if strings.Contains(err.Error(), opcontroller.UninitializedCephConfigError) {
logger.Info(opcontroller.OperatorNotInitializedMessage)
return opcontroller.WaitForRequeueIfOperatorNotInitialized, nil
}
updateStatus(r.client, request.NamespacedName, cephv1.ConditionFailure, nil)
return reconcileResponse, errors.Wrapf(err, "failed to create pool %q.", cephBlockPool.GetName())
}
// enable/disable RBD stats collection based on cephBlockPool spec
if err := configureRBDStats(r.context, clusterInfo); err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to enable/disable stats collection for pool(s)")
}
checker := newMirrorChecker(r.context, r.client, r.clusterInfo, request.NamespacedName, &cephBlockPool.Spec, cephBlockPool.Name)
// ADD PEERS
logger.Debug("reconciling create rbd mirror peer configuration")
if cephBlockPool.Spec.Mirroring.Enabled {
// Always create a bootstrap peer token in case another cluster wants to add us as a peer
reconcileResponse, err = opcontroller.CreateBootstrapPeerSecret(r.context, clusterInfo, cephBlockPool, k8sutil.NewOwnerInfo(cephBlockPool, r.scheme))
if err != nil {
updateStatus(r.client, request.NamespacedName, cephv1.ConditionFailure, nil)
return reconcileResponse, errors.Wrapf(err, "failed to create rbd-mirror bootstrap peer for pool %q.", cephBlockPool.GetName())
}
// Check if rbd-mirror CR and daemons are running
logger.Debug("listing rbd-mirror CR")
// Run the goroutine to update the mirroring status
if !cephBlockPool.Spec.StatusCheck.Mirror.Disabled {
// Start monitoring of the pool
if r.blockPoolContexts[blockPoolChannelKey].started {
logger.Debug("pool monitoring go routine already running!")
} else {
go checker.checkMirroring(r.blockPoolContexts[blockPoolChannelKey].internalCtx)
r.blockPoolContexts[blockPoolChannelKey].started = true
}
}
// Add bootstrap peer if any
logger.Debug("reconciling ceph bootstrap peers import")
reconcileResponse, err = r.reconcileAddBoostrapPeer(cephBlockPool, request.NamespacedName)
if err != nil {
return reconcileResponse, errors.Wrap(err, "failed to add ceph rbd mirror peer")
}
// ReconcilePoolIDMap updates the `rook-ceph-csi-mapping-config` with local and peer cluster pool ID map
err = peermap.ReconcilePoolIDMap(r.context, r.clusterInfo, cephBlockPool)
if err != nil {
return reconcileResponse, errors.Wrapf(err, "failed to update pool ID mapping config for the pool %q", cephBlockPool.Name)
}
// Set Ready status, we are done reconciling
updateStatus(r.client, request.NamespacedName, cephv1.ConditionReady, opcontroller.GenerateStatusInfo(cephBlockPool))
// If not mirrored there is no Status Info field to fulfil
} else {
// Set Ready status, we are done reconciling
updateStatus(r.client, request.NamespacedName, cephv1.ConditionReady, nil)
// Stop monitoring the mirroring status of this pool
if blockPoolContextsExists && r.blockPoolContexts[blockPoolChannelKey].started {
r.cancelMirrorMonitoring(cephBlockPool)
// Reset the MirrorHealthCheckSpec
checker.updateStatusMirroring(nil, nil, nil, "")
}
}
// Return and do not requeue
logger.Debug("done reconciling")
return reconcile.Result{}, nil
}
func (r *ReconcileCephBlockPool) reconcileCreatePool(clusterInfo *cephclient.ClusterInfo, cephCluster *cephv1.ClusterSpec, cephBlockPool *cephv1.CephBlockPool) (reconcile.Result, error) {
err := createPool(r.context, clusterInfo, cephCluster, cephBlockPool)
if err != nil {
return opcontroller.ImmediateRetryResult, errors.Wrapf(err, "failed to create pool %q.", cephBlockPool.GetName())
}
// Let's return here so that on the initial creation we don't check for update right away
return reconcile.Result{}, nil
}
// Create the pool
func createPool(context *clusterd.Context, clusterInfo *cephclient.ClusterInfo, clusterSpec *cephv1.ClusterSpec, p *cephv1.CephBlockPool) error {
// create the pool
logger.Infof("creating pool %q in namespace %q", p.Name, p.Namespace)
if err := cephclient.CreatePoolWithProfile(context, clusterInfo, clusterSpec, p.Name, p.Spec, poolApplicationNameRBD); err != nil {
return errors.Wrapf(err, "failed to create pool %q", p.Name)
}
logger.Infof("initializing pool %q", p.Name)
args := []string{"pool", "init", p.Name}
output, err := cephclient.NewRBDCommand(context, clusterInfo, args).Run()
if err != nil {
return errors.Wrapf(err, "failed to initialize pool %q. %s", p.Name, string(output))
}
logger.Infof("successfully initialized pool %q", p.Name)
return nil
}
// Delete the pool
func deletePool(context *clusterd.Context, clusterInfo *cephclient.ClusterInfo, p *cephv1.CephBlockPool) error {
pools, err := cephclient.ListPoolSummaries(context, clusterInfo)
if err != nil {
return errors.Wrap(err, "failed to list pools")
}
// Only delete the pool if it exists...
for _, pool := range pools {
if pool.Name == p.Name {
err := cephclient.DeletePool(context, clusterInfo, p.Name)
if err != nil {
return errors.Wrapf(err, "failed to delete pool %q", p.Name)
}
}
}
return nil
}
func configureRBDStats(clusterContext *clusterd.Context, clusterInfo *cephclient.ClusterInfo) error {
logger.Debug("configuring RBD per-image IO statistics collection")
namespaceListOpt := client.InNamespace(clusterInfo.Namespace)
cephBlockPoolList := &cephv1.CephBlockPoolList{}
var enableStatsForPools []string
err := clusterContext.Client.List(context.TODO(), cephBlockPoolList, namespaceListOpt)
if err != nil {
return errors.Wrap(err, "failed to retrieve list of CephBlockPool")
}
for _, cephBlockPool := range cephBlockPoolList.Items {
if cephBlockPool.GetDeletionTimestamp() == nil && cephBlockPool.Spec.EnableRBDStats {
// list of CephBlockPool with enableRBDStats set to true and not marked for deletion
enableStatsForPools = append(enableStatsForPools, cephBlockPool.Name)
}
}
logger.Debugf("RBD per-image IO statistics will be collected for pools: %v", enableStatsForPools)
monStore := config.GetMonStore(clusterContext, clusterInfo)
if len(enableStatsForPools) == 0 {
err = monStore.Delete("mgr.", "mgr/prometheus/rbd_stats_pools")
} else {
err = monStore.Set("mgr.", "mgr/prometheus/rbd_stats_pools", strings.Join(enableStatsForPools, ","))
}
if err != nil {
return errors.Wrapf(err, "failed to enable rbd_stats_pools")
}
logger.Debug("configured RBD per-image IO statistics collection")
return nil
}
func blockPoolChannelKeyName(p *cephv1.CephBlockPool) string {
return types.NamespacedName{Namespace: p.Namespace, Name: p.Name}.String()
}
// cancel mirror monitoring. This is a noop if monitoring is not running.
func (r *ReconcileCephBlockPool) cancelMirrorMonitoring(cephBlockPool *cephv1.CephBlockPool) {
channelKey := blockPoolChannelKeyName(cephBlockPool)
_, poolContextExists := r.blockPoolContexts[channelKey]
if poolContextExists {
// Cancel the context to stop the go routine
r.blockPoolContexts[channelKey].internalCancel()
// Remove ceph block pool from the map
delete(r.blockPoolContexts, channelKey)
}
}