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
package providermain
import (
"context"
"errors"
"fmt"
"log/slog"
"sync"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)
const (
TenantWatchRetryBase = time.Second
TenantWatchRetryMax = time.Minute
)
var errTenantWatchClosed = errors.New("clusterprovider: the tenant watch closed")
type tenantWatch struct {
tenant func(ctx context.Context, cluster string) (kubernetes.Interface, error)
notify func(cluster string)
log *slog.Logger
mu sync.Mutex
life context.Context
running map[string]context.CancelFunc
wg sync.WaitGroup
}
func newTenantWatch(tenant func(ctx context.Context, cluster string) (kubernetes.Interface, error), notify func(string), log *slog.Logger) *tenantWatch {
return &tenantWatch{tenant: tenant, notify: notify, log: log, running: map[string]context.CancelFunc{}}
}
func (t *tenantWatch) start(ctx context.Context) {
t.mu.Lock()
t.life = ctx
t.mu.Unlock()
}
func (t *tenantWatch) ensure(cluster string) {
t.mu.Lock()
defer t.mu.Unlock()
if _, ok := t.running[cluster]; ok || t.life == nil || t.life.Err() != nil {
return
}
ctx, cancel := context.WithCancel(t.life)
t.running[cluster] = cancel
t.wg.Go(func() { t.run(ctx, cluster) })
}
func (t *tenantWatch) stop(cluster string) {
t.mu.Lock()
cancel, ok := t.running[cluster]
delete(t.running, cluster)
t.mu.Unlock()
if ok {
cancel()
}
}
func (t *tenantWatch) stopAll() {
t.mu.Lock()
for cluster, cancel := range t.running {
cancel()
delete(t.running, cluster)
}
t.mu.Unlock()
t.wg.Wait()
}
func (t *tenantWatch) run(ctx context.Context, cluster string) {
retry := TenantWatchRetryBase
for ctx.Err() == nil {
started := time.Now()
err := t.follow(ctx, cluster)
if ctx.Err() != nil {
return
}
if time.Since(started) > TenantWatchRetryMax {
retry = TenantWatchRetryBase
}
t.log.Debug("cluster provider: the tenant watch ended; it reconnects", "cluster", cluster,
"retryIn", retry.String(), "err", fmt.Sprint(err))
timer := time.NewTimer(retry)
select {
case <-ctx.Done():
timer.Stop()
return
case <-timer.C:
}
retry = min(retry*2, TenantWatchRetryMax)
}
}
type podMark struct {
phase corev1.PodPhase
node string
sched corev1.ConditionStatus
}
func (t *tenantWatch) follow(ctx context.Context, cluster string) error {
client, err := t.tenant(ctx, cluster)
if err != nil {
return err
}
pods, err := client.CoreV1().Pods(metav1.NamespaceAll).Watch(ctx, metav1.ListOptions{})
if err != nil {
return err
}
defer pods.Stop()
nodes, err := client.CoreV1().Nodes().Watch(ctx, metav1.ListOptions{})
if err != nil {
return err
}
defer nodes.Stop()
podMarks := map[string]podMark{}
nodeMarks := map[string]string{}
for {
select {
case <-ctx.Done():
return nil
case ev, ok := <-pods.ResultChan():
if !ok {
return errTenantWatchClosed
}
if ev.Type == watch.Error {
return fmt.Errorf("clusterprovider: the tenant pod watch of %s: %v", cluster, ev.Object)
}
pod, ok := ev.Object.(*corev1.Pod)
if !ok {
continue
}
id := pod.Namespace + "/" + pod.Name
if ev.Type == watch.Deleted {
delete(podMarks, id)
t.notify(cluster)
continue
}
mark := podMark{phase: pod.Status.Phase, node: pod.Spec.NodeName}
for _, c := range pod.Status.Conditions {
if c.Type == corev1.PodScheduled {
mark.sched = c.Status
}
}
if prev, seen := podMarks[id]; !seen || prev != mark {
podMarks[id] = mark
t.notify(cluster)
}
case ev, ok := <-nodes.ResultChan():
if !ok {
return errTenantWatchClosed
}
if ev.Type == watch.Error {
return fmt.Errorf("clusterprovider: the tenant node watch of %s: %v", cluster, ev.Object)
}
node, ok := ev.Object.(*corev1.Node)
if !ok {
continue
}
if ev.Type == watch.Deleted {
delete(nodeMarks, node.Name)
t.notify(cluster)
continue
}
mark := fmt.Sprint(node.Spec.Unschedulable)
for _, c := range node.Status.Conditions {
if c.Type == corev1.NodeReady {
mark += "/" + string(c.Status)
}
}
if prev, seen := nodeMarks[node.Name]; !seen || prev != mark {
nodeMarks[node.Name] = mark
t.notify(cluster)
}
}
}
}