Skip to content

Commit

Permalink
Improve rke2-uninstall.ps1 (#6098)
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <[email protected]>
  • Loading branch information
manuelbuil authored Jun 6, 2024
1 parent 17cc54f commit 9eae919
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions bundle/bin/rke2-uninstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand All @@ -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"
Expand All @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit 9eae919

Please sign in to comment.