-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
143 lines (123 loc) · 4.56 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
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"flag"
"github.com/yaoice/meliodas/pkg/client"
"github.com/yaoice/meliodas/pkg/openstack/neutron/router"
corev1 "k8s.io/api/core/v1"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
typecorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
"os"
"time"
// Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters).
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"github.com/yaoice/meliodas/pkg/controller"
"github.com/yaoice/meliodas/pkg/signals"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
)
var (
masterURL string
kubeconfig string
kubeClient *kubernetes.Clientset
err error
LockNameSpace string
LockName string
LockComponentName string
LeaderElect bool
LeaseDuration time.Duration
RenewDeadline time.Duration
RetryPeriod time.Duration
)
func main() {
klog.InitFlags(nil)
flag.Parse()
kubeClient, err = client.GetInClusterClientSet()
if err != nil {
kubeClient, err = client.GetClusterClientSetWithKC(masterURL, kubeconfig)
if err != nil {
klog.Fatalf("Error building kubernetes clientset: %s", err.Error())
}
}
if !LeaderElect {
run()
return
}
broadcaster := record.NewBroadcaster()
broadcaster.StartRecordingToSink(&typecorev1.EventSinkImpl{Interface: typecorev1.New(kubeClient.CoreV1().RESTClient()).Events(LockNameSpace)})
recorder := broadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: LockComponentName})
rl, err := resourcelock.New(
resourcelock.EndpointsResourceLock,
LockNameSpace,
LockName,
kubeClient.CoreV1(),
kubeClient.CoordinationV1(),
resourcelock.ResourceLockConfig{
Identity: hostname(),
EventRecorder: recorder,
})
if err != nil {
panic(err)
}
// Try and become the leader and start cloud controller manager loops
leaderelection.RunOrDie(context.Background(),leaderelection.LeaderElectionConfig{
Lock: rl,
LeaseDuration: LeaseDuration,
RenewDeadline: RenewDeadline,
RetryPeriod: RetryPeriod,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: StartedLeading,
OnStoppedLeading: StoppedLeading,
OnNewLeader: NewLeader,
},
})
}
func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
flag.BoolVar(&LeaderElect, "leader-elect", false, "Enable leader election,defalut value: false")
flag.StringVar(&LockNameSpace, "leader-elect-namespace", "kube-system", "The resourcelock namespace,defalut value: operator")
flag.StringVar(&LockName, "leader-elect-lock-name", "kong-controller-leader-elect-lock", "The resourcelock name,defalut value: l5-controller-leader-elect-lock")
flag.StringVar(&LockComponentName, "leader-elect-componentname", "leader-elector", "The resourcelock namespace,defalut value: leader-elector" )
flag.DurationVar(&LeaseDuration, "leader-elect-lease-duration", 15*time.Second, "The leader-elect LeaseDuration")
flag.DurationVar(&RenewDeadline, "leader-elect-renew-deadline", 10*time.Second, "The leader-elect RenewDeadline")
flag.DurationVar(&RetryPeriod, "leader-elect-retry-period", 2*time.Second, "The leader-elect RetryPeriod")
}
func StartedLeading(ctx context.Context) {
klog.Infof("%s: started leading", hostname())
run()
}
func run() {
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
kubeInformerFactory := kubeinformers.NewSharedInformerFactory(kubeClient, time.Second*30)
neutronRouter, err := router.NewNeutronRouter()
if err != nil {
panic(err)
}
controller := controller.NewController(kubeClient, kubeInformerFactory.Core().V1().Pods(), neutronRouter)
// notice that there is no need to run Start methods in a separate goroutine. (i.e. go kubeInformerFactory.Start(stopCh)
// Start method is non-blocking and runs all registered informers in a dedicated goroutine.
kubeInformerFactory.Start(stopCh)
if err := controller.Run(2, stopCh); err != nil {
klog.Fatalf("Error running controller: %s", err.Error())
}
}
func StoppedLeading() {
klog.Infof("%s: stopped leading", hostname())
os.Exit(0)
}
func NewLeader(id string) {
klog.Infof("%s: new leader: %s", hostname(), id)
}
func hostname() string {
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
return hostname
}