I try to write a template, to list the names of my services as well as their external endpoints + ports. However, I don't find any examples or documentation how to select an element from an array, in this case port
from the ports
array.
I got that far:
kubectl get service -o=custom-columns=NAME:.metadata.name,IP:.spec.clusterIP,PORT:.spec.ports
To give a more concrete example, this are my running services:
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kafka-manager 10.3.242.200 146.148.20.235 9000:32619/TCP 11h
spark-master 10.3.242.209 104.199.21.235 7077:30588/TCP 11h
I wish to get:
NAME EXTERNAL-ENDPOINT
kafka-manager 146.148.20.225:9000
spark-master 104.199.21.225:7077
TLDR
for an element that is list use *
in square bracket.
So your query should look like this:
$ kubectl get service -n kube-system -o=custom-columns=NAME:.metadata.name,IP:.spec.clusterIP,PORT:.spec.ports[*].targetPort
NAME IP PORT
kube-dns 10.0.0.10 53,53
kubernetes-dashboard 10.0.0.250 9090
Notice the *
in PORT:.spec.ports[*].targetPort
.
Details:
So kubernetes is expecting a json-path-expr
after header
. The error I got when playing with expressions was following:
expected <header>:<json-path-expr>
So to iterate over all elements in a list instead of putting an index just use *
.
Various other json-path expressions can be found here.