Skip to content

Instantly share code, notes, and snippets.

@lastcoolnameleft
Last active September 15, 2021 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lastcoolnameleft/233cffa37065cd153c8866405f620eaf to your computer and use it in GitHub Desktop.
Save lastcoolnameleft/233cffa37065cd153c8866405f620eaf to your computer and use it in GitHub Desktop.
Azure AKS Run Command (for private clusters)
# Tested in zsh & bash
# This function is designed for Private Clusters with Azure CLI AKS Run Command
# https://docs.microsoft.com/en-us/azure/aks/private-clusters#use-aks-run-command
# It assumes your current context is the private cluster and parses that data from the context data
# Prerequisite: Your private cluster is the Kube config current-context (e.g. az aks get-credentials)
# Usage: azk <command>
# Example: azk kubectl get pods -n kube-system
function azk() {
AZK_CURRENT_CONTEXT=$(kubectl config current-context)
AZK_CURRENT_CONTEXT_USER=$(kubectl config view -o jsonpath="{.contexts[?(@.name == '$AZK_CURRENT_CONTEXT')].context.user}")
AZK_RG=$(echo $AZK_CURRENT_CONTEXT_USER | awk -F'_' '{print $2}')
AZK_AKS_CLUSTER=$(echo $AZK_CURRENT_CONTEXT_USER | awk -F'_' '{print $3}')
az aks command invoke -g $AZK_RG -n $AZK_AKS_CLUSTER -c "$*"
}
# TODO: Incorporate https://github.com/ahmetb/kubectl-aliases
@haitch
Copy link

haitch commented Sep 15, 2021

I believe this can be further simplified
from azk kubectl get pods -n kube-system
to azk get pods -n kube-system

in line 14
az aks command invoke -g $AZK_RG -n $AZK_AKS_CLUSTER -c "$*"
update to
az aks command invoke -g $AZK_RG -n $AZK_AKS_CLUSTER -c "kubectl $*"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment