Lesson  in  CKA Mock Exam

Question 2: Kubeconfig Extraction

Extract context names and the current context from a provided kubeconfig file.

You're asked to extract the following information out of the kubeconfig file /opt/course/1/kubeconfig:

  • Write all kubeconfig context names into /opt/course/1/contexts, one per line.
  • Write the name of the current context into /opt/course/1/current-context.
Hint 1

kubectl config --help lists a set of subcommands built exactly for reading a kubeconfig without opening it in an editor — one lists contexts, one prints the active one.

Solution (only open if you're stuck)

Both pieces of information can be read straight out of the kubeconfig with kubectl config, no editor required.

Contexts:

kubectl --kubeconfig /opt/course/1/kubeconfig config get-contexts -o name > /opt/course/1/contexts

Current context:

kubectl --kubeconfig /opt/course/1/kubeconfig config current-context > /opt/course/1/current-context