Skip to content

Commit 7061546

Browse files
committed
Change IPAddressSpec.Prefix from int to *int32
Signed-off-by: Stefan Büringer [email protected]
1 parent d8ff3b9 commit 7061546

File tree

10 files changed

+93
-53
lines changed

10 files changed

+93
-53
lines changed

api/ipam/v1alpha1/conversion.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1alpha1
1818

1919
import (
2020
apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
21+
"k8s.io/utils/ptr"
2122
"sigs.k8s.io/controller-runtime/pkg/conversion"
2223

2324
clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1"
@@ -118,6 +119,22 @@ func (dst *IPAddressClaim) ConvertFrom(srcRaw conversion.Hub) error {
118119
return nil
119120
}
120121

122+
func Convert_v1alpha1_IPAddressSpec_To_v1beta2_IPAddressSpec(in *IPAddressSpec, out *ipamv1.IPAddressSpec, s apimachineryconversion.Scope) error {
123+
if err := autoConvert_v1alpha1_IPAddressSpec_To_v1beta2_IPAddressSpec(in, out, s); err != nil {
124+
return err
125+
}
126+
out.Prefix = ptr.To(int32(in.Prefix))
127+
return nil
128+
}
129+
130+
func Convert_v1beta2_IPAddressSpec_To_v1alpha1_IPAddressSpec(in *ipamv1.IPAddressSpec, out *IPAddressSpec, s apimachineryconversion.Scope) error {
131+
if err := autoConvert_v1beta2_IPAddressSpec_To_v1alpha1_IPAddressSpec(in, out, s); err != nil {
132+
return err
133+
}
134+
out.Prefix = int(ptr.Deref(in.Prefix, 0))
135+
return nil
136+
}
137+
121138
func Convert_v1beta2_IPAddressClaimSpec_To_v1alpha1_IPAddressClaimSpec(from *ipamv1.IPAddressClaimSpec, to *IPAddressClaimSpec, scope apimachineryconversion.Scope) error {
122139
return autoConvert_v1beta2_IPAddressClaimSpec_To_v1alpha1_IPAddressClaimSpec(from, to, scope)
123140
}

api/ipam/v1alpha1/conversion_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
2727
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
28+
"k8s.io/utils/ptr"
2829
"sigs.k8s.io/randfill"
2930

3031
ipamv1 "sigs.k8s.io/cluster-api/api/ipam/v1beta2"
@@ -48,10 +49,19 @@ func TestFuzzyConversion(t *testing.T) {
4849

4950
func IPAddressFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
5051
return []interface{}{
52+
hubIPAddressSpec,
5153
spokeTypedLocalObjectReference,
5254
}
5355
}
5456

57+
func hubIPAddressSpec(in *ipamv1.IPAddressSpec, c randfill.Continue) {
58+
c.FillNoCustom(in)
59+
60+
if in.Prefix == nil {
61+
in.Prefix = ptr.To(int32(0)) // Prefix is a required field and nil does not round trip
62+
}
63+
}
64+
5565
func spokeTypedLocalObjectReference(in *corev1.TypedLocalObjectReference, c randfill.Continue) {
5666
c.FillNoCustom(in)
5767
if in.APIGroup != nil && *in.APIGroup == "" {

api/ipam/v1alpha1/zz_generated.conversion.go

Lines changed: 8 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/ipam/v1beta1/conversion.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ func Convert_v1beta2_IPAddressClaimReference_To_v1_LocalObjectReference(in *ipam
142142
return nil
143143
}
144144

145+
func Convert_v1beta1_IPAddressSpec_To_v1beta2_IPAddressSpec(in *IPAddressSpec, out *ipamv1.IPAddressSpec, s apimachineryconversion.Scope) error {
146+
if err := autoConvert_v1beta1_IPAddressSpec_To_v1beta2_IPAddressSpec(in, out, s); err != nil {
147+
return err
148+
}
149+
out.Prefix = ptr.To(int32(in.Prefix))
150+
return nil
151+
}
152+
153+
func Convert_v1beta2_IPAddressSpec_To_v1beta1_IPAddressSpec(in *ipamv1.IPAddressSpec, out *IPAddressSpec, s apimachineryconversion.Scope) error {
154+
if err := autoConvert_v1beta2_IPAddressSpec_To_v1beta1_IPAddressSpec(in, out, s); err != nil {
155+
return err
156+
}
157+
out.Prefix = int(ptr.Deref(in.Prefix, 0))
158+
return nil
159+
}
160+
145161
// Implement local conversion func because conversion-gen is not aware of conversion func in other packages (see https://github.com/kubernetes/code-generator/issues/94)
146162

147163
func Convert_v1_Condition_To_v1beta1_Condition(in *metav1.Condition, out *clusterv1beta1.Condition, s apimachineryconversion.Scope) error {

api/ipam/v1beta1/conversion_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
2727
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
28+
"k8s.io/utils/ptr"
2829
"sigs.k8s.io/randfill"
2930

3031
ipamv1 "sigs.k8s.io/cluster-api/api/ipam/v1beta2"
@@ -48,10 +49,19 @@ func TestFuzzyConversion(t *testing.T) {
4849

4950
func IPAddressFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
5051
return []interface{}{
52+
hubIPAddressSpec,
5153
spokeTypedLocalObjectReference,
5254
}
5355
}
5456

57+
func hubIPAddressSpec(in *ipamv1.IPAddressSpec, c randfill.Continue) {
58+
c.FillNoCustom(in)
59+
60+
if in.Prefix == nil {
61+
in.Prefix = ptr.To(int32(0)) // Prefix is a required field and nil does not round trip
62+
}
63+
}
64+
5565
func spokeTypedLocalObjectReference(in *corev1.TypedLocalObjectReference, c randfill.Continue) {
5666
c.FillNoCustom(in)
5767
if in.APIGroup != nil && *in.APIGroup == "" {

api/ipam/v1beta1/zz_generated.conversion.go

Lines changed: 12 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/ipam/v1beta2/ipaddress_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type IPAddressSpec struct {
4040
// +required
4141
// +kubebuilder:validation:Minimum=0
4242
// +kubebuilder:validation:Maximum=128
43-
Prefix int32 `json:"prefix"`
43+
Prefix *int32 `json:"prefix,omitempty"`
4444

4545
// gateway is the network gateway of the network the address is from.
4646
// +optional

api/ipam/v1beta2/zz_generated.deepcopy.go

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

exp/ipam/internal/webhooks/ipaddress.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/types"
2828
"k8s.io/apimachinery/pkg/util/validation/field"
2929
"k8s.io/klog/v2"
30+
"k8s.io/utils/ptr"
3031
ctrl "sigs.k8s.io/controller-runtime"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
3233
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -105,27 +106,27 @@ func (webhook *IPAddress) validate(ctx context.Context, ip *ipamv1.IPAddress) er
105106
))
106107
}
107108

108-
if ip.Spec.Prefix < 0 {
109+
if ptr.Deref(ip.Spec.Prefix, 0) < 0 {
109110
allErrs = append(allErrs,
110111
field.Invalid(
111112
specPath.Child("prefix"),
112113
ip.Spec.Prefix,
113114
"prefix cannot be negative",
114115
))
115116
}
116-
if addr.Is4() && ip.Spec.Prefix > 32 {
117+
if addr.Is4() && ptr.Deref(ip.Spec.Prefix, 0) > 32 {
117118
allErrs = append(allErrs,
118119
field.Invalid(
119120
specPath.Child("prefix"),
120-
ip.Spec.Prefix,
121+
ptr.Deref(ip.Spec.Prefix, 0),
121122
"prefix is too large for an IPv4 address",
122123
))
123124
}
124-
if addr.Is6() && ip.Spec.Prefix > 128 {
125+
if addr.Is6() && ptr.Deref(ip.Spec.Prefix, 0) > 128 {
125126
allErrs = append(allErrs,
126127
field.Invalid(
127128
specPath.Child("prefix"),
128-
ip.Spec.Prefix,
129+
ptr.Deref(ip.Spec.Prefix, 0),
129130
"prefix is too large for an IPv6 address",
130131
))
131132
}

exp/ipam/internal/webhooks/ipaddress_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
. "github.com/onsi/gomega"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/utils/ptr"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
2728
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2829

@@ -57,13 +58,13 @@ func TestIPAddressValidateCreate(t *testing.T) {
5758
ClaimRef: ipamv1.IPAddressClaimReference{Name: claim.Name},
5859
PoolRef: claim.Spec.PoolRef,
5960
Address: "10.0.0.1",
60-
Prefix: 24,
61+
Prefix: ptr.To(int32(24)),
6162
Gateway: "10.0.0.254",
6263
},
6364
}
6465
if v6 {
6566
addr.Spec.Address = "42::1"
66-
addr.Spec.Prefix = 64
67+
addr.Spec.Prefix = ptr.To(int32(64))
6768
addr.Spec.Gateway = "42::ffff"
6869
}
6970
fn(&addr)
@@ -91,23 +92,23 @@ func TestIPAddressValidateCreate(t *testing.T) {
9192
{
9293
name: "a prefix that is negative should be rejected",
9394
ip: getAddress(false, func(addr *ipamv1.IPAddress) {
94-
addr.Spec.Prefix = -1
95+
addr.Spec.Prefix = ptr.To(int32(-1))
9596
}),
9697
extraObjs: []client.Object{claim},
9798
expectErr: true,
9899
},
99100
{
100101
name: "a prefix that is too large for v4 should be rejected",
101102
ip: getAddress(false, func(addr *ipamv1.IPAddress) {
102-
addr.Spec.Prefix = 64
103+
addr.Spec.Prefix = ptr.To(int32(64))
103104
}),
104105
extraObjs: []client.Object{claim},
105106
expectErr: true,
106107
},
107108
{
108109
name: "a prefix that is too large for v6 should be rejected",
109110
ip: getAddress(true, func(addr *ipamv1.IPAddress) {
110-
addr.Spec.Prefix = 256
111+
addr.Spec.Prefix = ptr.To(int32(256))
111112
}),
112113
extraObjs: []client.Object{claim},
113114
expectErr: true,
@@ -174,7 +175,7 @@ func TestIPAddressValidateUpdate(t *testing.T) {
174175
ClaimRef: ipamv1.IPAddressClaimReference{},
175176
PoolRef: ipamv1.IPPoolReference{},
176177
Address: "10.0.0.1",
177-
Prefix: 24,
178+
Prefix: ptr.To(int32(24)),
178179
Gateway: "10.0.0.254",
179180
},
180181
}

0 commit comments

Comments
 (0)