Skip to content

Commit

Permalink
Deprecate RKE2ControlPlane.Spec.InfrastructureRef and .NodeDrainTimeout
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Mazzotti <[email protected]>
  • Loading branch information
anmazzotti committed Jan 9, 2025
1 parent 0a3f9be commit 7a7cd4e
Show file tree
Hide file tree
Showing 24 changed files with 179 additions and 105 deletions.
70 changes: 59 additions & 11 deletions controlplane/api/v1alpha1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ func (src *RKE2ControlPlane) ConvertTo(dstRaw conversion.Hub) error {
return err
}

dst.Spec.Version = src.Spec.AgentConfig.Version

// Manually restore data.
restored := &controlplanev1.RKE2ControlPlane{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
}

if restored.Spec.Version != "" {
dst.Spec.Version = restored.Spec.Version
} else if !ok {
// No stored data.
return nil
}

if restored.Spec.AgentConfig.AirGappedChecksum != "" {
Expand All @@ -58,6 +55,8 @@ func (src *RKE2ControlPlane) ConvertTo(dstRaw conversion.Hub) error {
}

dst.Spec.MachineTemplate = restored.Spec.MachineTemplate
dst.Spec.InfrastructureRef = restored.Spec.InfrastructureRef
dst.Spec.NodeDrainTimeout = restored.Spec.NodeDrainTimeout
dst.Status = restored.Status

return nil
Expand All @@ -73,8 +72,6 @@ func (dst *RKE2ControlPlane) ConvertFrom(srcRaw conversion.Hub) error {
return err
}

dst.Spec.AgentConfig.Version = src.Spec.Version

// Preserve Hub data on down-conversion
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
Expand Down Expand Up @@ -123,6 +120,9 @@ func (src *RKE2ControlPlaneTemplate) ConvertTo(dstRaw conversion.Hub) error {
restored := &controlplanev1.RKE2ControlPlaneTemplate{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
} else if !ok {
// No stored data.
return nil
}

if restored.Spec.Template.Spec.AgentConfig.AirGappedChecksum != "" {
Expand Down Expand Up @@ -184,9 +184,57 @@ func (dst *RKE2ControlPlaneTemplateList) ConvertFrom(srcRaw conversion.Hub) erro
}

func Convert_v1beta1_RKE2ControlPlaneSpec_To_v1alpha1_RKE2ControlPlaneSpec(in *controlplanev1.RKE2ControlPlaneSpec, out *RKE2ControlPlaneSpec, s apiconversion.Scope) error {
// Version was added in v1beta1.
// MachineTemplate was added in v1beta1.
return autoConvert_v1beta1_RKE2ControlPlaneSpec_To_v1alpha1_RKE2ControlPlaneSpec(in, out, s)
if err := Convert_v1beta1_RKE2ConfigSpec_To_v1alpha1_RKE2ConfigSpec(&in.RKE2ConfigSpec, &out.RKE2ConfigSpec, s); err != nil {
return err
}
out.Replicas = in.Replicas
out.AgentConfig.Version = in.Version
out.InfrastructureRef = in.MachineTemplate.InfrastructureRef
out.NodeDrainTimeout = in.MachineTemplate.NodeDrainTimeout
if err := Convert_v1beta1_RKE2ServerConfig_To_v1alpha1_RKE2ServerConfig(&in.ServerConfig, &out.ServerConfig, s); err != nil {
return err
}
out.ManifestsConfigMapReference = in.ManifestsConfigMapReference
out.RegistrationMethod = RegistrationMethod(in.RegistrationMethod)
out.RegistrationAddress = in.RegistrationAddress
if in.RolloutStrategy != nil {
out.RolloutStrategy = &RolloutStrategy{
Type: RolloutStrategyType(in.RolloutStrategy.Type),
}
if in.RolloutStrategy.RollingUpdate != nil {
out.RolloutStrategy.RollingUpdate = &RollingUpdate{
MaxSurge: in.RolloutStrategy.RollingUpdate.MaxSurge,
}
}
}
return nil
}

func Convert_v1alpha1_RKE2ControlPlaneSpec_To_v1beta1_RKE2ControlPlaneSpec(in *RKE2ControlPlaneSpec, out *controlplanev1.RKE2ControlPlaneSpec, s apiconversion.Scope) error {
if err := Convert_v1alpha1_RKE2ConfigSpec_To_v1beta1_RKE2ConfigSpec(&in.RKE2ConfigSpec, &out.RKE2ConfigSpec, s); err != nil {
return err
}
out.Replicas = in.Replicas
if err := Convert_v1alpha1_RKE2ServerConfig_To_v1beta1_RKE2ServerConfig(&in.ServerConfig, &out.ServerConfig, s); err != nil {
return err
}
out.ManifestsConfigMapReference = in.ManifestsConfigMapReference
out.MachineTemplate.InfrastructureRef = in.InfrastructureRef
out.MachineTemplate.NodeDrainTimeout = in.NodeDrainTimeout
out.Version = in.AgentConfig.Version
out.RegistrationMethod = controlplanev1.RegistrationMethod(in.RegistrationMethod)
out.RegistrationAddress = in.RegistrationAddress
if in.RolloutStrategy != nil {
out.RolloutStrategy = &controlplanev1.RolloutStrategy{
Type: controlplanev1.RolloutStrategyType(in.RolloutStrategy.Type),
}
if in.RolloutStrategy.RollingUpdate != nil {
out.RolloutStrategy.RollingUpdate = &controlplanev1.RollingUpdate{
MaxSurge: in.RolloutStrategy.RollingUpdate.MaxSurge,
}
}
}
return nil
}

func Convert_v1beta1_RKE2ControlPlaneStatus_To_v1alpha1_RKE2ControlPlaneStatus(in *controlplanev1.RKE2ControlPlaneStatus, out *RKE2ControlPlaneStatus, s apiconversion.Scope) error {
Expand Down
15 changes: 5 additions & 10 deletions controlplane/api/v1alpha1/zz_generated.conversion.go

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

4 changes: 3 additions & 1 deletion controlplane/api/v1beta1/rke2controlplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type RKE2ControlPlaneSpec struct {

// MachineTemplate contains information about how machines
// should be shaped when creating or updating a control plane.
// +optional
MachineTemplate RKE2ControlPlaneMachineTemplate `json:"machineTemplate,omitempty"`

// ServerConfig specifies configuration for the agent nodes.
Expand All @@ -72,11 +71,14 @@ type RKE2ControlPlaneSpec struct {

// InfrastructureRef is a required reference to a custom resource
// offered by an infrastructure provider.
// This field is deprecated. Use `.machineTemplate.infrastructureRef` instead.
//+optional
InfrastructureRef corev1.ObjectReference `json:"infrastructureRef"`

// NodeDrainTimeout is the total amount of time that the controller will spend on draining a controlplane node
// The default value is 0, meaning that the node can be drained without any time limitations.
// NOTE: NodeDrainTimeout is different from `kubectl drain --timeout`
// This field is deprecated. Use `.machineTemplate.nodeDrainTimeout` instead.
// +optional
NodeDrainTimeout *metav1.Duration `json:"nodeDrainTimeout,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,7 @@ spec:
description: |-
InfrastructureRef is a required reference to a custom resource
offered by an infrastructure provider.
This field is deprecated. Use `.machineTemplate.infrastructureRef` instead.
properties:
apiVersion:
description: API version of the referent.
Expand Down Expand Up @@ -1769,6 +1770,7 @@ spec:
NodeDrainTimeout is the total amount of time that the controller will spend on draining a controlplane node
The default value is 0, meaning that the node can be drained without any time limitations.
NOTE: NodeDrainTimeout is different from `kubectl drain --timeout`
This field is deprecated. Use `.machineTemplate.nodeDrainTimeout` instead.
type: string
postRKE2Commands:
description: PostRKE2Commands specifies extra commands to run after
Expand Down Expand Up @@ -2436,7 +2438,6 @@ spec:
pattern: (v\d\.\d{2}\.\d+\+rke2r\d)|^$
type: string
required:
- infrastructureRef
- rolloutStrategy
type: object
status:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ spec:
description: |-
InfrastructureRef is a required reference to a custom resource
offered by an infrastructure provider.
This field is deprecated. Use `.machineTemplate.infrastructureRef` instead.
properties:
apiVersion:
description: API version of the referent.
Expand Down Expand Up @@ -610,6 +611,7 @@ spec:
NodeDrainTimeout is the total amount of time that the controller will spend on draining a controlplane node
The default value is 0, meaning that the node can be drained without any time limitations.
NOTE: NodeDrainTimeout is different from `kubectl drain --timeout`
This field is deprecated. Use `.machineTemplate.nodeDrainTimeout` instead.
type: string
postRKE2Commands:
description: PostRKE2Commands specifies extra commands to
Expand Down Expand Up @@ -1296,7 +1298,6 @@ spec:
pattern: (v\d\.\d{2}\.\d+\+rke2r\d)|^$
type: string
required:
- infrastructureRef
- rolloutStrategy
type: object
required:
Expand Down
11 changes: 11 additions & 0 deletions controlplane/internal/controllers/rke2controlplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,17 @@ func (r *RKE2ControlPlaneReconciler) reconcileNormal(
logger := log.FromContext(ctx)
logger.Info("Reconcile RKE2 Control Plane")

// (Quirk)
// Tries to reconcile the v1beta1 `machineTemplate` usage for all those resources that were not updated due to missing conversion logic.
if len(rcp.Spec.MachineTemplate.InfrastructureRef.Name) == 0 && len(rcp.Spec.InfrastructureRef.Name) > 0 {
rcp.Spec.MachineTemplate.InfrastructureRef = rcp.Spec.InfrastructureRef
}

if rcp.Spec.MachineTemplate.NodeDrainTimeout == nil {
rcp.Spec.MachineTemplate.NodeDrainTimeout = rcp.Spec.NodeDrainTimeout
}
// (Quirk End)

// Wait for the cluster infrastructure to be ready before creating machines
if !cluster.Status.InfrastructureReady {
logger.Info("Cluster infrastructure is not ready yet")
Expand Down
6 changes: 3 additions & 3 deletions controlplane/internal/controllers/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ func (r *RKE2ControlPlaneReconciler) cloneConfigsAndGenerateMachine(
UID: rcp.UID,
}

rcp.Spec.InfrastructureRef.Namespace = cmp.Or(rcp.Spec.InfrastructureRef.Namespace, rcp.Namespace)
rcp.Spec.MachineTemplate.InfrastructureRef.Namespace = cmp.Or(rcp.Spec.MachineTemplate.InfrastructureRef.Namespace, rcp.Namespace)

// Clone the infrastructure template
infraRef, err := external.CreateFromTemplate(ctx, &external.CreateFromTemplateInput{
Client: r.Client,
TemplateRef: &rcp.Spec.InfrastructureRef,
TemplateRef: &rcp.Spec.MachineTemplate.InfrastructureRef,
Namespace: rcp.Namespace,
OwnerRef: infraCloneOwner,
ClusterName: cluster.Name,
Expand Down Expand Up @@ -460,7 +460,7 @@ func (r *RKE2ControlPlaneReconciler) generateMachine(
ConfigRef: bootstrapRef,
},
FailureDomain: failureDomain,
NodeDrainTimeout: rcp.Spec.NodeDrainTimeout,
NodeDrainTimeout: rcp.Spec.MachineTemplate.NodeDrainTimeout,
},
}

Expand Down
11 changes: 6 additions & 5 deletions examples/aws/cluster-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ spec:
- sudo hostnamectl set-hostname $(curl -s http://169.254.169.254/1.0/meta-data/hostname)
agentConfig:
airGapped: true
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSMachineTemplate
name: ${CLUSTER_NAME}-control-plane
nodeDrainTimeout: 2m
machineTemplate:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSMachineTemplate
name: ${CLUSTER_NAME}-control-plane
nodeDrainTimeout: 2m
replicas: ${CONTROL_PLANE_MACHINE_COUNT}
serverConfig:
cloudProviderName: external
Expand Down
11 changes: 6 additions & 5 deletions examples/docker/air-gapped/rke2-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ spec:
kubeProxy:
extraEnv:
hello: world
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
machineTemplate:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
Expand Down
11 changes: 6 additions & 5 deletions examples/docker/cis-profile/rke2-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ spec:
- --anonymous-auth=true
agentConfig:
cisProfile: ${CIS_PROFILE}
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
machineTemplate:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
Expand Down
7 changes: 2 additions & 5 deletions examples/docker/clusterclass/clusterclass-quick-start.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,15 @@ metadata:
spec:
template:
spec:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: rke2-class-control-plane
serverConfig:
cni: calico
kubeAPIServer:
extraArgs:
- --anonymous-auth=true
disableComponents:
kubernetesComponents: [ "cloudController"]
nodeDrainTimeout: 2m
machineTemplate:
nodeDrainTimeout: 2m
rolloutStrategy:
type: "RollingUpdate"
rollingUpdate:
Expand Down
11 changes: 6 additions & 5 deletions examples/docker/disable-components/rke2-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@ spec:
replicas: ${CABPR_CP_REPLICAS}
agentConfig:
version: ${KUBERNETES_VERSION}+rke2r1
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
serverConfig:
kubeAPIServer:
extraArgs:
- --anonymous-auth=true
disableComponents:
pluginComponents:
- "rke2-ingress-nginx"
nodeDrainTimeout: 2m
machineTemplate:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
Expand Down
11 changes: 6 additions & 5 deletions examples/docker/enable-multus/rke2-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ spec:
kubeAPIServer:
extraArgs:
- --anonymous-auth=true
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
machineTemplate:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
Expand Down
11 changes: 6 additions & 5 deletions examples/docker/enable-multus/rke2controlplane-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ spec:
version: v1.24.11+rke2r1
serverConfig:
cniMultusEnable: true
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
machineTemplate:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: controlplane
nodeDrainTimeout: 2m
Loading

0 comments on commit 7a7cd4e

Please sign in to comment.