Skip to content

Commit 0700f07

Browse files
committed
Drop unnecessary fields from contract-versioned object references
1 parent 64864e4 commit 0700f07

File tree

140 files changed

+4317
-3549
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+4317
-3549
lines changed

api/controlplane/kubeadm/v1beta1/conversion.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"errors"
21+
"fmt"
22+
23+
corev1 "k8s.io/api/core/v1"
2024
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2125
apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
2227
"k8s.io/utils/ptr"
2328
"sigs.k8s.io/controller-runtime/pkg/conversion"
2429

@@ -30,12 +35,26 @@ import (
3035
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
3136
)
3237

38+
var apiVersionGetter = func(_ schema.GroupKind) (string, error) {
39+
return "", errors.New("apiVersionGetter not set")
40+
}
41+
42+
func SetAPIVersionGetter(f func(gk schema.GroupKind) (string, error)) {
43+
apiVersionGetter = f
44+
}
45+
3346
func (src *KubeadmControlPlane) ConvertTo(dstRaw conversion.Hub) error {
3447
dst := dstRaw.(*controlplanev1.KubeadmControlPlane)
3548
if err := Convert_v1beta1_KubeadmControlPlane_To_v1beta2_KubeadmControlPlane(src, dst, nil); err != nil {
3649
return err
3750
}
3851

52+
infraRef, err := convertToContractVersionedObjectReference(&src.Spec.MachineTemplate.InfrastructureRef)
53+
if err != nil {
54+
return err
55+
}
56+
dst.Spec.MachineTemplate.InfrastructureRef = *infraRef
57+
3958
// Manually restore data.
4059
restored := &controlplanev1.KubeadmControlPlane{}
4160
ok, err := utilconversion.UnmarshalData(src, restored)
@@ -57,6 +76,12 @@ func (dst *KubeadmControlPlane) ConvertFrom(srcRaw conversion.Hub) error {
5776
return err
5877
}
5978

79+
infraRef, err := convertToObjectReference(&src.Spec.MachineTemplate.InfrastructureRef, src.Namespace)
80+
if err != nil {
81+
return err
82+
}
83+
dst.Spec.MachineTemplate.InfrastructureRef = *infraRef
84+
6085
// Convert timeouts moved from one struct to another.
6186
dst.Spec.KubeadmConfigSpec.ConvertFrom(&src.Spec.KubeadmConfigSpec)
6287

@@ -270,3 +295,45 @@ func Convert_v1_Condition_To_v1beta1_Condition(in *metav1.Condition, out *cluste
270295
func Convert_v1beta1_Condition_To_v1_Condition(in *clusterv1beta1.Condition, out *metav1.Condition, s apimachineryconversion.Scope) error {
271296
return clusterv1beta1.Convert_v1beta1_Condition_To_v1_Condition(in, out, s)
272297
}
298+
299+
func Convert_v1_ObjectReference_To_v1beta2_ContractVersionedObjectReference(_ *corev1.ObjectReference, _ *clusterv1.ContractVersionedObjectReference, _ apimachineryconversion.Scope) error {
300+
// This is implemented in ConvertTo/ConvertFrom as we have all necessary information available there.
301+
return nil
302+
}
303+
304+
func Convert_v1beta2_ContractVersionedObjectReference_To_v1_ObjectReference(_ *clusterv1.ContractVersionedObjectReference, _ *corev1.ObjectReference, _ apimachineryconversion.Scope) error {
305+
// This is implemented in ConvertTo/ConvertFrom as we have all necessary information available there.
306+
return nil
307+
}
308+
309+
func convertToContractVersionedObjectReference(ref *corev1.ObjectReference) (*clusterv1.ContractVersionedObjectReference, error) {
310+
var apiGroup string
311+
if ref.APIVersion != "" {
312+
gv, err := schema.ParseGroupVersion(ref.APIVersion)
313+
if err != nil {
314+
return nil, fmt.Errorf("failed to convert object: failed to parse apiVersion: %v", err)
315+
}
316+
apiGroup = gv.Group
317+
}
318+
return &clusterv1.ContractVersionedObjectReference{
319+
APIGroup: apiGroup,
320+
Kind: ref.Kind,
321+
Name: ref.Name,
322+
}, nil
323+
}
324+
325+
func convertToObjectReference(ref *clusterv1.ContractVersionedObjectReference, namespace string) (*corev1.ObjectReference, error) {
326+
apiVersion, err := apiVersionGetter(schema.GroupKind{
327+
Group: ref.APIGroup,
328+
Kind: ref.Kind,
329+
})
330+
if err != nil {
331+
return nil, fmt.Errorf("failed to convert object: %v", err)
332+
}
333+
return &corev1.ObjectReference{
334+
APIVersion: apiVersion,
335+
Kind: ref.Kind,
336+
Namespace: namespace,
337+
Name: ref.Name,
338+
}, nil
339+
}

api/controlplane/kubeadm/v1beta1/conversion_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ limitations under the License.
1919
package v1beta1
2020

2121
import (
22+
"fmt"
2223
"reflect"
2324
"testing"
2425
"time"
2526

2627
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
2830
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
2931
"k8s.io/utils/ptr"
3032
"sigs.k8s.io/randfill"
@@ -43,6 +45,18 @@ const (
4345
// Test is disabled when the race detector is enabled (via "//go:build !race" above) because otherwise the fuzz tests would just time out.
4446

4547
func TestFuzzyConversion(t *testing.T) {
48+
SetAPIVersionGetter(func(gk schema.GroupKind) (string, error) {
49+
for _, gvk := range testGVKs {
50+
if gvk.GroupKind() == gk {
51+
return schema.GroupVersion{
52+
Group: gk.Group,
53+
Version: gvk.Version,
54+
}.String(), nil
55+
}
56+
}
57+
return "", fmt.Errorf("failed to map GroupKind %s to version", gk.String())
58+
})
59+
4660
t.Run("for KubeadmControlPlane", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
4761
Hub: &controlplanev1.KubeadmControlPlane{},
4862
Spoke: &KubeadmControlPlane{},
@@ -57,7 +71,9 @@ func TestFuzzyConversion(t *testing.T) {
5771

5872
func KubeadmControlPlaneFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
5973
return []interface{}{
74+
hubMachineTemplateSpec,
6075
hubKubeadmControlPlaneStatus,
76+
spokeKubeadmControlPlane,
6177
spokeKubeadmControlPlaneStatus,
6278
spokeKubeadmConfigSpec,
6379
spokeClusterConfiguration,
@@ -116,6 +132,28 @@ func spokeBootstrapTokenString(in *bootstrapv1beta1.BootstrapTokenString, _ rand
116132
in.Secret = fakeSecret
117133
}
118134

135+
func hubMachineTemplateSpec(in *controlplanev1.KubeadmControlPlaneMachineTemplate, c randfill.Continue) {
136+
c.FillNoCustom(in)
137+
138+
// Ensure ref field is always set to realistic values.
139+
gvk := testGVKs[c.Int31n(4)]
140+
in.InfrastructureRef.APIGroup = gvk.Group
141+
in.InfrastructureRef.Kind = gvk.Kind
142+
}
143+
144+
func spokeKubeadmControlPlane(in *KubeadmControlPlane, c randfill.Continue) {
145+
c.FillNoCustom(in)
146+
147+
// Ensure ref fields are always set to realistic values.
148+
gvk := testGVKs[c.Int31n(4)]
149+
in.Spec.MachineTemplate.InfrastructureRef.APIVersion = gvk.GroupVersion().String()
150+
in.Spec.MachineTemplate.InfrastructureRef.Kind = gvk.Kind
151+
in.Spec.MachineTemplate.InfrastructureRef.Namespace = in.Namespace
152+
in.Spec.MachineTemplate.InfrastructureRef.UID = ""
153+
in.Spec.MachineTemplate.InfrastructureRef.ResourceVersion = ""
154+
in.Spec.MachineTemplate.InfrastructureRef.FieldPath = ""
155+
}
156+
119157
func hubKubeadmControlPlaneStatus(in *controlplanev1.KubeadmControlPlaneStatus, c randfill.Continue) {
120158
c.FillNoCustom(in)
121159
// Always create struct with at least one mandatory fields.
@@ -232,3 +270,26 @@ func spokeBootstrapToken(in *bootstrapv1beta1.BootstrapToken, c randfill.Continu
232270
in.TTL = ptr.To[metav1.Duration](metav1.Duration{Duration: time.Duration(c.Int31()) * time.Second})
233271
}
234272
}
273+
274+
var testGVKs = []schema.GroupVersionKind{
275+
{
276+
Group: "controlplane.cluster.x-k8s.io",
277+
Version: "v1beta4",
278+
Kind: "KubeadmControlPlane",
279+
},
280+
{
281+
Group: "controlplane.cluster.x-k8s.io",
282+
Version: "v1beta7",
283+
Kind: "AWSManagedControlPlane",
284+
},
285+
{
286+
Group: "infrastructure.cluster.x-k8s.io",
287+
Version: "v1beta3",
288+
Kind: "DockerCluster",
289+
},
290+
{
291+
Group: "infrastructure.cluster.x-k8s.io",
292+
Version: "v1beta6",
293+
Kind: "AWSCluster",
294+
},
295+
}

api/controlplane/kubeadm/v1beta1/zz_generated.conversion.go

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

api/controlplane/kubeadm/v1beta2/kubeadm_control_plane_types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20-
corev1 "k8s.io/api/core/v1"
2120
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2221
"k8s.io/apimachinery/pkg/util/intstr"
2322

@@ -486,7 +485,7 @@ type KubeadmControlPlaneMachineTemplate struct {
486485
// infrastructureRef is a required reference to a custom resource
487486
// offered by an infrastructure provider.
488487
// +required
489-
InfrastructureRef corev1.ObjectReference `json:"infrastructureRef"`
488+
InfrastructureRef clusterv1.ContractVersionedObjectReference `json:"infrastructureRef"`
490489

491490
// readinessGates specifies additional conditions to include when evaluating Machine Ready condition;
492491
// KubeadmControlPlane will always add readinessGates for the condition it is setting on the Machine:

0 commit comments

Comments
 (0)