forked from deislabs/osiris
/
app_activation.go
132 lines (124 loc) · 2.85 KB
/
app_activation.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
package activator
import (
"context"
"sync"
"time"
"github.com/golang/glog"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
k8s "github.com/dailymotion-oss/osiris/pkg/kubernetes"
)
type appActivation struct {
readyAppPodIPs map[string]struct{}
endpoints *corev1.Endpoints
lock sync.Mutex
done bool
successCh chan struct{}
timeoutCh chan struct{}
dependencies []*appActivation
}
func (a *appActivation) watchForCompletion(
kubeClient kubernetes.Interface,
app *app,
appPodSelector labels.Selector,
) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Watch the pods managed by this deployment/statefulSet
podsInformer := k8s.PodsIndexInformer(
kubeClient,
app.Namespace,
nil,
appPodSelector,
0,
)
podsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: a.syncPod,
UpdateFunc: func(_, newObj interface{}) {
a.syncPod(newObj)
},
DeleteFunc: a.syncPod,
})
// Watch the corresponding endpoints resource for this service
endpointsInformer := k8s.EndpointsIndexInformer(
kubeClient,
app.Namespace,
fields.OneTermEqualSelector(
"metadata.name",
app.ServiceName,
),
nil,
0,
)
endpointsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: a.syncEndpoints,
UpdateFunc: func(_, newObj interface{}) {
a.syncEndpoints(newObj)
},
})
go podsInformer.Run(ctx.Done())
go endpointsInformer.Run(ctx.Done())
timer := time.NewTimer(2 * time.Minute)
defer timer.Stop()
for {
select {
case <-a.successCh:
return
case <-timer.C:
glog.Errorf(
"Activation of %s %s in namespace %s timed out",
app.Kind,
app.Name,
app.Namespace,
)
close(a.timeoutCh)
return
}
}
}
func (a *appActivation) syncPod(obj interface{}) {
a.lock.Lock()
defer a.lock.Unlock()
pod := obj.(*corev1.Pod)
var ready bool
for _, condition := range pod.Status.Conditions {
if condition.Type == corev1.PodReady {
if condition.Status == corev1.ConditionTrue {
ready = true
}
break
}
}
// Keep track of which pods are ready
if ready {
a.readyAppPodIPs[pod.Status.PodIP] = struct{}{}
} else {
delete(a.readyAppPodIPs, pod.Status.PodIP)
}
a.checkActivationComplete()
}
func (a *appActivation) syncEndpoints(obj interface{}) {
a.lock.Lock()
defer a.lock.Unlock()
a.endpoints = obj.(*corev1.Endpoints)
a.checkActivationComplete()
}
func (a *appActivation) checkActivationComplete() {
if a.endpoints != nil {
for _, subset := range a.endpoints.Subsets {
for _, address := range subset.Addresses {
if _, ok := a.readyAppPodIPs[address.IP]; ok {
glog.Infof("App pod with ip %s is in service", address.IP)
if !a.done {
close(a.successCh)
a.done = true
}
return
}
}
}
}
}