<p>How can I get an <code>ApiVersion</code> for some <code>resource kind</code>, using the kubectl.</p>

<p>Example:</p>

<pre><code>kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1  ( complete string )
</code></pre>

<p>I tried <code>kubectl api-resources</code>, and <code>kubectl api-versions</code> but could not find some complete mapping. Is there any way to concatenate the output of those commands and get the complete string for each resource kind? Or maybe there is some other command.</p>

Ijaz Ahmad
<p>I'm not aware of a <code>kubectl</code> command that can provide mapping between <em>all</em> resources in a cluster - built-in and custom, used or not - and their API Groups and group versions (<code>apiVersion: $GROUP_NAME/$VERSION</code>).</p>

<p>If using <code>curl</code> and <code>jq</code> is an option, the following one-liner will provide such mapping:</p>

<pre><code>for v in `curl -ks https://&lt;k8s-master&gt;:&lt;port&gt;/apis | jq -r .groups[].versions[].groupVersion`; do for r in `curl -ks "https://&lt;k8s-master&gt;:&lt;port&gt;/apis/${v}" | jq -r '.resources[]?.kind' | sort -u`; do echo ${r} - ${v}; done ; done
</code></pre>

<p>Few explanations:</p>

<ul>
<li>replace <code>&lt;k8s-master&gt;:&lt;port&gt;</code> with the name/IP and port of the Kubernetes API
Server master.</li>
<li>break it into multiple lines for better readability.</li>
<li><code>-k</code> in the <code>curl</code> command is to trust any cert.</li>
<li><code>-s</code> in the <code>curl</code> command is for silent output. E.g. no progress output.</li>
<li><code>-r</code> in the <code>jq</code> command is for raw output. E.g. no double quotes will be output for strings.</li>
<li>change <code>echo ${r} - ${v}</code> in a way to output the mapping as desired.</li>
</ul>

<p>Notice that the above doesn't handle <code>api/v1</code>. It is a legacy API Group and its resources are now also under named groups - see <a href="https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-groups" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-groups</a></p>

<p>Partial output for a Kubernetes cluster:</p>

<pre><code>APIService - apiregistration.k8s.io/v1
APIService - apiregistration.k8s.io/v1beta1
DaemonSet - extensions/v1beta1
Deployment - extensions/v1beta1
DeploymentRollback - extensions/v1beta1
...
Role - rbac.authorization.k8s.io/v1
RoleBinding - rbac.authorization.k8s.io/v1
ClusterRole - rbac.authorization.k8s.io/v1beta1
ClusterRoleBinding - rbac.authorization.k8s.io/v1beta1
Role - rbac.authorization.k8s.io/v1beta1
RoleBinding - rbac.authorization.k8s.io/v1beta1
...
</code></pre>
