Skip to content

Commit 74bf721

Browse files
authored
cloudmap: resolve hostnames before registering
AWS load balancers are DNS-based rather than IP-based. This means that we need to resolve those host names before registering endpoints to the service registry, as they only accept IPs. This commit introduces this change and allows the operator to work fine with EKS as well.
1 parent dbd7f33 commit 74bf721

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

pkg/servregistry/aws/cloudmap/handler.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2021 Cisco
1+
// Copyright © 2021, 2022 Cisco
22
//
33
// SPDX-License-Identifier: Apache-2.0
44
//
@@ -23,6 +23,7 @@ import (
2323
"crypto/sha256"
2424
"encoding/hex"
2525
"fmt"
26+
"net"
2627
"time"
2728

2829
sr "github.com/CloudNativeSDWAN/cnwan-operator/pkg/servregistry"
@@ -96,20 +97,52 @@ func (h *Handler) ExtractData(ns *corev1.Namespace, serv *corev1.Service) (*sr.N
9697

9798
// Get the endpoints from the service
9899
// First, build the ips
99-
ips := []string{}
100-
ips = append(ips, serv.Spec.ExternalIPs...)
100+
// De-duplicate and chek the original host name
101+
ipsSet := map[string]string{}
102+
103+
for _, ip := range serv.Spec.ExternalIPs {
104+
ipsSet[ip] = ""
105+
}
101106

102107
// Get data from load balancers
103108
for _, ing := range serv.Status.LoadBalancer.Ingress {
104-
ips = append(ips, ing.IP)
109+
if ing.IP != "" {
110+
ipsSet[ing.IP] = ""
111+
}
112+
}
113+
114+
// Get host names
115+
for _, ing := range serv.Status.LoadBalancer.Ingress {
116+
if ing.Hostname != "" {
117+
addrs, err := net.LookupHost(ing.Hostname)
118+
if err != nil {
119+
// TODO: this needs to be logged
120+
continue
121+
}
122+
123+
// TODO: log to what this has been resolved to.
124+
for _, addr := range addrs {
125+
if _, exists := ipsSet[addr]; !exists {
126+
ipsSet[addr] = ing.Hostname
127+
}
128+
}
129+
}
105130
}
106131

107132
endpointsData := []*sr.Endpoint{}
108133
for _, port := range serv.Spec.Ports {
109-
for _, ip := range ips {
134+
for addr, hostname := range ipsSet {
135+
metadata := map[string]string{}
136+
137+
if hostname != "" {
138+
metadata = map[string]string{
139+
"cnwan.io/hostname": hostname,
140+
"cnwan.io/last-resolved": time.Now().Format(time.RFC822),
141+
}
142+
}
110143

111144
// Create an hashed name for this
112-
toBeHashed := fmt.Sprintf("%s-%d", ip, port.Port)
145+
toBeHashed := fmt.Sprintf("%s-%d", addr, port.Port)
113146
h := sha256.New()
114147
h.Write([]byte(toBeHashed))
115148
hash := hex.EncodeToString(h.Sum(nil))
@@ -120,9 +153,9 @@ func (h *Handler) ExtractData(ns *corev1.Namespace, serv *corev1.Service) (*sr.N
120153
Name: name,
121154
NsName: namespaceData.Name,
122155
ServName: serviceData.Name,
123-
Address: ip,
156+
Address: addr,
124157
Port: port.Port,
125-
Metadata: map[string]string{},
158+
Metadata: metadata,
126159
})
127160
}
128161
}

0 commit comments

Comments
 (0)