This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
132 lines (110 loc) · 3.73 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"flag"
"net/http"
"os"
"time"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
kinkyclientset "github.com/barpilot/kinky/pkg/client/clientset/versioned"
"github.com/barpilot/kinky/pkg/controller"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/kubernetes"
etcdclientset "github.com/coreos/etcd-operator/pkg/generated/clientset/versioned"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
)
var (
kuberconfig = flag.String("kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
master = flag.String("master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
baseHost = flag.String("baseHost", "example.com", "The base dns, to declare ingresses")
listenAddr = flag.String("listen-addr", "0.0.0.0:8080", "The address on which the HTTP server will listen to")
)
func main() {
flag.Parse()
http.Handle("/metrics", prometheus.Handler())
go http.ListenAndServe(*listenAddr, nil)
cfg, err := clientcmd.BuildConfigFromFlags(*master, *kuberconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
k8sClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building etcd clientset: %v", err)
}
id, err := os.Hostname()
if err != nil {
logrus.Fatalf("failed to get hostname: %v", err)
}
rl, err := resourcelock.New(resourcelock.EndpointsResourceLock,
"default",
"kinky-operator",
k8sClient.CoreV1(),
resourcelock.ResourceLockConfig{
Identity: id,
EventRecorder: createRecorder(k8sClient, id, "default"),
})
if err != nil {
logrus.Fatalf("error creating lock: %v", err)
}
leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{
Lock: rl,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: run,
OnStoppedLeading: func() {
logrus.Fatalf("leader election lost")
},
},
})
panic("unreachable")
}
func run(stop <-chan struct{}) {
cfg := newControllerConfig()
c := controller.New(cfg)
c.Run(stop)
}
func newControllerConfig() controller.Config {
cfg, err := clientcmd.BuildConfigFromFlags(*master, *kuberconfig)
if err != nil {
glog.Fatalf("Error building kubeconfig: %v", err)
}
etcdClient, err := etcdclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building etcd clientset: %v", err)
}
k8sClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building etcd clientset: %v", err)
}
apiExtClient, err := apiextensionsclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building etcd clientset: %v", err)
}
kinkyClient, err := kinkyclientset.NewForConfig(cfg)
if err != nil {
glog.Fatalf("Error building kinky clientset: %v", err)
}
config := controller.Config{
BaseHost: *baseHost,
K8sClient: k8sClient,
EtcdClient: etcdClient,
APIExtClient: apiExtClient,
KinkyClient: kinkyClient,
}
return config
}
func createRecorder(kubecli kubernetes.Interface, name, namespace string) record.EventRecorder {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(logrus.Infof)
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: v1core.New(kubecli.Core().RESTClient()).Events(namespace)})
return eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: name})
}