-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
175 lines (152 loc) · 5.65 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/StatCan/ingress-istio-controller/pkg/controller"
istio "istio.io/client-go/pkg/clientset/versioned"
istioinformers "istio.io/client-go/pkg/informers/externalversions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/transport"
"k8s.io/klog"
)
var (
masterURL string
kubeconfig string
clusterDomain string
defaultGateway string
scopedGateways bool
ingressClass string
defaultWeight int
lockName string
lockNamespace string
lockIdentity string
)
func main() {
klog.InitFlags(nil)
flag.Parse()
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
if err != nil {
klog.Fatalf("error building kubeconfig: %v", err)
}
kubeclient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Fatalf("error building kubernetes clientset: %v", err)
}
istioclient, err := istio.NewForConfig(cfg)
if err != nil {
klog.Fatalf("error building istio client: %v", err)
}
kubeInformerFactory := kubeinformers.NewSharedInformerFactory(kubeclient, time.Second*30)
istioInformerFactory := istioinformers.NewSharedInformerFactory(istioclient, time.Second*30)
ctlr := controller.NewController(
kubeclient,
istioclient,
clusterDomain,
defaultGateway,
scopedGateways,
ingressClass,
defaultWeight,
kubeInformerFactory.Networking().V1().Ingresses(),
kubeInformerFactory.Networking().V1().IngressClasses(),
kubeInformerFactory.Core().V1().Services(),
istioInformerFactory.Networking().V1beta1().VirtualServices(),
istioInformerFactory.Networking().V1beta1().Gateways())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wait := make(chan os.Signal, 1)
signal.Notify(wait, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-wait
klog.Info("received signal, shutting down")
cancel()
}()
kubeInformerFactory.Start(ctx.Done())
istioInformerFactory.Start(ctx.Done())
runWithLeaderElection(ctlr, cfg, kubeclient, ctx)
}
func runWithLeaderElection(ctlr *controller.Controller, cfg *rest.Config, kubeclient *kubernetes.Clientset, ctx context.Context) {
// Acquire a lock
// Identity used to distinguish between multiple cloud controller manager instances
klog.Infof("leader identity id: %s", lockIdentity)
var lock resourcelock.Interface
lock = &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{
Name: lockName,
Namespace: lockNamespace,
},
Client: kubeclient.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: lockIdentity,
},
}
cfg.Wrap(transport.ContextCanceller(ctx, fmt.Errorf("the leader is shutting down")))
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: lock,
ReleaseOnCancel: true,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(ctx context.Context) {
if err := ctlr.Run(2, ctx); err != nil {
if err != context.Canceled {
klog.Errorf("error running controller: %v", err)
}
}
},
OnStoppedLeading: func() {
klog.Info("stopped leading")
},
OnNewLeader: func(identity string) {
if identity == lockIdentity {
// We just acquired the lock
return
}
klog.Infof("new leader elected: %v", identity)
},
},
})
}
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.StringVar(&clusterDomain, "cluster-domain", "cluster.local", "The cluster domain.")
flag.StringVar(&defaultGateway, "default-gateway", "istio-system/istio-autogenerated-k8s-ingress", "The default Istio gateway used when no existing VirtualService is located matching the host.")
flag.BoolVar(&scopedGateways, "scoped-gateways", false, "Gateways are scoped to the same namespace they exist within. This will limit the Service search for Load Balancer status. In istiod, this is controlled via the PILOT_SCOPE_GATEWAY_TO_NAMESPACE environment variable.")
flag.StringVar(&ingressClass, "ingress-class", "", "The ingress class annotation to monitor (empty string to skip checking annotation)")
flag.IntVar(&defaultWeight, "virtual-service-weight", 100, "The weight of the Virtual Service destination.")
flag.StringVar(&lockName, "lock-name", getEnvVarOrDefault("LOCK_NAME", "ingress-istio-controller"), "The name of the leader lock.")
flag.StringVar(&lockNamespace, "lock-namespace", getEnvVarOrDefault("LOCK_NAMESPACE", "ingress-istio-controller-system"), "The namespace where the leader lock resides.")
flag.StringVar(&lockIdentity, "lock-identity", getEnvVarOrDefault("LOCK_IDENTITY", createIdentity()), "The unique identity of the replica. (Pod name is best)")
}
// Returns an environment variables value if set, otherwise returns dflt.
func getEnvVarOrDefault(envVar, dflt string) string {
val, ok := os.LookupEnv(envVar)
if ok {
return val
} else {
return dflt
}
}
// Creates a unique identity.
func createIdentity() string {
hostname, err := os.Hostname()
if err != nil {
klog.Fatal(err)
}
// add a uniquifier so that two processes on the same host don't accidentally both become active
return hostname + "_" + string(uuid.NewUUID())
}