From 04dff5a7b59240928672110d553a868a9263712c Mon Sep 17 00:00:00 2001 From: Manuel Buil Date: Tue, 4 Jun 2024 12:08:50 +0200 Subject: [PATCH] Improve rke2-uninstall.ps1 Signed-off-by: Manuel Buil --- bundle/bin/rke2-uninstall.ps1 | 57 ++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/bundle/bin/rke2-uninstall.ps1 b/bundle/bin/rke2-uninstall.ps1 index 4134e42871..7a7d9068b7 100644 --- a/bundle/bin/rke2-uninstall.ps1 +++ b/bundle/bin/rke2-uninstall.ps1 @@ -301,20 +301,45 @@ function Remove-Containerd () { } } - # Some resources in the namespace take a while to disappear. Try several times to remove the namespace and give up after 30s - $endTime = (Get-Date).AddSeconds(30) - while ((Get-Date) -lt $endTime) { + # Resources in the namespace take a while to disappear. Snapshots are always the last to go. + # For 180s, snapshots are checked and namespace won't be removed until snapshots are gone. + # If timeout is reached, we will try removing snapshots directly. If that does not work, we stop trying and user is warned + $endTime = (Get-Date).AddSeconds(180) + Write-Output "Tasks, containers, images and snapshots are being deleted. This may take a while (timeout 180s)" + while ($true) { $namespaces = $(Find-Namespaces) - if ($namespaces) { + # If there are still namespaces and timeout was not reached + if ($namespaces -and (Get-Date) -lt $endTime) { foreach ($ns in $namespaces) { - Remove-Namespace $ns + $snapshots = $(Find-Snapshots $ns) + if ($snapshots) { + Write-Output "Snapshots still present. Retrying namespace deletion in 10s..." + Start-Sleep -Seconds 10 + } else { + Remove-Namespace $ns + } } - } else { + # if there are still namespaces and timeout was reached + } elseif ($namespaces -and (Get-Date) -ge $endTime) { + Write-Output "Warning! Not all resources in containerd namespace $ns were able to be removed. " ` + "The uninstallation script might not be able to remove all files under /var/lib/rancher/rke2" + break + # if there are no namespaces + } elseif (-not $namespaces) { + Write-Output "All containerd resources have been deleted" break } - Start-Sleep -Seconds 5 if ((Get-Date) -ge $endTime) { - Write-Output "Unable to remove all namespaces" + Write-Output "Time out waiting for containerd resources to be removed. Trying to remove snapshots directly" + foreach ($ns in $namespaces) { + $snapshots = $(Find-Snapshots $ns) + foreach ($snapshot in $snapshots) { + Remove-Snapshot $ns $snapshot + } + # We wait for 20 seconds, try to remove the namespaces again and iterate one last time + Start-Sleep -Seconds 20 + Remove-Namespace $ns + } } } } @@ -327,6 +352,7 @@ function Remove-Containerd () { function Find-Namespaces () { Invoke-Ctr -cmd "namespace list -q" } + function Find-ContainersInNamespace() { $namespace = $args[0] Invoke-Ctr -cmd "-n $namespace container list -q" @@ -342,6 +368,15 @@ function Find-Images() { Invoke-Ctr -cmd "-n $namespace image list -q" } +# The snapshots list does not have option "-q" to list only the keys +function Find-Snapshots() { + $namespace = $args[0] + # We skip the first line which is the header KEY + Invoke-Ctr -cmd "-n $namespace snapshot list" | Select-Object -Skip 1 | ForEach-Object { + ($_ -split '\s+')[0] + } +} + function Remove-Image() { $namespace = $args[0] $image = $args[1] @@ -365,6 +400,12 @@ function Remove-Namespace() { Invoke-Ctr -cmd "namespace remove $namespace" } +function Remove-Snapshot() { + $namespace = $args[0] + $snapshot = $args[1] + Invoke-Ctr -cmd "-n $namespace snapshot delete $snapshot" +} + function Create-Lockfile() { # We fetch ctr.exe path and place the lock there $command = Get-Command ctr -ErrorAction SilentlyContinue