-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSKubeContext.psm1
178 lines (163 loc) · 5.71 KB
/
PSKubeContext.psm1
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
Function Select-KubeNamespace {
[CmdletBinding()]
param (
[parameter(Mandatory = $False, Position = 0, ValueFromRemainingArguments = $True)]
[Object[]] $Arguments
)
begin {
if ($Arguments.Length -ge 1) {
$ns = & kubectl get namespace -o=name $Arguments[0] 2>&1
$ns = $ns -replace '^namespace/'
if ($ns -ne $Arguments[0]) {
$allNamespaces = kubectl get namespace -o=name;
$allNamespaces = $allNamespaces | ForEach-Object {$_ -replace '^namespace/'};
$filteredNamespaces = $allNamespaces | Where-Object {$_.StartsWith($Arguments[0])}
if($null -eq $filteredNamespaces){
$filteredNamespaces = $allNamespaces;
}
$ns = Select-MenuOption -MenuOptions $filteredNamespaces;
}
}
else {
$allNamespaces = kubectl get namespace -o=name;
$allNamespaces = $allNamespaces | ForEach-Object {$_ -replace '^namespace/'};
$ns = Select-MenuOption -MenuOptions $allNamespaces;
}
}
process {
if ($ns -ne '') {
$ns = $ns -replace '^namespace/'
Write-Host "Set namespace to $ns";
& kubectl config set-context --current --namespace=$ns
}
}
}
#kubectx
Function Select-KubeContext {
[CmdletBinding()]
param (
[parameter(Mandatory = $False, Position = 0, ValueFromRemainingArguments = $True)]
[Object[]] $Arguments
)
begin {
if ($Arguments.Length -ge 1) {
$ctx = & kubectl config get-contexts -o=name $Arguments[0] 2>&1
if ($ctx -ne $Arguments[0]) {
$allCtx = & kubectl config get-contexts -o=name
$filteredCtx = $allCtx | Where-Object {$_.StartsWith($Arguments[0])}
if($null -eq $filteredCtx){
$filteredCtx = $allCtx;
}
$ctx = Select-MenuOption -MenuOptions $filteredCtx;
}
}
else {
$allCtx = & kubectl config get-contexts -o=name
$ctx = Select-MenuOption -MenuOptions $allCtx.Where({ $_ -ne '' });
}
}
process {
if ($ctx -ne '') {
& kubectl config use-context $ctx
}
}
}
Function Register-PSKubeContextComplete {
param (
[parameter(Mandatory = $False, Position = 0)]
[switch] $DisableForKubeNS,
[parameter(Mandatory = $False, Position = 1)]
[switch] $DisableForKubeCtx
)
if (!$DisableForKubeCtx) {
$rootCommands = @('Select-KubeContext');
$aliases = (get-alias).Where( { $_.Definition -in $rootCommands }).Name;
if ($aliases) {
$rootCommands += $aliases
}
Register-ArgumentCompleter -Native -CommandName $rootCommands -ScriptBlock {
param($wordToComplete, $fullCommand, $cursorPosition)
kubectl config get-contexts -o=name | ForEach-Object {
$ctx = $_;
if ($ctx.StartsWith($wordToComplete)) {
[System.Management.Automation.CompletionResult]::new($ctx, $ctx, 'ParameterValue', $ctx)
}
else {
# Write-Host "$ns doesnt startswith $wordToComplete"
}
}
}
} else {
Write-Host "Autocomplete for Select-KubeContext (kubectx) will be disabled"
}
if (!$DisableForKubeNS) {
$rootCommands = @('Select-KubeNamespace');
$aliases = (get-alias).Where( { $_.Definition -in $rootCommands }).Name;
if ($aliases) {
$rootCommands += $aliases
}
Register-ArgumentCompleter -Native -CommandName $rootCommands -ScriptBlock {
param($wordToComplete, $fullCommand, $cursorPosition)
kubectl get namespace -o=name | ForEach-Object {
$ns = $_ -replace '^namespace/'
if ($ns.StartsWith($wordToComplete)) {
[System.Management.Automation.CompletionResult]::new($ns, $ns, 'ParameterValue', $ns)
}
else {
# Write-Host "$ns doesnt startswith $wordToComplete"
}
}
}
} else {
Write-Host "Autocomplete for Select-KubeContext (kubectx) will be disabled"
}
}
# menu helper
Function Select-MenuOption (){
Param(
[Parameter(Mandatory=$True)]
[string[]]$MenuOptions
)
$Count = $MenuOptions.count;
$Selection = 0
$EnterPressed = $False
Clear-Host
While($EnterPressed -eq $False){
For ($i=0; $i -lt $Count; $i++){
$back = $Host.UI.RawUI.BackgroundColor;
$front = $Host.UI.RawUI.ForegroundColor;
If ($i -eq $Selection){
Write-Host -BackgroundColor $front -ForegroundColor $back "[ $($MenuOptions[$i]) ]"
} Else {
Write-Host " $($MenuOptions[$i]) "
}
}
$KeyInput = $host.ui.rawui.readkey("NoEcho,IncludeKeyDown").virtualkeycode;
Switch($KeyInput){
13{
$EnterPressed = $True
Return $MenuOptions[$Selection]
Clear-Host
break
}
9{
$Selection = ($Selection + 1) % $Count
Clear-Host
break
}
38{
$Selection = ($Selection - 1 +$Count) % $Count
Clear-Host
break
}
40{
$Selection = ($Selection + 1) % $Count
Clear-Host
break
}
Default{
Clear-Host
}
}
}
}