From 90d2137cf6e68bbecff6f973132fbc98e5f197e5 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Fri, 3 Jan 2025 16:06:25 -0500 Subject: [PATCH] Log the resolver used to obtain the local endpoint public IP ...to aid in debugging. Signed-off-by: Tom Pantelis --- pkg/endpoint/local_endpoint.go | 4 +++- pkg/endpoint/public_ip.go | 26 ++++++++++++----------- pkg/endpoint/public_ip_internal_test.go | 28 ++++++++++++++++--------- pkg/endpoint/public_ip_watcher.go | 5 +++-- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/pkg/endpoint/local_endpoint.go b/pkg/endpoint/local_endpoint.go index 4d65663af..32a1cb8ae 100644 --- a/pkg/endpoint/local_endpoint.go +++ b/pkg/endpoint/local_endpoint.go @@ -181,11 +181,13 @@ func GetLocalSpec(ctx context.Context, submSpec *types.SubmarinerSpecification, BackendConfig: backendConfig, } - publicIP, err := getPublicIP(submSpec, k8sClient, backendConfig, airGappedDeployment) + publicIP, resolver, err := getPublicIP(submSpec, k8sClient, backendConfig, airGappedDeployment) if err != nil { return nil, errors.Wrap(err, "could not determine public IP") } + logger.Infof("Obtained local endpoint public IP %q using resolver %q", publicIP, resolver) + endpointSpec.PublicIP = publicIP if submSpec.HealthCheckEnabled && !globalnetEnabled { diff --git a/pkg/endpoint/public_ip.go b/pkg/endpoint/public_ip.go index aef89e2a2..569a0bc70 100644 --- a/pkg/endpoint/public_ip.go +++ b/pkg/endpoint/public_ip.go @@ -65,7 +65,7 @@ func getPublicIPResolvers() string { func getPublicIP(submSpec *types.SubmarinerSpecification, k8sClient kubernetes.Interface, backendConfig map[string]string, airGapped bool, -) (string, error) { +) (string, string, error) { // If the node is annotated with a public-ip, the same is used as the public-ip of local endpoint. config, ok := backendConfig[v1.PublicIP] if !ok { @@ -77,13 +77,13 @@ func getPublicIP(submSpec *types.SubmarinerSpecification, k8sClient kubernetes.I } if airGapped { - ip, err := resolveIPInAirGappedDeployment(k8sClient, submSpec.Namespace, config) + ip, resolver, err := resolveIPInAirGappedDeployment(k8sClient, submSpec.Namespace, config) if err != nil { logger.Errorf(err, "Error resolving public IP in an air-gapped deployment, using empty value: %s", config) - return "", nil + return "", "", nil } - return ip, nil + return ip, resolver, nil } resolvers := strings.Split(config, ",") @@ -94,12 +94,12 @@ func getPublicIP(submSpec *types.SubmarinerSpecification, k8sClient kubernetes.I parts := strings.Split(resolver, ":") if len(parts) != 2 { - return "", errors.Errorf("invalid format for %q annotation: %q", v1.GatewayConfigPrefix+v1.PublicIP, config) + return "", "", errors.Errorf("invalid format for %q annotation: %q", v1.GatewayConfigPrefix+v1.PublicIP, config) } ip, err := resolvePublicIP(k8sClient, submSpec.Namespace, parts) if err == nil { - return ip, nil + return ip, resolver, nil } // If this resolver failed, we log it, but we fall back to the next one @@ -107,13 +107,13 @@ func getPublicIP(submSpec *types.SubmarinerSpecification, k8sClient kubernetes.I } if len(resolvers) > 0 { - return "", errors.Wrapf(k8serrors.NewAggregate(errs), "Unable to resolve public IP by any of the resolver methods") + return "", "", errors.Wrapf(k8serrors.NewAggregate(errs), "Unable to resolve public IP by any of the resolver methods") } - return "", nil + return "", "", nil } -func resolveIPInAirGappedDeployment(k8sClient kubernetes.Interface, namespace, config string) (string, error) { +func resolveIPInAirGappedDeployment(k8sClient kubernetes.Interface, namespace, config string) (string, string, error) { resolvers := strings.Split(config, ",") for _, resolver := range resolvers { @@ -121,17 +121,19 @@ func resolveIPInAirGappedDeployment(k8sClient kubernetes.Interface, namespace, c parts := strings.Split(resolver, ":") if len(parts) != 2 { - return "", errors.Errorf("invalid format for %q annotation: %q", v1.GatewayConfigPrefix+v1.PublicIP, config) + return "", "", errors.Errorf("invalid format for %q annotation: %q", v1.GatewayConfigPrefix+v1.PublicIP, config) } if parts[0] != v1.IPv4 { continue } - return resolvePublicIP(k8sClient, namespace, parts) + ip, err := resolvePublicIP(k8sClient, namespace, parts) + + return ip, resolver, err } - return "", nil + return "", "", nil } func resolvePublicIP(k8sClient kubernetes.Interface, namespace string, parts []string) (string, error) { diff --git a/pkg/endpoint/public_ip_internal_test.go b/pkg/endpoint/public_ip_internal_test.go index b3620aba9..85c800eb6 100644 --- a/pkg/endpoint/public_ip_internal_test.go +++ b/pkg/endpoint/public_ip_internal_test.go @@ -81,9 +81,10 @@ var _ = Describe("public ip resolvers", func() { It("should return the IP", func() { backendConfig[publicIPConfig] = lbPublicIP client := fake.NewClientset(serviceWithIngress(v1.LoadBalancerIngress{Hostname: "", IP: testIP})) - ip, err := getPublicIP(submSpec, client, backendConfig, false) + ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false) Expect(err).ToNot(HaveOccurred()) Expect(ip).To(Equal(testIP)) + Expect(resolver).To(Equal(lbPublicIP)) }) }) @@ -94,9 +95,10 @@ var _ = Describe("public ip resolvers", func() { Hostname: dnsHost, IP: "", })) - ip, err := getPublicIP(submSpec, client, backendConfig, false) + ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false) Expect(err).ToNot(HaveOccurred()) Expect(ip).To(Equal(testIPDNS)) + Expect(resolver).To(Equal(lbPublicIP)) }) }) @@ -107,7 +109,7 @@ var _ = Describe("public ip resolvers", func() { loadBalancerRetryConfig.Steps = 1 backendConfig[publicIPConfig] = lbPublicIP client := fake.NewClientset(serviceWithIngress()) - _, err := getPublicIP(submSpec, client, backendConfig, false) + _, _, err := getPublicIP(submSpec, client, backendConfig, false) Expect(err).To(HaveOccurred()) }) }) @@ -116,9 +118,10 @@ var _ = Describe("public ip resolvers", func() { It("should return the IP", func() { backendConfig[publicIPConfig] = ipv4PublicIP client := fake.NewClientset() - ip, err := getPublicIP(submSpec, client, backendConfig, false) + ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false) Expect(err).ToNot(HaveOccurred()) Expect(ip).To(Equal(testIP)) + Expect(resolver).To(Equal(ipv4PublicIP)) }) }) @@ -126,9 +129,10 @@ var _ = Describe("public ip resolvers", func() { It("should return the IP and not an empty value", func() { backendConfig[publicIPConfig] = ipv4PublicIP client := fake.NewClientset() - ip, err := getPublicIP(submSpec, client, backendConfig, true) + ip, resolver, err := getPublicIP(submSpec, client, backendConfig, true) Expect(err).ToNot(HaveOccurred()) Expect(ip).To(Equal(testIP)) + Expect(resolver).To(Equal(ipv4PublicIP)) }) }) @@ -136,9 +140,10 @@ var _ = Describe("public ip resolvers", func() { It("should return the IP", func() { backendConfig[publicIPConfig] = "dns:" + dnsHost client := fake.NewClientset() - ip, err := getPublicIP(submSpec, client, backendConfig, false) + ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false) Expect(err).ToNot(HaveOccurred()) Expect(ip).To(Equal(testIPDNS)) + Expect(resolver).To(Equal(backendConfig[publicIPConfig])) }) }) @@ -146,9 +151,10 @@ var _ = Describe("public ip resolvers", func() { It("should return some IP", func() { backendConfig[publicIPConfig] = "api:4.icanhazip.com/" client := fake.NewClientset() - ip, err := getPublicIP(submSpec, client, backendConfig, false) + ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false) Expect(err).ToNot(HaveOccurred()) Expect(net.ParseIP(ip)).NotTo(BeNil()) + Expect(resolver).To(Equal(backendConfig[publicIPConfig])) }) }) @@ -156,19 +162,21 @@ var _ = Describe("public ip resolvers", func() { It("should return the first working one", func() { backendConfig[publicIPConfig] = ipv4PublicIP + ",dns:" + dnsHost client := fake.NewClientset() - ip, err := getPublicIP(submSpec, client, backendConfig, false) + ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false) Expect(err).ToNot(HaveOccurred()) Expect(ip).To(Equal(testIP)) + Expect(resolver).To(Equal(ipv4PublicIP)) }) }) When("multiple entries are specified and the first one doesn't succeed", func() { It("should return the first working one", func() { - backendConfig[publicIPConfig] = "dns:thisdomaindoesntexistforsure.badbadbad,ipv4:" + testIP + backendConfig[publicIPConfig] = "dns:thisdomaindoesntexistforsure.badbadbad," + ipv4PublicIP client := fake.NewClientset() - ip, err := getPublicIP(submSpec, client, backendConfig, false) + ip, resolver, err := getPublicIP(submSpec, client, backendConfig, false) Expect(err).ToNot(HaveOccurred()) Expect(ip).To(Equal(testIP)) + Expect(resolver).To(Equal(ipv4PublicIP)) }) }) }) diff --git a/pkg/endpoint/public_ip_watcher.go b/pkg/endpoint/public_ip_watcher.go index 0653605d3..7ff5b001f 100644 --- a/pkg/endpoint/public_ip_watcher.go +++ b/pkg/endpoint/public_ip_watcher.go @@ -65,14 +65,15 @@ func (p *PublicIPWatcher) Run(stopCh <-chan struct{}) { func (p *PublicIPWatcher) syncPublicIP() { localEndpointSpec := p.config.LocalEndpoint.Spec() - publicIP, err := getPublicIP(p.config.SubmSpec, p.config.K8sClient, localEndpointSpec.BackendConfig, false) + publicIP, resolver, err := getPublicIP(p.config.SubmSpec, p.config.K8sClient, localEndpointSpec.BackendConfig, false) if err != nil { logger.Warningf("Could not determine public IP of the gateway node %q: %v", localEndpointSpec.Hostname, err) return } if localEndpointSpec.PublicIP != publicIP { - logger.Infof("Public IP changed for the Gateway, updating the local endpoint with publicIP %q", publicIP) + logger.Infof("Public IP changed for the Gateway, updating the local endpoint with public IP %q obtained from resolver %q", + publicIP, resolver) if err := p.updateLocalEndpoint(publicIP); err != nil { logger.Error(err, "Error updating the public IP for local endpoint")