-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
67 lines (53 loc) · 1.44 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
package main
import (
"flag"
"log"
"time"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
consulapi "github.com/hashicorp/consul/api"
)
const (
resyncPeriod = 5 * time.Minute
)
var (
kubeCfgFile = flag.String("kube-config", "", "Kubernetes Config File")
consulAddress = flag.String("consul-address", "localhost:8500", "Consul Server address")
)
func main() {
flag.Parse()
kubeClient, err := newKubeClient(*kubeCfgFile)
if err != nil {
log.Fatal(err)
}
consulClient, err := newConsulClient(*consulAddress)
if err != nil {
log.Fatal(err)
}
kc := newKube2Consul(kubeClient, consulClient)
kc.watchForServices()
select {}
}
// newKubeClient creates a new Kubernetes API Client
func newKubeClient(kubeCfgFile string) (*kclient.Client, error) {
rules := &kclientcmd.ClientConfigLoadingRules{ExplicitPath: kubeCfgFile}
config, err := kclientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &kclientcmd.ConfigOverrides{}).ClientConfig()
if err != nil {
return nil, err
}
client, err := kclient.New(config)
if err != nil {
return nil, err
}
return client, nil
}
// newConsulClient creates a new Consul API Client
func newConsulClient(serverAddress string) (*consulapi.Client, error) {
config := consulapi.DefaultConfig()
config.Address = serverAddress
client, err := consulapi.NewClient(config)
if err != nil {
return nil, err
}
return client, nil
}