forked from rook/rook
/
controller.go
458 lines (388 loc) · 15.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
Copyright 2019 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.
Portions of this file came from https://github.com/cockroachdb/cockroach, which uses the same license.
*/
// Package cluster to manage a edgefs cluster.
package cluster
import (
"fmt"
"reflect"
"time"
"github.com/coreos/pkg/capnslog"
opkit "github.com/rook/operator-kit"
edgefsv1 "github.com/rook/rook/pkg/apis/edgefs.rook.io/v1"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/operator/edgefs/iscsi"
"github.com/rook/rook/pkg/operator/edgefs/isgw"
"github.com/rook/rook/pkg/operator/edgefs/nfs"
"github.com/rook/rook/pkg/operator/edgefs/s3"
"github.com/rook/rook/pkg/operator/edgefs/s3x"
"github.com/rook/rook/pkg/operator/edgefs/swift"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
)
var (
logger = capnslog.NewPackageLogger("github.com/rook/rook", "edgefs-op-cluster")
finalizerName = fmt.Sprintf("%s.%s", ClusterResource.Name, ClusterResource.Group)
)
const (
CustomResourceName = "cluster"
CustomResourceNamePlural = "clusters"
appName = "rook-edgefs"
clusterCreateInterval = 6 * time.Second
clusterCreateTimeout = 5 * time.Minute
updateClusterInterval = 30 * time.Second
updateClusterTimeout = 1 * time.Hour
clusterDeleteRetryInterval = 2 //seconds
clusterDeleteMaxRetries = 15
defaultEdgefsImageName = "edgefs/edgefs:latest"
)
var ClusterResource = opkit.CustomResource{
Name: CustomResourceName,
Plural: CustomResourceNamePlural,
Group: edgefsv1.CustomResourceGroup,
Version: edgefsv1.Version,
Scope: apiextensionsv1beta1.NamespaceScoped,
Kind: reflect.TypeOf(edgefsv1.Cluster{}).Name(),
}
type ClusterController struct {
context *clusterd.Context
containerImage string
devicesInUse bool
clusterMap map[string]*cluster
}
func NewClusterController(context *clusterd.Context, containerImage string) *ClusterController {
return &ClusterController{
context: context,
containerImage: containerImage,
clusterMap: make(map[string]*cluster),
}
}
func ClusterOwnerRef(clusterName, clusterID string) metav1.OwnerReference {
blockOwner := true
return metav1.OwnerReference{
APIVersion: fmt.Sprintf("%s/%s", ClusterResource.Group, ClusterResource.Version),
Kind: ClusterResource.Kind,
Name: clusterName,
UID: types.UID(clusterID),
BlockOwnerDeletion: &blockOwner,
}
}
func (c *ClusterController) StartWatch(namespace string, stopCh chan struct{}) error {
resourceHandlerFuncs := cache.ResourceEventHandlerFuncs{
AddFunc: c.onAdd,
UpdateFunc: c.onUpdate,
DeleteFunc: c.onDelete,
}
logger.Infof("start watching edgefs clusters in all namespaces")
watcher := opkit.NewWatcher(ClusterResource, namespace, resourceHandlerFuncs, c.context.RookClientset.EdgefsV1().RESTClient())
go watcher.Watch(&edgefsv1.Cluster{}, stopCh)
return nil
}
func (c *ClusterController) StopWatch() {
for _, cluster := range c.clusterMap {
close(cluster.stopCh)
}
c.clusterMap = make(map[string]*cluster)
}
func (c *ClusterController) onAdd(obj interface{}) {
clusterObj := obj.(*edgefsv1.Cluster).DeepCopy()
logger.Infof("new cluster %s added to namespace %s", clusterObj.Name, clusterObj.Namespace)
cluster := newCluster(clusterObj, c.context)
c.clusterMap[cluster.Namespace] = cluster
//Override rook containerImage value
c.containerImage = defaultEdgefsImageName
if cluster.Spec.EdgefsImageName != "" {
c.containerImage = cluster.Spec.EdgefsImageName
}
logger.Infof("starting cluster in namespace %s", cluster.Namespace)
if c.devicesInUse && cluster.Spec.Storage.AnyUseAllDevices() {
c.updateClusterStatus(clusterObj.Namespace, clusterObj.Name, edgefsv1.ClusterStateError, "using all devices in more than one namespace is not supported")
return
}
if cluster.Spec.Storage.AnyUseAllDevices() {
c.devicesInUse = true
}
// Start the Rook cluster components. Retry several times in case of failure.
err := wait.Poll(clusterCreateInterval, clusterCreateTimeout, func() (bool, error) {
c.updateClusterStatus(clusterObj.Namespace, clusterObj.Name, edgefsv1.ClusterStateCreating, "")
err := cluster.createInstance(c.containerImage, false)
if err != nil {
logger.Errorf("failed to create cluster in namespace %s. %+v", cluster.Namespace, err)
return false, nil
}
// cluster is created, update the cluster CRD status now
c.updateClusterStatus(clusterObj.Namespace, clusterObj.Name, edgefsv1.ClusterStateCreated, "")
return true, nil
})
if err != nil {
c.updateClusterStatus(clusterObj.Namespace, clusterObj.Name, edgefsv1.ClusterStateError,
fmt.Sprintf("giving up creating cluster in namespace %s after %s", cluster.Namespace, clusterCreateTimeout))
return
}
logger.Infof("succeeded creating and initializing EdgeFS cluster in namespace %s", cluster.Namespace)
// Start NFS service CRD watcher
NFSController := nfs.NewNFSController(c.context,
cluster.Namespace,
c.containerImage,
cluster.Spec.Network,
cluster.Spec.DataDirHostPath, cluster.Spec.DataVolumeSize,
edgefsv1.GetTargetPlacement(cluster.Spec.Placement),
cluster.Spec.Resources,
cluster.Spec.ResourceProfile,
cluster.ownerRef,
cluster.Spec.UseHostLocalTime)
NFSController.StartWatch(cluster.stopCh)
// Start S3 service CRD watcher
S3Controller := s3.NewS3Controller(c.context,
cluster.Namespace,
c.containerImage,
cluster.Spec.Network,
cluster.Spec.DataDirHostPath, cluster.Spec.DataVolumeSize,
edgefsv1.GetTargetPlacement(cluster.Spec.Placement),
cluster.Spec.Resources,
cluster.Spec.ResourceProfile,
cluster.ownerRef,
cluster.Spec.UseHostLocalTime)
S3Controller.StartWatch(cluster.stopCh)
// Start SWIFT service CRD watcher
SWIFTController := swift.NewSWIFTController(c.context,
cluster.Namespace,
c.containerImage,
cluster.Spec.Network,
cluster.Spec.DataDirHostPath, cluster.Spec.DataVolumeSize,
edgefsv1.GetTargetPlacement(cluster.Spec.Placement),
cluster.Spec.Resources,
cluster.Spec.ResourceProfile,
cluster.ownerRef,
cluster.Spec.UseHostLocalTime)
SWIFTController.StartWatch(cluster.stopCh)
// Start S3X service CRD watcher
S3XController := s3x.NewS3XController(c.context,
cluster.Namespace,
c.containerImage,
cluster.Spec.Network,
cluster.Spec.DataDirHostPath, cluster.Spec.DataVolumeSize,
edgefsv1.GetTargetPlacement(cluster.Spec.Placement),
cluster.Spec.Resources,
cluster.Spec.ResourceProfile,
cluster.ownerRef,
cluster.Spec.UseHostLocalTime)
S3XController.StartWatch(cluster.stopCh)
// Start ISCSI service CRD watcher
ISCSIController := iscsi.NewISCSIController(c.context,
cluster.Namespace,
c.containerImage,
cluster.Spec.Network,
cluster.Spec.DataDirHostPath, cluster.Spec.DataVolumeSize,
edgefsv1.GetTargetPlacement(cluster.Spec.Placement),
cluster.Spec.Resources,
cluster.Spec.ResourceProfile,
cluster.ownerRef,
cluster.Spec.UseHostLocalTime)
ISCSIController.StartWatch(cluster.stopCh)
// Start ISGW service CRD watcher
ISGWController := isgw.NewISGWController(c.context,
cluster.Namespace,
c.containerImage,
cluster.Spec.Network,
cluster.Spec.DataDirHostPath, cluster.Spec.DataVolumeSize,
edgefsv1.GetTargetPlacement(cluster.Spec.Placement),
cluster.Spec.Resources,
cluster.Spec.ResourceProfile,
cluster.ownerRef,
cluster.Spec.UseHostLocalTime)
ISGWController.StartWatch(cluster.stopCh)
cluster.childControllers = []childController{
NFSController, S3Controller, S3XController, SWIFTController, ISCSIController, ISGWController,
}
// add the finalizer to the crd
err = c.addFinalizer(clusterObj)
if err != nil {
logger.Errorf("failed to add finalizer to cluster crd. %+v", err)
}
}
func (c *ClusterController) onUpdate(oldObj, newObj interface{}) {
oldCluster := oldObj.(*edgefsv1.Cluster).DeepCopy()
newCluster := newObj.(*edgefsv1.Cluster).DeepCopy()
logger.Infof("update event for cluster %s", newCluster.Namespace)
// Check if the cluster is being deleted. This code path is called when a finalizer is specified in the crd.
// When a cluster is requested for deletion, K8s will only set the deletion timestamp if there are any finalizers in the list.
// K8s will only delete the crd and child resources when the finalizers have been removed from the crd.
if newCluster.DeletionTimestamp != nil {
logger.Infof("cluster %s has a deletion timestamp", newCluster.Namespace)
err := c.handleDelete(newCluster, time.Duration(clusterDeleteRetryInterval)*time.Second)
if err != nil {
logger.Errorf("failed finalizer for cluster. %+v", err)
return
}
// remove the finalizer from the crd, which indicates to k8s that the resource can safely be deleted
c.removeFinalizer(newCluster)
return
}
if !clusterChanged(oldCluster.Spec, newCluster.Spec) {
logger.Infof("update event for cluster %s is not supported", newCluster.Namespace)
return
}
logger.Infof("update event for cluster %s is supported, orchestrating update now", newCluster.Namespace)
logger.Debugf("old cluster: %+v", oldCluster.Spec)
logger.Debugf("new cluster: %+v", newCluster.Spec)
cluster, ok := c.clusterMap[newCluster.Namespace]
if !ok {
logger.Errorf("Cannot update cluster %s that does not exist", newCluster.Namespace)
return
}
cluster.Spec = newCluster.Spec
// attempt to update the cluster. note this is done outside of wait.Poll because that function
// will wait for the retry interval before trying for the first time.
done, _ := c.handleUpdate(newCluster, cluster)
if done {
return
}
err := wait.Poll(updateClusterInterval, updateClusterTimeout, func() (bool, error) {
return c.handleUpdate(newCluster, cluster)
})
if err != nil {
c.updateClusterStatus(newCluster.Namespace, newCluster.Name, edgefsv1.ClusterStateError,
fmt.Sprintf("giving up trying to update cluster in namespace %s after %s", cluster.Namespace, updateClusterTimeout))
return
}
logger.Infof("cluster %s updated in namespace %s", newCluster.Name, newCluster.Namespace)
}
func (c *ClusterController) handleUpdate(newClust *edgefsv1.Cluster, cluster *cluster) (bool, error) {
c.updateClusterStatus(newClust.Namespace, newClust.Name, edgefsv1.ClusterStateUpdating, "")
if newClust.Spec.EdgefsImageName != "" {
c.containerImage = newClust.Spec.EdgefsImageName
}
if err := cluster.createInstance(c.containerImage, true); err != nil {
logger.Errorf("failed to update cluster in namespace %s. %+v", newClust.Namespace, err)
return false, nil
}
c.updateClusterStatus(newClust.Namespace, newClust.Name, edgefsv1.ClusterStateCreated, "")
logger.Infof("succeeded updating cluster in namespace %s", newClust.Namespace)
return true, nil
}
func (c *ClusterController) onDelete(obj interface{}) {
clust, ok := obj.(*edgefsv1.Cluster)
if !ok {
return
}
clust = clust.DeepCopy()
logger.Infof("delete event for cluster %s in namespace %s", clust.Name, clust.Namespace)
err := c.handleDelete(clust, time.Duration(clusterDeleteRetryInterval)*time.Second)
if err != nil {
logger.Errorf("failed to delete cluster. %+v", err)
}
if cluster, ok := c.clusterMap[clust.Namespace]; ok {
close(cluster.stopCh)
delete(c.clusterMap, clust.Namespace)
}
if clust.Spec.Storage.AnyUseAllDevices() {
c.devicesInUse = false
}
}
func (c *ClusterController) handleDelete(clust *edgefsv1.Cluster, retryInterval time.Duration) error {
cluster, ok := c.clusterMap[clust.Namespace]
if !ok {
return fmt.Errorf("Cannot delete cluster %s that does not exist", clust.Namespace)
}
// grace on misconfigured crd deletions
if cluster.targets == nil || cluster.targets.Storage.Nodes == nil {
return nil
}
for _, node := range cluster.targets.Storage.Nodes {
cluster.UnlabelTargetNode(node.Name)
}
// delete associated node labels
return nil
}
func (c *ClusterController) updateClusterStatus(namespace, name string, state edgefsv1.ClusterState, message string) {
// get the most recent cluster CRD object
cluster, err := c.context.RookClientset.EdgefsV1().Clusters(namespace).Get(name, metav1.GetOptions{})
if err != nil {
logger.Errorf("failed to get cluster from namespace %s prior to updating its status: %+v", namespace, err)
return
}
// update the status on the retrieved cluster object
cluster.Status = edgefsv1.ClusterStatus{State: state, Message: message}
if _, err := c.context.RookClientset.EdgefsV1().Clusters(cluster.Namespace).Update(cluster); err != nil {
logger.Errorf("failed to update cluster %s status: %+v", cluster.Namespace, err)
}
}
func (c *ClusterController) addFinalizer(clust *edgefsv1.Cluster) error {
// get the latest cluster object since we probably updated it before we got to this point (e.g. by updating its status)
clust, err := c.context.RookClientset.EdgefsV1().Clusters(clust.Namespace).Get(clust.Name, metav1.GetOptions{})
if err != nil {
return err
}
// add the finalizer (cluster.edgefs.rook.io) if it is not yet defined on the cluster CRD
for _, finalizer := range clust.Finalizers {
if finalizer == finalizerName {
logger.Infof("finalizer already set on cluster %s", clust.Namespace)
return nil
}
}
// adding finalizer to the cluster crd
clust.Finalizers = append(clust.Finalizers, finalizerName)
// update the crd
_, err = c.context.RookClientset.EdgefsV1().Clusters(clust.Namespace).Update(clust)
if err != nil {
return fmt.Errorf("failed to add finalizer to cluster. %+v", err)
}
logger.Infof("added finalizer to cluster %s", clust.Name)
return nil
}
func (c *ClusterController) removeFinalizer(obj interface{}) {
var fname string
var objectMeta *metav1.ObjectMeta
// first determine what type/version of cluster we are dealing with
if cl, ok := obj.(*edgefsv1.Cluster); ok {
fname = finalizerName
objectMeta = &cl.ObjectMeta
} else {
logger.Warningf("cannot remove finalizer from object that is not a cluster: %+v", obj)
return
}
// remove the finalizer from the slice if it exists
found := false
for i, finalizer := range objectMeta.Finalizers {
if finalizer == fname {
objectMeta.Finalizers = append(objectMeta.Finalizers[:i], objectMeta.Finalizers[i+1:]...)
found = true
break
}
}
if !found {
logger.Infof("finalizer %s not found in the cluster crd '%s'", fname, objectMeta.Name)
return
}
// update the crd to remove the finalizer for good. retry several times in case of intermittent failures.
maxRetries := 5
retrySeconds := 5 * time.Second
for i := 0; i < maxRetries; i++ {
var err error
if cluster, ok := obj.(*edgefsv1.Cluster); ok {
_, err = c.context.RookClientset.EdgefsV1().Clusters(cluster.Namespace).Update(cluster)
}
if err != nil {
logger.Errorf("failed to remove finalizer %s from cluster %s. %+v", fname, objectMeta.Name, err)
time.Sleep(retrySeconds)
continue
}
logger.Infof("removed finalizer %s from cluster %s", fname, objectMeta.Name)
return
}
logger.Warningf("giving up from removing the %s cluster finalizer", fname)
}