Skip to content

Commit

Permalink
Improve rke2-uninstall.ps1
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <mbuil@suse.com>
  • Loading branch information
manuelbuil committed Jun 4, 2024
1 parent 71d00cc commit 5dbe937
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions bundle/bin/rke2-uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,43 @@ 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 240s, 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(240)
$forceRemove = $false
while ($true) {
$namespaces = $(Find-Namespaces)
if ($namespaces) {
foreach ($ns in $namespaces) {
Remove-Namespace $ns
$snapshots = $(Find-Snapshots $ns)
if ($snapshots) {
Write-Output "Tasks, containers, images and snapshots still being deleted. Retrying (timeout 240s)"
Start-Sleep -Seconds 10
} else {
Remove-Namespace $ns
}
}
} else {
Write-Output "All containerd resources have been deleted"
break
}
# if $forceRemove=true, timeout was reached and it was not possible to remove the snapshots
if ($forceRemove) {
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
}
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"
$snapshots = $(Find-Snapshots $ns)
foreach ($snapshot in $snapshots) {
Remove-Snapshot $ns $snapshot
}
# We wait for 20 seconds and we try to remove the namespace for the last time
Start-Sleep -Seconds 20
$forceRemove = $true
Remove-Namespace $ns
}
}
}
Expand All @@ -327,6 +350,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"
Expand All @@ -342,6 +366,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]
Expand All @@ -365,6 +398,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
Expand Down

0 comments on commit 5dbe937

Please sign in to comment.