-
Notifications
You must be signed in to change notification settings - Fork 10
/
router.go
70 lines (67 loc) · 1.95 KB
/
router.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
package main
import (
"log"
"os"
"reflect"
"strconv"
"github.com/teamhephy/router/model"
"github.com/teamhephy/router/nginx"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
)
func main() {
nginx.Start()
cfg, err := rest.InClusterConfig()
if err != nil {
log.Fatalf("Failed to create config: %v", err)
}
if qps, err := strconv.ParseFloat(os.Getenv("RATE_LIMIT_QPS"), 32); err == nil {
cfg.QPS = float32(qps)
log.Printf("INFO: Setting QPS %f\n", qps)
}
if burst, err := strconv.Atoi(os.Getenv("RATE_LIMIT_BURST")); err == nil {
cfg.Burst = burst
log.Printf("INFO: Setting Burst %d\n", burst)
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
log.Fatalf("Failed to create client: %v.", err)
}
rateLimiter := flowcontrol.NewTokenBucketRateLimiter(0.1, 1)
known := &model.RouterConfig{}
// Main loop
for {
rateLimiter.Accept()
routerConfig, err := model.Build(kubeClient)
if err != nil {
log.Printf("Error building model; not modifying certs or configuration: %v.", err)
continue
}
if reflect.DeepEqual(routerConfig, known) {
continue
}
log.Println("INFO: Router configuration has changed in k8s.")
err = nginx.WriteCerts(routerConfig, "/opt/router/ssl")
if err != nil {
log.Printf("Failed to write certs; continuing with existing certs, dhparam, and configuration: %v", err)
continue
}
err = nginx.WriteDHParam(routerConfig, "/opt/router/ssl")
if err != nil {
log.Printf("Failed to write dhparam; continuing with existing dhparam and configuration: %v", err)
continue
}
err = nginx.WriteConfig(routerConfig, "/opt/router/conf/nginx.conf")
if err != nil {
log.Printf("Failed to write new nginx configuration; continuing with existing configuration: %v", err)
continue
}
err = nginx.Reload()
if err != nil {
log.Printf("Failed to reload nginx; continuing with existing configuration: %v", err)
continue
}
known = routerConfig
}
}