Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.4] 🐛 ipam: fix gateway being required for IPAddress #8574

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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