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 11, 2023
1 parent 701b97b commit e498306
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 10 additions & 8 deletions exp/ipam/internal/webhooks/ipaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,16 @@ 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 != "" {
_, 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.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 e498306

Please sign in to comment.