Kubernetes costum-columns select item from array
I am trying to write a template, list my service names as well as their external endpoints + ports. However, I have not found examples or documentation on how to select an item from an array, in this case an port
array ports
.
I got this far:
kubectl get service -o=custom-columns=NAME:.metadata.name,IP:.spec.clusterIP,PORT:.spec.ports
To give a more specific example, these 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 want to receive:
NAME EXTERNAL-ENDPOINT
kafka-manager 146.148.20.225:9000
spark-master 104.199.21.225:7077
source to share
TL; DR
for an element that uses a *
bracketed list .
So your request 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
Pay attention to *
in PORT:.spec.ports[*].targetPort
.
Details:
So kubernetes is waiting json-path-expr
after header
. The error I got when playing with expressions was the following:
expected <header>:<json-path-expr>
So, to iterate over all the elements in the list instead of putting the index, just use *
.
Various json-path expressions can be found here .
source to share
You can use * to understand what data is in json. For example:
kubectl get svc gdpr -o custom-columns=svc:*
As for me, kubectl get svc -o custom-columns=svc:.metadata.name,IP:.metadata.annotations.domainName,PORT:.spec.ports[*].targetPort
was perfect (due to external IP info) and looks like:
event site1.com 9000
gdpr site2.com 3333,8080
svcInt none 80
ui site6.com 80,6123,6124,6125,8081
about the list of external IPs and hosts:
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name} {.status.addresses[?(@.type=="ExternalIP")].address}{"\n"}'
ip-10-10-40-13.xxxxx.internal xx.xx.xx.175
ip-10-10-40-15.xxxxx.internal xx.xx.xx.236
ip-10-10-40-18.xxxxx.internal xx.xx.xx.207
kubectl get nodes -o jsonpath='{range .items[*]}{.status.addresses[?(@.type=="ExternalIP")].address}{"\n"}'
xx.xx.xx.175
xx.xx.xx.236
xx.xx.xx.207
source to share