This repository has been archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
k8s.go
192 lines (166 loc) · 4.45 KB
/
k8s.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Jussi Maki
package goreactivek8s
import (
"context"
"fmt"
"sync"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/fields"
k8sRuntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
. "github.com/joamaki/goreactive"
)
type Item[T k8sRuntime.Object] struct {
// Key for the object in the form "<namespace>/<name>"
Key string
// The object itself, nil if object was deleted.
Object T
}
func Resource[T k8sRuntime.Object](ctx context.Context, resource string, namespace string, client rest.Interface) Observable[[]Item[T]] {
// Number of unique items in queue before backpressure from subscriber
// TODO: configurable
const queueBufSize = 16
listWatcher := cache.NewListWatchFromClient(
client,
resource,
namespace,
fields.Everything(),
)
var (
mu sync.RWMutex
subId int
queues = make(map[int]workqueue.RateLimitingInterface)
)
// function to push the object to all subscribed queues.
push := func(key string) {
mu.RLock()
for _, queue := range queues {
queue.Add(key)
}
mu.RUnlock()
}
var empty T
indexer, informer := cache.NewIndexerInformer(listWatcher, empty, 0, cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
push(key)
}
},
UpdateFunc: func(old interface{}, new interface{}) {
key, err := cache.MetaNamespaceKeyFunc(new)
if err == nil {
push(key)
}
},
DeleteFunc: func(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
push(key)
}
},
}, cache.Indexers{})
// TODO: wait for Run() to return?
go informer.Run(ctx.Done())
return FuncObservable[[]Item[T]](
func(subCtx context.Context, next func([]Item[T]) error) error {
// Wait for the cache to sync, so we can emit a consistent object set
// as the first item.
if !cache.WaitForCacheSync(ctx.Done(), informer.HasSynced) {
return fmt.Errorf("Timed out waiting for caches to sync")
}
nextErrs := make(chan error, 1)
defer close(nextErrs)
outErr := make(chan error, 1)
// Subscribe to changes first so they would not be missed.
mu.Lock()
subId++
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
queues[subId] = queue
mu.Unlock()
// Fork a goroutine to wait for either context cancellation or
// for completion to then clean up the subscriber queue.
go func() {
var err error
select {
case <-ctx.Done():
err = ctx.Err()
case <-subCtx.Done():
err = ctx.Err()
case err = <-nextErrs:
}
outErr <- err
close(outErr)
mu.Lock()
delete(queues, subId)
mu.Unlock()
queue.ShutDownWithDrain()
}()
// Emit all existing objects as the first slice
var items []Item[T]
initialVersions := make(map[string]string)
for _, obj := range indexer.List() {
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
items = append(items, Item[T]{key, obj.(T)})
initialVersions[key] = resourceVersion(obj)
}
}
if err := next(items); err != nil{
nextErrs <- err
return <-outErr
}
// And then start draining the queue.
for {
rawKey, shutdown := queue.Get()
if shutdown {
break
}
queue.Done(rawKey)
key := rawKey.(string)
rawObj, exists, err := indexer.GetByKey(key)
if err != nil {
// TODO: double check if this handling makes any sense. what possible
// errors could we get from the cache.Store?
if queue.NumRequeues(key) < 5 {
queue.AddRateLimited(rawKey)
continue
}
}
queue.Forget(rawKey)
var obj T
if exists {
obj = rawObj.(T)
}
if len(initialVersions) > 0 {
version := resourceVersion(obj)
if initialVersion, ok := initialVersions[key]; ok {
// We can now forget the initial version.
delete(initialVersions, key)
if initialVersion == version {
// Already emitted, skip.
continue
}
}
}
// TODO: here we could requeue and try the item again on error
items = []Item[T]{{key, obj}}
if err := next(items); err != nil {
nextErrs <- err
return <-outErr
}
}
nextErrs <- nil
return <-outErr
})
}
func resourceVersion(obj any) (version string) {
meta, err := meta.Accessor(obj)
if err == nil {
return meta.GetResourceVersion()
}
return ""
}