Kubernetes ApiVersion for a Resource kind

11/2/2018

How can I get an ApiVersion for some resource kind, using the kubectl.

Example:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1  ( complete string )

I tried kubectl api-resources, and kubectl api-versions 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.

-- Ijaz Ahmad Khan
kubernetes

2 Answers

11/3/2018

You can try

kubectl get roles --all-namespaces -o jsonpath='{.items[*].apiVersion}'
-- hoque
Source: StackOverflow

11/3/2018

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

If using curl and jq is an option, the following one-liner will provide such mapping:

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

Few explanations:

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

Notice that the above doesn't handle api/v1. It is a legacy API Group and its resources are now also under named groups - see https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-groups

Partial output for a Kubernetes cluster:

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
...
-- apisim
Source: StackOverflow