-
Notifications
You must be signed in to change notification settings - Fork 66
/
kubernetes.go
684 lines (592 loc) · 21.5 KB
/
kubernetes.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package gateway
import (
"context"
"fmt"
"net"
"net/netip"
"regexp"
"strings"
"github.com/miekg/dns"
nginx_v1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1"
k8s_nginx "github.com/nginxinc/kubernetes-ingress/pkg/client/clientset/versioned"
core "k8s.io/api/core/v1"
networking "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
gatewayapi_v1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapi_v1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayClient "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned"
)
const (
defaultResyncPeriod = 0
ingressHostnameIndex = "ingressHostname"
serviceHostnameIndex = "serviceHostname"
gatewayUniqueIndex = "gatewayIndex"
httpRouteHostnameIndex = "httpRouteHostname"
tlsRouteHostnameIndex = "tlsRouteHostname"
grpcRouteHostnameIndex = "grpcRouteHostname"
virtualServerHostnameIndex = "virtualServerHostname"
hostnameAnnotationKey = "coredns.io/hostname"
externalDnsHostnameAnnotationKey = "external-dns.alpha.kubernetes.io/hostname"
)
// KubeController stores the current runtime configuration and cache
type KubeController struct {
client kubernetes.Interface
nginxClient k8s_nginx.Interface
gwClient gatewayClient.Interface
controllers []cache.SharedIndexInformer
hasSynced bool
}
func newKubeController(ctx context.Context, c *kubernetes.Clientset, gw *gatewayClient.Clientset, nc *k8s_nginx.Clientset) *KubeController {
log.Infof("Building k8s_gateway controller")
ctrl := &KubeController{
client: c,
nginxClient: nc,
gwClient: gw,
}
if existGatewayCRDs(ctx, gw) {
gatewayController := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: gatewayLister(ctx, ctrl.gwClient, core.NamespaceAll),
WatchFunc: gatewayWatcher(ctx, ctrl.gwClient, core.NamespaceAll),
},
&gatewayapi_v1.Gateway{},
defaultResyncPeriod,
cache.Indexers{gatewayUniqueIndex: gatewayIndexFunc},
)
ctrl.controllers = append(ctrl.controllers, gatewayController)
if resource := lookupResource("HTTPRoute"); resource != nil {
httpRouteController := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: httpRouteLister(ctx, ctrl.gwClient, core.NamespaceAll),
WatchFunc: httpRouteWatcher(ctx, ctrl.gwClient, core.NamespaceAll),
},
&gatewayapi_v1.HTTPRoute{},
defaultResyncPeriod,
cache.Indexers{httpRouteHostnameIndex: httpRouteHostnameIndexFunc},
)
resource.lookup = lookupHttpRouteIndex(httpRouteController, gatewayController)
ctrl.controllers = append(ctrl.controllers, httpRouteController)
}
if resource := lookupResource("TLSRoute"); resource != nil {
tlsRouteController := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: tlsRouteLister(ctx, ctrl.gwClient, core.NamespaceAll),
WatchFunc: tlsRouteWatcher(ctx, ctrl.gwClient, core.NamespaceAll),
},
&gatewayapi_v1alpha2.TLSRoute{},
defaultResyncPeriod,
cache.Indexers{tlsRouteHostnameIndex: tlsRouteHostnameIndexFunc},
)
resource.lookup = lookupTLSRouteIndex(tlsRouteController, gatewayController)
ctrl.controllers = append(ctrl.controllers, tlsRouteController)
}
if resource := lookupResource("GRPCRoute"); resource != nil {
grpcRouteController := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: grpcRouteLister(ctx, ctrl.gwClient, core.NamespaceAll),
WatchFunc: grpcRouteWatcher(ctx, ctrl.gwClient, core.NamespaceAll),
},
&gatewayapi_v1alpha2.GRPCRoute{},
defaultResyncPeriod,
cache.Indexers{grpcRouteHostnameIndex: grpcRouteHostnameIndexFunc},
)
resource.lookup = lookupGRPCRouteIndex(grpcRouteController, gatewayController)
ctrl.controllers = append(ctrl.controllers, grpcRouteController)
}
}
if existVirtualServerCRDs(ctx, nc) {
if resource := lookupResource("VirtualServer"); resource != nil {
virtualServerController := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: virtualServerLister(ctx, ctrl.nginxClient, core.NamespaceAll),
WatchFunc: virtualServerWatcher(ctx, ctrl.nginxClient, core.NamespaceAll),
},
&nginx_v1.VirtualServer{},
defaultResyncPeriod,
cache.Indexers{virtualServerHostnameIndex: virtualServerHostnameIndexFunc},
)
resource.lookup = lookupVirtualServerIndex(virtualServerController)
ctrl.controllers = append(ctrl.controllers, virtualServerController)
}
}
if resource := lookupResource("Ingress"); resource != nil {
ingressController := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: ingressLister(ctx, ctrl.client, core.NamespaceAll),
WatchFunc: ingressWatcher(ctx, ctrl.client, core.NamespaceAll),
},
&networking.Ingress{},
defaultResyncPeriod,
cache.Indexers{ingressHostnameIndex: ingressHostnameIndexFunc},
)
resource.lookup = lookupIngressIndex(ingressController)
ctrl.controllers = append(ctrl.controllers, ingressController)
}
if resource := lookupResource("Service"); resource != nil {
serviceController := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: serviceLister(ctx, ctrl.client, core.NamespaceAll),
WatchFunc: serviceWatcher(ctx, ctrl.client, core.NamespaceAll),
},
&core.Service{},
defaultResyncPeriod,
cache.Indexers{serviceHostnameIndex: serviceHostnameIndexFunc},
)
resource.lookup = lookupServiceIndex(serviceController)
ctrl.controllers = append(ctrl.controllers, serviceController)
}
return ctrl
}
func (ctrl *KubeController) run() {
stopCh := make(chan struct{})
defer close(stopCh)
var synced []cache.InformerSynced
log.Infof("Starting k8s_gateway controller")
for _, ctrl := range ctrl.controllers {
go ctrl.Run(stopCh)
synced = append(synced, ctrl.HasSynced)
}
log.Infof("Waiting for controllers to sync")
if !cache.WaitForCacheSync(stopCh, synced...) {
ctrl.hasSynced = false
}
log.Infof("Synced all required resources")
ctrl.hasSynced = true
<-stopCh
}
// HasSynced returns true if all controllers have been synced
func (ctrl *KubeController) HasSynced() bool {
return ctrl.hasSynced
}
// RunKubeController kicks off the k8s controllers
func (gw *Gateway) RunKubeController(ctx context.Context) error {
config, err := gw.getClientConfig()
if err != nil {
return err
}
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
nginxClient, err := k8s_nginx.NewForConfig(config)
if err != nil {
panic(err.Error())
}
gwAPIClient, err := gatewayClient.NewForConfig(config)
if err != nil {
return err
}
gw.Controller = newKubeController(ctx, kubeClient, gwAPIClient, nginxClient)
go gw.Controller.run()
return nil
}
func existGatewayCRDs(ctx context.Context, c *gatewayClient.Clientset) bool {
_, err := c.GatewayV1().Gateways("").List(ctx, metav1.ListOptions{})
return handleCRDCheckError(err, "GatewayAPI", "gateway.networking.k8s.io")
}
func existVirtualServerCRDs(ctx context.Context, c *k8s_nginx.Clientset) bool {
_, err := c.K8sV1().VirtualServers("").List(ctx, metav1.ListOptions{})
return handleCRDCheckError(err, "VirtualServer", "k8s.nginx.org/v1")
}
func handleCRDCheckError(err error, resourceName string, apiGroup string) bool {
if meta.IsNoMatchError(err) || runtime.IsNotRegisteredError(err) || apierrors.IsNotFound(err) {
log.Infof("%s CRDs are not found. Not syncing %s resources.", resourceName, resourceName)
return false
}
if apierrors.IsForbidden(err) {
log.Infof("access to `%s` is forbidden, please check RBAC. Not syncing %s resources.", apiGroup, resourceName)
return false
}
if err != nil {
panic(err)
}
return true
}
func (gw *Gateway) getClientConfig() (*rest.Config, error) {
if gw.configFile != "" {
overrides := &clientcmd.ConfigOverrides{}
overrides.CurrentContext = gw.configContext
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: gw.configFile},
overrides,
)
return config.ClientConfig()
}
return rest.InClusterConfig()
}
func httpRouteLister(ctx context.Context, c gatewayClient.Interface, ns string) func(metav1.ListOptions) (runtime.Object, error) {
return func(opts metav1.ListOptions) (runtime.Object, error) {
return c.GatewayV1().HTTPRoutes(ns).List(ctx, opts)
}
}
func tlsRouteLister(ctx context.Context, c gatewayClient.Interface, ns string) func(metav1.ListOptions) (runtime.Object, error) {
return func(opts metav1.ListOptions) (runtime.Object, error) {
return c.GatewayV1alpha2().TLSRoutes(ns).List(ctx, opts)
}
}
func grpcRouteLister(ctx context.Context, c gatewayClient.Interface, ns string) func(metav1.ListOptions) (runtime.Object, error) {
return func(opts metav1.ListOptions) (runtime.Object, error) {
return c.GatewayV1alpha2().GRPCRoutes(ns).List(ctx, opts)
}
}
func gatewayLister(ctx context.Context, c gatewayClient.Interface, ns string) func(metav1.ListOptions) (runtime.Object, error) {
return func(opts metav1.ListOptions) (runtime.Object, error) {
return c.GatewayV1().Gateways(ns).List(ctx, opts)
}
}
func ingressLister(ctx context.Context, c kubernetes.Interface, ns string) func(metav1.ListOptions) (runtime.Object, error) {
return func(opts metav1.ListOptions) (runtime.Object, error) {
return c.NetworkingV1().Ingresses(ns).List(ctx, opts)
}
}
func serviceLister(ctx context.Context, c kubernetes.Interface, ns string) func(metav1.ListOptions) (runtime.Object, error) {
return func(opts metav1.ListOptions) (runtime.Object, error) {
return c.CoreV1().Services(ns).List(ctx, opts)
}
}
func virtualServerLister(ctx context.Context, c k8s_nginx.Interface, ns string) func(metav1.ListOptions) (runtime.Object, error) {
return func(opts metav1.ListOptions) (runtime.Object, error) {
return c.K8sV1().VirtualServers(ns).List(ctx, opts)
}
}
func httpRouteWatcher(ctx context.Context, c gatewayClient.Interface, ns string) func(metav1.ListOptions) (watch.Interface, error) {
return func(opts metav1.ListOptions) (watch.Interface, error) {
return c.GatewayV1().HTTPRoutes(ns).Watch(ctx, opts)
}
}
func tlsRouteWatcher(ctx context.Context, c gatewayClient.Interface, ns string) func(metav1.ListOptions) (watch.Interface, error) {
return func(opts metav1.ListOptions) (watch.Interface, error) {
return c.GatewayV1alpha2().TLSRoutes(ns).Watch(ctx, opts)
}
}
func grpcRouteWatcher(ctx context.Context, c gatewayClient.Interface, ns string) func(metav1.ListOptions) (watch.Interface, error) {
return func(opts metav1.ListOptions) (watch.Interface, error) {
return c.GatewayV1alpha2().GRPCRoutes(ns).Watch(ctx, opts)
}
}
func gatewayWatcher(ctx context.Context, c gatewayClient.Interface, ns string) func(metav1.ListOptions) (watch.Interface, error) {
return func(opts metav1.ListOptions) (watch.Interface, error) {
return c.GatewayV1().Gateways(ns).Watch(ctx, opts)
}
}
func ingressWatcher(ctx context.Context, c kubernetes.Interface, ns string) func(metav1.ListOptions) (watch.Interface, error) {
return func(opts metav1.ListOptions) (watch.Interface, error) {
return c.NetworkingV1().Ingresses(ns).Watch(ctx, opts)
}
}
func serviceWatcher(ctx context.Context, c kubernetes.Interface, ns string) func(metav1.ListOptions) (watch.Interface, error) {
return func(opts metav1.ListOptions) (watch.Interface, error) {
return c.CoreV1().Services(ns).Watch(ctx, opts)
}
}
func virtualServerWatcher(ctx context.Context, c k8s_nginx.Interface, ns string) func(metav1.ListOptions) (watch.Interface, error) {
return func(opts metav1.ListOptions) (watch.Interface, error) {
return c.K8sV1().VirtualServers(ns).Watch(ctx, opts)
}
}
// indexes based on "namespace/name" as the key
func gatewayIndexFunc(obj interface{}) ([]string, error) {
metaObj, err := meta.Accessor(obj)
if err != nil {
return []string{""}, fmt.Errorf("object has no meta: %v", err)
}
return []string{fmt.Sprintf("%s/%s", metaObj.GetNamespace(), metaObj.GetName())}, nil
}
func httpRouteHostnameIndexFunc(obj interface{}) ([]string, error) {
httpRoute, ok := obj.(*gatewayapi_v1.HTTPRoute)
if !ok {
return []string{}, nil
}
var hostnames []string
for _, hostname := range httpRoute.Spec.Hostnames {
log.Debugf("Adding index %s for httpRoute %s", httpRoute.Name, hostname)
hostnames = append(hostnames, string(hostname))
}
return hostnames, nil
}
func tlsRouteHostnameIndexFunc(obj interface{}) ([]string, error) {
tlsRoute, ok := obj.(*gatewayapi_v1alpha2.TLSRoute)
if !ok {
return []string{}, nil
}
var hostnames []string
for _, hostname := range tlsRoute.Spec.Hostnames {
log.Debugf("Adding index %s for tlsRoute %s", tlsRoute.Name, hostname)
hostnames = append(hostnames, string(hostname))
}
return hostnames, nil
}
func grpcRouteHostnameIndexFunc(obj interface{}) ([]string, error) {
grpcRoute, ok := obj.(*gatewayapi_v1alpha2.GRPCRoute)
if !ok {
return []string{}, nil
}
var hostnames []string
for _, hostname := range grpcRoute.Spec.Hostnames {
log.Debugf("Adding index %s for grpcRoute %s", grpcRoute.Name, hostname)
hostnames = append(hostnames, string(hostname))
}
return hostnames, nil
}
func ingressHostnameIndexFunc(obj interface{}) ([]string, error) {
ingress, ok := obj.(*networking.Ingress)
if !ok {
return []string{}, nil
}
var hostnames []string
for _, rule := range ingress.Spec.Rules {
log.Debugf("Adding index %s for ingress %s", rule.Host, ingress.Name)
hostnames = append(hostnames, rule.Host)
}
return hostnames, nil
}
func serviceHostnameIndexFunc(obj interface{}) ([]string, error) {
service, ok := obj.(*core.Service)
if !ok {
return []string{}, nil
}
if service.Spec.Type != core.ServiceTypeLoadBalancer {
return []string{}, nil
}
hostname := service.Name + "." + service.Namespace
if annotation, exists := checkServiceAnnotation(hostnameAnnotationKey, service); exists {
hostname = annotation
} else if annotation, exists := checkServiceAnnotation(externalDnsHostnameAnnotationKey, service); exists {
hostname = annotation
}
log.Debugf("Adding index %s for service %s", hostname, service.Name)
return []string{hostname}, nil
}
func checkServiceAnnotation(annotation string, service *core.Service) (string, bool) {
if annotationValue, exists := service.Annotations[annotation]; exists {
// checking the hostname length limits
if _, ok := dns.IsDomainName(annotationValue); ok {
// checking RFC 1123 conformance (same as metadata labels)
if valid := isdns1123Hostname(annotationValue); valid {
return strings.ToLower(annotationValue), true
} else {
log.Infof("RFC 1123 conformance failed for FQDN: %s", annotationValue)
}
} else {
log.Infof("Invalid FQDN length: %s", annotationValue)
}
}
return "", false
}
func virtualServerHostnameIndexFunc(obj interface{}) ([]string, error) {
virtualServer, ok := obj.(*nginx_v1.VirtualServer)
if !ok {
return []string{}, nil
}
log.Debugf("Adding index %s for VirtualServer %s", virtualServer.Spec.Host, virtualServer.Name)
return []string{virtualServer.Spec.Host}, nil
}
func lookupServiceIndex(ctrl cache.SharedIndexInformer) func([]string) []netip.Addr {
return func(indexKeys []string) (result []netip.Addr) {
var objs []interface{}
for _, key := range indexKeys {
obj, _ := ctrl.GetIndexer().ByIndex(serviceHostnameIndex, strings.ToLower(key))
objs = append(objs, obj...)
}
log.Debugf("Found %d matching Service objects", len(objs))
for _, obj := range objs {
service, _ := obj.(*core.Service)
if len(service.Spec.ExternalIPs) > 0 {
for _, ip := range service.Spec.ExternalIPs {
result = append(result, netip.MustParseAddr(ip))
}
// in case externalIPs are defined, ignoring status field completely
return
}
result = append(result, fetchServiceLoadBalancerIPs(service.Status.LoadBalancer.Ingress)...)
}
return
}
}
func lookupVirtualServerIndex(ctrl cache.SharedIndexInformer) func([]string) []netip.Addr {
return func(indexKeys []string) (result []netip.Addr) {
var objs []interface{}
for _, key := range indexKeys {
obj, _ := ctrl.GetIndexer().ByIndex(virtualServerHostnameIndex, strings.ToLower(key))
objs = append(objs, obj...)
}
log.Debugf("Found %d matching VirtualServer objects", len(objs))
for _, obj := range objs {
virtualServer, _ := obj.(*nginx_v1.VirtualServer)
for _, endpoint := range virtualServer.Status.ExternalEndpoints {
addr, err := netip.ParseAddr(endpoint.IP)
if err != nil {
continue
}
result = append(result, addr)
}
}
return
}
}
func lookupHttpRouteIndex(http, gw cache.SharedIndexInformer) func([]string) []netip.Addr {
return func(indexKeys []string) (result []netip.Addr) {
var objs []interface{}
for _, key := range indexKeys {
obj, _ := http.GetIndexer().ByIndex(httpRouteHostnameIndex, strings.ToLower(key))
objs = append(objs, obj...)
}
log.Debugf("Found %d matching httpRoute objects", len(objs))
for _, obj := range objs {
httpRoute, _ := obj.(*gatewayapi_v1.HTTPRoute)
result = append(result, lookupGateways(gw, httpRoute.Spec.ParentRefs, httpRoute.Namespace)...)
}
return
}
}
func lookupTLSRouteIndex(tls, gw cache.SharedIndexInformer) func([]string) []netip.Addr {
return func(indexKeys []string) (result []netip.Addr) {
var objs []interface{}
for _, key := range indexKeys {
obj, _ := tls.GetIndexer().ByIndex(tlsRouteHostnameIndex, strings.ToLower(key))
objs = append(objs, obj...)
}
log.Debugf("Found %d matching tlsRoute objects", len(objs))
for _, obj := range objs {
tlsRoute, _ := obj.(*gatewayapi_v1alpha2.TLSRoute)
result = append(result, lookupGateways(gw, tlsRoute.Spec.ParentRefs, tlsRoute.Namespace)...)
}
return
}
}
func lookupGRPCRouteIndex(grpc, gw cache.SharedIndexInformer) func([]string) []netip.Addr {
return func(indexKeys []string) (result []netip.Addr) {
var objs []interface{}
for _, key := range indexKeys {
obj, _ := grpc.GetIndexer().ByIndex(grpcRouteHostnameIndex, strings.ToLower(key))
objs = append(objs, obj...)
}
log.Debugf("Found %d matching grpcRoute objects", len(objs))
for _, obj := range objs {
grpcRoute, _ := obj.(*gatewayapi_v1alpha2.GRPCRoute)
result = append(result, lookupGateways(gw, grpcRoute.Spec.ParentRefs, grpcRoute.Namespace)...)
}
return
}
}
func lookupGateways(gw cache.SharedIndexInformer, refs []gatewayapi_v1.ParentReference, ns string) (result []netip.Addr) {
for _, gwRef := range refs {
if gwRef.Namespace != nil {
ns = string(*gwRef.Namespace)
}
gwKey := fmt.Sprintf("%s/%s", ns, gwRef.Name)
gwObjs, _ := gw.GetIndexer().ByIndex(gatewayUniqueIndex, gwKey)
log.Debugf("Found %d matching gateway objects", len(gwObjs))
for _, gwObj := range gwObjs {
gw, _ := gwObj.(*gatewayapi_v1.Gateway)
result = append(result, fetchGatewayIPs(gw)...)
}
}
return
}
func lookupIngressIndex(ctrl cache.SharedIndexInformer) func([]string) []netip.Addr {
return func(indexKeys []string) (result []netip.Addr) {
var objs []interface{}
for _, key := range indexKeys {
obj, _ := ctrl.GetIndexer().ByIndex(ingressHostnameIndex, strings.ToLower(key))
objs = append(objs, obj...)
}
log.Debugf("Found %d matching Ingress objects", len(objs))
for _, obj := range objs {
ingress, _ := obj.(*networking.Ingress)
result = append(result, fetchIngressLoadBalancerIPs(ingress.Status.LoadBalancer.Ingress)...)
}
return
}
}
func fetchGatewayIPs(gw *gatewayapi_v1.Gateway) (results []netip.Addr) {
for _, addr := range gw.Status.Addresses {
if *addr.Type == gatewayapi_v1.IPAddressType {
addr, err := netip.ParseAddr(addr.Value)
if err != nil {
continue
}
results = append(results, addr)
continue
}
if *addr.Type == gatewayapi_v1.HostnameAddressType {
ips, err := net.LookupIP(addr.Value)
if err != nil {
continue
}
for _, ip := range ips {
addr, err := netip.ParseAddr(ip.String())
if err != nil {
continue
}
results = append(results, addr)
}
}
}
return
}
func fetchServiceLoadBalancerIPs(ingresses []core.LoadBalancerIngress) (results []netip.Addr) {
for _, address := range ingresses {
if address.Hostname != "" {
log.Debugf("Looking up hostname %s", address.Hostname)
ips, err := net.LookupIP(address.Hostname)
if err != nil {
continue
}
for _, ip := range ips {
addr, err := netip.ParseAddr(ip.String())
if err != nil {
continue
}
results = append(results, addr)
}
} else if address.IP != "" {
addr, err := netip.ParseAddr(address.IP)
if err != nil {
continue
}
results = append(results, addr)
}
}
return
}
func fetchIngressLoadBalancerIPs(ingresses []networking.IngressLoadBalancerIngress) (results []netip.Addr) {
for _, address := range ingresses {
if address.Hostname != "" {
log.Debugf("Looking up hostname %s", address.Hostname)
ips, err := net.LookupIP(address.Hostname)
if err != nil {
continue
}
for _, ip := range ips {
addr, err := netip.ParseAddr(ip.String())
if err != nil {
continue
}
results = append(results, addr)
}
} else if address.IP != "" {
addr, err := netip.ParseAddr(address.IP)
if err != nil {
continue
}
results = append(results, addr)
}
}
return
}
// the below is borrowed from k/k's github repo
const dns1123ValueFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
const dns1123SubdomainFmt string = dns1123ValueFmt + "(\\." + dns1123ValueFmt + ")*"
var dns1123SubdomainRegexp = regexp.MustCompile("^" + dns1123SubdomainFmt + "$")
func isdns1123Hostname(value string) bool {
return dns1123SubdomainRegexp.MatchString(value)
}