Last active
September 24, 2024 20:24
-
-
Save rleap-m/e4fc591e05570849308282082682a361 to your computer and use it in GitHub Desktop.
Gets the taints associated with each node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Gets the taints associated with each node | |
.PARAMETER NodeName | |
Specify a single node (if not provided all nodes returned) | |
.PARAMETER Flatten | |
Flattens out the taints such that there is a node object returned per taint | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
[string] | |
$NodeName, | |
[switch] $Flatten | |
) | |
if ($NodeName) { | |
$nodes = @(kubectl get node $NodeName -o json | ConvertFrom-Json -ErrorAction Stop) | |
} | |
else { | |
$nodes = @((kubectl get nodes -o json | ConvertFrom-Json -ErrorAction Stop).items) | |
} | |
foreach ($node in $nodes) { | |
$taints = $node.spec.taints | |
$rawLabels = ($node.metadata.labels | ConvertTo-Json -Compress).Trim('{','}') -split ',' | |
$labels = foreach ($rawLabel in $rawLabels) { | |
$name,$value = $rawLabel.Split(':') | |
[pscustomobject]@{ | |
'LabelName' = $name.Trim("""") | |
'LabelValue' = $value.Trim("""") | |
} | |
} | |
$roles = @($labels.LabelName -match 'node-role.kubernetes.io') | |
$roleNames = @(foreach ($role in $roles) { | |
($role -split '/')[1] | |
}) | |
if ($roleNames.Count -eq 0) { | |
$roleNames = '<none>' | |
} | |
if ($Flatten) { | |
if ($taints) { | |
foreach ($taint in $taints) { | |
[PSCustomObject]@{ | |
NodeName = $node.metadata.name | |
Role = $roleNames | |
OS = $node.status.nodeInfo.operatingSystem | |
TaintKey = $taint.key | |
TaintValue = $taint.value | |
TaintEffect = $taint.effect | |
} | |
} | |
} | |
else { | |
[PSCustomObject]@{ | |
NodeName = $node.metadata.name | |
Role = $roleNames | |
OS = $node.status.nodeInfo.operatingSystem | |
TaintKey = '<none>' | |
TaintValue = '<none>' | |
TaintEffect = '<none>' | |
} | |
} | |
} | |
else { | |
if ($taints) { | |
[PSCustomObject]@{ | |
NodeName = $node.metadata.name | |
Role = $roleNames | |
OS = $node.status.nodeInfo.operatingSystem | |
Taints = $taints | |
} | |
} | |
else { | |
[PSCustomObject]@{ | |
NodeName = $node.metadata.name | |
Role = $roleNames | |
OS = $node.status.nodeInfo.operatingSystem | |
Taints = '<none>' | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment