-
Notifications
You must be signed in to change notification settings - Fork 87
/
appstate.go
261 lines (223 loc) · 6.55 KB
/
appstate.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package appstate
import (
"context"
"log"
"sync"
"time"
"github.com/replicatedhq/kots/pkg/appstate/types"
corev1 "k8s.io/api/core/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
)
type Monitor struct {
clientset kubernetes.Interface
targetNamespace string
appInformersCh chan appInformer
appStatusCh chan types.AppStatus
cancel context.CancelFunc
}
type EventHandler interface {
ObjectCreated(obj interface{})
ObjectUpdated(obj interface{})
ObjectDeleted(obj interface{})
}
type appInformer struct {
appID string
sequence int64
informers []types.StatusInformer
}
func NewMonitor(clientset kubernetes.Interface, targetNamespace string) *Monitor {
if targetNamespace == "" {
targetNamespace = corev1.NamespaceDefault
}
ctx, cancel := context.WithCancel(context.Background())
m := &Monitor{
clientset: clientset,
targetNamespace: targetNamespace,
appInformersCh: make(chan appInformer),
appStatusCh: make(chan types.AppStatus),
cancel: cancel,
}
go m.run(ctx)
return m
}
func (m *Monitor) Shutdown() {
m.cancel()
}
func (m *Monitor) Apply(appID string, sequence int64, informers []types.StatusInformer) {
m.appInformersCh <- struct {
appID string
sequence int64
informers []types.StatusInformer
}{
appID: appID,
sequence: sequence,
informers: informers,
}
}
func (m *Monitor) AppStatusChan() <-chan types.AppStatus {
return m.appStatusCh
}
func (m *Monitor) run(ctx context.Context) {
log.Println("Starting monitor loop")
defer close(m.appStatusCh)
appMonitors := make(map[string]*AppMonitor)
defer func() {
for _, appMonitor := range appMonitors {
appMonitor.Shutdown()
}
}()
for {
select {
case <-ctx.Done():
return
case appInformer := <-m.appInformersCh:
appMonitor, ok := appMonitors[appInformer.appID]
if !ok || appMonitor.sequence != appInformer.sequence {
if appMonitor != nil {
appMonitor.Shutdown()
}
appMonitor = NewAppMonitor(m.clientset, m.targetNamespace, appInformer.appID, appInformer.sequence)
go func() {
for appStatus := range appMonitor.AppStatusChan() {
m.appStatusCh <- appStatus
}
}()
appMonitors[appInformer.appID] = appMonitor
}
appMonitor.Apply(appInformer.informers)
}
}
}
type AppMonitor struct {
clientset kubernetes.Interface
targetNamespace string
appID string
informersCh chan []types.StatusInformer
appStatusCh chan types.AppStatus
cancel context.CancelFunc
sequence int64
}
func NewAppMonitor(clientset kubernetes.Interface, targetNamespace, appID string, sequence int64) *AppMonitor {
ctx, cancel := context.WithCancel(context.Background())
m := &AppMonitor{
appID: appID,
clientset: clientset,
targetNamespace: targetNamespace,
informersCh: make(chan []types.StatusInformer),
appStatusCh: make(chan types.AppStatus),
cancel: cancel,
sequence: sequence,
}
go m.run(ctx)
return m
}
func (m *AppMonitor) Shutdown() {
m.cancel()
}
func (m *AppMonitor) Apply(informers []types.StatusInformer) {
m.informersCh <- informers
}
func (m *AppMonitor) AppStatusChan() <-chan types.AppStatus {
return m.appStatusCh
}
func (m *AppMonitor) run(ctx context.Context) {
log.Println("Starting app monitor loop")
defer close(m.informersCh)
defer close(m.appStatusCh)
prevCancel := context.CancelFunc(func() {})
defer func() {
// wrap this in a function to cancel the variable when updated
prevCancel()
}()
for {
select {
case <-ctx.Done():
return
case informers := <-m.informersCh:
prevCancel() // cancel previous loop
log.Println("App monitor got new informers")
ctx, cancel := context.WithCancel(ctx)
prevCancel = cancel
go m.runInformers(ctx, informers)
}
}
}
type runControllerFunc func(context.Context, kubernetes.Interface, string, []types.StatusInformer, chan<- types.ResourceState)
func (m *AppMonitor) runInformers(ctx context.Context, informers []types.StatusInformer) {
informers = normalizeStatusInformers(informers, m.targetNamespace)
log.Printf("Running informers: %#v", informers)
appStatus := types.AppStatus{
AppID: m.appID,
ResourceStates: buildResourceStatesFromStatusInformers(informers),
UpdatedAt: time.Now(),
Sequence: m.sequence,
}
m.appStatusCh <- appStatus // reset last app status
var shutdown sync.WaitGroup
resourceStateCh := make(chan types.ResourceState)
defer func() {
shutdown.Wait()
close(resourceStateCh)
}()
// Collect namespace/kind pairs
namespaceKinds := make(map[string]map[string][]types.StatusInformer)
for _, informer := range informers {
kindsInNs, ok := namespaceKinds[informer.Namespace]
if !ok {
kindsInNs = make(map[string][]types.StatusInformer)
}
kindsInNs[informer.Kind] = append(kindsInNs[informer.Kind], informer)
namespaceKinds[informer.Namespace] = kindsInNs
}
goRun := func(fn runControllerFunc, namespace string, informers []types.StatusInformer) {
shutdown.Add(1)
go func() {
fn(ctx, m.clientset, namespace, informers, resourceStateCh)
shutdown.Done()
}()
}
kindImpls := map[string]runControllerFunc{
DaemonSetResourceKind: runDaemonSetController,
DeploymentResourceKind: runDeploymentController,
IngressResourceKind: runIngressController,
PersistentVolumeClaimResourceKind: runPersistentVolumeClaimController,
ServiceResourceKind: runServiceController,
StatefulSetResourceKind: runStatefulSetController,
}
for namespace, kinds := range namespaceKinds {
for kind, informers := range kinds {
if impl, ok := kindImpls[kind]; ok {
goRun(impl, namespace, informers)
} else {
log.Printf("Informer requested for unsupported resource kind %v", kind)
}
}
}
for {
select {
case <-ctx.Done():
return
case resourceState := <-resourceStateCh:
appStatus.ResourceStates, _ = resourceStatesApplyNew(appStatus.ResourceStates, informers, resourceState)
appStatus.UpdatedAt = time.Now() // TODO: this should come from the informer
m.appStatusCh <- appStatus
}
}
}
func runInformer(ctx context.Context, informer cache.SharedInformer, eventHandler EventHandler) {
defer utilruntime.HandleCrash()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
eventHandler.ObjectCreated(obj)
},
UpdateFunc: func(old, new interface{}) {
eventHandler.ObjectUpdated(new)
},
DeleteFunc: func(obj interface{}) {
eventHandler.ObjectDeleted(obj)
},
})
informer.Run(ctx.Done())
}