<p>I have the following yaml file, where I create a kubernetes CRD and a Custom Resource, as an instance of that CRD.</p>

<pre><code>apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: mycrds.example.com
spec:
  group: example.com
  version: v1
  names:
    kind: MyCrd
    plural: mycrds
    singular: mycrd
  scope: Namespaced
---
apiVersion: example.com/v1
kind: MyCrd
metadata:
  name: example-instance
  namespace: default
spec:
  selectors:
    cluster:
      matchName:
        - '*'
</code></pre>

<p>If I do <code>kubectl apply</code> or <code>kubectl create</code> on this file, I get an error sometimes that <code>no matches for kind "MyCrd" in version "example.com/v1"</code> because the CRD registration is not yet complete. This does not happen always also. </p>

<p>Sample error messages:</p>

<pre><code>$ kubectl apply -f crd-and-instance.yaml
customresourcedefinition.apiextensions.k8s.io/mycrds.example.com created
error: unable to recognize "/home/psankar/Downloads/crd-and-instance.yaml": no matches for kind "MyCrd" in version "example.com/v1"

$ # Same command executed after 2-3 seconds

$ kubectl apply -f crd-and-instance.yaml
customresourcedefinition.apiextensions.k8s.io/mycrds.example.com unchanged
mycrd.example.com/example-instance created
</code></pre>

<p>This does not happen always. Sometimes, the CRD gets registered fast and the kubectl create/apply command works fine in the first attempt.</p>

<p>I know that I can achieve this via shell script like:</p>

<pre><code>kubectl create crd.yaml
kubectl wait until crd is published
kubectl create crdInstance.yaml
</code></pre>

<p>But I do not want to do this and want a single yaml file without any accompanying shell script. Is this possible ?</p>

<p><code>kubectl apply -f</code> is just an API call to k8s api-server which creates a resource in k8s etcd storage. It neither guarantees that the required resource is actually created nor checks its status. The resource may stuck in roll-out due to missing external dependencies, get in crash loop etc.</p>

<p>In order to make the sequence work synchronously you need to inject some sort of health checking (e.g. querying HTTP endpoint of the created resource). Another approach is just to delay creation by including <code>sleep X</code> seconds between the calls to let k8s finalize the creation. Of course, this will not check if the resource was actually created or failed. </p>
