Skip to content

Commit 1d794a8

Browse files
committed
cloudmap: resolve hostnames before registering (#137)
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 1d794a8

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

pkg/servregistry/aws/cloudmap/handler.go

Lines changed: 39 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,50 @@ 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] = ing.Hostname
111+
continue
112+
}
113+
114+
if ing.Hostname != "" {
115+
addrs, err := net.LookupHost(ing.Hostname)
116+
if err != nil {
117+
// TODO: this needs to be logged
118+
continue
119+
}
120+
121+
// TODO: log to what this has been resolved to.
122+
for _, addr := range addrs {
123+
if _, exists := ipsSet[addr]; !exists {
124+
ipsSet[addr] = ing.Hostname
125+
}
126+
}
127+
}
105128
}
106129

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

111142
// Create an hashed name for this
112-
toBeHashed := fmt.Sprintf("%s-%d", ip, port.Port)
143+
toBeHashed := fmt.Sprintf("%s-%d", addr, port.Port)
113144
h := sha256.New()
114145
h.Write([]byte(toBeHashed))
115146
hash := hex.EncodeToString(h.Sum(nil))
@@ -120,9 +151,9 @@ func (h *Handler) ExtractData(ns *corev1.Namespace, serv *corev1.Service) (*sr.N
120151
Name: name,
121152
NsName: namespaceData.Name,
122153
ServName: serviceData.Name,
123-
Address: ip,
154+
Address: addr,
124155
Port: port.Port,
125-
Metadata: map[string]string{},
156+
Metadata: metadata,
126157
})
127158
}
128159
}

0 commit comments

Comments
 (0)