0
votes

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.)

1
does kubectl get pods --all-namespaces it works?Sahadat Hossain
Yes. I think it's something specific to the jsonpath; if I specify a namespace with -n <namespace>, it gives an error: error parsing jsonpath '{range, unclosed action.Roddy of the Frozen Peas

1 Answers

2
votes

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.