Search code examples
azureazure-cliazure-container-service

Assign current AKS resource and name to linux variables


I am running a script that requires the resource group and name of current AKS client config. Previously configured with az aks get-credentials ...

Current script: (I type AKS=something and RG=SOMETHING before running)

az aks update -g $RG -n $AKSNAME ...

Wanted script: (I type nothing before running)

AKSNAME=$(what goes here?)
RG=$(what goes here?)
az aks update -g $RG -n $AKSNAME ...

How can I load RG and AKSNAME values automatically through a shell script?

EDIT: I current assign the values to those variables by hand. I want the script to find the values automatically, corresponding to the cluster in the current context e.g. which kubectl is using.


Solution

  • If you just get the credential via the command az aks get-credentials .... without parameter --admin, then you can get the cluster name like this:

    AKSNAME=$(kubectl config current-context)
    

    And if you use the parameter --admin, then you need to change the command like this:

    AKSNAME=$(kubectl config view --minify -o jsonpath='{.contexts[0].context.cluster}')
    

    Then you can get the group name like this:

    RG=$(az aks list --query "[?name == '$AKSNAME'].resourceGroup" -o tsv)