Search code examples
kubernetesazure-devopswaitkubectlkubernetes-custom-resources

How to kubectl wait for crd creation?


What is the best method for checking to see if a custom resource definition exists before running a script, using only kubectl command line?

We have a yaml file that contains definitions for a NATS cluster ServiceAccount, Role, ClusterRoleBinding and Deployment. The image used in the Deployment creates the crd, and the second script uses that crd to deploy a set of pods. At the moment our CI pipeline needs to run the second script a few times, only completing successfully once the crd has been fully created. I've tried to use kubectl wait but cannot figure out what condition to use that applies to the completion of a crd.

Below is my most recent, albeit completely wrong, attempt, however this illustrates the general sequence we'd like.

kubectl wait --for=condition=complete kubectl apply -f 1.nats-cluster-operator.yaml kubectl apply -f 2.nats-cluster.yaml


Solution

  • The condition for a CRD would be established:

    kubectl -n <namespace-here> wait --for condition=established --timeout=60s crd/<crd-name-here>
    

    You may want to adjust --timeout appropriately.