Skip to content

⚠️ Drop unnecessary fields from contract-versioned object references #12356

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

Merged
merged 2 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions api/controlplane/kubeadm/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ limitations under the License.
package v1beta1

import (
"errors"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/conversion"

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

var apiVersionGetter = func(_ schema.GroupKind) (string, error) {
return "", errors.New("apiVersionGetter not set")
}

func SetAPIVersionGetter(f func(gk schema.GroupKind) (string, error)) {
apiVersionGetter = f
}

func (src *KubeadmControlPlane) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*controlplanev1.KubeadmControlPlane)
if err := Convert_v1beta1_KubeadmControlPlane_To_v1beta2_KubeadmControlPlane(src, dst, nil); err != nil {
return err
}

infraRef, err := convertToContractVersionedObjectReference(&src.Spec.MachineTemplate.InfrastructureRef)
if err != nil {
return err
}
dst.Spec.MachineTemplate.InfrastructureRef = *infraRef

// Manually restore data.
restored := &controlplanev1.KubeadmControlPlane{}
ok, err := utilconversion.UnmarshalData(src, restored)
Expand All @@ -57,6 +76,12 @@ func (dst *KubeadmControlPlane) ConvertFrom(srcRaw conversion.Hub) error {
return err
}

infraRef, err := convertToObjectReference(&src.Spec.MachineTemplate.InfrastructureRef, src.Namespace)
if err != nil {
return err
}
dst.Spec.MachineTemplate.InfrastructureRef = *infraRef

// Convert timeouts moved from one struct to another.
dst.Spec.KubeadmConfigSpec.ConvertFrom(&src.Spec.KubeadmConfigSpec)

Expand Down Expand Up @@ -270,3 +295,45 @@ func Convert_v1_Condition_To_v1beta1_Condition(in *metav1.Condition, out *cluste
func Convert_v1beta1_Condition_To_v1_Condition(in *clusterv1beta1.Condition, out *metav1.Condition, s apimachineryconversion.Scope) error {
return clusterv1beta1.Convert_v1beta1_Condition_To_v1_Condition(in, out, s)
}

func Convert_v1_ObjectReference_To_v1beta2_ContractVersionedObjectReference(_ *corev1.ObjectReference, _ *clusterv1.ContractVersionedObjectReference, _ apimachineryconversion.Scope) error {
// This is implemented in ConvertTo/ConvertFrom as we have all necessary information available there.
return nil
}

func Convert_v1beta2_ContractVersionedObjectReference_To_v1_ObjectReference(_ *clusterv1.ContractVersionedObjectReference, _ *corev1.ObjectReference, _ apimachineryconversion.Scope) error {
// This is implemented in ConvertTo/ConvertFrom as we have all necessary information available there.
return nil
}

func convertToContractVersionedObjectReference(ref *corev1.ObjectReference) (*clusterv1.ContractVersionedObjectReference, error) {
var apiGroup string
if ref.APIVersion != "" {
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, fmt.Errorf("failed to convert object: failed to parse apiVersion: %v", err)
}
apiGroup = gv.Group
}
return &clusterv1.ContractVersionedObjectReference{
APIGroup: apiGroup,
Kind: ref.Kind,
Name: ref.Name,
}, nil
}

func convertToObjectReference(ref *clusterv1.ContractVersionedObjectReference, namespace string) (*corev1.ObjectReference, error) {
apiVersion, err := apiVersionGetter(schema.GroupKind{
Group: ref.APIGroup,
Kind: ref.Kind,
})
if err != nil {
return nil, fmt.Errorf("failed to convert object: %v", err)
}
return &corev1.ObjectReference{
APIVersion: apiVersion,
Kind: ref.Kind,
Namespace: namespace,
Name: ref.Name,
}, nil
}
61 changes: 61 additions & 0 deletions api/controlplane/kubeadm/v1beta1/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ limitations under the License.
package v1beta1

import (
"fmt"
"reflect"
"testing"
"time"

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

func TestFuzzyConversion(t *testing.T) {
SetAPIVersionGetter(func(gk schema.GroupKind) (string, error) {
for _, gvk := range testGVKs {
if gvk.GroupKind() == gk {
return schema.GroupVersion{
Group: gk.Group,
Version: gvk.Version,
}.String(), nil
}
}
return "", fmt.Errorf("failed to map GroupKind %s to version", gk.String())
})

t.Run("for KubeadmControlPlane", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &controlplanev1.KubeadmControlPlane{},
Spoke: &KubeadmControlPlane{},
Expand All @@ -57,7 +71,9 @@ func TestFuzzyConversion(t *testing.T) {

func KubeadmControlPlaneFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
hubMachineTemplateSpec,
hubKubeadmControlPlaneStatus,
spokeKubeadmControlPlane,
spokeKubeadmControlPlaneStatus,
spokeKubeadmConfigSpec,
spokeClusterConfiguration,
Expand Down Expand Up @@ -116,6 +132,28 @@ func spokeBootstrapTokenString(in *bootstrapv1beta1.BootstrapTokenString, _ rand
in.Secret = fakeSecret
}

func hubMachineTemplateSpec(in *controlplanev1.KubeadmControlPlaneMachineTemplate, c randfill.Continue) {
c.FillNoCustom(in)

// Ensure ref field is always set to realistic values.
gvk := testGVKs[c.Int31n(4)]
in.InfrastructureRef.APIGroup = gvk.Group
in.InfrastructureRef.Kind = gvk.Kind
}

func spokeKubeadmControlPlane(in *KubeadmControlPlane, c randfill.Continue) {
c.FillNoCustom(in)

// Ensure ref fields are always set to realistic values.
gvk := testGVKs[c.Int31n(4)]
in.Spec.MachineTemplate.InfrastructureRef.APIVersion = gvk.GroupVersion().String()
in.Spec.MachineTemplate.InfrastructureRef.Kind = gvk.Kind
in.Spec.MachineTemplate.InfrastructureRef.Namespace = in.Namespace
in.Spec.MachineTemplate.InfrastructureRef.UID = ""
in.Spec.MachineTemplate.InfrastructureRef.ResourceVersion = ""
in.Spec.MachineTemplate.InfrastructureRef.FieldPath = ""
}

func hubKubeadmControlPlaneStatus(in *controlplanev1.KubeadmControlPlaneStatus, c randfill.Continue) {
c.FillNoCustom(in)
// Always create struct with at least one mandatory fields.
Expand Down Expand Up @@ -232,3 +270,26 @@ func spokeBootstrapToken(in *bootstrapv1beta1.BootstrapToken, c randfill.Continu
in.TTL = ptr.To[metav1.Duration](metav1.Duration{Duration: time.Duration(c.Int31()) * time.Second})
}
}

var testGVKs = []schema.GroupVersionKind{
{
Group: "controlplane.cluster.x-k8s.io",
Version: "v1beta4",
Kind: "KubeadmControlPlane",
},
{
Group: "controlplane.cluster.x-k8s.io",
Version: "v1beta7",
Kind: "AWSManagedControlPlane",
},
{
Group: "infrastructure.cluster.x-k8s.io",
Version: "v1beta3",
Kind: "DockerCluster",
},
{
Group: "infrastructure.cluster.x-k8s.io",
Version: "v1beta6",
Kind: "AWSCluster",
},
}
19 changes: 17 additions & 2 deletions api/controlplane/kubeadm/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1beta2

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

Expand Down Expand Up @@ -486,7 +485,7 @@ type KubeadmControlPlaneMachineTemplate struct {
// infrastructureRef is a required reference to a custom resource
// offered by an infrastructure provider.
// +required
InfrastructureRef corev1.ObjectReference `json:"infrastructureRef"`
InfrastructureRef clusterv1.ContractVersionedObjectReference `json:"infrastructureRef"`

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