From ad88b303b3bf8fcb114b72095559c4c476246d31 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 --- .../ipam.cluster.x-k8s.io_ipaddresses.yaml | 1 - exp/ipam/api/v1alpha1/ipaddress_types.go | 3 ++- exp/ipam/internal/webhooks/ipaddress.go | 17 +++++++++-------- exp/ipam/internal/webhooks/ipaddress_test.go | 8 ++++++++ 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/config/crd/bases/ipam.cluster.x-k8s.io_ipaddresses.yaml b/config/crd/bases/ipam.cluster.x-k8s.io_ipaddresses.yaml index cdee9f5a8a0b..81be5e44b689 100644 --- a/config/crd/bases/ipam.cluster.x-k8s.io_ipaddresses.yaml +++ b/config/crd/bases/ipam.cluster.x-k8s.io_ipaddresses.yaml @@ -92,7 +92,6 @@ spec: required: - address - claimRef - - gateway - poolRef - prefix type: object diff --git a/exp/ipam/api/v1alpha1/ipaddress_types.go b/exp/ipam/api/v1alpha1/ipaddress_types.go index 14585552772d..01af31d26769 100644 --- a/exp/ipam/api/v1alpha1/ipaddress_types.go +++ b/exp/ipam/api/v1alpha1/ipaddress_types.go @@ -36,7 +36,8 @@ type IPAddressSpec struct { Prefix int `json:"prefix"` // Gateway is the network gateway of the network the address is from. - Gateway string `json:"gateway"` + // +optional + Gateway string `json:"gateway,omitempty"` } // +kubebuilder:object:root=true 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) {