Skip to content

Commit

Permalink
Merge pull request #8574 from k8s-infra-cherrypick-robot/cherry-pick-…
Browse files Browse the repository at this point in the history
…8506-to-release-1.4

[release-1.4] 🐛 ipam: fix gateway being required for IPAddress
  • Loading branch information
k8s-ci-robot committed Apr 26, 2023
2 parents dea2b8b + ad88b30 commit 3f4750a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 0 additions & 1 deletion config/crd/bases/ipam.cluster.x-k8s.io_ipaddresses.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion exp/ipam/api/v1alpha1/ipaddress_types.go
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions exp/ipam/internal/webhooks/ipaddress.go
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
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 3f4750a

Please sign in to comment.