Skip to content

Commit e567d21

Browse files
MCO: update custom MCP cleanup flow for MCN tests
1 parent 88edc2f commit e567d21

File tree

2 files changed

+77
-47
lines changed

2 files changed

+77
-47
lines changed

test/extended/machine_config/helpers.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,85 @@ func WaitForMCPToBeReady(oc *exutil.CLI, machineConfigClient *machineconfigclien
548548
}
549549
// Check if the pool is in an updated state with the correct number of ready machines
550550
if IsMachineConfigPoolConditionTrue(mcp.Status.Conditions, mcfgv1.MachineConfigPoolUpdated) && mcp.Status.UpdatedMachineCount == readyMachineCount {
551+
framework.Logf("MCP '%v' has the desired %v ready machines.", poolName, mcp.Status.UpdatedMachineCount)
551552
return true
552553
}
553-
framework.Logf("MCP '%v' has %v ready machines. Waiting for the desired ready machine count of %v.", poolName, mcp.Status.UpdatedMachineCount, readyMachineCount)
554+
// Log details of what is outstanding for the pool to be considered ready
555+
if mcp.Status.UpdatedMachineCount == readyMachineCount {
556+
framework.Logf("MCP '%v' has the desired %v ready machines, but is not in an 'Updated' state.", poolName, mcp.Status.UpdatedMachineCount)
557+
} else {
558+
framework.Logf("MCP '%v' has %v ready machines. Waiting for the desired ready machine count of %v.", poolName, mcp.Status.UpdatedMachineCount, readyMachineCount)
559+
}
554560
return false
555561
}, 5*time.Minute, 10*time.Second).Should(o.BeTrue(), "Timed out waiting for MCP '%v' to be in 'Updated' state with %v ready machines.", poolName, readyMachineCount)
556562
}
557563

564+
// `CleanupCustomMCP` cleans up a custom MCP through the following steps:
565+
// 1. Remove the custom MCP role label from the node
566+
// 2. Wait for the custom MCP to be updated with no ready machines
567+
// 3. Optionally, if a MC has been provided, delete it; if none has been provided, skip to step 4
568+
// 4. Wait for the node to have a current config version equal to the config version of the worker MCP
569+
// 5. Remove the custom MCP
570+
func CleanupCustomMCP(oc *exutil.CLI, clientSet *machineconfigclient.Clientset, customMCPName string, nodeName string, mcName *string) error {
571+
// Unlabel node
572+
framework.Logf("Removing label node-role.kubernetes.io/%v from node %v", customMCPName, nodeName)
573+
unlabelErr := oc.Run("label").Args(fmt.Sprintf("node/%s", nodeName), fmt.Sprintf("node-role.kubernetes.io/%s-", customMCPName)).Execute()
574+
if unlabelErr != nil {
575+
return fmt.Errorf("could not remove label 'node-role.kubernetes.io/%v' from node '%v'; err: %v", customMCPName, nodeName, unlabelErr)
576+
}
577+
578+
// Wait for custom MCP to report no ready nodes
579+
framework.Logf("Waiting for %v MCP to be updated with %v ready machines.", customMCPName, 0)
580+
WaitForMCPToBeReady(oc, clientSet, customMCPName, 0)
581+
582+
// Delete the MC, if one was provided
583+
if mcName != nil {
584+
deleteMCErr := oc.Run("delete").Args("machineconfig", *mcName).Execute()
585+
if deleteMCErr != nil {
586+
return fmt.Errorf("could delete MachineConfig '%v'; err: %v", mcName, deleteMCErr)
587+
588+
}
589+
}
590+
591+
// Wait for node to have a current config version equal to the worker MCP's config version
592+
workerMcp, workerMcpErr := clientSet.MachineconfigurationV1().MachineConfigPools().Get(context.TODO(), worker, metav1.GetOptions{})
593+
if workerMcpErr != nil {
594+
return fmt.Errorf("could not get worker MCP; err: %v", workerMcpErr)
595+
}
596+
workerMcpConfig := workerMcp.Spec.Configuration.Name
597+
framework.Logf("Waiting for %v node to be updated with %v config version.", nodeName, workerMcpConfig)
598+
WaitForNodeCurrentConfig(oc, nodeName, workerMcpConfig)
599+
600+
// Delete custom MCP
601+
framework.Logf("Deleting MCP %v", customMCPName)
602+
deleteMCPErr := oc.Run("delete").Args("mcp", customMCPName).Execute()
603+
if deleteMCPErr != nil {
604+
return fmt.Errorf("error deleting MCP '%v': %v", customMCPName, deleteMCPErr)
605+
}
606+
607+
return nil
608+
}
609+
610+
// `WaitForNodeCurrentConfig` waits up to 5 minutes for a input node to have a current config equal to the `config` parameter
611+
func WaitForNodeCurrentConfig(oc *exutil.CLI, nodeName string, config string) {
612+
o.Eventually(func() bool {
613+
node, nodeErr := oc.AsAdmin().KubeClient().CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
614+
if nodeErr != nil {
615+
framework.Logf("Failed to get node '%v', error :%v", nodeName, nodeErr)
616+
return false
617+
}
618+
619+
// Check if the node's current config matches the input config version
620+
nodeCurrentConfig := node.Annotations[currentConfigAnnotationKey]
621+
if nodeCurrentConfig == config {
622+
framework.Logf("Node '%v' has successfully updated and has a current config version of '%v'.", nodeName, nodeCurrentConfig)
623+
return true
624+
}
625+
framework.Logf("Node '%v' has a current config version of '%v'. Waiting for the node's current config version to be '%v'.", nodeName, nodeCurrentConfig, config)
626+
return false
627+
}, 5*time.Minute, 10*time.Second).Should(o.BeTrue(), "Timed out waiting for node '%v' to have a current config version of '%v'.", nodeName, config)
628+
}
629+
558630
// `GetUpdatingNodeSNO` returns the SNO node when the `master` MCP of the cluster starts updating
559631
func GetUpdatingNodeSNO(oc *exutil.CLI, mcpName string) corev1.Node {
560632
// Wait for the MCP to start updating

test/extended/machine_config/machine_config_node.go

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -137,32 +137,14 @@ func ValidateMCNPropertiesCustomMCP(oc *exutil.CLI, fixture string) {
137137
clientSet, clientErr := machineconfigclient.NewForConfig(oc.KubeFramework().ClientConfig())
138138
o.Expect(clientErr).NotTo(o.HaveOccurred(), "Error creating client set for test.")
139139

140-
// Get starting state of default worker MCP, so we know what the correct number of nodes is during cleanup
141-
workerMcp, err := clientSet.MachineconfigurationV1().MachineConfigPools().Get(context.TODO(), worker, metav1.GetOptions{})
142-
o.Expect(err).NotTo(o.HaveOccurred(), "Could not get worker MCP.")
143-
workerMcpMachines := workerMcp.Status.MachineCount
144-
145140
// Grab a random node from each default pool
146141
workerNode := GetRandomNode(oc, worker)
147142
o.Expect(workerNode.Name).NotTo(o.Equal(""), "Could not get a worker node.")
148143

149144
// Cleanup custom MCP on test completion or failure
150145
defer func() {
151-
// Unlabel node
152-
framework.Logf("Removing label node-role.kubernetes.io/%v from node %v", custom, workerNode.Name)
153-
unlabelErr := oc.Run("label").Args(fmt.Sprintf("node/%s", workerNode.Name), fmt.Sprintf("node-role.kubernetes.io/%s-", custom)).Execute()
154-
o.Expect(unlabelErr).NotTo(o.HaveOccurred(), fmt.Sprintf("Could not remove label 'node-role.kubernetes.io/%s' from node '%v'.", custom, workerNode.Name))
155-
156-
// Wait for infra pool to report no nodes & for worker MCP to be ready
157-
framework.Logf("Waiting for %v MCP to be updated with %v ready machines.", custom, 0)
158-
WaitForMCPToBeReady(oc, clientSet, custom, 0)
159-
framework.Logf("Waiting for %v MCP to be updated with %v ready machines.", worker, workerMcpMachines)
160-
WaitForMCPToBeReady(oc, clientSet, worker, workerMcpMachines)
161-
162-
// Delete custom MCP
163-
framework.Logf("Deleting MCP %v", custom)
164-
deleteMCPErr := oc.Run("delete").Args("mcp", custom).Execute()
165-
o.Expect(deleteMCPErr).NotTo(o.HaveOccurred(), fmt.Sprintf("Error deleting MCP '%v': %v", custom, deleteMCPErr))
146+
cleanupErr := CleanupCustomMCP(oc, clientSet, custom, workerNode.Name, nil)
147+
o.Expect(cleanupErr).NotTo(o.HaveOccurred(), fmt.Sprintf("Failed cleaning up '%v' MCP: %v.", custom, cleanupErr))
166148
}()
167149

168150
// Apply the fixture to create a custom MCP called "infra" & label the worker node accordingly
@@ -197,11 +179,6 @@ func ValidateMCNConditionTransitionsOnRebootlessUpdate(oc *exutil.CLI, nodeDisru
197179
clientSet, clientErr := machineconfigclient.NewForConfig(oc.KubeFramework().ClientConfig())
198180
o.Expect(clientErr).NotTo(o.HaveOccurred(), "Error creating client set for test.")
199181

200-
// Get starting state of default worker MCP, so we know what the correct number of nodes is during cleanup
201-
workerMcp, err := clientSet.MachineconfigurationV1().MachineConfigPools().Get(context.TODO(), worker, metav1.GetOptions{})
202-
o.Expect(err).NotTo(o.HaveOccurred(), "Could not get worker MCP.")
203-
workerMcpMachines := workerMcp.Status.MachineCount
204-
205182
// Grab a random worker node
206183
workerNode := GetRandomNode(oc, worker)
207184
o.Expect(workerNode.Name).NotTo(o.Equal(""), "Could not get a worker node.")
@@ -218,27 +195,8 @@ func ValidateMCNConditionTransitionsOnRebootlessUpdate(oc *exutil.CLI, nodeDisru
218195

219196
// Cleanup custom MCP, and delete MC on test completion or failure
220197
defer func() {
221-
// Unlabel node
222-
framework.Logf("Removing label node-role.kubernetes.io/%v from node %v", custom, workerNode.Name)
223-
unlabelErr := oc.Run("label").Args(fmt.Sprintf("node/%s", workerNode.Name), fmt.Sprintf("node-role.kubernetes.io/%s-", custom)).Execute()
224-
o.Expect(unlabelErr).NotTo(o.HaveOccurred(), fmt.Sprintf("Could not remove label 'node-role.kubernetes.io/%s' from node '%v'.", custom, workerNode.Name))
225-
226-
// Wait for infra MCP to report no ready nodes
227-
framework.Logf("Waiting for %v MCP to be updated with %v ready machines.", custom, 0)
228-
WaitForMCPToBeReady(oc, clientSet, custom, 0)
229-
230-
// Delete applied MC
231-
deleteMCErr := oc.Run("delete").Args("machineconfig", mcName).Execute()
232-
o.Expect(deleteMCErr).NotTo(o.HaveOccurred(), fmt.Sprintf("Could not delete MachineConfig '%v'.", mcName))
233-
234-
// Wait for worker MCP to be ready
235-
framework.Logf("Waiting for %v MCP to be updated with %v ready machines.", worker, workerMcpMachines)
236-
WaitForMCPToBeReady(oc, clientSet, worker, workerMcpMachines)
237-
238-
// Delete custom MCP
239-
framework.Logf("Deleting MCP %v", custom)
240-
deleteMCPErr := oc.Run("delete").Args("mcp", custom).Execute()
241-
o.Expect(deleteMCPErr).NotTo(o.HaveOccurred(), fmt.Sprintf("Error deleting MCP '%v': %v", custom, deleteMCPErr))
198+
cleanupErr := CleanupCustomMCP(oc, clientSet, custom, workerNode.Name, &mcName)
199+
o.Expect(cleanupErr).NotTo(o.HaveOccurred(), fmt.Sprintf("Failed cleaning up '%v' MCP: %v.", custom, cleanupErr))
242200
}()
243201

244202
// Apply the fixture to create a custom MCP called "infra" & label the worker node accordingly

0 commit comments

Comments
 (0)