Skip to content

Commit 6600589

Browse files
tpantelisskitt
authored andcommitted
Ignore empty or invalid IP in Endpoint Set*IP
Signed-off-by: Tom Pantelis <[email protected]>
1 parent 662dbfd commit 6600589

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/apis/submariner.io/v1/endpoint.go

+12
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ import (
2323
"strconv"
2424

2525
"github.com/pkg/errors"
26+
"github.com/submariner-io/admiral/pkg/log"
2627
"github.com/submariner-io/admiral/pkg/resource"
2728
"k8s.io/apimachinery/pkg/api/equality"
2829
k8snet "k8s.io/utils/net"
30+
logf "sigs.k8s.io/controller-runtime/pkg/log"
2931
)
3032

33+
var logger = log.Logger{Logger: logf.Log.WithName("EndpointAPI")}
34+
3135
func (ep *EndpointSpec) GetBackendPort(configName string, defaultValue int32) (int32, error) {
3236
if portStr := ep.BackendConfig[configName]; portStr != "" {
3337
port, err := parsePort(portStr)
@@ -119,7 +123,15 @@ func getIPFrom(family k8snet.IPFamily, ips []string, ipv4Fallback string) string
119123
}
120124

121125
func setIP(ips []string, ipv4Fallback, newIP string) ([]string, string) {
126+
if newIP == "" {
127+
return ips, ipv4Fallback
128+
}
129+
122130
family := k8snet.IPFamilyOfString(newIP)
131+
if family == k8snet.IPFamilyUnknown {
132+
logger.Errorf(nil, "Unable to determine IP family for %q - ignoring", newIP)
133+
return ips, ipv4Fallback
134+
}
123135

124136
if family == k8snet.IPv4 {
125137
ipv4Fallback = newIP

pkg/apis/submariner.io/v1/endpoint_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ func testGetIP(ipsSetter func(*v1.EndpointSpec, []string, string), ipsGetter fun
218218
})
219219
})
220220
})
221+
222+
When("the specified family is IPFamilyUnknown", func() {
223+
BeforeEach(func() {
224+
ips = []string{ipV4Addr, ipV6Addr}
225+
})
226+
227+
It("should return empty string", func() {
228+
Expect(ipsGetter(spec, k8snet.IPFamilyUnknown)).To(BeEmpty())
229+
})
230+
})
221231
}
222232

223233
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
309319
})
310320
})
311321
})
322+
323+
When("the specified IP is empty", func() {
324+
BeforeEach(func() {
325+
initialIPs = []string{ipV6Addr}
326+
})
327+
328+
It("should not add it", func() {
329+
verifyIPs([]string{ipV6Addr}, "")
330+
})
331+
})
332+
333+
When("the specified IP is invalid", func() {
334+
BeforeEach(func() {
335+
ipToSet = "invalid"
336+
})
337+
338+
It("should not add it", func() {
339+
verifyIPs([]string{}, "")
340+
})
341+
})
312342
}
313343

314344
func testGetHealthCheckIP() {

0 commit comments

Comments
 (0)