<p>How can I check and/or wait that apiVersion and kind exists before trying to apply resource using those?</p>

<p><strong>Example:</strong></p>

<p><em>Install cilium and network policy using cilium</em></p>

<pre><code>kubectl apply -f cilium.yaml
kubectl apply -f policy.yaml # fails if run just after installing cilium, since cilium.io/v2 and CiliumNetworkPolicy doesn't exist yet
</code></pre>

<p><a href="https://github.com/cilium/cilium/blob/master/examples/kubernetes/1.13/cilium.yaml" rel="nofollow noreferrer"><em>cilium.yaml</em></a></p>

<p><em>policy.yaml</em></p>

<pre><code>apiVersion: cilium.io/v2
description: example policy
kind: CiliumNetworkPolicy
...
</code></pre>

<p><strong>EDIT:</strong> <em>(solved with following script)</em></p>

<pre><code>#! /bin/bash

function check_api {
    local try=0
    local retries=30
    until (kubectl "api-$1s" | grep -P "\b$2\b") &amp;&gt;/dev/null; do
        (( ++try &gt; retries )) &amp;&amp; exit 1
        echo "$2 not found. Retry $try/$retries"
        sleep 3
    done
}

kubectl apply -f cilium.yaml
check_api version cilium.io/v2
check_api resource CiliumNetworkPolicy
kubectl apply -f policy.yaml
</code></pre>

<p>You can use the following to check for supported versions and kinds, that is, check what the API server you're talking to supports:</p>

<pre><code>$ kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
...
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
</code></pre>

<p>There's also <code>kubectl api-resources</code> that provides you with a tabular overview of the kinds, shortnames, and if a resource is namespaced or not.</p>
