Kubernetes CKA sample exam question 49 with answer

Question
You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts
Next, write a command to display the current context into /opt/course/1/context_default_kubectl.sh, the command should use kubectl.
Finally, write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh, but without the use of kubectl.

Answer
Construct query to get all contexts:

kubectl config get-contexts
kubectl config get-contexts --no-headers
kubectl config get-contexts --no-headers|awk '{print $2}'
kubectl config get-contexts --no-headers|awk '{print $2}' > /opt/course/1/contexts
To get the current context using kubectl:
kubectl config current-context
echo "kubectl config current-context" > /opt/course/1/context_default_kubectl.sh
bash /opt/course/1/context_default_kubectl.sh
To get the current context without using kubectl:
cat ~/.kube/config |grep current-context
cat ~/.kube/config |grep current-context | awk '{print $2}'
echo "cat ~/.kube/config |grep current-context | awk '{print $2}'" > /opt/course/1/context_default_no_kubectl.sh
bash /opt/course/1/context_default_no_kubectl.sh