Skip to content

Adds RKE2ControlPlane labels to Machines #611

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 3 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions controlplane/internal/controllers/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ func (r *RKE2ControlPlaneReconciler) generateMachine(
},
}

for k, v := range rcp.Spec.MachineTemplate.ObjectMeta.Labels {
machine.Labels[k] = v
}

logger.Info("generating machine:", "machine-spec-version", machine.Spec.Version)

// Machine's bootstrap config may be missing RKE2Config if it is not the first machine in the control plane.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ spec:
kubeAPIServer:
extraArgs:
- --anonymous-auth=true
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: "${CLUSTER_NAME}-control-plane"
nodeDrainTimeout: 30s
machineTemplate:
metadata:
labels:
key: value
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: "${CLUSTER_NAME}-control-plane"
nodeDrainTimeout: 30s
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
Expand Down
14 changes: 9 additions & 5 deletions test/e2e/data/infrastructure/cluster-template-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ spec:
kubeAPIServer:
extraArgs:
- --anonymous-auth=true
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: "${CLUSTER_NAME}-control-plane"
nodeDrainTimeout: 30s
machineTemplate:
metadata:
labels:
key: value
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
name: "${CLUSTER_NAME}-control-plane"
nodeDrainTimeout: 30s
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: DockerMachineTemplate
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/e2e_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ var _ = Describe("Workload cluster creation", func() {
WaitForClusterIntervals: e2eConfig.GetIntervals(specName, "wait-cluster"),
WaitForControlPlaneIntervals: e2eConfig.GetIntervals(specName, "wait-control-plane"),
WaitForMachineDeployments: e2eConfig.GetIntervals(specName, "wait-worker-nodes"),
SkipMachineLabelCheck: true,
}, result)

WaitForControlPlaneToBeReady(ctx, WaitForControlPlaneToBeReadyInput{
Expand Down Expand Up @@ -146,6 +147,7 @@ var _ = Describe("Workload cluster creation", func() {
WaitForClusterIntervals: e2eConfig.GetIntervals(specName, "wait-cluster"),
WaitForControlPlaneIntervals: e2eConfig.GetIntervals(specName, "wait-control-plane"),
WaitForMachineDeployments: e2eConfig.GetIntervals(specName, "wait-worker-nodes"),
SkipMachineLabelCheck: true,
}, result)

WaitForControlPlaneToBeReady(ctx, WaitForControlPlaneToBeReadyInput{
Expand All @@ -171,6 +173,7 @@ var _ = Describe("Workload cluster creation", func() {
WaitForClusterIntervals: e2eConfig.GetIntervals(specName, "wait-cluster"),
WaitForControlPlaneIntervals: e2eConfig.GetIntervals(specName, "wait-control-plane"),
WaitForMachineDeployments: e2eConfig.GetIntervals(specName, "wait-worker-nodes"),
SkipMachineLabelCheck: true,
}, result)

WaitForClusterToUpgrade(ctx, WaitForClusterToUpgradeInput{
Expand Down
46 changes: 34 additions & 12 deletions test/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type ApplyClusterTemplateAndWaitInput struct {
Args []string
PreWaitForCluster func()
PostMachinesProvisioned func()
SkipMachineLabelCheck bool
ControlPlaneWaiters
}

Expand All @@ -78,6 +79,7 @@ type ApplyCustomClusterTemplateAndWaitInput struct {
Args []string
PreWaitForCluster func()
PostMachinesProvisioned func()
SkipMachineLabelCheck bool
ControlPlaneWaiters
}

Expand Down Expand Up @@ -152,6 +154,7 @@ func ApplyClusterTemplateAndWait(ctx context.Context, input ApplyClusterTemplate
PreWaitForCluster: input.PreWaitForCluster,
PostMachinesProvisioned: input.PostMachinesProvisioned,
ControlPlaneWaiters: input.ControlPlaneWaiters,
SkipMachineLabelCheck: input.SkipMachineLabelCheck,
}, (*ApplyCustomClusterTemplateAndWaitResult)(result))
}

Expand Down Expand Up @@ -304,9 +307,10 @@ func GetRKE2ControlPlaneByCluster(ctx context.Context, input GetRKE2ControlPlane

// WaitForControlPlaneAndMachinesReadyInput is the input type for WaitForControlPlaneAndMachinesReady.
type WaitForControlPlaneAndMachinesReadyInput struct {
GetLister framework.GetLister
Cluster *clusterv1.Cluster
ControlPlane *controlplanev1.RKE2ControlPlane
GetLister framework.GetLister
Cluster *clusterv1.Cluster
ControlPlane *controlplanev1.RKE2ControlPlane
SkipMachineLabelCheck bool
}

// WaitForControlPlaneAndMachinesReady waits for a RKE2ControlPlane object to be ready (all the machine provisioned and one node ready).
Expand All @@ -319,9 +323,10 @@ func WaitForControlPlaneAndMachinesReady(ctx context.Context, input WaitForContr
if input.ControlPlane.Spec.Replicas != nil && int(*input.ControlPlane.Spec.Replicas) > 1 {
By(fmt.Sprintf("Waiting for the remaining control plane machines managed by %s to be provisioned", klog.KObj(input.ControlPlane)))
WaitForRKE2ControlPlaneMachinesToExist(ctx, WaitForRKE2ControlPlaneMachinesToExistInput{
Lister: input.GetLister,
Cluster: input.Cluster,
ControlPlane: input.ControlPlane,
Lister: input.GetLister,
Cluster: input.Cluster,
ControlPlane: input.ControlPlane,
SkipMachineLabelCheck: input.SkipMachineLabelCheck,
}, intervals...)
}

Expand All @@ -340,9 +345,10 @@ func WaitForControlPlaneAndMachinesReady(ctx context.Context, input WaitForContr

// WaitForRKE2ControlPlaneMachinesToExistInput is the input for WaitForRKE2ControlPlaneMachinesToExist.
type WaitForRKE2ControlPlaneMachinesToExistInput struct {
Lister framework.Lister
Cluster *clusterv1.Cluster
ControlPlane *controlplanev1.RKE2ControlPlane
Lister framework.Lister
Cluster *clusterv1.Cluster
ControlPlane *controlplanev1.RKE2ControlPlane
SkipMachineLabelCheck bool
}

// WaitForRKE2ControlPlaneMachinesToExist will wait until all control plane machines have node refs.
Expand All @@ -369,6 +375,21 @@ func WaitForRKE2ControlPlaneMachinesToExist(ctx context.Context, input WaitForRK
}
return count, nil
}, intervals...).Should(Equal(int(*input.ControlPlane.Spec.Replicas)), "Timed out waiting for %d control plane machines to exist", int(*input.ControlPlane.Spec.Replicas))

if !input.SkipMachineLabelCheck {
// check if machines owned by RKE2ControlPlane have the labels provided in .spec.machineTemplate.metadata.labels
controlPlaneMachineTemplateLabels := input.ControlPlane.Spec.MachineTemplate.ObjectMeta.Labels
machineList := &clusterv1.MachineList{}
err := input.Lister.List(ctx, machineList, inClustersNamespaceListOption, matchClusterListOption)
Expect(err).To(BeNil(), "failed to list the machines")

for _, machine := range machineList.Items {
machineLabels := machine.ObjectMeta.Labels
for k := range controlPlaneMachineTemplateLabels {
Expect(machineLabels[k]).To(Equal(controlPlaneMachineTemplateLabels[k]))
}
}
}
}

// WaitForControlPlaneToBeReadyInput is the input for WaitForControlPlaneToBeReady.
Expand Down Expand Up @@ -503,9 +524,10 @@ func setDefaults(input *ApplyCustomClusterTemplateAndWaitInput) {
if input.WaitForControlPlaneMachinesReady == nil {
input.WaitForControlPlaneMachinesReady = func(ctx context.Context, input ApplyCustomClusterTemplateAndWaitInput, result *ApplyCustomClusterTemplateAndWaitResult) {
WaitForControlPlaneAndMachinesReady(ctx, WaitForControlPlaneAndMachinesReadyInput{
GetLister: input.ClusterProxy.GetClient(),
Cluster: result.Cluster,
ControlPlane: result.ControlPlane,
GetLister: input.ClusterProxy.GetClient(),
Cluster: result.Cluster,
ControlPlane: result.ControlPlane,
SkipMachineLabelCheck: input.SkipMachineLabelCheck,
}, input.WaitForControlPlaneIntervals...)
}
}
Expand Down