-
Notifications
You must be signed in to change notification settings - Fork 0
/
globaldns_common.go
75 lines (64 loc) · 1.75 KB
/
globaldns_common.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
package globaldns
import (
"fmt"
"strings"
"github.com/rancher/types/apis/management.cattle.io/v3"
"k8s.io/api/core/v1"
)
func gatherIngressEndpoints(ingressEps []v1.LoadBalancerIngress) []string {
var endpoints []string
for _, ep := range ingressEps {
if ep.IP != "" {
endpoints = append(endpoints, ep.IP)
} else if ep.Hostname != "" {
endpoints = append(endpoints, ep.Hostname)
}
}
return endpoints
}
func getMultiClusterAppName(multiClusterAppName string) (string, error) {
split := strings.SplitN(multiClusterAppName, ":", 2)
if len(split) != 2 {
return "", fmt.Errorf("error in splitting multiclusterapp ID %v", multiClusterAppName)
}
mcappName := split[1]
return mcappName, nil
}
func ifEndpointsDiffer(endpointsOne []string, endpointsTwo []string) bool {
if len(endpointsOne) != len(endpointsTwo) {
return true
}
mapEndpointsOne := make(map[string]bool)
for _, ep := range endpointsOne {
mapEndpointsOne[ep] = true
}
for _, ep := range endpointsTwo {
if !mapEndpointsOne[ep] {
return true
}
}
return false
}
func dedupEndpoints(endpoints []string) []string {
mapEndpoints := make(map[string]bool)
res := []string{}
for _, ep := range endpoints {
if !mapEndpoints[ep] {
mapEndpoints[ep] = true
res = append(res, ep)
}
}
return res
}
func reconcileGlobalDNSEndpoints(globalDNS *v3.GlobalDNS) {
//aggregate all clusterEndpoints and form the final DNS endpoints[]
var reconciledEps []string
originalEps := globalDNS.Status.Endpoints
for _, clusterEndpoints := range globalDNS.Status.ClusterEndpoints {
reconciledEps = append(reconciledEps, clusterEndpoints...)
}
//update the DNS endpoints if different
if ifEndpointsDiffer(originalEps, reconciledEps) {
globalDNS.Status.Endpoints = reconciledEps
}
}