forked from chaosblade-io/chaosblade-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
144 lines (130 loc) · 4.76 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
/*
* 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 exec
import (
"context"
"sync"
"github.com/sirupsen/logrus"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/deepsola/chaosblade-operator/channel"
"github.com/deepsola/chaosblade-operator/exec/container"
"github.com/deepsola/chaosblade-operator/exec/model"
"github.com/deepsola/chaosblade-operator/exec/node"
"github.com/deepsola/chaosblade-operator/exec/pod"
"github.com/deepsola/chaosblade-operator/pkg/apis/chaosblade/v1alpha1"
)
// ResourceDispatchedController contains all resource controllers exclude node resource
type ResourceDispatchedController struct {
Controllers map[string]model.ExperimentController
}
var executor *ResourceDispatchedController
var once sync.Once
// NewDispatcherExecutor initialized when operator starting
func NewDispatcherExecutor(client *channel.Client) *ResourceDispatchedController {
once.Do(func() {
executor = &ResourceDispatchedController{
Controllers: make(map[string]model.ExperimentController, 0),
}
executor.register(
node.NewExpController(client),
pod.NewExpController(client),
container.NewExpController(client),
)
})
return executor
}
func (e *ResourceDispatchedController) Name() string {
return "dispatch"
}
func (e *ResourceDispatchedController) Create(bladeName string, expSpec v1alpha1.ExperimentSpec) v1alpha1.ExperimentStatus {
logrus.WithField("experiment", bladeName).Infof("start to create experiment")
controller := e.Controllers[expSpec.Scope]
if controller == nil {
logrus.WithField("experiment", bladeName).WithField("scope", expSpec.Scope).Errorf("controller not found")
return v1alpha1.ExperimentStatus{
State: "Error",
Error: "can not find the scope controller for creating",
}
}
ctx := model.SetExperimentIdToContext(context.Background(), bladeName)
response := controller.Create(ctx, expSpec)
experimentStatus := createExperimentStatusByResponse(response)
experimentStatus.Scope = expSpec.Scope
experimentStatus.Target = expSpec.Target
experimentStatus.Action = expSpec.Action
return experimentStatus
}
func (e *ResourceDispatchedController) Destroy(bladeName string, expSpec v1alpha1.ExperimentSpec, oldExpStatus v1alpha1.ExperimentStatus) v1alpha1.ExperimentStatus {
controller := e.Controllers[expSpec.Scope]
if controller == nil {
return v1alpha1.ExperimentStatus{
State: "Error",
Error: "can not find the scope controller for destroying",
}
}
if oldExpStatus.ResStatuses == nil ||
len(oldExpStatus.ResStatuses) == 0 {
return model.CreateDestroyedStatus(oldExpStatus)
}
ctx := spec.SetDestroyFlag(context.Background(), bladeName)
ctx = model.SetExperimentIdToContext(ctx, bladeName)
response := controller.Destroy(ctx, expSpec, oldExpStatus)
newExpStatus := createExperimentStatusByResponse(response)
newExpStatus = validateAndSetNecessaryFields(newExpStatus, oldExpStatus)
return newExpStatus
}
// validateAndSetNecessaryFields to resolve status overwriting when the experiment is destroyed.
func validateAndSetNecessaryFields(status v1alpha1.ExperimentStatus, oldExpStatus v1alpha1.ExperimentStatus) v1alpha1.ExperimentStatus {
status.Scope = oldExpStatus.Scope
status.Target = oldExpStatus.Target
status.Action = oldExpStatus.Action
if status.State == "Error" {
status.State = oldExpStatus.State
}
if status.ResStatuses == nil {
return status
}
for _, s := range status.ResStatuses {
for _, os := range oldExpStatus.ResStatuses {
if s.Id != os.Id {
continue
}
if s.State == "Error" {
s.State = os.State
}
}
}
return status
}
// createExperimentStatusByResponse wraps experiment statuses
func createExperimentStatusByResponse(response *spec.Response) v1alpha1.ExperimentStatus {
experimentStatus := v1alpha1.ExperimentStatus{}
if response.Result != nil {
experimentStatus = response.Result.(v1alpha1.ExperimentStatus)
} else {
if response.Success {
experimentStatus = v1alpha1.CreateSuccessExperimentStatus([]v1alpha1.ResourceStatus{})
} else {
experimentStatus = v1alpha1.CreateFailExperimentStatus(response.Err, []v1alpha1.ResourceStatus{})
}
}
return experimentStatus
}
func (e *ResourceDispatchedController) register(cs ...model.ExperimentController) {
for _, c := range cs {
e.Controllers[c.Name()] = c
}
}