Skip to content

Commit 5d9e777

Browse files
authored
⚠️ Promote v1beta2 condition in CAPD (#12362)
* move v1beta2 conditions top level, v1beta1 conditions to deprecated Signed-off-by: sivchari <[email protected]> * fix convertion between docker cluster and dev cluster Signed-off-by: sivchari <[email protected]> * use randfill Signed-off-by: sivchari <[email protected]> * implement conversion diff for capd Signed-off-by: sivchari <[email protected]> * implement conversion diff for exp capd Signed-off-by: sivchari <[email protected]> * fix conversion test Signed-off-by: sivchari <[email protected]> * fix: build failure Signed-off-by: sivchari <[email protected]> * re-generate config Signed-off-by: sivchari <[email protected]> * fix review findings Signed-off-by: sivchari <[email protected]> * re-generate Signed-off-by: sivchari <[email protected]> * fix review findings Signed-off-by: sivchari <[email protected]> * make conditions consistent Signed-off-by: sivchari <[email protected]> --------- Signed-off-by: sivchari <[email protected]>
1 parent c28b413 commit 5d9e777

34 files changed

+2134
-902
lines changed

test/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ require (
3535
sigs.k8s.io/cluster-api v0.0.0-00010101000000-000000000000
3636
sigs.k8s.io/controller-runtime v0.21.0
3737
sigs.k8s.io/kind v0.29.0
38+
sigs.k8s.io/randfill v1.0.0
3839
sigs.k8s.io/yaml v1.4.0
3940
)
4041

@@ -164,6 +165,5 @@ require (
164165
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
165166
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.2 // indirect
166167
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
167-
sigs.k8s.io/randfill v1.0.0 // indirect
168168
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
169169
)

test/infrastructure/docker/api/v1alpha3/conversion.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021
apiconversion "k8s.io/apimachinery/pkg/conversion"
2122
"sigs.k8s.io/controller-runtime/pkg/conversion"
2223

24+
clusterv1alpha3 "sigs.k8s.io/cluster-api/internal/api/core/v1alpha3"
2325
infrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta2"
2426
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2527
)
@@ -31,6 +33,15 @@ func (src *DockerCluster) ConvertTo(dstRaw conversion.Hub) error {
3133
return err
3234
}
3335

36+
// Reset conditions from autogenerated conversions
37+
// NOTE: v1alpha3 conditions should not be automatically be converted into v1beta2 conditions.
38+
dst.Status.Conditions = nil
39+
if src.Status.Conditions != nil {
40+
dst.Status.Deprecated = &infrav1.DockerClusterDeprecatedStatus{}
41+
dst.Status.Deprecated.V1Beta1 = &infrav1.DockerClusterV1Beta1DeprecatedStatus{}
42+
clusterv1alpha3.Convert_v1alpha3_Conditions_To_v1beta2_Deprecated_V1Beta1_Conditions(&src.Status.Conditions, &dst.Status.Deprecated.V1Beta1.Conditions)
43+
}
44+
3445
// Manually restore data.
3546
restored := &infrav1.DockerCluster{}
3647
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
@@ -48,7 +59,8 @@ func (src *DockerCluster) ConvertTo(dstRaw conversion.Hub) error {
4859
if restored.Spec.LoadBalancer.CustomHAProxyConfigTemplateRef != nil {
4960
dst.Spec.LoadBalancer.CustomHAProxyConfigTemplateRef = restored.Spec.LoadBalancer.CustomHAProxyConfigTemplateRef
5061
}
51-
dst.Status.V1Beta2 = restored.Status.V1Beta2
62+
63+
dst.Status.Conditions = restored.Status.Conditions
5264

5365
return nil
5466
}
@@ -60,6 +72,13 @@ func (dst *DockerCluster) ConvertFrom(srcRaw conversion.Hub) error {
6072
return err
6173
}
6274

75+
// Reset conditions from autogenerated conversions
76+
// NOTE: v1beta2 conditions should not be automatically be converted into v1alpha3 conditions.
77+
dst.Status.Conditions = nil
78+
if src.Status.Deprecated != nil && src.Status.Deprecated.V1Beta1 != nil && src.Status.Deprecated.V1Beta1.Conditions != nil {
79+
clusterv1alpha3.Convert_v1beta2_Deprecated_V1Beta1_Conditions_To_v1alpha3_Conditions(&src.Status.Deprecated.V1Beta1.Conditions, &dst.Status.Conditions)
80+
}
81+
6382
// Preserve Hub data on down-conversion except for metadata
6483
if err := utilconversion.MarshalData(src, dst); err != nil {
6584
return err
@@ -75,6 +94,16 @@ func (src *DockerMachine) ConvertTo(dstRaw conversion.Hub) error {
7594
return err
7695
}
7796

97+
// Reset conditions from autogenerated conversions
98+
// NOTE: v1alpha3 conditions should not be automatically be converted into v1beta2 conditions.
99+
dst.Status.Conditions = nil
100+
101+
if src.Status.Conditions != nil {
102+
dst.Status.Deprecated = &infrav1.DockerMachineDeprecatedStatus{}
103+
dst.Status.Deprecated.V1Beta1 = &infrav1.DockerMachineV1Beta1DeprecatedStatus{}
104+
clusterv1alpha3.Convert_v1alpha3_Conditions_To_v1beta2_Deprecated_V1Beta1_Conditions(&src.Status.Conditions, &dst.Status.Deprecated.V1Beta1.Conditions)
105+
}
106+
78107
// Manually restore data.
79108
restored := &infrav1.DockerMachine{}
80109
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
@@ -84,7 +113,8 @@ func (src *DockerMachine) ConvertTo(dstRaw conversion.Hub) error {
84113
if restored.Spec.BootstrapTimeout != nil {
85114
dst.Spec.BootstrapTimeout = restored.Spec.BootstrapTimeout
86115
}
87-
dst.Status.V1Beta2 = restored.Status.V1Beta2
116+
117+
dst.Status.Conditions = restored.Status.Conditions
88118

89119
return nil
90120
}
@@ -96,6 +126,13 @@ func (dst *DockerMachine) ConvertFrom(srcRaw conversion.Hub) error {
96126
return err
97127
}
98128

129+
// Reset conditions from autogenerated conversions
130+
// NOTE: v1beta2 conditions should not be automatically be converted into v1alpha3 conditions.
131+
dst.Status.Conditions = nil
132+
if src.Status.Deprecated != nil && src.Status.Deprecated.V1Beta1 != nil && src.Status.Deprecated.V1Beta1.Conditions != nil {
133+
clusterv1alpha3.Convert_v1beta2_Deprecated_V1Beta1_Conditions_To_v1alpha3_Conditions(&src.Status.Deprecated.V1Beta1.Conditions, &dst.Status.Conditions)
134+
}
135+
99136
if err := utilconversion.MarshalData(src, dst); err != nil {
100137
return err
101138
}
@@ -161,3 +198,13 @@ func Convert_v1beta2_DockerClusterStatus_To_v1alpha3_DockerClusterStatus(in *inf
161198
func Convert_v1beta2_DockerMachineStatus_To_v1alpha3_DockerMachineStatus(in *infrav1.DockerMachineStatus, out *DockerMachineStatus, s apiconversion.Scope) error {
162199
return autoConvert_v1beta2_DockerMachineStatus_To_v1alpha3_DockerMachineStatus(in, out, s)
163200
}
201+
202+
// 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)
203+
204+
func Convert_v1alpha3_Condition_To_v1_Condition(in *clusterv1alpha3.Condition, out *metav1.Condition, s apiconversion.Scope) error {
205+
return clusterv1alpha3.Convert_v1alpha3_Condition_To_v1_Condition(in, out, s)
206+
}
207+
208+
func Convert_v1_Condition_To_v1alpha3_Condition(in *metav1.Condition, out *clusterv1alpha3.Condition, s apiconversion.Scope) error {
209+
return clusterv1alpha3.Convert_v1_Condition_To_v1alpha3_Condition(in, out, s)
210+
}

test/infrastructure/docker/api/v1alpha3/conversion_test.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ limitations under the License.
1919
package v1alpha3
2020

2121
import (
22+
"reflect"
2223
"testing"
2324

25+
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
26+
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
27+
"sigs.k8s.io/randfill"
28+
2429
infrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta2"
2530
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2631
)
@@ -29,17 +34,51 @@ import (
2934

3035
func TestFuzzyConversion(t *testing.T) {
3136
t.Run("for DockerCluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
32-
Hub: &infrav1.DockerCluster{},
33-
Spoke: &DockerCluster{},
37+
Hub: &infrav1.DockerCluster{},
38+
Spoke: &DockerCluster{},
39+
FuzzerFuncs: []fuzzer.FuzzerFuncs{DockerClusterFuzzFunc},
3440
}))
3541

3642
t.Run("for DockerMachine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
37-
Hub: &infrav1.DockerMachine{},
38-
Spoke: &DockerMachine{},
43+
Hub: &infrav1.DockerMachine{},
44+
Spoke: &DockerMachine{},
45+
FuzzerFuncs: []fuzzer.FuzzerFuncs{DockerMachineFuzzFunc},
3946
}))
4047

4148
t.Run("for DockerMachineTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
4249
Hub: &infrav1.DockerMachineTemplate{},
4350
Spoke: &DockerMachineTemplate{},
4451
}))
4552
}
53+
54+
func DockerClusterFuzzFunc(_ runtimeserializer.CodecFactory) []any {
55+
return []any{
56+
hubDockerClusterStatus,
57+
}
58+
}
59+
60+
func hubDockerClusterStatus(in *infrav1.DockerClusterStatus, c randfill.Continue) {
61+
c.FillNoCustom(in)
62+
63+
if in.Deprecated != nil {
64+
if in.Deprecated.V1Beta1 == nil || reflect.DeepEqual(in.Deprecated.V1Beta1, &infrav1.DockerClusterV1Beta1DeprecatedStatus{}) {
65+
in.Deprecated = nil
66+
}
67+
}
68+
}
69+
70+
func DockerMachineFuzzFunc(_ runtimeserializer.CodecFactory) []any {
71+
return []any{
72+
hubDockerMachineStatus,
73+
}
74+
}
75+
76+
func hubDockerMachineStatus(in *infrav1.DockerMachineStatus, c randfill.Continue) {
77+
c.FillNoCustom(in)
78+
79+
if in.Deprecated != nil {
80+
if in.Deprecated.V1Beta1 == nil || reflect.DeepEqual(in.Deprecated.V1Beta1, &infrav1.DockerMachineV1Beta1DeprecatedStatus{}) {
81+
in.Deprecated = nil
82+
}
83+
}
84+
}

test/infrastructure/docker/api/v1alpha3/zz_generated.conversion.go

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

0 commit comments

Comments
 (0)