From 04b8f52c1c2229096726a9bd822b584b09e38ef2 Mon Sep 17 00:00:00 2001 From: Zhaohua Sun Date: Tue, 3 Mar 2026 10:50:08 +0800 Subject: [PATCH 1/2] fix create machine with wrong secret issue --- ...chine_migration_capi_authoritative_test.go | 2 ++ e2e/machine_migration_helpers.go | 35 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/e2e/machine_migration_capi_authoritative_test.go b/e2e/machine_migration_capi_authoritative_test.go index bfd22d139b..bf3afc83c6 100644 --- a/e2e/machine_migration_capi_authoritative_test.go +++ b/e2e/machine_migration_capi_authoritative_test.go @@ -48,6 +48,8 @@ var _ = Describe("[sig-cluster-lifecycle][OCPFeatureGate:MachineAPIMigration] Ma var mapiMachineAuthCAPIName string BeforeAll(func() { + skipIfNoWorkerCAPIMachines() + mapiMachineAuthCAPIName = generateName("machine-auth-capi-") newCapiMachine = createCAPIMachine(ctx, cl, mapiMachineAuthCAPIName) newMapiMachine = createMAPIMachineWithAuthority(ctx, cl, mapiMachineAuthCAPIName, mapiv1beta1.MachineAuthorityClusterAPI) diff --git a/e2e/machine_migration_helpers.go b/e2e/machine_migration_helpers.go index b6fb257499..9f061b162e 100644 --- a/e2e/machine_migration_helpers.go +++ b/e2e/machine_migration_helpers.go @@ -37,24 +37,38 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest/komega" ) -func createCAPIMachine(ctx context.Context, cl client.Client, machineName string) *clusterv1.Machine { - GinkgoHelper() - - Expect(machineName).NotTo(BeEmpty(), "Machine name cannot be empty") - +// getWorkerCAPIMachines returns all worker CAPI machines in the cluster. +func getWorkerCAPIMachines() []*clusterv1.Machine { workerLabelSelector := metav1.LabelSelector{ MatchExpressions: []metav1.LabelSelectorRequirement{ { - Key: clusterv1.MachineControlPlaneLabel, - Operator: metav1.LabelSelectorOpDoesNotExist, + Key: clusterv1.NodeRoleLabelPrefix + "/worker", + Operator: metav1.LabelSelectorOpExists, }, }, } - capiMachineList := capiframework.GetMachines(&workerLabelSelector) - // The test requires at least one existing CAPI machine to act as a reference for creating a new one. - Expect(capiMachineList).NotTo(BeEmpty(), "Should have found CAPI machines in the openshift-cluster-api namespace to use as a reference for creating a new one") + return capiframework.GetMachines(&workerLabelSelector) +} + +// skipIfNoWorkerCAPIMachines skips the test if no worker CAPI machines exist in the cluster. +func skipIfNoWorkerCAPIMachines() { + GinkgoHelper() + + capiMachineList := getWorkerCAPIMachines() + if len(capiMachineList) == 0 { + Skip("No worker CAPI machines found in cluster. This test requires at least one existing worker CAPI machine as a reference.") + } +} + +func createCAPIMachine(ctx context.Context, cl client.Client, machineName string) *clusterv1.Machine { + GinkgoHelper() + + Expect(machineName).NotTo(BeEmpty(), "Machine name cannot be empty") + capiMachineList := getWorkerCAPIMachines() + Expect(capiMachineList).NotTo(BeEmpty(), + "Should have found at least one worker CAPI machine to use as reference for creating new machine") // Select the first machine from the list as our reference. referenceCapiMachine := capiMachineList[0] By(fmt.Sprintf("Using CAPI machine %s as a reference", referenceCapiMachine.Name)) @@ -75,6 +89,7 @@ func createCAPIMachine(ctx context.Context, cl client.Client, machineName string // Clear status and other instance-specific fields that should not be copied. newCapiMachine.Spec.ProviderID = "" newCapiMachine.Spec.InfrastructureRef.Name = machineName + newCapiMachine.Spec.Bootstrap.DataSecretName = ptr.To("worker-user-data") newCapiMachine.ObjectMeta.Labels = nil newCapiMachine.Status = clusterv1.MachineStatus{} From 69fbffb492fb77a72669c9c92c22b4d2d090d441 Mon Sep 17 00:00:00 2001 From: Zhaohua Sun Date: Tue, 3 Mar 2026 13:55:43 +0800 Subject: [PATCH 2/2] remove machineset-related labels from machine spec.metadata --- e2e/machine_migration_helpers.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/e2e/machine_migration_helpers.go b/e2e/machine_migration_helpers.go index 9f061b162e..4573ed13a7 100644 --- a/e2e/machine_migration_helpers.go +++ b/e2e/machine_migration_helpers.go @@ -164,6 +164,12 @@ func createMAPIMachineWithAuthority(ctx context.Context, cl client.Client, machi // Clear status and other instance-specific fields that should not be copied. newMachine.Spec.ProviderID = nil newMachine.ObjectMeta.Labels = nil + // Clear spec.metadata.labels to avoid MachineSet adoption via spec labels + if newMachine.Spec.ObjectMeta.Labels != nil { + // Remove machineset-related labels from spec.metadata + delete(newMachine.Spec.ObjectMeta.Labels, "machine.openshift.io/cluster-api-machineset") + delete(newMachine.Spec.ObjectMeta.Labels, "cluster.x-k8s.io/set-name") + } newMachine.Status = mapiv1beta1.MachineStatus{} newMachine.Spec.AuthoritativeAPI = authority By(fmt.Sprintf("Creating a new MAPI machine with AuthoritativeAPI: %s in namespace: %s", authority, newMachine.Namespace))