Skip to content

Commit

Permalink
Ignore empty or invalid IP in Endpoint Set*IP
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Jan 21, 2025
1 parent 662dbfd commit eb33509
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/apis/submariner.io/v1/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions pkg/apis/submariner.io/v1/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit eb33509

Please sign in to comment.