Last active
September 15, 2021 21:53
-
-
Save lastcoolnameleft/233cffa37065cd153c8866405f620eaf to your computer and use it in GitHub Desktop.
Azure AKS Run Command (for private clusters)
This file contains 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 $*"