From 450658e32b6d1f7529ad322d7361a680a97888df Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Tue, 11 Apr 2023 10:58:32 +0200 Subject: [PATCH] ipam: fix gateway being required for IPAddress --- exp/ipam/internal/webhooks/ipaddress.go | 17 +++++++++-------- exp/ipam/internal/webhooks/ipaddress_test.go | 8 ++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/exp/ipam/internal/webhooks/ipaddress.go b/exp/ipam/internal/webhooks/ipaddress.go index 45c4f5cf05e9..6cf6b056ae9a 100644 --- a/exp/ipam/internal/webhooks/ipaddress.go +++ b/exp/ipam/internal/webhooks/ipaddress.go @@ -123,14 +123,15 @@ func (webhook *IPAddress) validate(ctx context.Context, ip *ipamv1.IPAddress) er )) } - _, err = netip.ParseAddr(ip.Spec.Gateway) - if err != nil { - allErrs = append(allErrs, - field.Invalid( - specPath.Child("gateway"), - ip.Spec.Gateway, - "not a valid IP address", - )) + if ip.Spec.Gateway != "" { + if _, err := netip.ParseAddr(ip.Spec.Gateway); err != nil { + allErrs = append(allErrs, + field.Invalid( + specPath.Child("gateway"), + ip.Spec.Gateway, + "not a valid IP address", + )) + } } if ip.Spec.PoolRef.APIGroup == nil { diff --git a/exp/ipam/internal/webhooks/ipaddress_test.go b/exp/ipam/internal/webhooks/ipaddress_test.go index 5c8809fa253c..5cdad9a5ddcf 100644 --- a/exp/ipam/internal/webhooks/ipaddress_test.go +++ b/exp/ipam/internal/webhooks/ipaddress_test.go @@ -130,6 +130,14 @@ func TestIPAddressValidateCreate(t *testing.T) { extraObjs: []client.Object{claim}, expectErr: true, }, + { + name: "an empty gateway should be allowed", + ip: getAddress(false, func(addr *ipamv1.IPAddress) { + addr.Spec.Gateway = "" + }), + extraObjs: []client.Object{claim}, + expectErr: false, + }, { name: "a pool reference that does not match the claim should be rejected", ip: getAddress(false, func(addr *ipamv1.IPAddress) {