diff --git a/controlplane/internal/controllers/scale.go b/controlplane/internal/controllers/scale.go index e8516191..3710a8f0 100644 --- a/controlplane/internal/controllers/scale.go +++ b/controlplane/internal/controllers/scale.go @@ -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. diff --git a/test/e2e/data/infrastructure/cluster-template-docker-updated.yaml b/test/e2e/data/infrastructure/cluster-template-docker-updated.yaml index 38d9325d..90b12866 100644 --- a/test/e2e/data/infrastructure/cluster-template-docker-updated.yaml +++ b/test/e2e/data/infrastructure/cluster-template-docker-updated.yaml @@ -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 diff --git a/test/e2e/data/infrastructure/cluster-template-docker.yaml b/test/e2e/data/infrastructure/cluster-template-docker.yaml index 0110ad07..58d37ef3 100644 --- a/test/e2e/data/infrastructure/cluster-template-docker.yaml +++ b/test/e2e/data/infrastructure/cluster-template-docker.yaml @@ -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 diff --git a/test/e2e/e2e_upgrade_test.go b/test/e2e/e2e_upgrade_test.go index 7ad01872..db954a29 100644 --- a/test/e2e/e2e_upgrade_test.go +++ b/test/e2e/e2e_upgrade_test.go @@ -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{ @@ -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{ @@ -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{ diff --git a/test/e2e/helpers.go b/test/e2e/helpers.go index 588df9ea..23a57f3a 100644 --- a/test/e2e/helpers.go +++ b/test/e2e/helpers.go @@ -62,6 +62,7 @@ type ApplyClusterTemplateAndWaitInput struct { Args []string PreWaitForCluster func() PostMachinesProvisioned func() + SkipMachineLabelCheck bool ControlPlaneWaiters } @@ -78,6 +79,7 @@ type ApplyCustomClusterTemplateAndWaitInput struct { Args []string PreWaitForCluster func() PostMachinesProvisioned func() + SkipMachineLabelCheck bool ControlPlaneWaiters } @@ -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)) } @@ -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). @@ -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...) } @@ -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. @@ -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. @@ -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...) } }