-
Notifications
You must be signed in to change notification settings - Fork 56
/
router.go
60 lines (57 loc) · 1.64 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
package main
import (
"log"
"reflect"
"github.com/deis/router/model"
"github.com/deis/router/nginx"
"k8s.io/client-go/1.4/kubernetes"
"k8s.io/client-go/1.4/pkg/util/flowcontrol"
"k8s.io/client-go/1.4/rest"
)
func main() {
nginx.Start()
cfg, err := rest.InClusterConfig()
if err != nil {
log.Fatalf("Failed to create config: %v", err)
}
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
}
}