-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
95 lines (83 loc) · 3.48 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
94
95
package main
import (
"errors"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"github.com/albertrdixon/gearbox/logger"
"github.com/timelinelabs/romulus/kubernetes"
"github.com/timelinelabs/romulus/loadbalancer"
"github.com/timelinelabs/romulus/loadbalancer/traefik"
"github.com/timelinelabs/romulus/loadbalancer/vulcand"
"golang.org/x/net/context"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
lbs = []string{"vulcand", "traefik"}
ro = kingpin.New("romulusd", "A kubernetes ingress controller")
kubeAPI = ro.Flag("kube-api", "URL for kubernetes api").Short('k').Default("http://127.0.0.1:8080").OverrideDefaultFromEnvar("KUBE_API").URL()
kubeVer = ro.Flag("kube-api-ver", "kubernetes api version").Default("v1").OverrideDefaultFromEnvar("KUBE_API_VER").String()
kubeUser = ro.Flag("kube-user", "kubernetes username").String()
kubePass = ro.Flag("kube-pass", "kubernetes password").String()
kubeSec = ro.Flag("kube-insecure", "Run kubernetes client in insecure mode").OverrideDefaultFromEnvar("KUBE_INSECURE").Bool()
selector = ro.Flag("selector", "label selectors. Leave blank for Everything(). Form: key=value").Short('s').PlaceHolder("label=value").OverrideDefaultFromEnvar("SVC_SELECTOR").StringMap()
annoKey = ro.Flag("annotations-prefix", "annotations key prefix").Short('a').Default("romulus/").String()
provider = ro.Flag("provider", "LoadBalancer provider").Short('p').Default("vulcand").Enum(lbs...)
resync = ro.Flag("sync-interval", "Resync period with kube api").Default("1h").Duration()
timeout = ro.Flag("lb-timeout", "Timeout for communicating with loadbalancer provider").Default("10s").Duration()
vulcanAPI = ro.Flag("vulcand-api", "URL for vulcand api").Default("http://127.0.0.1:8182").OverrideDefaultFromEnvar("VULCAND_API").URL()
traefikEtcd = ro.Flag("traefik-etcd", "etcd peers for traefik").OverrideDefaultFromEnvar("TRAEFIK_ETCD").URLList()
logLevel = ro.Flag("log-level", "log level. One of: fatal, error, warn, info, debug").Short('l').Default("info").OverrideDefaultFromEnvar("LOG_LEVEL").Enum(logger.Levels...)
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
kingpin.Version(getVersion())
kingpin.MustParse(ro.Parse(os.Args[1:]))
logger.Configure(*logLevel, "[romulusd] ", os.Stdout)
logger.Infof("Starting up romulusd version=%s", getVersion())
ctx, cancel := context.WithCancel(context.Background())
lb, er := getLBProvider(*provider, ctx)
if er != nil {
logger.Fatalf(er.Error())
}
kubernetes.Keyspace = normalizeAnnotationsKey(*annoKey)
ng, er := NewEngine((*kubeAPI).String(), *kubeUser, *kubePass, *kubeSec, lb, *timeout, ctx)
if er != nil {
logger.Fatalf(er.Error())
}
if er := ng.Start(*selector, *resync); er != nil {
logger.Fatalf(er.Error())
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
select {
case <-sig:
logger.Infof("Shutting Down...")
cancel()
time.Sleep(100 * time.Millisecond)
os.Exit(0)
}
}
func getLBProvider(kind string, c context.Context) (loadbalancer.LoadBalancer, error) {
switch kind {
default:
return nil, errors.New("Unknown LB type")
case "vulcand":
return vulcand.New((*vulcanAPI).String(), nil, c)
case "traefik":
peers := make([]string, 0, len(*traefikEtcd))
for _, u := range *traefikEtcd {
peers = append(peers, u.String())
}
return traefik.New(traefik.DefaultPrefix, peers, *timeout, c)
}
}
func normalizeAnnotationsKey(key string) string {
if !strings.HasSuffix(key, "/") {
return key + "/"
}
return key
}