/
status.go
376 lines (323 loc) · 10.5 KB
/
status.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ingress
import (
"errors"
"fmt"
"net"
"os"
"sort"
"strings"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/tools/record"
meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/serviceregistry/kube"
"istio.io/istio/pkg/log"
)
const (
updateInterval = 60 * time.Second
ingressElectionID = "istio-ingress-controller-leader"
)
// StatusSyncer keeps the status IP in each Ingress resource updated
type StatusSyncer struct {
client kubernetes.Interface
ingressClass string
defaultIngressClass string
ingressNameSpace string
ingressService string
pod *v1.Pod
queue kube.Queue
informer cache.SharedIndexInformer
elector *leaderelection.LeaderElector
handler *kube.ChainHandler
}
// Run the syncer until stopCh is closed
func (s *StatusSyncer) Run(stopCh <-chan struct{}) {
go s.informer.Run(stopCh)
go s.elector.Run()
<-stopCh
// TODO: should we remove current IPs on shutting down?
}
// NewStatusSyncer creates a new instance
func NewStatusSyncer(mesh *meshconfig.MeshConfig,
client kubernetes.Interface,
ingressNamespace string,
options kube.ControllerOptions) (*StatusSyncer, error) {
podName, exists := os.LookupEnv("POD_NAME")
if !exists {
return nil, errors.New("POD_NAME environment variable must be defined")
}
podNamespace, exists := os.LookupEnv("POD_NAMESPACE")
if !exists {
return nil, errors.New("POD_NAMESPACE environment variable must be defined")
}
pod, _ := client.CoreV1().Pods(podNamespace).Get(podName, meta_v1.GetOptions{})
if pod == nil {
return nil, fmt.Errorf("unable to get POD information")
}
// we need to use the defined ingress class to allow multiple leaders
// in order to update information about ingress status
ingressClass, defaultIngressClass := convertIngressControllerMode(mesh.IngressControllerMode, mesh.IngressClass)
electionID := fmt.Sprintf("%v-%v", ingressElectionID, defaultIngressClass)
if ingressClass != "" {
electionID = fmt.Sprintf("%v-%v", ingressElectionID, ingressClass)
}
handler := &kube.ChainHandler{}
// queue requires a time duration for a retry delay after a handler error
queue := kube.NewQueue(1 * time.Second)
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(opts meta_v1.ListOptions) (runtime.Object, error) {
return client.ExtensionsV1beta1().Ingresses(options.WatchedNamespace).List(opts)
},
WatchFunc: func(opts meta_v1.ListOptions) (watch.Interface, error) {
return client.ExtensionsV1beta1().Ingresses(options.WatchedNamespace).Watch(opts)
},
},
&v1beta1.Ingress{}, options.ResyncPeriod, cache.Indexers{},
)
st := StatusSyncer{
client: client,
informer: informer,
queue: queue,
ingressClass: ingressClass,
defaultIngressClass: defaultIngressClass,
ingressNameSpace: ingressNamespace,
ingressService: mesh.IngressService,
pod: pod,
handler: handler,
}
callbacks := leaderelection.LeaderCallbacks{
OnStartedLeading: func(stop <-chan struct{}) {
log.Infof("I am the new status update leader")
go st.queue.Run(stop)
err := wait.PollUntil(updateInterval, func() (bool, error) {
st.queue.Push(kube.NewTask(st.handler.Apply, "Start leading", model.EventUpdate))
return false, nil
}, stop)
if err != nil {
log.Errorf("Stop requested")
}
},
OnStoppedLeading: func() {
log.Infof("I am not status update leader anymore")
},
OnNewLeader: func(identity string) {
log.Infof("New leader elected: %v", identity)
},
}
broadcaster := record.NewBroadcaster()
hostname, _ := os.Hostname()
recorder := broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{
Component: "ingress-leader-elector",
Host: hostname,
})
lock := resourcelock.ConfigMapLock{
ConfigMapMeta: meta_v1.ObjectMeta{Namespace: podNamespace, Name: electionID},
Client: client.CoreV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: podName,
EventRecorder: recorder,
},
}
ttl := 30 * time.Second
le, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: &lock,
LeaseDuration: ttl,
RenewDeadline: ttl / 2,
RetryPeriod: ttl / 4,
Callbacks: callbacks,
})
if err != nil {
log.Errorf("unexpected error starting leader election: %v", err)
}
st.elector = le
// Register handler at the beginning
handler.Append(func(obj interface{}, event model.Event) error {
addrs, err := st.runningAddresses()
if err != nil {
return err
}
return st.updateStatus(sliceToStatus(addrs))
})
return &st, nil
}
// updateStatus updates ingress status with the list of IP
func (s *StatusSyncer) updateStatus(status []v1.LoadBalancerIngress) error {
ingressStore := s.informer.GetStore()
for _, obj := range ingressStore.List() {
currIng := obj.(*v1beta1.Ingress)
if !classIsValid(currIng, s.ingressClass, s.defaultIngressClass) {
continue
}
curIPs := currIng.Status.LoadBalancer.Ingress
sort.SliceStable(status, lessLoadBalancerIngress(status))
sort.SliceStable(curIPs, lessLoadBalancerIngress(curIPs))
if ingressSliceEqual(status, curIPs) {
log.Infof("skipping update of Ingress %v/%v (no change)", currIng.Namespace, currIng.Name)
return nil
}
currIng.Status.LoadBalancer.Ingress = status
ingClient := s.client.ExtensionsV1beta1().Ingresses(currIng.Namespace)
_, err := ingClient.UpdateStatus(currIng)
if err != nil {
log.Warnf("error updating ingress status: %v", err)
}
}
return nil
}
// runningAddresses returns a list of IP addresses and/or FQDN where the
// ingress controller is currently running
func (s *StatusSyncer) runningAddresses() ([]string, error) {
addrs := []string{}
if s.ingressService != "" {
svc, err := s.client.CoreV1().Services(s.ingressNameSpace).Get(s.ingressService, meta_v1.GetOptions{})
if err != nil {
return nil, err
}
if svc.Spec.Type == v1.ServiceTypeExternalName {
addrs = append(addrs, svc.Spec.ExternalName)
return addrs, nil
}
for _, ip := range svc.Status.LoadBalancer.Ingress {
if ip.IP == "" {
addrs = append(addrs, ip.Hostname)
} else {
addrs = append(addrs, ip.IP)
}
}
addrs = append(addrs, svc.Spec.ExternalIPs...)
return addrs, nil
}
// get information about all the pods running the ingress controller
pods, err := s.client.CoreV1().Pods(s.pod.GetNamespace()).List(meta_v1.ListOptions{
LabelSelector: labels.SelectorFromSet(s.pod.GetLabels()).String(),
})
if err != nil {
return nil, err
}
for _, pod := range pods.Items {
// only Running pods are valid
if pod.Status.Phase != v1.PodRunning {
continue
}
// Find node external IP
node, err := s.client.CoreV1().Nodes().Get(pod.Spec.NodeName, meta_v1.GetOptions{})
if err != nil {
continue
}
for _, address := range node.Status.Addresses {
if address.Type == v1.NodeExternalIP {
if address.Address != "" && !addressInSlice(address.Address, addrs) {
addrs = append(addrs, address.Address)
}
}
}
}
return addrs, nil
}
func addressInSlice(addr string, list []string) bool {
for _, v := range list {
if v == addr {
return true
}
}
return false
}
// sliceToStatus converts a slice of IP and/or hostnames to LoadBalancerIngress
func sliceToStatus(endpoints []string) []v1.LoadBalancerIngress {
lbi := []v1.LoadBalancerIngress{}
for _, ep := range endpoints {
if net.ParseIP(ep) == nil {
lbi = append(lbi, v1.LoadBalancerIngress{Hostname: ep})
} else {
lbi = append(lbi, v1.LoadBalancerIngress{IP: ep})
}
}
return lbi
}
func lessLoadBalancerIngress(addrs []v1.LoadBalancerIngress) func(int, int) bool {
return func(a, b int) bool {
switch strings.Compare(addrs[a].Hostname, addrs[b].Hostname) {
case -1:
return true
case 1:
return false
}
return addrs[a].IP < addrs[b].IP
}
}
func ingressSliceEqual(lhs, rhs []v1.LoadBalancerIngress) bool {
if len(lhs) != len(rhs) {
return false
}
for i := range lhs {
if lhs[i].IP != rhs[i].IP {
return false
}
if lhs[i].Hostname != rhs[i].Hostname {
return false
}
}
return true
}
// convertIngressControllerMode converts Ingress controller mode into k8s ingress status syncer ingress class and
// default ingress class. Ingress class and default ingress class are used by the syncer to determine whether or not to
// update the IP of a ingress resource.
func convertIngressControllerMode(mode meshconfig.MeshConfig_IngressControllerMode,
class string) (string, string) {
var ingressClass, defaultIngressClass string
switch mode {
case meshconfig.MeshConfig_DEFAULT:
defaultIngressClass = class
ingressClass = class
case meshconfig.MeshConfig_STRICT:
ingressClass = class
}
return ingressClass, defaultIngressClass
}
// classIsValid returns true if the given Ingress either doesn't specify
// the ingress.class annotation, or it's set to the configured in the
// ingress controller.
func classIsValid(ing *v1beta1.Ingress, controller, defClass string) bool {
// ingress fetched through annotation.
var ingress string
if ing != nil && len(ing.GetAnnotations()) != 0 {
ingress = ing.GetAnnotations()[kube.IngressClassAnnotation]
}
// we have 2 valid combinations
// 1 - ingress with default class | blank annotation on ingress
// 2 - ingress with specific class | same annotation on ingress
//
// and 2 invalid combinations
// 3 - ingress with default class | fixed annotation on ingress
// 4 - ingress with specific class | different annotation on ingress
if ingress == "" && controller == defClass {
return true
}
return ingress == controller
}