forked from chaosblade-io/chaosblade-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predicate.go
123 lines (113 loc) · 3.48 KB
/
predicate.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
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* 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 chaosblade
import (
"encoding/json"
"reflect"
"github.com/sirupsen/logrus"
"sigs.k8s.io/controller-runtime/pkg/event"
"github.com/deepsola/chaosblade-operator/pkg/apis/chaosblade/v1alpha1"
)
type SpecUpdatedPredicateForRunningPhase struct {
}
func (sup *SpecUpdatedPredicateForRunningPhase) Create(e event.CreateEvent) bool {
if e.Object == nil {
return false
}
obj, ok := e.Object.(*v1alpha1.ChaosBlade)
if !ok {
return false
}
logrus.Infof("trigger create event, name: %s", obj.Name)
logrus.Debugf("creating obj: %+v", obj)
if obj.GetDeletionTimestamp() != nil {
logrus.Infof("unexpected phase for cb creating, name: %s, phase: %s", obj.Name, obj.Status.Phase)
return false
}
if obj.Status.Phase == v1alpha1.ClusterPhaseInitial {
return true
}
logrus.Infof("unexpected phase for cb creating, name: %s, phase: %s", obj.Name, obj.Status.Phase)
return false
}
func (*SpecUpdatedPredicateForRunningPhase) Delete(e event.DeleteEvent) bool {
if e.Object == nil {
return false
}
obj, ok := e.Object.(*v1alpha1.ChaosBlade)
if !ok {
return false
}
logrus.Infof("trigger delete event, name: %s", obj.Name)
logrus.Debugf("deleting obj: %+v", obj)
return contains(obj.GetFinalizers(), chaosbladeFinalizer)
}
func (*SpecUpdatedPredicateForRunningPhase) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil {
return false
}
oldObj, ok := e.ObjectOld.(*v1alpha1.ChaosBlade)
if !ok {
return false
}
logrus.Infof("trigger update event, name: %s", oldObj.Name)
newObj, ok := e.ObjectNew.(*v1alpha1.ChaosBlade)
if !ok {
return false
}
logrus.Debugf("updating oldObj: %+v", oldObj)
logrus.Debugf("updating newObj: %+v", newObj)
if !reflect.DeepEqual(newObj.Spec, oldObj.Spec) {
bytes, err := json.Marshal(oldObj.Spec.DeepCopy())
if err != nil {
logrus.Warningf("marshal old spec failed, %+v", err)
return false
}
newObj.SetAnnotations(map[string]string{"preSpec": string(bytes)})
return true
}
if newObj.Status.Phase == v1alpha1.ClusterPhaseInitial {
return true
}
// delete Error chaosblade
if oldObj.GetDeletionTimestamp() == nil &&
newObj.GetDeletionTimestamp() != nil {
return true
}
if newObj.Status.Phase == v1alpha1.ClusterPhaseRunning ||
newObj.Status.Phase == v1alpha1.ClusterPhaseError ||
newObj.Status.Phase == v1alpha1.ClusterPhaseDestroying {
return false
}
if newObj.Status.Phase != oldObj.Status.Phase {
return true
}
if !reflect.DeepEqual(newObj.Status, oldObj.Status) {
return true
}
if newObj.GetDeletionTimestamp() != nil {
if contains(newObj.GetFinalizers(), chaosbladeFinalizer) {
return true
}
logrus.Infof("cannot find the %s finalizer, so skip the update event", chaosbladeFinalizer)
return false
}
logrus.Infof("spec not changed under %s phase, so skip the update event", newObj.Status.Phase)
return false
}
func (*SpecUpdatedPredicateForRunningPhase) Generic(e event.GenericEvent) bool {
return false
}