-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubetools.psm1
71 lines (66 loc) · 2.4 KB
/
kubetools.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
function Start-KubernetesPortForward {
<#
.SYNOPSIS
Initiates port-forwarding to a Kubernetes Pod, Service, Deployment, etc.
#>
[CmdletBinding()]
param (
[Parameter(Position = 0)]
[string] $Target,
[string] $Namespace,
[int] $Port
)
$RandomPort = Get-Random -Minimum 25000 -Maximum 35000
kubectl port-forward $Target --namespace $Namespace $RandomPort`:$Port
}
function Get-KubernetesPortForwardingResources {
<#
.SYNOPSIS
Helper function for Intellisense. Retrieves Kubernetes resources and returns them to the argument completer function.
#>
[CmdletBinding()]
param (
)
$ObjectList = @()
$ObjectList = (kubectl get service --all-namespaces --output=json | ConvertFrom-Json).items
$ObjectList += (kubectl get deployment --all-namespaces --output=json | ConvertFrom-Json).items
$ObjectList += (kubectl get pod --all-namespaces --output=json | ConvertFrom-Json).items
return $ObjectList
}
function Get-KubernetesPortFromResource {
<#
.Synopsis
Given an input Kubernetes resource, as an object, this function attempts to determine the port that someone wants to port-forward to.
#>
[CmdletBinding()]
param (
[object] $Resource
)
switch ($Resource.kind) {
'Service' {
return $Resource.spec.ports[0].port
break;
}
'Deployment' {
return $Resource.spec.template.spec.containers[0].ports[0].containerPort
}
'Pod' {
return $Resource.spec.containers[0].ports[0].containerPort
}
}
}
Register-ArgumentCompleter -CommandName Start-KubernetesPortForward -ParameterName Target -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$CursorPosition = $Host.UI.RawUI.CursorPosition
Write-Host -Object 'Loading ...' -ForegroundColor Green -NoNewLine
Get-KubernetesPortForwardingResources |
Where-Object -FilterScript { $PSItem.metadata.name -match $wordToComplete } |
ForEach-Object -Process {
$PortNumber = Get-KubernetesPortFromResource -Resource $PSItem
'{0}/{1} -Namespace {2} -Port {3}' -f $PSItem.kind, $PSItem.metadata.name, $PSItem.metadata.namespace, $PortNumber;
}
$Host.UI.RawUI.CursorPosition = $CursorPosition
Write-Host -Object (' '*11) -NoNewline
}
New-Alias -Name kpf -Value Start-KubernetesPortForward -Description 'Initiates Kubernetes port forwarding against a Pod, Deployment, Service, etc.'
Export-ModuleMember -Function * -Alias *