-
Notifications
You must be signed in to change notification settings - Fork 34
/
k8s.go
85 lines (67 loc) · 1.64 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
package config
import (
"context"
"fmt"
"log/slog"
"go.flipt.io/reverst/internal/k8s"
"gopkg.in/yaml.v2"
v1 "k8s.io/api/core/v1"
)
func watchK8sConfigMap(ctx context.Context, ch chan<- *TunnelGroups, namespace, name, key string, watch bool) error {
watcher, err := k8s.New(namespace, name)
if err != nil {
return err
}
// get initial configmap before we return to ensure
// we fail fast if we can't get at-least one
cfg, err := watcher.Get(ctx)
if err != nil {
return err
}
groups, err := buildTunnelGroupsFromConfigMap(cfg, key)
if err != nil {
return err
}
ch <- groups
if !watch {
return nil
}
slog.Info("Starting ConfigMap watcher", "namespace", namespace, "name", name)
cfgs := make(chan v1.ConfigMap)
watcher.StartWatching(ctx, cfgs)
go func() {
defer close(ch)
defer close(cfgs)
for {
select {
case <-ctx.Done():
return
case cfg, ok := <-cfgs:
if !ok {
return
}
groups, err := buildTunnelGroupsFromConfigMap(cfg, key)
if err != nil {
slog.Error("Building tunnel groups from ConfigMap", "error", err, "namespace", namespace, "name", name)
continue
}
ch <- groups
}
}
}()
return nil
}
func buildTunnelGroupsFromConfigMap(cfg v1.ConfigMap, key string) (*TunnelGroups, error) {
raw, ok := cfg.Data[key]
if !ok {
return nil, fmt.Errorf("key %q not found in ConfigMap", key)
}
var groups TunnelGroups
if err := yaml.Unmarshal([]byte(raw), &groups); err != nil {
return nil, fmt.Errorf("decoding tunnel groups: %w", err)
}
if err := groups.Validate(); err != nil {
return nil, fmt.Errorf("validating tunnel groups: %w", err)
}
return &groups, nil
}