'kubectl get pods' command fails on Windows, works on WSL2

2/22/2021

The kubernetes documentation includes an example command for listing container images by pod:

List Container images by Pod

The formatting can be controlled further by using the range operation to iterate over elements individually.

kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\
sort

When I run this command in my Debian WSL2 instance, it correctly lists the containers for each pod I have running across all namespaces.

When I run the same command in the Windows commandline, I get an error: error: a resource cannot be retrieved by name across all namespaces:

C:\workspace>kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' | sort
error: a resource cannot be retrieved by name across all namespaces

Is this a bug with kubectl, or is this command *nix only? Is there an OS-independent command for getting container images by pod across all namespaces?

(I'm running Debian on WSL2, Windows 10 Enterprise. Docker Desktop using WSL2 integration, K8S with minikube.)

-- Roddy of the Frozen Peas
kubectl
kubernetes

1 Answer

2/22/2021

From k8s offical doc:

On Windows, you must double quote any JSONPath template that contains spaces (not single quote as shown above for bash). This in turn means that you must use a single quote or escaped double quote around any literals in the template. For example: kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}"

I think this will solve your problem.

-- Sahadat Hossain
Source: StackOverflow