/
cephmoncluster_controller.go
309 lines (249 loc) · 9.33 KB
/
cephmoncluster_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
package cephmoncluster
import (
"context"
cephv1alpha1 "github.com/PolarGeospatialCenter/ceph-operator/pkg/apis/ceph/v1alpha1"
"github.com/PolarGeospatialCenter/ceph-operator/pkg/controller/common"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"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"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("controller_cephmoncluster")
// Add creates a new CephMonCluster 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) error {
return add(mgr, newReconciler(mgr))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileCephMonCluster{client: mgr.GetClient(), scheme: mgr.GetScheme()}
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("cephmoncluster-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to primary resource CephMonCluster
err = c.Watch(&source.Kind{Type: &cephv1alpha1.CephMonCluster{}}, &handler.EnqueueRequestForObject{})
if err != nil {
return err
}
// TODO(user): Modify this to be the types you create that are owned by the primary resource
// Watch for changes to secondary resource Pods and requeue the owner CephMonCluster
err = c.Watch(&source.Kind{Type: &cephv1alpha1.CephMon{}}, &handler.EnqueueRequestsFromMapFunc{
ToRequests: &MonEventMapper{},
})
if err != nil {
return err
}
err = c.Watch(&source.Kind{Type: &cephv1alpha1.CephCluster{}}, &handler.EnqueueRequestsFromMapFunc{
ToRequests: &common.CephClusterEventMapper{Client: mgr.GetClient(), Scheme: mgr.GetScheme(),
ApiVersion: cephv1alpha1.SchemeGroupVersion.String(), Kind: "CephMonCluster"},
})
if err != nil {
return err
}
return nil
}
var _ reconcile.Reconciler = &ReconcileCephMonCluster{}
// ReconcileCephMonCluster reconciles a CephMonCluster object
type ReconcileCephMonCluster struct {
// This client, initialized using mgr.Client() above, is a split client
// that reads objects from the cache and writes to the apiserver
client client.Client
scheme *runtime.Scheme
}
// Reconcile reads that state of the cluster for a CephMonCluster object and makes changes based on the state read
// and what is in the CephMonCluster.Spec
// Note:
// 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 *ReconcileCephMonCluster) Reconcile(request reconcile.Request) (reconcile.Result, error) {
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling CephMonCluster")
// Fetch the CephMonCluster instance
instance := &cephv1alpha1.CephMonCluster{}
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
cephCluster, err := r.getCephCluster(instance)
if err != nil {
return reconcile.Result{}, err
}
if !cephCluster.GetDaemonEnabled(cephv1alpha1.CephDaemonTypeMon) &&
!instance.CheckMonClusterState(cephv1alpha1.MonClusterIdle, cephv1alpha1.MonClusterLostQuorum) {
instance.SetMonClusterState(cephv1alpha1.MonClusterLostQuorum)
return r.updateAndRequeue(instance)
}
if !cephCluster.GetDaemonEnabled(cephv1alpha1.CephDaemonTypeMon) &&
instance.CheckMonClusterState(cephv1alpha1.MonClusterIdle) {
return reconcile.Result{}, nil
}
fullMonMap, err := r.getMonMap(instance)
if err != nil {
return reconcile.Result{}, err
}
monMap := fullMonMap.GetInitalMonMap()
switch instance.GetMonClusterState() {
case cephv1alpha1.MonClusterIdle:
for _, k := range []Keyring{MON_KEYRING, CLIENT_ADMIN_KEYRING} {
err := r.generateKeyringSecret(k, instance.GetNamespace(), instance.Spec.ClusterName)
if err != nil {
return reconcile.Result{}, err
}
}
if fullMonMap.Empty() {
return reconcile.Result{}, nil
}
cm, err := instance.GetMonMapConfigMap(monMap)
cm.Namespace = instance.Namespace
if err != nil {
return reconcile.Result{}, err
}
_, err = r.createOrUpdate(cm)
if err != nil {
return reconcile.Result{}, err
}
instance.SetMonClusterState(cephv1alpha1.MonClusterLaunching)
instance.Status.StartEpoch++
_, err = r.updateAndRequeue(instance)
if err != nil {
return reconcile.Result{}, err
}
if fullMonMap.CountInitalMembers() > 0 {
return reconcile.Result{}, nil
}
initialMonNamespacedName := fullMonMap.GetRandomEntry().NamespacedName
initialMon := &cephv1alpha1.CephMon{}
err = r.client.Get(context.TODO(), initialMonNamespacedName, initialMon)
if err != nil {
return reconcile.Result{}, err
}
initialMon.Status.InitalMember = true
return r.updateAndRequeue(initialMon)
case cephv1alpha1.MonClusterLaunching:
if monMap.QuorumAtEpoch(instance.Status.StartEpoch) {
cm, err := instance.GetMonMapConfigMap(monMap)
cm.Namespace = instance.Namespace
if err != nil {
return reconcile.Result{}, err
}
_, err = r.createOrUpdate(cm)
if err != nil {
return reconcile.Result{}, err
}
instance.SetMonClusterState(cephv1alpha1.MonClusterEstablishingQuorum)
return r.updateAndRequeue(instance)
}
return reconcile.Result{}, nil
case cephv1alpha1.MonClusterEstablishingQuorum:
totalInQuorum := monMap.CountInState(cephv1alpha1.MonInQuorum)
if totalInQuorum >= monMap.QuorumCount() {
instance.SetMonClusterState(cephv1alpha1.MonClusterInQuorum)
return r.updateAndRequeue(instance)
}
totalInIdle := monMap.CountInState(cephv1alpha1.MonIdle)
if totalInIdle >= monMap.QuorumCount() {
instance.SetMonClusterState(cephv1alpha1.MonClusterLostQuorum)
return r.updateAndRequeue(instance)
}
return reconcile.Result{}, nil
case cephv1alpha1.MonClusterInQuorum:
totalInQuorum := monMap.CountInState(cephv1alpha1.MonInQuorum)
if totalInQuorum < monMap.QuorumCount() {
instance.SetMonClusterState(cephv1alpha1.MonClusterLostQuorum)
return r.updateAndRequeue(instance)
}
return reconcile.Result{}, nil
case cephv1alpha1.MonClusterLostQuorum:
if monMap.AllInState(cephv1alpha1.MonIdle) {
instance.SetMonClusterState(cephv1alpha1.MonClusterIdle)
return r.updateAndRequeue(instance)
}
return reconcile.Result{}, nil
default:
instance.SetMonClusterState(cephv1alpha1.MonClusterLostQuorum)
return r.updateAndRequeue(instance)
}
}
func (r *ReconcileCephMonCluster) getMonMap(instance *cephv1alpha1.CephMonCluster) (cephv1alpha1.MonMap, error) {
monitors := &cephv1alpha1.CephMonList{}
listOptions := &client.ListOptions{}
listOptions.MatchingLabels(map[string]string{cephv1alpha1.ClusterNameLabel: instance.Spec.ClusterName})
err := r.client.List(context.TODO(), listOptions, monitors)
if err != nil {
return nil, err
}
monMap := make(cephv1alpha1.MonMap, len(monitors.Items))
for _, mon := range monitors.Items {
monMap[mon.Spec.ID] = mon.GetMonMapEntry()
}
return monMap, nil
}
func (r *ReconcileCephMonCluster) updateAndRequeue(object runtime.Object) (reconcile.Result, error) {
err := r.client.Update(context.TODO(), object)
if err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
func (r *ReconcileCephMonCluster) createOrUpdate(object runtime.Object) (reconcile.Result, error) {
err := r.client.Create(context.TODO(), object)
if !errors.IsAlreadyExists(err) {
return reconcile.Result{}, err
}
err = r.client.Update(context.TODO(), object)
if err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
func (r *ReconcileCephMonCluster) generateKeyringSecret(keyring Keyring, namespace, clusterName string) error {
secret := &corev1.Secret{}
secretNamespacedName := &types.NamespacedName{
Namespace: namespace,
Name: keyring.GetSecretName(clusterName),
}
err := r.client.Get(context.TODO(), *secretNamespacedName, secret)
if err != nil && !errors.IsNotFound(err) {
return err
}
if errors.IsNotFound(err) {
err = keyring.GenerateKey()
if err != nil {
return err
}
secret = keyring.GetSecret(clusterName)
secret.Namespace = namespace
err = r.client.Create(context.TODO(), secret)
if err != nil {
return err
}
}
return nil
}
func (r *ReconcileCephMonCluster) getCephCluster(d *cephv1alpha1.CephMonCluster) (*cephv1alpha1.CephCluster, error) {
cephCluster := &cephv1alpha1.CephCluster{}
cephClusterNamespacedName := types.NamespacedName{
Name: d.GetCephClusterName(),
Namespace: d.GetNamespace(),
}
return cephCluster, r.client.Get(context.TODO(), cephClusterNamespacedName, cephCluster)
}