Public/Kubernetes/Set-Kubectl.ps1

Function Set-Kubectl {
    <#
    .SYNOPSIS
        Quick function to install krew and stern
 
    .DESCRIPTION
        This function will check if krew and stern are installed, if not it will install them.
 
    .EXAMPLE
        Set-Kubectl
 
    .NOTES
        pretty straight forward.
 
        Author: Michiel VH
        Requires: kubectl to be installed on the system.
 
    .LINK
        kubectl plugin docs:
        https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/
 
        krew - package manager for kubectl plugins:
        https://krew.sigs.k8s.io/
    #>


    # function definiton
    $Krew = kubectl krew version
    if (!$Krew) {
        choco install krew -y
    }

    $Stern = kubectl stern -v
    if (!$Stern) {
        kubectl krew install stern
    }

    # Write the completion configuration to powershell profile
    kubectl completion powershell >> $PROFILE

    # We need to add some more configurations to the profile that is not generated by the kubectl completion command
    if (-not (Select-String -Path $PROFILE -Pattern '^\s*Set-Alias -Name k -Value kubectl' -Quiet -ErrorAction SilentlyContinue)) { 
        
        $kubectlConfig = @"
Set-Alias -Name k -Value kubectl
Register-ArgumentCompleter -CommandName k -ScriptBlock `$__kubectlCompleterBlock
# Set-PSReadLineKeyHandler -Key Tab -Function Complete # probably not needed but adding for reference
"@

        
        Add-Content -Path $PROFILE -Value $kubectlConfig
    } else {
        Write-Host "Alias already set in profile"
    }
}