Skip to main content

Pregunta 1 - Gestión de Contextos kubectl

Question 1 | Contexts

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.


k config get-contexts -o name > /opt/course/1/contexts

# Usando json path

# Solo para hacer una verificación
k config view -o yaml
# Traerá todos los resultados en una línea separados por espacios
k config view -o jsonpath="{.contexts[*].name}"
# Vamos a cambiar espacios por nueva línea y escribir en el archivo
k config view -o jsonpath="{.contexts[*].name}" | tr " " "\n" > /opt/course/1/contexts

# Obteniendo el current-context y colocándolo en el archivo

echo 'kubectl config current-context' > /opt/course/1/context_default_kubectl.sh
sh /opt/course/1/context_default_kubectl.sh

# El segundo método sin usar kubectl

# Podríamos filtrar por current-context e imprimir solo la segunda parte
cat ~/.kube/config | grep current | awk '{print $2}'
# o podríamos eliminar la primera
cat ~/.kube/config | grep current | sed -e "s/current-context: //"

# Escribe el comando en /opt/course/1/context_default_no_kubectl.sh
sh /opt/course/1/context_default_no_kubectl.sh