-
Notifications
You must be signed in to change notification settings - Fork 37
/
chaos.go
152 lines (140 loc) · 4.22 KB
/
chaos.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
package client
import (
"context"
"fmt"
"time"
"github.com/cdk8s-team/cdk8s-core-go/cdk8s/v2"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/rs/zerolog/log"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/smartcontractkit/chainlink-testing-framework/k8s/config"
)
// Chaos is controller that manages Chaosmesh CRD instances to run experiments
type Chaos struct {
Client *K8sClient
ResourceByName map[string]string
Namespace string
}
type ChaosState struct {
ChaosDetails v1alpha1.ChaosStatus `json:"status"`
}
// NewChaos creates controller to run and stop chaos experiments
func NewChaos(client *K8sClient, namespace string) *Chaos {
return &Chaos{
Client: client,
ResourceByName: make(map[string]string),
Namespace: namespace,
}
}
// Run runs experiment and saves its ID
func (c *Chaos) Run(app cdk8s.App, id string, resource string) (string, error) {
log.Info().Msg("Applying chaos experiment")
config.JSIIGlobalMu.Lock()
manifest := *app.SynthYaml()
config.JSIIGlobalMu.Unlock()
log.Trace().Str("Raw", manifest).Msg("Manifest")
c.ResourceByName[id] = resource
if err := c.Client.Apply(context.Background(), manifest, c.Namespace, false); err != nil {
return id, err
}
if err := c.checkForPodsExistence(app); err != nil {
return id, err
}
err := c.waitForChaosStatus(id, v1alpha1.ConditionAllInjected, 5*time.Minute)
if err != nil {
return id, err
}
return id, nil
}
func (c *Chaos) waitForChaosStatus(id string, condition v1alpha1.ChaosConditionType, timeout time.Duration) error {
var result ChaosState
log.Info().Msgf("waiting for chaos experiment state %s", condition)
if timeout < time.Minute {
log.Info().Msg("timeout is less than 1 minute, setting to 1 minute")
timeout = time.Minute
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return wait.PollUntilContextTimeout(ctx, 2*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
data, err := c.Client.ClientSet.
RESTClient().
Get().
RequestURI(fmt.Sprintf("/apis/chaos-mesh.org/v1alpha1/namespaces/%s/%s/%s", c.Namespace, c.ResourceByName[id], id)).
Do(ctx).
Raw()
if err == nil {
err = json.Unmarshal(data, &result)
if err != nil {
return false, err
}
for _, c := range result.ChaosDetails.Conditions {
if c.Type == condition && c.Status == v1.ConditionTrue {
return true, err
}
}
}
return false, nil
})
}
func (c *Chaos) WaitForAllRecovered(id string, timeout time.Duration) error {
return c.waitForChaosStatus(id, v1alpha1.ConditionAllRecovered, timeout)
}
// Stop removes a chaos experiment
func (c *Chaos) Stop(id string) error {
defer delete(c.ResourceByName, id)
return c.Client.DeleteResource(c.Namespace, c.ResourceByName[id], id)
}
func (c *Chaos) checkForPodsExistence(app cdk8s.App) error {
charts := app.Charts()
var selectors []string
for _, chart := range *charts {
json := chart.ToJson()
for _, j := range *json {
m := j.(map[string]interface{})
fmt.Println(m)
kind := m["kind"].(string)
if kind == "PodChaos" || kind == "NetworkChaos" {
selectors = append(selectors, getLabelSelectors(m["spec"].(map[string]interface{})))
}
if kind == "NetworkChaos" {
target := m["spec"].(map[string]interface{})["target"].(map[string]interface{})
selectors = append(selectors, getLabelSelectors(target))
}
}
}
for _, selector := range selectors {
podList, err := c.Client.ListPods(c.Namespace, selector)
if err != nil {
return err
}
if podList == nil || len(podList.Items) == 0 {
return fmt.Errorf("no pods found for selector %s", selector)
}
log.Info().
Int("podsCount", len(podList.Items)).
Str("selector", selector).
Msgf("found pods for chaos experiment")
}
return nil
}
func getLabelSelectors(spec map[string]interface{}) string {
if spec == nil {
return ""
}
s := spec["selector"].(map[string]interface{})
if s == nil {
return ""
}
m := s["labelSelectors"].(map[string]interface{})
selector := ""
for key, value := range m {
if selector == "" {
selector = fmt.Sprintf("%s=%s", key, value)
} else {
selector = fmt.Sprintf("%s, %s=%s", selector, key, value)
}
}
return selector
}