4

In a simple bash script I want to run multiple kubectl and helm commands, like:

helm install \
  cert-manager jetstack/cert-manager \
  --namespace cert-manager \
  --create-namespace \
  --version v1.5.4 \
  --set installCRDs=true
kubectl apply -f deploy/cert-manager/cluster-issuers.yaml

My problem here is, that after the helm install command I have to wait until the cert-manager pod is running, then the kubectl apply command can be used. Right now the script is calling it too early, so it will fail.

user3142695
  • 15,844
  • 47
  • 176
  • 332
  • Does https://stackoverflow.com/q/65938572/4957508 help? (Answer in the Question, potentially) – Jeff Schaller Nov 02 '21 at 14:24
  • 1
    I know that, @Aserre; I was pointing to the *Question*, which includes `kubectl wait --for condition=Ready ...` as a potential solution. – Jeff Schaller Nov 02 '21 at 14:27
  • Sorry, I didn't really mean to close this; I didn't realize it had a [tag:bash] tag -- please ping me if the answer doesn't solve your problem and I'll be happy to reopen. – tripleee Nov 03 '21 at 13:20

1 Answers1

7

As stated in the comments kubectl wait is the way to go.
Example from the kubectl wait --help

Examples:
  # Wait for the pod "busybox1" to contain the status condition of type "Ready"
  kubectl wait --for=condition=Ready pod/busybox1

This way your script will pause until specified pod is Running, and kubectl will output

<pod-name> condition met

to STDOUT.


kubectl wait is still in experimental phase. If you want to avoid experimental features, you can achieve similar result with bash while loop. By pod name:

while [[ $(kubectl get pods <pod-name> -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do
   sleep 1
done

or by label:

while [[ $(kubectl get pods -l <label>=<label-value> -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do
   sleep 1
done