Created
May 29, 2018 09:23
-
-
Save JeffreyVdb/8c01d1c3aaba5821a2e6356f79e7c39c to your computer and use it in GitHub Desktop.
Install kubectl mutli platform
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
#!/usr/bin/pwsh | |
function Get-KubectlUnix { | |
param ( | |
[string]$Version, | |
[string]$Platform | |
) | |
$tempPath = [System.IO.Path]::GetTempFileName() | |
$kubectlURL = "https://storage.googleapis.com/kubernetes-release/release/$($Version.Trim("`n"))/bin/$Platform/amd64/kubectl" | |
Write-Host "Downloading kubectl to $tempPath" | |
(New-Object System.Net.WebClient).DownloadFile($kubectlURL, $tempPath) | |
chmod +x $tempPath | |
sudo mv $tempPath /usr/local/bin/kubectl | |
if (-Not $?) { | |
exit 1 | |
} | |
} | |
function Install-Kubectl { | |
$latestKubernetesVersion = Invoke-WebRequest 'https://storage.googleapis.com/kubernetes-release/release/stable.txt' ` | |
| Select-Object -ExpandProperty Content | |
if ($IsLinux) { | |
Get-KubectlUnix -Version $latestKubernetesVersion -Platform linux | |
} | |
elseif ($IsMacOS) { | |
Get-KubectlUnix -Version $latestKubernetesVersion -Platform darwin | |
} | |
else { | |
if (Test-CommandExists choco) { | |
choco install kubernetes-cli | |
} | |
else { | |
Install-Script -Name install-kubectl -Scope CurrentUser -Force | |
install-kubectl.ps1 | |
} | |
} | |
} | |
# Source: https://blogs.technet.microsoft.com/heyscriptingguy/2013/02/19/use-a-powershell-function-to-see-if-a-command-exists/ | |
function Test-CommandExists | |
{ | |
Param ([string]$command) | |
$oldPreference = $ErrorActionPreference | |
$ErrorActionPreference = 'stop' | |
try { | |
if(Get-Command $command) { | |
return $true | |
} | |
} | |
Catch { | |
return $false | |
} | |
Finally { | |
$ErrorActionPreference = $oldPreference | |
} | |
} | |
# Install Kubectl depending on platform | |
if (-Not (Test-CommandExists kubectl)) { | |
Install-Kubectl | |
Write-Host "Kubectl installed!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment