Skip to content

Commit 90d2137

Browse files
committed
Log the resolver used to obtain the local endpoint public IP
...to aid in debugging. Signed-off-by: Tom Pantelis <[email protected]>
1 parent 53ac157 commit 90d2137

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

pkg/endpoint/local_endpoint.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,13 @@ func GetLocalSpec(ctx context.Context, submSpec *types.SubmarinerSpecification,
181181
BackendConfig: backendConfig,
182182
}
183183

184-
publicIP, err := getPublicIP(submSpec, k8sClient, backendConfig, airGappedDeployment)
184+
publicIP, resolver, err := getPublicIP(submSpec, k8sClient, backendConfig, airGappedDeployment)
185185
if err != nil {
186186
return nil, errors.Wrap(err, "could not determine public IP")
187187
}
188188

189+
logger.Infof("Obtained local endpoint public IP %q using resolver %q", publicIP, resolver)
190+
189191
endpointSpec.PublicIP = publicIP
190192

191193
if submSpec.HealthCheckEnabled && !globalnetEnabled {

pkg/endpoint/public_ip.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func getPublicIPResolvers() string {
6565

6666
func getPublicIP(submSpec *types.SubmarinerSpecification, k8sClient kubernetes.Interface,
6767
backendConfig map[string]string, airGapped bool,
68-
) (string, error) {
68+
) (string, string, error) {
6969
// If the node is annotated with a public-ip, the same is used as the public-ip of local endpoint.
7070
config, ok := backendConfig[v1.PublicIP]
7171
if !ok {
@@ -77,13 +77,13 @@ func getPublicIP(submSpec *types.SubmarinerSpecification, k8sClient kubernetes.I
7777
}
7878

7979
if airGapped {
80-
ip, err := resolveIPInAirGappedDeployment(k8sClient, submSpec.Namespace, config)
80+
ip, resolver, err := resolveIPInAirGappedDeployment(k8sClient, submSpec.Namespace, config)
8181
if err != nil {
8282
logger.Errorf(err, "Error resolving public IP in an air-gapped deployment, using empty value: %s", config)
83-
return "", nil
83+
return "", "", nil
8484
}
8585

86-
return ip, nil
86+
return ip, resolver, nil
8787
}
8888

8989
resolvers := strings.Split(config, ",")
@@ -94,44 +94,46 @@ func getPublicIP(submSpec *types.SubmarinerSpecification, k8sClient kubernetes.I
9494

9595
parts := strings.Split(resolver, ":")
9696
if len(parts) != 2 {
97-
return "", errors.Errorf("invalid format for %q annotation: %q", v1.GatewayConfigPrefix+v1.PublicIP, config)
97+
return "", "", errors.Errorf("invalid format for %q annotation: %q", v1.GatewayConfigPrefix+v1.PublicIP, config)
9898
}
9999

100100
ip, err := resolvePublicIP(k8sClient, submSpec.Namespace, parts)
101101
if err == nil {
102-
return ip, nil
102+
return ip, resolver, nil
103103
}
104104

105105
// If this resolver failed, we log it, but we fall back to the next one
106106
errs = append(errs, errors.Wrapf(err, "\nResolver[%q]", resolver))
107107
}
108108

109109
if len(resolvers) > 0 {
110-
return "", errors.Wrapf(k8serrors.NewAggregate(errs), "Unable to resolve public IP by any of the resolver methods")
110+
return "", "", errors.Wrapf(k8serrors.NewAggregate(errs), "Unable to resolve public IP by any of the resolver methods")
111111
}
112112

113-
return "", nil
113+
return "", "", nil
114114
}
115115

116-
func resolveIPInAirGappedDeployment(k8sClient kubernetes.Interface, namespace, config string) (string, error) {
116+
func resolveIPInAirGappedDeployment(k8sClient kubernetes.Interface, namespace, config string) (string, string, error) {
117117
resolvers := strings.Split(config, ",")
118118

119119
for _, resolver := range resolvers {
120120
resolver = strings.Trim(resolver, " ")
121121

122122
parts := strings.Split(resolver, ":")
123123
if len(parts) != 2 {
124-
return "", errors.Errorf("invalid format for %q annotation: %q", v1.GatewayConfigPrefix+v1.PublicIP, config)
124+
return "", "", errors.Errorf("invalid format for %q annotation: %q", v1.GatewayConfigPrefix+v1.PublicIP, config)
125125
}
126126

127127
if parts[0] != v1.IPv4 {
128128
continue
129129
}
130130

131-
return resolvePublicIP(k8sClient, namespace, parts)
131+
ip, err := resolvePublicIP(k8sClient, namespace, parts)
132+
133+
return ip, resolver, err
132134
}
133135

134-
return "", nil
136+
return "", "", nil
135137
}
136138

137139
func resolvePublicIP(k8sClient kubernetes.Interface, namespace string, parts []string) (string, error) {

pkg/endpoint/public_ip_internal_test.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ var _ = Describe("public ip resolvers", func() {
8181
It("should return the IP", func() {
8282
backendConfig[publicIPConfig] = lbPublicIP
8383
client := fake.NewClientset(serviceWithIngress(v1.LoadBalancerIngress{Hostname: "", IP: testIP}))
84-
ip, err := getPublicIP(submSpec, client, backendConfig, false)
84+
ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false)
8585
Expect(err).ToNot(HaveOccurred())
8686
Expect(ip).To(Equal(testIP))
87+
Expect(resolver).To(Equal(lbPublicIP))
8788
})
8889
})
8990

@@ -94,9 +95,10 @@ var _ = Describe("public ip resolvers", func() {
9495
Hostname: dnsHost,
9596
IP: "",
9697
}))
97-
ip, err := getPublicIP(submSpec, client, backendConfig, false)
98+
ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false)
9899
Expect(err).ToNot(HaveOccurred())
99100
Expect(ip).To(Equal(testIPDNS))
101+
Expect(resolver).To(Equal(lbPublicIP))
100102
})
101103
})
102104

@@ -107,7 +109,7 @@ var _ = Describe("public ip resolvers", func() {
107109
loadBalancerRetryConfig.Steps = 1
108110
backendConfig[publicIPConfig] = lbPublicIP
109111
client := fake.NewClientset(serviceWithIngress())
110-
_, err := getPublicIP(submSpec, client, backendConfig, false)
112+
_, _, err := getPublicIP(submSpec, client, backendConfig, false)
111113
Expect(err).To(HaveOccurred())
112114
})
113115
})
@@ -116,59 +118,65 @@ var _ = Describe("public ip resolvers", func() {
116118
It("should return the IP", func() {
117119
backendConfig[publicIPConfig] = ipv4PublicIP
118120
client := fake.NewClientset()
119-
ip, err := getPublicIP(submSpec, client, backendConfig, false)
121+
ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false)
120122
Expect(err).ToNot(HaveOccurred())
121123
Expect(ip).To(Equal(testIP))
124+
Expect(resolver).To(Equal(ipv4PublicIP))
122125
})
123126
})
124127

125128
When("an IPv4 entry specified in air-gapped deployment", func() {
126129
It("should return the IP and not an empty value", func() {
127130
backendConfig[publicIPConfig] = ipv4PublicIP
128131
client := fake.NewClientset()
129-
ip, err := getPublicIP(submSpec, client, backendConfig, true)
132+
ip, resolver, err := getPublicIP(submSpec, client, backendConfig, true)
130133
Expect(err).ToNot(HaveOccurred())
131134
Expect(ip).To(Equal(testIP))
135+
Expect(resolver).To(Equal(ipv4PublicIP))
132136
})
133137
})
134138

135139
When("a DNS entry specified", func() {
136140
It("should return the IP", func() {
137141
backendConfig[publicIPConfig] = "dns:" + dnsHost
138142
client := fake.NewClientset()
139-
ip, err := getPublicIP(submSpec, client, backendConfig, false)
143+
ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false)
140144
Expect(err).ToNot(HaveOccurred())
141145
Expect(ip).To(Equal(testIPDNS))
146+
Expect(resolver).To(Equal(backendConfig[publicIPConfig]))
142147
})
143148
})
144149

145150
When("an API entry specified", func() {
146151
It("should return some IP", func() {
147152
backendConfig[publicIPConfig] = "api:4.icanhazip.com/"
148153
client := fake.NewClientset()
149-
ip, err := getPublicIP(submSpec, client, backendConfig, false)
154+
ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false)
150155
Expect(err).ToNot(HaveOccurred())
151156
Expect(net.ParseIP(ip)).NotTo(BeNil())
157+
Expect(resolver).To(Equal(backendConfig[publicIPConfig]))
152158
})
153159
})
154160

155161
When("multiple entries are specified", func() {
156162
It("should return the first working one", func() {
157163
backendConfig[publicIPConfig] = ipv4PublicIP + ",dns:" + dnsHost
158164
client := fake.NewClientset()
159-
ip, err := getPublicIP(submSpec, client, backendConfig, false)
165+
ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false)
160166
Expect(err).ToNot(HaveOccurred())
161167
Expect(ip).To(Equal(testIP))
168+
Expect(resolver).To(Equal(ipv4PublicIP))
162169
})
163170
})
164171

165172
When("multiple entries are specified and the first one doesn't succeed", func() {
166173
It("should return the first working one", func() {
167-
backendConfig[publicIPConfig] = "dns:thisdomaindoesntexistforsure.badbadbad,ipv4:" + testIP
174+
backendConfig[publicIPConfig] = "dns:thisdomaindoesntexistforsure.badbadbad," + ipv4PublicIP
168175
client := fake.NewClientset()
169-
ip, err := getPublicIP(submSpec, client, backendConfig, false)
176+
ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false)
170177
Expect(err).ToNot(HaveOccurred())
171178
Expect(ip).To(Equal(testIP))
179+
Expect(resolver).To(Equal(ipv4PublicIP))
172180
})
173181
})
174182
})

pkg/endpoint/public_ip_watcher.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ func (p *PublicIPWatcher) Run(stopCh <-chan struct{}) {
6565
func (p *PublicIPWatcher) syncPublicIP() {
6666
localEndpointSpec := p.config.LocalEndpoint.Spec()
6767

68-
publicIP, err := getPublicIP(p.config.SubmSpec, p.config.K8sClient, localEndpointSpec.BackendConfig, false)
68+
publicIP, resolver, err := getPublicIP(p.config.SubmSpec, p.config.K8sClient, localEndpointSpec.BackendConfig, false)
6969
if err != nil {
7070
logger.Warningf("Could not determine public IP of the gateway node %q: %v", localEndpointSpec.Hostname, err)
7171
return
7272
}
7373

7474
if localEndpointSpec.PublicIP != publicIP {
75-
logger.Infof("Public IP changed for the Gateway, updating the local endpoint with publicIP %q", publicIP)
75+
logger.Infof("Public IP changed for the Gateway, updating the local endpoint with public IP %q obtained from resolver %q",
76+
publicIP, resolver)
7677

7778
if err := p.updateLocalEndpoint(publicIP); err != nil {
7879
logger.Error(err, "Error updating the public IP for local endpoint")

0 commit comments

Comments
 (0)