From eb3350979bf018e672a0c95dd5865f04a544c0de Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Tue, 21 Jan 2025 08:49:51 -0500 Subject: [PATCH] Ignore empty or invalid IP in Endpoint Set*IP Signed-off-by: Tom Pantelis --- pkg/apis/submariner.io/v1/endpoint.go | 12 +++++++++ pkg/apis/submariner.io/v1/endpoint_test.go | 30 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/pkg/apis/submariner.io/v1/endpoint.go b/pkg/apis/submariner.io/v1/endpoint.go index 95ebabd8c..869c2f45c 100644 --- a/pkg/apis/submariner.io/v1/endpoint.go +++ b/pkg/apis/submariner.io/v1/endpoint.go @@ -23,11 +23,15 @@ import ( "strconv" "github.com/pkg/errors" + "github.com/submariner-io/admiral/pkg/log" "github.com/submariner-io/admiral/pkg/resource" "k8s.io/apimachinery/pkg/api/equality" k8snet "k8s.io/utils/net" + logf "sigs.k8s.io/controller-runtime/pkg/log" ) +var logger = log.Logger{Logger: logf.Log.WithName("EndpointAPI")} + func (ep *EndpointSpec) GetBackendPort(configName string, defaultValue int32) (int32, error) { if portStr := ep.BackendConfig[configName]; portStr != "" { port, err := parsePort(portStr) @@ -119,7 +123,15 @@ func getIPFrom(family k8snet.IPFamily, ips []string, ipv4Fallback string) string } func setIP(ips []string, ipv4Fallback, newIP string) ([]string, string) { + if newIP == "" { + return ips, ipv4Fallback + } + family := k8snet.IPFamilyOfString(newIP) + if family == k8snet.IPFamilyUnknown { + logger.Errorf(nil, "Unable to determine IP family for %q - ignoring", newIP) + return ips, ipv4Fallback + } if family == k8snet.IPv4 { ipv4Fallback = newIP diff --git a/pkg/apis/submariner.io/v1/endpoint_test.go b/pkg/apis/submariner.io/v1/endpoint_test.go index 13e197486..aef4c07ac 100644 --- a/pkg/apis/submariner.io/v1/endpoint_test.go +++ b/pkg/apis/submariner.io/v1/endpoint_test.go @@ -218,6 +218,16 @@ func testGetIP(ipsSetter func(*v1.EndpointSpec, []string, string), ipsGetter fun }) }) }) + + When("the specified family is IPFamilyUnknown", func() { + BeforeEach(func() { + ips = []string{ipV4Addr, ipV6Addr} + }) + + It("should return empty string", func() { + Expect(ipsGetter(spec, k8snet.IPFamilyUnknown)).To(BeEmpty()) + }) + }) } func testSetIP(initIPs func(*v1.EndpointSpec, []string), ipsSetter func(*v1.EndpointSpec, string), @@ -309,6 +319,26 @@ func testSetIP(initIPs func(*v1.EndpointSpec, []string), ipsSetter func(*v1.Endp }) }) }) + + When("the specified IP is empty", func() { + BeforeEach(func() { + initialIPs = []string{ipV6Addr} + }) + + It("should not add it", func() { + verifyIPs([]string{ipV6Addr}, "") + }) + }) + + When("the specified IP is invalid", func() { + BeforeEach(func() { + ipToSet = "invalid" + }) + + It("should not add it", func() { + verifyIPs([]string{}, "") + }) + }) } func testGetHealthCheckIP() {