-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress.go
63 lines (55 loc) · 1.49 KB
/
ingress.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
package main
import (
"context"
"fmt"
"regexp"
v1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
)
var (
externalDNSRegex = regexp.MustCompile(`^external-dns\.alpha\.kubernetes\.io/.*`)
)
func ingressList(clientset *kubernetes.Clientset) ([]v1.Ingress, error) {
ingressList, err := clientset.NetworkingV1().Ingresses("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to list Ingress resources: %w", err)
}
return ingressList.Items, nil
}
func allIngressHosts(clientset *kubernetes.Clientset) ([]string, error) {
var hostnames []string
ingresses, err := ingressList(clientset)
if err != nil {
return hostnames, err
}
for _, ingress := range ingresses {
for _, rule := range ingress.Spec.Rules {
if rule.Host != "" {
hostnames = append(hostnames, rule.Host)
}
}
}
return hostnames, nil
}
func externalDNSIngressHostnames(clientset *kubernetes.Clientset) ([]string, error) {
var hostnames []string
ingresses, err := ingressList(clientset)
if err != nil {
return hostnames, err
}
for _, ingress := range ingresses {
for key := range ingress.Annotations {
if externalDNSRegex.MatchString(key) {
for _, rule := range ingress.Spec.Rules {
if rule.Host != "" {
hostnames = append(hostnames, rule.Host)
}
}
break // No need to check other annotations for this ingress
}
}
}
return hostnames, nil
}