forked from observatorium/thanos-receive-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statefulset.go
81 lines (67 loc) · 1.71 KB
/
statefulset.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
package main
import (
"context"
"github.com/go-kit/kit/log"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)
type StatefulSetWatcher struct {
klient kubernetes.Interface
logger log.Logger
statefulsets map[string]StatefulSetUpdate
}
type StatefulSetUpdate struct {
Name string
Replicas int
}
// Name will always be the same, as check outside of this func already
func (current StatefulSetUpdate) Changed(update StatefulSetUpdate) bool {
return current.Replicas != update.Replicas
}
func (stsw *StatefulSetWatcher) Watch(ctx context.Context, namespace string, label string, updates chan<- StatefulSetUpdate) error {
if stsw.logger == nil {
stsw.logger = log.NewNopLogger()
}
if stsw.statefulsets == nil {
stsw.statefulsets = make(map[string]StatefulSetUpdate)
}
stsWatch, err := stsw.klient.AppsV1().StatefulSets(namespace).Watch(metav1.ListOptions{
LabelSelector: label,
})
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
close(updates)
stsWatch.Stop()
return nil
case event := <-stsWatch.ResultChan():
switch event.Type {
case watch.Modified, watch.Added:
if event.Object == nil {
continue
}
sts := event.Object.(*appsv1.StatefulSet)
u := StatefulSetUpdate{
Name: sts.Name,
Replicas: int(*sts.Spec.Replicas),
}
// Check if StatefulSet update is actually something we care about
if current, exists := stsw.statefulsets[u.Name]; exists {
if current.Changed(u) {
stsw.statefulsets[u.Name] = u
updates <- u
}
continue
}
stsw.statefulsets[u.Name] = u
updates <- u
case watch.Deleted:
}
}
}
}