-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
93 lines (81 loc) · 2.03 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"golang.org/x/sync/errgroup"
"github.com/luizalabs/mitose/config"
"github.com/luizalabs/mitose/controller"
"github.com/luizalabs/mitose/gauge"
"github.com/luizalabs/mitose/k8s"
)
func main() {
currentNS, err := k8s.GetCurrentNamespace()
if err != nil {
printErrorAndExit("getting current namespace name", err)
}
configWatcher := getConfigWatcher(currentNS)
go gauge.Run()
for {
ctx, cancel := context.WithCancel(context.Background())
errChan := make(chan error)
go func() { errChan <- run(ctx, currentNS) }()
select {
case err, ok := <-configWatcher:
if err != nil {
printErrorAndExit("watching configmap", err)
}
if !ok {
configWatcher = getConfigWatcher(currentNS)
}
log.Println("rebuilding controllers")
cancel()
case err := <-errChan:
log.Println("error received on errChan:", err)
if err != nil && err != context.Canceled {
printErrorAndExit("running controllers", err)
}
}
}
}
func getConfigWatcher(namespace string) <-chan error {
configWatcher, err := k8s.WatchConfigMap(namespace)
if err != nil {
printErrorAndExit("watching configmaps", err)
}
<-configWatcher // expected at least one config map
return configWatcher
}
func run(ctx context.Context, currentNS string) error {
configData, err := k8s.GetConfigMapData(currentNS, "config")
if err != nil {
return err
}
controllers := make([]*controller.Controller, 0)
for _, v := range configData {
conf := new(config.Config)
if err := json.Unmarshal([]byte(v), conf); err != nil {
return err
}
if !conf.Active {
continue
}
c, err := controller.Factory(conf.Type, v)
if err != nil {
return err
}
controllers = append(controllers, c)
}
g, ctx := errgroup.WithContext(ctx)
for _, currentController := range controllers {
c := currentController
g.Go(func() error { return c.Run(ctx) })
}
return g.Wait()
}
func printErrorAndExit(phase string, err error) {
fmt.Fprintf(os.Stderr, "error %s: %s", phase, err)
os.Exit(2)
}