Skip to content

Commit

Permalink
ipam: fix gateway being required for IPAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
schrej committed Apr 12, 2023
1 parent 701b97b commit 450658e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
17 changes: 9 additions & 8 deletions exp/ipam/internal/webhooks/ipaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions exp/ipam/internal/webhooks/ipaddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 450658e

Please sign in to comment.