I'm trying to use kubectl to wait for a service to get an external ip assigned. I've been trying to use the below just to get started
kubectl wait --for='jsonpath={.spec.externalTrafficPolicy==Cluster}' --timeout=30s --namespace cloud-endpoints svc/esp-echo
But I keep getting the below error message
error: unrecognized condition: "jsonpath={.spec.externalTrafficPolicy==Cluster}"
It is not possible to pass arbitrary jsonpath and there is already a request for the feature.
However, you can use a bash script with some sleep and monitor the service using other kubectl commands:
kubectl get --namespace cloud-endpoints svc/esp-echo --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}"
The above command will return the external IP for the LoadBalancer service for example.
You can write a simple bash file using the above as:
#!/bin/bash
ip=""
while [ -z $ip ]; do
echo "Waiting for external IP"
ip=$(kubectl get svc $1 --namespace cloud-endpoints --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}")
[ -z "$ip" ] && sleep 10
done
echo 'Found external IP: '$ip
Updated from Krishna Chaurasia's answer, kubectl have implemented the feature to be able to wait on arbitrary jsonpath value now.
However, the catch is the value will only be primitive value, excluding nested primitive value (map[string]interface{} or []interface{})
You need to provide a condition. Like:
kubectl -n foobar wait --for=condition=complete --timeout=32s foo/bar
Here is a good article that explains that: https://mrkaran.dev/posts/kubectl-wait/
In your case, you might use one of the k8s probes.