forked from chaos-mesh/chaos-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
165 lines (137 loc) · 4.63 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
// Copyright 2021 Chaos Mesh Authors.
//
// 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 desiredphase
import (
"context"
"time"
"github.com/go-logr/logr"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/chaos-mesh/chaos-mesh/controllers/utils/recorder"
)
// Reconciler for common chaos
type Reconciler struct {
// Object is used to mark the target type of this Reconciler
Object v1alpha1.InnerObject
// Client is used to operate on the Kubernetes cluster
client.Client
Recorder recorder.ChaosRecorder
Log logr.Logger
}
// Reconcile the common chaos
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
obj := r.Object.DeepCopyObject().(v1alpha1.InnerObject)
if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil {
if apierrors.IsNotFound(err) {
r.Log.Info("chaos not found")
} else {
// TODO: handle this error
r.Log.Error(err, "unable to get chaos")
}
return ctrl.Result{}, nil
}
reconcileInfo := &reconcileInfo{
obj: obj,
Reconciler: r,
shouldUpdate: false,
}
return reconcileInfo.Reconcile(req)
}
type reconcileInfo struct {
obj v1alpha1.InnerObject
*Reconciler
shouldUpdate bool
requeueAfter time.Duration
}
func (info *reconcileInfo) GetCreationTimestamp() metav1.Time {
return info.obj.GetCreationTimestamp()
}
func (info *reconcileInfo) CalcDesiredPhase() (v1alpha1.DesiredPhase, []recorder.ChaosEvent) {
events := []recorder.ChaosEvent{}
// Consider the finalizers
if info.obj.IsDeleted() {
if info.obj.GetStatus().Experiment.DesiredPhase != v1alpha1.StoppedPhase {
events = append(events, recorder.Deleted{})
}
return v1alpha1.StoppedPhase, events
}
if info.obj.IsOneShot() {
// An oneshot chaos should always be in running phase, so that it cannot
// be applied multiple times or cause other bugs :(
return v1alpha1.RunningPhase, events
}
// Consider the duration
now := time.Now()
durationExceeded, untilStop, err := info.obj.DurationExceeded(now)
if err != nil {
info.Log.Error(err, "failed to parse duration")
}
if durationExceeded {
if info.obj.GetStatus().Experiment.DesiredPhase != v1alpha1.StoppedPhase {
events = append(events, recorder.TimeUp{})
}
return v1alpha1.StoppedPhase, events
}
info.requeueAfter = untilStop
// Then decide the pause logic
if info.obj.IsPaused() {
if info.obj.GetStatus().Experiment.DesiredPhase != v1alpha1.StoppedPhase {
events = append(events, recorder.Paused{})
}
return v1alpha1.StoppedPhase, events
}
if info.obj.GetStatus().Experiment.DesiredPhase != v1alpha1.RunningPhase {
events = append(events, recorder.Started{})
}
return v1alpha1.RunningPhase, events
}
func (info *reconcileInfo) Reconcile(req ctrl.Request) (ctrl.Result, error) {
desiredPhase, events := info.CalcDesiredPhase()
info.Log.Info("modify desiredPhase", "desiredPhase", desiredPhase)
if info.obj.GetStatus().Experiment.DesiredPhase != desiredPhase {
for _, ev := range events {
info.Recorder.Event(info.obj, ev)
}
updateError := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
obj := info.Object.DeepCopyObject().(v1alpha1.InnerObject)
if err := info.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil {
info.Log.Error(err, "unable to get chaos")
return err
}
if obj.GetStatus().Experiment.DesiredPhase != desiredPhase {
obj.GetStatus().Experiment.DesiredPhase = desiredPhase
info.Log.Info("update object", "namespace", obj.GetNamespace(), "name", obj.GetName())
return info.Client.Update(context.TODO(), obj)
}
return nil
})
if updateError != nil {
info.Log.Error(updateError, "fail to update")
info.Recorder.Event(info.obj, recorder.Failed{
Activity: "update desiredphase",
Err: updateError.Error(),
})
return ctrl.Result{}, nil
}
info.Recorder.Event(info.obj, recorder.Updated{
Field: "desiredPhase",
})
}
return ctrl.Result{RequeueAfter: info.requeueAfter}, nil
}