-
Notifications
You must be signed in to change notification settings - Fork 45
/
informer.go
139 lines (119 loc) · 3.55 KB
/
informer.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
package replicaset
import (
"context"
"fmt"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
watchtools "k8s.io/client-go/tools/watch"
"github.com/werf/kubedog/pkg/tracker"
"github.com/werf/kubedog/pkg/tracker/debug"
"github.com/werf/kubedog/pkg/tracker/pod"
"github.com/werf/kubedog/pkg/utils"
)
type ReplicaSet struct {
Name string
IsNew bool
}
// TODO: add containers!
type ReplicaSetPod struct {
ReplicaSet ReplicaSet
Name string
}
type ReplicaSetPodLogChunk struct {
*pod.PodLogChunk
ReplicaSet ReplicaSet
}
type ReplicaSetPodError struct {
pod.PodError
ReplicaSet ReplicaSet
}
// ReplicaSetInformer monitor ReplicaSet events to use with controllers (Deployment, StatefulSet, DaemonSet)
type ReplicaSetInformer struct {
tracker.Tracker
Controller utils.ControllerMetadata
ReplicaSetAdded chan *appsv1.ReplicaSet
ReplicaSetModified chan *appsv1.ReplicaSet
ReplicaSetDeleted chan *appsv1.ReplicaSet
Errors chan error
}
func NewReplicaSetInformer(trk *tracker.Tracker, controller utils.ControllerMetadata) *ReplicaSetInformer {
if debug.Debug() {
fmt.Printf("> NewReplicaSetInformer\n")
}
return &ReplicaSetInformer{
Tracker: tracker.Tracker{
Kube: trk.Kube,
Namespace: trk.Namespace,
FullResourceName: trk.FullResourceName,
},
Controller: controller,
ReplicaSetAdded: make(chan *appsv1.ReplicaSet, 1),
ReplicaSetModified: make(chan *appsv1.ReplicaSet, 1),
ReplicaSetDeleted: make(chan *appsv1.ReplicaSet, 1),
Errors: make(chan error),
}
}
func (r *ReplicaSetInformer) WithChannels(added chan *appsv1.ReplicaSet,
modified chan *appsv1.ReplicaSet,
deleted chan *appsv1.ReplicaSet,
errors chan error,
) *ReplicaSetInformer {
r.ReplicaSetAdded = added
r.ReplicaSetModified = modified
r.ReplicaSetDeleted = deleted
r.Errors = errors
return r
}
func (r *ReplicaSetInformer) Run(ctx context.Context) {
client := r.Kube
selector, err := metav1.LabelSelectorAsSelector(r.Controller.LabelSelector())
if err != nil {
// TODO rescue this error!
return
}
tweakListOptions := func(options metav1.ListOptions) metav1.ListOptions {
options.LabelSelector = selector.String()
return options
}
lw := &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return client.AppsV1().ReplicaSets(r.Namespace).List(ctx, tweakListOptions(options))
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return client.AppsV1().ReplicaSets(r.Namespace).Watch(ctx, tweakListOptions(options))
},
}
go func() {
_, err := watchtools.UntilWithSync(ctx, lw, &appsv1.ReplicaSet{}, nil, func(e watch.Event) (bool, error) {
if debug.Debug() {
fmt.Printf(" %s replica set event: %#v\n", r.FullResourceName, e.Type)
}
var object *appsv1.ReplicaSet
if e.Type != watch.Error {
var ok bool
object, ok = e.Object.(*appsv1.ReplicaSet)
if !ok {
return true, fmt.Errorf("appsv1.ReplicaSet informer for %s got unexpected object %T", r.FullResourceName, e.Object)
}
}
switch e.Type {
case watch.Added:
r.ReplicaSetAdded <- object
case watch.Modified:
r.ReplicaSetModified <- object
case watch.Deleted:
r.ReplicaSetDeleted <- object
}
return false, nil
})
if err := tracker.AdaptInformerError(err); err != nil {
r.Errors <- err
}
if debug.Debug() {
fmt.Printf(" %s replicaSets informer DONE\n", r.FullResourceName)
}
}()
}