-
Notifications
You must be signed in to change notification settings - Fork 6
/
chaos.go
118 lines (97 loc) · 2.8 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
package gatekeeper
import (
"math/rand"
"sync"
"time"
"github.com/juju/errors"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
var (
killed = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "gravity",
Subsystem: "gatekeeper",
Name: "chaos",
Help: "killed component by chaos",
}, []string{"kind", "name"})
)
type ChaosMonkey struct {
namespace string
kube dynamic.Interface
tickerInterval time.Duration
closeC <-chan struct{}
wg sync.WaitGroup
}
func NewChaosMonkey(namespace string, kube dynamic.Interface, duration string, stopCh <-chan struct{}) *ChaosMonkey {
prometheus.MustRegister(killed)
monkey := &ChaosMonkey{
namespace: namespace,
kube: kube,
closeC: stopCh,
}
tickerInterval, err := time.ParseDuration(duration)
if err != nil {
log.Fatalf("wrong duration format %s", duration)
}
monkey.tickerInterval = tickerInterval
return monkey
}
func (c *ChaosMonkey) Start() error {
go c.run()
return nil
}
func (c *ChaosMonkey) run() {
ticker := time.NewTicker(c.tickerInterval)
defer ticker.Stop()
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
m := map[string]string{
"app.kubernetes.io/name": "gravity",
}
selector := labels.SelectorFromSet(m)
podGvr := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
statefulSetGvr := schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}
for {
select {
case <-c.closeC:
return
case <-ticker.C:
if rnd.Float32() < 0.5 {
continue
}
gvr := podGvr
if rnd.Float32() < 0.3 {
gvr = statefulSetGvr
}
list, err := c.kube.Resource(gvr).Namespace(c.namespace).List(metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
log.Error(errors.Annotatef(err, "[ChaosMonkey] fail to list %s", gvr.Resource))
continue
}
if len(list.Items) == 0 {
log.Infof("[ChaosMonkey] no %s to kill", gvr.String())
continue
}
item := list.Items[rnd.Intn(len(list.Items))]
err = c.kube.Resource(gvr).Namespace(item.GetNamespace()).Delete(item.GetName(), deleteOps(rnd))
if err != nil {
log.Error(errors.Annotatef(err, "[ChaosMonkey] fail to delete %s %s", item.GetKind(), item.GetName()))
} else {
killed.WithLabelValues(item.GetKind(), item.GetLabels()["app.kubernetes.io/instance"]).Add(1)
log.Infof("[ChaosMonkey] deleted %s %s", item.GetKind(), item.GetLabels()["app.kubernetes.io/instance"])
}
}
}
}
func deleteOps(rnd *rand.Rand) *metav1.DeleteOptions {
ops := metav1.NewDeleteOptions(int64(rnd.Intn(8)))
return ops
}
func (c *ChaosMonkey) Close() {
c.wg.Wait()
}