/
k8s.go
390 lines (334 loc) · 11.4 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2018-2022 (c) The Go Service Components authors. All rights reserved. Issued under the Apache 2.0 License.
package server // import "github.com/leaf-ai/go-service/pkg/server"
// This file contains functions related to Kubernetes (k8s) support for pod based servers.
// The server can use k8s to watch and load ConfigMap information that it can use
// to manage its life cycle and in the future to load configuration information.
//
// The choice to make use of the package from Eric Chiang is driven by the
// package dependency issues with using the official go client. It rivals
// the spagetti dependencies of Dockers buildkit, borderline horrific. The chosen
// package has a single dependency and trades off using generated protobuf structures
// and so it wired to the k8s versions via that method, a tradeoff I'm willing to
// make based on my attempts with BuildKit.
import (
"context"
"fmt"
"github.com/leaf-ai/go-service/pkg/log"
"os"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/karlmutch/k8s"
core "github.com/karlmutch/k8s/apis/core/v1"
"github.com/go-stack/stack"
"github.com/lthibault/jitterbug"
"github.com/jjeffery/kv" // MIT License
"github.com/leaf-ai/go-service/pkg/types"
)
var (
k8sClient *k8s.Client
k8sInitErr kv.Error
_ = attemptK8sStart()
protect sync.Mutex
)
func attemptK8sStart() (err kv.Error) {
protect.Lock()
defer protect.Unlock()
if client, errGo := k8s.NewInClusterClient(); errGo != nil {
k8sInitErr = kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
} else {
k8sClient = client
}
return k8sInitErr
}
func watchCMaps(ctx context.Context, namespace string) (cmChange chan *core.ConfigMap, err kv.Error) {
configMap := core.ConfigMap{}
watcher, errGo := k8sClient.Watch(ctx, namespace, &configMap)
if errGo != nil {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
cmChange = make(chan *core.ConfigMap, 1)
go func() {
defer func() {
if watcher != nil {
watcher.Close() // Always close the returned watcher.
}
}()
for {
cm := &core.ConfigMap{}
// Next does not support cancellation and is blocking so we have to
// abandon this thread and simply let it run unmanaged
_, err := watcher.Next(cm)
if err != nil {
select {
case <-ctx.Done():
return
default:
}
if watcher != nil {
watcher.Close()
watcher = nil
}
// watcher encountered and error, create a new watcher
watcher, _ = k8sClient.Watch(ctx, namespace, &configMap)
continue
}
select {
case cmChange <- cm:
case <-time.After(time.Second):
spew.Dump(*cm)
}
}
}()
return cmChange, nil
}
func K8sUpdateSecret(config string, secret string, content []byte) (err kv.Error) {
if err := IsAliveK8s(); err != nil {
return err
}
// The downward API within K8s is configured within the build YAML
// to pass the pods namespace into the pods environment table.
namespace, isPresent := os.LookupEnv("K8S_NAMESPACE")
if !isPresent {
return kv.NewError("K8S_NAMESPACE missing").With("stack", stack.Trace().TrimRuntime())
}
// Use the kubernetes client to modify the config map
client, errGo := k8s.NewInClusterClient()
if errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
signatures := &core.Secret{}
if errGo = client.Get(context.Background(), namespace, config, signatures); errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
signatures.Data[secret] = content
if errGo := client.Update(context.Background(), signatures); errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
return nil
}
// MonitorK8s is used to send appropriate errors into an error reporting channel
// on a regular basis if the k8s connectivity state changes
//
func MonitorK8s(ctx context.Context, errC chan<- kv.Error) {
t := jitterbug.New(time.Second*30, &jitterbug.Norm{Stdev: time.Second * 3})
defer t.Stop()
k8sLastErr := kv.NewError("")
for {
select {
case <-ctx.Done():
return
case <-t.C:
func() {
attemptRestart := false
protect.Lock()
if k8sClient == nil {
if k8sInitErr != nil && k8sInitErr != k8sLastErr {
// Attempt to send the error back to any listener and timeout after a second.
// The jitter based timer will provide another opportunity in 30 or thereabouts
// seconds should this timeout
select {
case errC <- k8sInitErr:
k8sLastErr = k8sInitErr
case <-time.After(time.Second):
}
}
attemptRestart = true
}
protect.Unlock()
if attemptRestart {
// Doing this re-applies the lock so we do it outside of the earlier block
attemptK8sStart()
}
}()
}
}
}
// IsAliveK8s is used to extract any kv.in the state of the k8s client api connection.
//
// A nil returned indicates k8s is working and in use, otherwise a descriptive error
// is returned.
//
func IsAliveK8s() (err kv.Error) {
protect.Lock()
defer protect.Unlock()
if k8sClient == nil {
k8sInitErr = kv.NewError("Kubernetes uninitialized or no cluster present").With("stack", stack.Trace().TrimRuntime())
}
return k8sInitErr
}
// ConfigK8s is used to pull the values from a named config map in k8s
//
// This function will return an empty map and and error value on failure.
//
func ConfigK8s(ctx context.Context, namespace string, name string) (values map[string]string, err kv.Error) {
values = map[string]string{}
if err = IsAliveK8s(); err != nil {
return values, nil
}
cfg := &core.ConfigMap{}
if errGo := k8sClient.Get(ctx, namespace, name, cfg); errGo != nil {
return values, kv.Wrap(errGo).With("namespace", namespace).With("name", name).With("stack", stack.Trace().TrimRuntime())
}
if name == *cfg.Metadata.Name {
fmt.Println(spew.Sdump(cfg.Data), stack.Trace().TrimRuntime())
return cfg.Data, nil
}
return values, kv.NewError("configMap not found").With("namespace", namespace).With("name", name).With("stack", stack.Trace().TrimRuntime())
}
// K8sStateUpdate encapsulates the known kubernetes state within which the server finds itself.
//
type K8sStateUpdate struct {
Name string
State types.K8sState
}
// K8sConfigUpdate encapsulates an update for some config map in the scope of server run-time environment.
//
type K8sConfigUpdate struct {
NameSpace string
Name string
State map[string]string
}
// ListenK8s will register a listener to watch for pod specific configMaps in k8s
// and will relay state changes to a channel, the global state map should exist
// at the bare minimum. A state change in either map superseded any previous
// state.
//
// This is a blocking function that will return either upon an error in API calls
// to the cluster API or when the ctx is Done().
//
func ListenK8s(ctx context.Context, namespace string, globalMap string, podMap string, updateC chan<- K8sStateUpdate, errC chan<- kv.Error, logger *log.Logger) (err kv.Error) {
// If k8s is not being used ignore this feature
if err = IsAliveK8s(); err != nil {
return err
}
// Starts the application level state watching
currentState := K8sStateUpdate{
State: types.K8sUnknown,
}
// Start the k8s configMap watcher
cmChanges, err := watchCMaps(ctx, namespace)
if err != nil {
// The implication of an error here is that we will never get updates from k8s
return err
}
fmt.Println("k8s watcher starting in namespace", namespace)
defer fmt.Println("k8s watcher stopping")
// Once every 3 minutes for so we will force the state propagation
// to ensure that modules started after this module has started see something
refresh := jitterbug.New(time.Minute*3, &jitterbug.Norm{Stdev: time.Second * 15})
defer refresh.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-refresh.C:
// Try resending an existing state to listeners to refresh things
select {
case updateC <- currentState:
case <-time.After(2 * time.Second):
}
case cm := <-cmChanges:
if cm == nil {
fmt.Println("k8s watcher channel closed", namespace)
return
}
if logger != nil {
logger.Debug("DETECTED ConfigMap update: ", "configMap: ", *cm)
}
if *cm.Metadata.Namespace == namespace && (*cm.Metadata.Name == globalMap || *cm.Metadata.Name == podMap) {
currentState = processState(cm, currentState, updateC, errC)
}
}
}
}
// ListenK8sConfigMaps will register a listener to watch for k8s config maps updates in a specified namespace
// and will relay these changes to a channel.
//
// This is a blocking function that will return either upon an error in API calls
// to the cluster API or when the ctx is Done().
//
func ListenK8sConfigMaps(ctx context.Context, namespace string, updateC chan<- K8sConfigUpdate, errC chan<- kv.Error, logger *log.Logger) (err kv.Error) {
// If k8s is not being used ignore this feature
if err = IsAliveK8s(); err != nil {
return err
}
hasUpdate := false
lastConfigUpdate := K8sConfigUpdate{}
// Start the k8s configMap watcher
cmChanges, err := watchCMaps(ctx, namespace)
if err != nil {
// The implication of an error here is that we will never get updates from k8s
return err
}
logger.Info("k8s config watcher starting in namespace", namespace)
defer logger.Info("k8s config watcher stopping for namespace", namespace)
// Once every 2 minutes or so we will force the state propagation
// to ensure that modules started after this module has started see something
refresh := jitterbug.New(time.Minute*2, &jitterbug.Norm{Stdev: time.Second * 15})
defer refresh.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-refresh.C:
// Try resending the latest update to listeners to refresh things
if hasUpdate {
select {
case updateC <- lastConfigUpdate:
case <-time.After(2 * time.Second):
}
}
case cm := <-cmChanges:
if cm == nil {
logger.Info("k8s config watcher channel closed", namespace)
return
}
logger.Debug("RECEIVED ConfigMap update: ", "configMap: ", *cm)
lastConfigUpdate = K8sConfigUpdate{
NameSpace: namespace,
Name: *cm.Metadata.Name,
State: cm.Data,
}
hasUpdate = true
select {
case updateC <- lastConfigUpdate:
case <-time.After(2 * time.Second):
}
}
}
}
func processState(cm *core.ConfigMap, currentState K8sStateUpdate, updateC chan<- K8sStateUpdate, errC chan<- kv.Error) (newState K8sStateUpdate) {
if state := cm.Data["STATE"]; len(state) != 0 {
newState, errGo := types.K8sStateString(state)
if errGo != nil {
msg := kv.Wrap(errGo).With("namespace", *cm.Metadata.Namespace).With("config", *cm.Metadata.Name).With("state", state).With("stack", stack.Trace().TrimRuntime())
select {
case errC <- msg:
case <-time.After(2 * time.Second):
fmt.Println(errGo)
}
}
if newState == currentState.State && *cm.Metadata.Name == currentState.Name {
return currentState
}
update := K8sStateUpdate{
Name: *cm.Metadata.Name,
State: newState,
}
// Try sending the new state to listeners within the server invoking this function
select {
case updateC <- update:
currentState = update
case <-time.After(2 * time.Second):
// If the message could not be sent try to wakeup the error logger
msg := kv.NewError("could not update state").With("namespace", *cm.Metadata.Namespace).With("config", *cm.Metadata.Name).With("state", state).With("stack", stack.Trace().TrimRuntime())
select {
case errC <- msg:
case <-time.After(2 * time.Second):
fmt.Println(msg)
}
}
}
return currentState
}