forked from apecloud/kubeblocks
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcluster_plan_builder.go
More file actions
421 lines (377 loc) · 13.6 KB
/
Copy pathcluster_plan_builder.go
File metadata and controls
421 lines (377 loc) · 13.6 KB
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
/*
Copyright (C) 2022-2024 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package apps
import (
"context"
"fmt"
"reflect"
"github.com/go-logr/logr"
snapshotv1beta1 "github.com/kubernetes-csi/external-snapshotter/client/v3/apis/volumesnapshot/v1beta1"
snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
appsv1beta1 "github.com/apecloud/kubeblocks/apis/apps/v1beta1"
dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
extensionsv1alpha1 "github.com/apecloud/kubeblocks/apis/extensions/v1alpha1"
"github.com/apecloud/kubeblocks/apis/workloads/legacy"
workloadsv1alpha1 "github.com/apecloud/kubeblocks/apis/workloads/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/graph"
"github.com/apecloud/kubeblocks/pkg/controller/model"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)
const (
defaultWeight int = iota
workloadWeight
clusterWeight
)
// clusterTransformContext a graph.TransformContext implementation for Cluster reconciliation
type clusterTransformContext struct {
context.Context
Client client.Reader
record.EventRecorder
logr.Logger
Cluster *appsv1alpha1.Cluster
OrigCluster *appsv1alpha1.Cluster
ClusterDef *appsv1alpha1.ClusterDefinition
ClusterVer *appsv1alpha1.ClusterVersion
ComponentDefs map[string]*appsv1alpha1.ComponentDefinition
// ComponentSpecs includes all cluster component specs generated from ComponentSpecs and ShardingSpecs
ComponentSpecs []*appsv1alpha1.ClusterComponentSpec
// ShardingComponentSpecs includes all sharding component specs generated from ShardingSpecs
ShardingComponentSpecs map[string][]*appsv1alpha1.ClusterComponentSpec
// Labels to be added to components, mapping with ComponentSpecs.
Labels map[string]map[string]string
// Annotations to be added to components, mapping with ComponentSpecs.
Annotations map[string]map[string]string
}
// clusterPlanBuilder a graph.PlanBuilder implementation for Cluster reconciliation
type clusterPlanBuilder struct {
req ctrl.Request
cli client.Client
transCtx *clusterTransformContext
transformers graph.TransformerChain
}
// clusterPlan a graph.Plan implementation for Cluster reconciliation
type clusterPlan struct {
dag *graph.DAG
walkFunc graph.WalkFunc
cli client.Client
transCtx *clusterTransformContext
}
var _ graph.TransformContext = &clusterTransformContext{}
var _ graph.PlanBuilder = &clusterPlanBuilder{}
var _ graph.Plan = &clusterPlan{}
// TransformContext implementation
func (c *clusterTransformContext) GetContext() context.Context {
return c.Context
}
func (c *clusterTransformContext) GetClient() client.Reader {
return c.Client
}
func (c *clusterTransformContext) GetRecorder() record.EventRecorder {
return c.EventRecorder
}
func (c *clusterTransformContext) GetLogger() logr.Logger {
return c.Logger
}
func init() {
model.AddScheme(appsv1alpha1.AddToScheme)
model.AddScheme(dpv1alpha1.AddToScheme)
model.AddScheme(snapshotv1.AddToScheme)
model.AddScheme(snapshotv1beta1.AddToScheme)
model.AddScheme(extensionsv1alpha1.AddToScheme)
model.AddScheme(workloadsv1alpha1.AddToScheme)
model.AddScheme(appsv1beta1.AddToScheme)
model.AddScheme(apiextv1.AddToScheme)
model.AddScheme(legacy.AddToScheme)
}
// PlanBuilder implementation
func (c *clusterPlanBuilder) Init() error {
cluster := &appsv1alpha1.Cluster{}
if err := c.cli.Get(c.transCtx.Context, c.req.NamespacedName, cluster); err != nil {
return err
}
c.AddTransformer(&clusterInitTransformer{cluster: cluster})
return nil
}
func (c *clusterPlanBuilder) AddTransformer(transformer ...graph.Transformer) graph.PlanBuilder {
c.transformers = append(c.transformers, transformer...)
return c
}
func (c *clusterPlanBuilder) AddParallelTransformer(transformer ...graph.Transformer) graph.PlanBuilder {
c.transformers = append(c.transformers, &ParallelTransformers{transformers: transformer})
return c
}
// Build runs all transformers to generate a plan
func (c *clusterPlanBuilder) Build() (graph.Plan, error) {
var err error
defer func() {
// set apply resource condition
// if cluster is being deleted, no need to set apply resource condition
if c.transCtx.Cluster.IsDeleting() {
return
}
preCheckCondition := meta.FindStatusCondition(c.transCtx.Cluster.Status.Conditions, appsv1alpha1.ConditionTypeProvisioningStarted)
if preCheckCondition == nil {
// this should not happen
return
}
// if pre-check failed, this is a fast return, no need to set apply resource condition
if preCheckCondition.Status != metav1.ConditionTrue {
sendWarningEventWithError(c.transCtx.GetRecorder(), c.transCtx.Cluster, ReasonPreCheckFailed, err)
return
}
setApplyResourceCondition(&c.transCtx.Cluster.Status.Conditions, c.transCtx.Cluster.Generation, err)
sendWarningEventWithError(c.transCtx.GetRecorder(), c.transCtx.Cluster, ReasonApplyResourcesFailed, err)
}()
// new a DAG and apply chain on it
dag := graph.NewDAG()
err = c.transformers.ApplyTo(c.transCtx, dag)
c.transCtx.Logger.V(1).Info(fmt.Sprintf("DAG: %s", dag))
// construct execution plan
plan := &clusterPlan{
dag: dag,
walkFunc: c.defaultWalkFuncWithLogging,
cli: c.cli,
transCtx: c.transCtx,
}
return plan, err
}
// Plan implementation
func (p *clusterPlan) Execute() error {
less := func(v1, v2 graph.Vertex) bool {
getWeight := func(v graph.Vertex) int {
lifecycleVertex, ok := v.(*model.ObjectVertex)
if !ok {
return defaultWeight
}
switch lifecycleVertex.Obj.(type) {
case *appsv1alpha1.Cluster:
return clusterWeight
case *appsv1.StatefulSet, *appsv1.Deployment:
return workloadWeight
default:
return defaultWeight
}
}
return getWeight(v1) <= getWeight(v2)
}
err := p.dag.WalkReverseTopoOrder(p.walkFunc, less)
if err != nil {
if hErr := p.handlePlanExecutionError(err); hErr != nil {
return hErr
}
}
return err
}
func (p *clusterPlan) handlePlanExecutionError(err error) error {
clusterCopy := p.transCtx.OrigCluster.DeepCopy()
condition := newFailedApplyResourcesCondition(err)
meta.SetStatusCondition(&clusterCopy.Status.Conditions, condition)
return p.cli.Status().Patch(p.transCtx.Context, clusterCopy, client.MergeFrom(p.transCtx.OrigCluster))
}
// Do the real works
// newClusterPlanBuilder returns a clusterPlanBuilder powered PlanBuilder
func newClusterPlanBuilder(ctx intctrlutil.RequestCtx, cli client.Client) graph.PlanBuilder {
return &clusterPlanBuilder{
req: ctx.Req,
cli: cli,
transCtx: &clusterTransformContext{
Context: ctx.Ctx,
Client: model.NewGraphClient(cli),
EventRecorder: ctx.Recorder,
Logger: ctx.Log,
},
}
}
func (c *clusterPlanBuilder) defaultWalkFuncWithLogging(vertex graph.Vertex) error {
node, ok := vertex.(*model.ObjectVertex)
err := c.defaultWalkFunc(vertex)
switch {
case err == nil:
return err
case !ok:
c.transCtx.Logger.Error(err, "")
case node.Action == nil:
c.transCtx.Logger.Error(err, fmt.Sprintf("%T", node))
case apierrors.IsConflict(err):
return err
default:
c.transCtx.Logger.Error(err, fmt.Sprintf("%s %T error", *node.Action, node.Obj))
}
return err
}
func (c *clusterPlanBuilder) defaultWalkFunc(vertex graph.Vertex) error {
node, ok := vertex.(*model.ObjectVertex)
if !ok {
return fmt.Errorf("wrong vertex type %v", vertex)
}
if node.Action == nil {
return fmt.Errorf("node action can't be nil")
}
// cluster object has more business to do, handle them here
if _, ok = node.Obj.(*appsv1alpha1.Cluster); ok {
if err := c.reconcileCluster(node); err != nil {
return err
}
}
return c.reconcileObject(node)
}
func (c *clusterPlanBuilder) reconcileCluster(node *model.ObjectVertex) error {
cluster := node.Obj.(*appsv1alpha1.Cluster).DeepCopy()
origCluster := node.OriObj.(*appsv1alpha1.Cluster)
switch *node.Action {
// cluster.meta and cluster.spec might change
case model.STATUS:
if !reflect.DeepEqual(cluster.ObjectMeta, origCluster.ObjectMeta) || !reflect.DeepEqual(cluster.Spec, origCluster.Spec) {
patch := client.MergeFrom(origCluster.DeepCopy())
if err := c.cli.Patch(c.transCtx.Context, cluster, patch); err != nil {
return err
}
}
case model.CREATE, model.UPDATE:
return fmt.Errorf("cluster can't be created or updated: %s", cluster.Name)
}
return nil
}
func (c *clusterPlanBuilder) reconcileObject(node *model.ObjectVertex) error {
ctx := c.transCtx.Context
switch *node.Action {
case model.CREATE:
return c.reconcileCreateObject(ctx, node)
case model.UPDATE:
return c.reconcileUpdateObject(ctx, node)
case model.PATCH:
return c.reconcilePatchObject(ctx, node)
case model.DELETE:
return c.reconcileDeleteObject(ctx, node)
case model.STATUS:
return c.reconcileStatusObject(ctx, node)
case model.NOOP:
return c.reconcileNoopObject(ctx, node)
}
return nil
}
func (c *clusterPlanBuilder) reconcileCreateObject(ctx context.Context, node *model.ObjectVertex) error {
err := c.cli.Create(ctx, node.Obj, clientOption(node))
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
func (c *clusterPlanBuilder) reconcileUpdateObject(ctx context.Context, node *model.ObjectVertex) error {
err := c.cli.Update(ctx, node.Obj, clientOption(node))
if err != nil && !apierrors.IsNotFound(err) {
return err
}
return nil
}
func (c *clusterPlanBuilder) reconcilePatchObject(ctx context.Context, node *model.ObjectVertex) error {
patch := client.MergeFrom(node.OriObj)
err := c.cli.Patch(ctx, node.Obj, patch, clientOption(node))
if err != nil && !apierrors.IsNotFound(err) {
return err
}
return nil
}
func (c *clusterPlanBuilder) reconcileDeleteObject(ctx context.Context, node *model.ObjectVertex) error {
if controllerutil.RemoveFinalizer(node.Obj, constant.DBClusterFinalizerName) {
err := c.cli.Update(ctx, node.Obj, clientOption(node))
if err != nil && !apierrors.IsNotFound(err) {
return err
}
}
backgroundDeleteObject := func() error {
deletePropagation := metav1.DeletePropagationBackground
deleteOptions := &client.DeleteOptions{
PropagationPolicy: &deletePropagation,
}
if err := c.cli.Delete(ctx, node.Obj, deleteOptions, clientOption(node)); err != nil {
return client.IgnoreNotFound(err)
}
return nil
}
// delete secondary objects
if _, ok := node.Obj.(*appsv1alpha1.Cluster); !ok {
err := backgroundDeleteObject()
if err != nil && !apierrors.IsNotFound(err) {
return err
}
}
return nil
}
func (c *clusterPlanBuilder) reconcileStatusObject(ctx context.Context, node *model.ObjectVertex) error {
patch := client.MergeFrom(node.OriObj)
if err := c.cli.Status().Patch(ctx, node.Obj, patch, clientOption(node)); err != nil {
return err
}
// handle condition and phase changing triggered events
if newCluster, ok := node.Obj.(*appsv1alpha1.Cluster); ok {
oldCluster, _ := node.OriObj.(*appsv1alpha1.Cluster)
c.emitConditionUpdatingEvent(oldCluster.Status.Conditions, newCluster.Status.Conditions)
c.emitStatusUpdatingEvent(oldCluster.Status, newCluster.Status)
}
return nil
}
func (c *clusterPlanBuilder) reconcileNoopObject(ctx context.Context, node *model.ObjectVertex) error {
return nil
}
func (c *clusterPlanBuilder) emitConditionUpdatingEvent(oldConditions, newConditions []metav1.Condition) {
for _, newCondition := range newConditions {
oldCondition := meta.FindStatusCondition(oldConditions, newCondition.Type)
// filtered in cluster creation
if oldCondition == nil && newCondition.Status == metav1.ConditionFalse {
return
}
if !reflect.DeepEqual(oldCondition, &newCondition) {
eType := corev1.EventTypeNormal
if newCondition.Status == metav1.ConditionFalse {
eType = corev1.EventTypeWarning
}
c.transCtx.EventRecorder.Event(c.transCtx.Cluster, eType, newCondition.Reason, newCondition.Message)
}
}
}
func (c *clusterPlanBuilder) emitStatusUpdatingEvent(oldStatus, newStatus appsv1alpha1.ClusterStatus) {
cluster := c.transCtx.Cluster
newPhase := newStatus.Phase
if newPhase == oldStatus.Phase {
return
}
eType := corev1.EventTypeNormal
message := ""
switch newPhase {
case appsv1alpha1.RunningClusterPhase:
message = fmt.Sprintf("Cluster: %s is ready, current phase is %s", cluster.Name, newPhase)
case appsv1alpha1.StoppedClusterPhase:
message = fmt.Sprintf("Cluster: %s stopped successfully.", cluster.Name)
case appsv1alpha1.FailedClusterPhase, appsv1alpha1.AbnormalClusterPhase:
message = fmt.Sprintf("Cluster: %s is %s, check according to the components message", cluster.Name, newPhase)
eType = corev1.EventTypeWarning
}
if len(message) > 0 {
c.transCtx.EventRecorder.Event(cluster, eType, string(newPhase), message)
}
}