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) {