Hello all, jsonpath question in lab; Question is Use a JSON PATH query to iden . .

양상헌:
Hello all,

jsonpath question in lab;

Question is
Use a JSON PATH query to identify the context configured for the aws-user in the my-kube-config context file and store the result in /opt/outputs/aws-context-name.

my answer is ;

controlplane $ k config view --kubeconfig=my-kube-config -o jsonpath=‘{.contexts[?(@.context.user == “aws-user”)].context}’ > /opt/outputs/aws-context-name
cat /opt/outputs/aws-context-name
{“cluster”:“kubernetes-on-aws”,“user”:“aws-user”}controlplane $

As mentioned in question request, I got the context for aws-user,

But actual answer is the context name for aws-user;

kubectl config view --kubeconfig=my-kube-config -o jsonpath=“{.contexts[?(@.context.user==‘aws-user’)].name}” > /opt/outputs/aws-context-name

controlplane $ kubectl config view --kubeconfig=my-kube-config -o jsonpath=“{.contexts[?(@.context.user==‘aws-user’)].name}”
aws-user@kubernetes-on-awscontrolplane $

Did I miss something ?

Lakshminarayanan Krishnan:
could you share the contents of your kubeconfig file?

Lakshminarayanan Krishnan:
Also, isn’t this working for you? k config view --kubeconfig=my-kube-config -o jsonpath='{.contexts[?(@.context.user == "aws-user")].context.name}'

Kicky:
@양상헌 you are calling the “.context” of the matching element instead of “.name” towards the end of your query . That should fix ur issue

This works:
k config view --kubeconfig=my-kube-config -o jsonpath=‘{.contexts[?(@.context.user == “aws-user”)].name}’

Actually, that gives you the name of your context, not the name of the user. What you really want is

k config view --kubeconfig=my-kube-config -o jsonpath='{.contexts[?(@.context.user == "aws-user")].context.user}'