Skip to content

Commit e7ba6c8

Browse files
authored
Merge pull request #8506 from schrej/ipam-fix-required-gateway
🐛 ipam: fix gateway being required for IPAddress
2 parents 5f3e095 + d9a6aa7 commit e7ba6c8

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

config/crd/bases/ipam.cluster.x-k8s.io_ipaddresses.yaml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/ipam/api/v1alpha1/ipaddress_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ type IPAddressSpec struct {
3636
Prefix int `json:"prefix"`
3737

3838
// Gateway is the network gateway of the network the address is from.
39-
Gateway string `json:"gateway"`
39+
// +optional
40+
Gateway string `json:"gateway,omitempty"`
4041
}
4142

4243
// +kubebuilder:object:root=true

exp/ipam/internal/webhooks/ipaddress.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,15 @@ func (webhook *IPAddress) validate(ctx context.Context, ip *ipamv1.IPAddress) er
123123
))
124124
}
125125

126-
_, err = netip.ParseAddr(ip.Spec.Gateway)
127-
if err != nil {
128-
allErrs = append(allErrs,
129-
field.Invalid(
130-
specPath.Child("gateway"),
131-
ip.Spec.Gateway,
132-
"not a valid IP address",
133-
))
126+
if ip.Spec.Gateway != "" {
127+
if _, err := netip.ParseAddr(ip.Spec.Gateway); err != nil {
128+
allErrs = append(allErrs,
129+
field.Invalid(
130+
specPath.Child("gateway"),
131+
ip.Spec.Gateway,
132+
"not a valid IP address",
133+
))
134+
}
134135
}
135136

136137
if ip.Spec.PoolRef.APIGroup == nil {

exp/ipam/internal/webhooks/ipaddress_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ func TestIPAddressValidateCreate(t *testing.T) {
130130
extraObjs: []client.Object{claim},
131131
expectErr: true,
132132
},
133+
{
134+
name: "an empty gateway should be allowed",
135+
ip: getAddress(false, func(addr *ipamv1.IPAddress) {
136+
addr.Spec.Gateway = ""
137+
}),
138+
extraObjs: []client.Object{claim},
139+
expectErr: false,
140+
},
133141
{
134142
name: "a pool reference that does not match the claim should be rejected",
135143
ip: getAddress(false, func(addr *ipamv1.IPAddress) {

0 commit comments

Comments
 (0)