OpenShift - oc command Tips - api-resources

OpenShift の様々な操作で使用する `oc` コマンドの `api-resources` に関する留意点をご紹介します。

### oc api-resources
`oc api-resources` コマンドは、Cluster に登録されている API の一覧を表示します。

```shell
$ oc api-resources --help
Print the supported API resources on the server.

Examples:
  # Print the supported API resources
  oc api-resources

  # Print the supported API resources with more information
  oc api-resources -o wide

  # Print the supported API resources sorted by a column
  oc api-resources --sort-by=name

  # Print the supported namespaced resources
  oc api-resources --namespaced=true

  # Print the supported non-namespaced resources
  oc api-resources --namespaced=false

  # Print the supported API resources with a specific APIGroup
  oc api-resources --api-group=rbac.authorization.k8s.io

Options:
    --api-group='':
        Limit to resources in the specified API group.

    --cached=false:
        Use the cached list of resources if available.

    --categories=[]:
        Limit to resources that belong to the specified categories.

    --namespaced=true:
        If false, non-namespaced resources will be returned, otherwise returning namespaced resources by default.

    --no-headers=false:
        When using the default or custom-column output format, don't print headers (default print headers).

    -o, --output='':
        Output format. One of: (wide, name).

    --sort-by='':
        If non-empty, sort list of resources using specified field. The field can be either 'name' or 'kind'.

    --verbs=[]:
        Limit to resources that support the specified verbs.

Usage:
  oc api-resources [flags] [options]

Use "oc options" for a list of global command-line options (applies to all commands).
```
API の一覧は、API Server に `APIGroupDiscoveryList` を要求することで取得します。

https://pkg.go.dev/k8s.io/kubernetes/pkg/apis/apidiscovery#APIGroupDiscoveryList

> APIGroupDiscoveryList is a resource containing a list of APIGroupDiscovery. This is one of the types able to be returned from the /api and /apis endpoint and contains an aggregated list of API resources (built-ins, Custom Resource Definitions, resources from aggregated servers) that a cluster supports. 

### VERBS の確認
`-o wide` によって API で使用可能な `Verbs` を表示します。

```shell
$ oc api-resources -o wide | head -n 10
NAME                                  SHORTNAMES                                                                             APIVERSION                                    NAMESPACED   KIND                                 VERBS                                                        CATEGORIES
bindings                                                                                                                     v1                                            true         Binding                              create
componentstatuses                     cs                                                                                     v1                                            false        ComponentStatus                      get,list
configmaps                            cm                                                                                     v1                                            true         ConfigMap                            create,delete,deletecollection,get,list,patch,update,watch
endpoints                             ep                                                                                     v1                                            true         Endpoints                            create,delete,deletecollection,get,list,patch,update,watch
events                                ev                                                                                     v1                                            true         Event                                create,delete,deletecollection,get,list,patch,update,watch
limitranges                           limits                                                                                 v1                                            true         LimitRange                           create,delete,deletecollection,get,list,patch,update,watch
namespaces                            ns                                                                                     v1                                            false        Namespace                            create,delete,get,list,patch,update,watch
nodes                                 no                                                                                     v1                                            false        Node                                 create,delete,deletecollection,get,list,patch,update,watch
persistentvolumeclaims                pvc                                                                                    v1                                            true         PersistentVolumeClaim                create,delete,deletecollection,get,list,patch,update,watch
```

https://kubernetes.io/docs/reference/using-api/api-concepts/#api-verbs

### CATEGORY 指定
`-o wide` で `CATEGORIES` が設定されている場合、該当 CATEGORY のみを表示します。
```shell
$ oc api-resources --categories=cluster-api
NAME                         SHORTNAMES                                                                             APIVERSION                                NAMESPACED   KIND
metal3remediations           m3r,m3remediation                                                                      infrastructure.cluster.x-k8s.io/v1beta1   true         Metal3Remediation
metal3remediationtemplates   m3rt,m3remediationtemplate,m3remediationtemplates,metal3rt,metal3remediationtemplate   infrastructure.cluster.x-k8s.io/v1beta1   true         Metal3RemediationTemplate
ipaddressclaims                                                                                                     ipam.cluster.x-k8s.io/v1beta1             true         IPAddressClaim
ipaddresses                                                                                                         ipam.cluster.x-k8s.io/v1beta1             true         IPAddress
```

### API GROUP 指定
`APIVERSION` で `API GROUP` が設定されている場合、該当 `API GROUP` のみを表示します。
```shell
$ oc api-resources --api-group=ceph.rook.io
NAME                            SHORTNAMES        APIVERSION        NAMESPACED   KIND
cephblockpoolradosnamespaces                      ceph.rook.io/v1   true         CephBlockPoolRadosNamespace
cephblockpools                                    ceph.rook.io/v1   true         CephBlockPool
cephbucketnotifications                           ceph.rook.io/v1   true         CephBucketNotification
cephbuckettopics                                  ceph.rook.io/v1   true         CephBucketTopic
cephclients                                       ceph.rook.io/v1   true         CephClient
cephclusters                                      ceph.rook.io/v1   true         CephCluster
cephcosidrivers                 cephcosi          ceph.rook.io/v1   true         CephCOSIDriver
cephfilesystemmirrors                             ceph.rook.io/v1   true         CephFilesystemMirror
cephfilesystems                                   ceph.rook.io/v1   true         CephFilesystem
cephfilesystemsubvolumegroups                     ceph.rook.io/v1   true         CephFilesystemSubVolumeGroup
cephnfses                       nfs               ceph.rook.io/v1   true         CephNFS
cephobjectrealms                                  ceph.rook.io/v1   true         CephObjectRealm
cephobjectstores                                  ceph.rook.io/v1   true         CephObjectStore
cephobjectstoreusers            rcou,objectuser   ceph.rook.io/v1   true         CephObjectStoreUser
cephobjectzonegroups                              ceph.rook.io/v1   true         CephObjectZoneGroup
cephobjectzones                                   ceph.rook.io/v1   true         CephObjectZone
cephrbdmirrors                                    ceph.rook.io/v1   true         CephRBDMirror
```

https://kubernetes.io/docs/reference/using-api/#api-groups

### NAME が重複する場合の確認
NAME が重複する例として `configs` を確認してみます。

```shell
$ oc api-resources | grep "^configs"
configs                                                                                                                      imageregistry.operator.openshift.io/v1        false        Config
configs                                                                                                                      operator.openshift.io/v1                      false        Config
configs                                                                                                                      samples.operator.openshift.io/v1              false        Config
```
`configs` でリソースを取得すると、上記１番目のリソースのみが取得されることが分かります。
```shell
$ oc get configs -A
NAME      AGE
cluster   11d

$ oc get configs -A -o name
config.imageregistry.operator.openshift.io/cluster
```
重複するリソースそれぞれを取得する場合は `-o name` で取得した FULL-NAME を指定します。
```shell
$ oc api-resources -o name | grep "^configs"
configs.imageregistry.operator.openshift.io
configs.operator.openshift.io
configs.samples.operator.openshift.io

$ for R in $(oc api-resources -o name | grep "^configs")
> do
> echo $R
> oc get $R -A
> oc -o yaml get $R -A | head -n 4
> echo
> done
configs.imageregistry.operator.openshift.io
NAME      AGE
cluster   11d
apiVersion: v1
items:
- apiVersion: imageregistry.operator.openshift.io/v1
  kind: Config

configs.operator.openshift.io
NAME      AGE
cluster   11d
apiVersion: v1
items:
- apiVersion: operator.openshift.io/v1
  kind: Config

configs.samples.operator.openshift.io
NAME      AGE
cluster   11d
apiVersion: v1
items:
- apiVersion: samples.operator.openshift.io/v1
  kind: Config
```