-
Notifications
You must be signed in to change notification settings - Fork 3
/
Get-KubectlAll.ps1
68 lines (63 loc) · 2.62 KB
/
Get-KubectlAll.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<#
.Synopsis
Gets all the entities from a Kubernetes namespace; or, alternatively, the set of all non-namespaced items.
.PARAMETER Namespace
The namespace from which entities should be retrieved. Omit this parameter to retrieve non-namespaced items.
.DESCRIPTION
Gets a list of all the API resources available in the Kubernetes cluster that are namespaced (or non-namespaced,
as the case may be.) Once that list has been retrieved, removes the 'events' objects if there are any (these get
too long and numerous to be valuable), then gets everything as requested.
This can be a lot of data, so it make take a few seconds. Stick with it.
.EXAMPLE
Get-KubectlAll
.EXAMPLE
Get-KubectlAll mynamespace
#>
function Get-KubectlAll {
[CmdletBinding()]
[OutputType([String])]
Param
(
[Parameter(ValueFromPipeline = $True, Position = 0)]
[string]$Namespace
)
Begin {
$kubectl = Get-Command kubectl -ErrorAction Ignore
if ($Null -eq $kubectl) {
Write-Error "Unable to locate kubectl."
Exit 1
}
$kubectl = $kubectl.Source
}
Process {
Write-Progress -Activity "Getting resources" -PercentComplete 0
if ([String]::IsNullOrEmpty($Namespace)) {
$namespaced = "--namespaced=false"
}
else {
$namespaced = "--namespaced=true"
}
Write-Progress -Activity "Getting resources..."
Write-Progress -Activity "Getting resources..." -CurrentOperation "Retrieving resource IDs from Kubernetes..." -PercentComplete 5
$allResources = &"$kubectl" api-resources -o name $namespaced
$filteredResources = $allResources | Where-Object {
# No events, too noisy
(-not ($_ -match 'events')) -and
# No external metrics, causes problems with kubectl
(-not ($_ -match '\.external\.metrics\.k8s\.io'))
}
$resourceList = [String]::Join(',', $filteredResources)
Write-Verbose "Resource list: $resourceList"
Write-Progress -Activity "Getting resources..." -CurrentOperation "Retrieving resources from Kubernetes..." -PercentComplete 50
# Redirect the stderr to stdout so everything shows up correctly rather than overlapping.
# The Write-Progress moving the cursor around really messes things up. :(
if ([String]::IsNullOrEmpty($Namespace)) {
$results = (&kubectl get $resourceList 2>&1)
}
else {
$results = (&kubectl get $resourceList -n $Namespace 2>&1)
}
Write-Progress -Activity "Getting resources..." -Completed
$results
}
}