OpenShift - oc command Tips - node

OpenShift の様々な操作で使用する `oc` コマンドにおいて、node 関連で思いのほか面倒と思われるものを、使用例としてご紹介します。

### Sort
#### Sort by node-role
```shell
$ oc get node --sort-by='.metadata.labels.node-role\.kubernetes\.io/worker'
NAME                                               STATUS   ROLES                  AGE     VERSION
ip-99-888-77-45.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
ip-99-888-77-82.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
ip-99-888-77-40.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
ip-99-888-77-16.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-50.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-102.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-19.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-118.us-eastwest-10.machine.external   Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-41.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-42.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-116.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-49.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-122.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-55.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-71.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-72.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-78.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-113.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-93.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-94.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-99.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d

$ oc get node --sort-by='.metadata.labels.node-role\.kubernetes\.io/control-plane'
NAME                                               STATUS   ROLES                  AGE     VERSION
ip-99-888-77-49.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-94.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-19.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-78.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-93.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-55.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-72.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-122.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-113.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-116.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-99.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-50.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-71.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-42.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-102.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-118.us-eastwest-10.machine.external   Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-41.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-16.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-82.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
ip-99-888-77-45.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
ip-99-888-77-40.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
```

#### Sort by node IP address
```shell
$ oc get node --sort-by=.status.addresses[0].address
NAME                                               STATUS   ROLES                  AGE     VERSION
ip-99-888-77-16.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-19.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-40.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
ip-99-888-77-41.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-42.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-45.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
ip-99-888-77-49.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-50.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-55.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-71.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-72.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-78.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-82.us-eastwest-10.machine.external    Ready    control-plane,master   5d19h   v1.29.8+f10c92d
ip-99-888-77-93.us-eastwest-10.machine.external    Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-94.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-99.us-eastwest-10.machine.external    Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-102.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-113.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-116.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
ip-99-888-77-118.us-eastwest-10.machine.external   Ready    infra,worker           20h     v1.29.8+f10c92d
ip-99-888-77-122.us-eastwest-10.machine.external   Ready    worker                 5d19h   v1.29.8+f10c92d
```

### Label
#### Add node label
```shell
$ oc label node ip-99-888-77-122.us-eastwest-10.machine.external my-label.private/label1="true"
node/ip-99-888-77-122.us-eastwest-10.machine.external labeled
```

#### Show labeled nodes
```shell
$ oc get node -l my-label.private/label1
NAME                                               STATUS   ROLES    AGE     VERSION
ip-99-888-77-122.us-eastwest-10.machine.external   Ready    worker   5d19h   v1.29.8+f10c92d

$ oc get node -l my-label.private/label1="true"
NAME                                               STATUS   ROLES    AGE     VERSION
ip-99-888-77-122.us-eastwest-10.machine.external   Ready    worker   5d19h   v1.29.8+f10c92d

$ oc get node -l my-label.private/label1=true
NAME                                               STATUS   ROLES    AGE     VERSION
ip-99-888-77-122.us-eastwest-10.machine.external   Ready    worker   5d19h   v1.29.8+f10c92d

$ oc get node -l my-label.private/label1=false
No resources found
```
#### Show all node labels
```shell
$ oc -o json get node | jq -r '.items[].metadata.labels | keys[]' | sort | uniq
beta.kubernetes.io/arch
beta.kubernetes.io/instance-type
beta.kubernetes.io/os
failure-domain.beta.kubernetes.io/region
failure-domain.beta.kubernetes.io/zone
kubernetes.io/arch
kubernetes.io/hostname
kubernetes.io/os
node.kubernetes.io/instance-type
node.openshift.io/os_id
node-role.kubernetes.io/control-plane
node-role.kubernetes.io/master
node-role.kubernetes.io/worker
topology.abc.123.xyz.com/zone
topology.kubernetes.io/region
topology.kubernetes.io/zone

$ for V in $(oc -o json get node | jq -r '.items[].metadata.labels | keys[]' | sort | uniq)
do
  O=$O" -L $V "
done

$ oc get node $O
NAME                                               STATUS   ROLES                  AGE     VERSION           ARCH    INSTANCE-TYPE   OS      REGION           ZONE              ARCH    HOSTNAME                                           OS      INSTANCE-TYPE   OS_ID   CONTROL-PLANE   MASTER   WORKER   ZONE              REGION           ZONE
ip-99-888-77-102.us-eastwest-10.machine.external   Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-102.us-eastwest-10.machine.external   linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-113.us-eastwest-10.machine.external   Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-113.us-eastwest-10.machine.external   linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-116.us-eastwest-10.machine.external   Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-116.us-eastwest-10.machine.external   linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-122.us-eastwest-10.machine.external   Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-122.us-eastwest-10.machine.external   linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-16.us-eastwest-10.machine.external    Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-16.us-eastwest-10.machine.external    linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-40.us-eastwest-10.machine.external    Ready    control-plane,master   4d22h   v1.29.8+f10c92d   amd64   c6i.8xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-40.us-eastwest-10.machine.external    linux   c6i.8xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-41.us-eastwest-10.machine.external    Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-41.us-eastwest-10.machine.external    linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-42.us-eastwest-10.machine.external    Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-42.us-eastwest-10.machine.external    linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-45.us-eastwest-10.machine.external    Ready    control-plane,master   4d22h   v1.29.8+f10c92d   amd64   c6i.8xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-45.us-eastwest-10.machine.external    linux   c6i.8xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-49.us-eastwest-10.machine.external    Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-49.us-eastwest-10.machine.external    linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-50.us-eastwest-10.machine.external    Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-50.us-eastwest-10.machine.external    linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-55.us-eastwest-10.machine.external    Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-55.us-eastwest-10.machine.external    linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-71.us-eastwest-10.machine.external    Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-71.us-eastwest-10.machine.external    linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-82.us-eastwest-10.machine.external    Ready    control-plane,master   4d22h   v1.29.8+f10c92d   amd64   c6i.8xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-82.us-eastwest-10.machine.external    linux   c6i.8xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
ip-99-888-77-93.us-eastwest-10.machine.external    Ready    worker                 4d22h   v1.29.8+f10c92d   amd64   r5.16xyzbig     linux   us-eastwest-10   us-eastwest-10z   amd64   ip-99-888-77-93.us-eastwest-10.machine.external    linux   r5.16xyzbig     rhcos                                     us-eastwest-10z   us-eastwest-10   us-eastwest-10z
```
#### Remove node label
```shell
$ oc label node ip-99-888-77-122.us-eastwest-10.machine.external my-label.private/label1-
node/ip-99-888-77-122.us-eastwest-10.machine.external unlabeled

$ oc get node -l my-label.private/label1
No resources found
```
### Taint
#### Add node taint
```shell
$ oc adm taint node ip-99-888-77-122.us-eastwest-10.machine.external my-taint.private/taint1="true":NoSchedule
node/ip-99-888-77-122.us-eastwest-10.machine.external tainted
```

#### Show node taints
```shell
$ oc get node ip-99-888-77-122.us-eastwest-10.machine.external -o jsonpath='{.spec.taints[]}{"\n"}'
[{"effect":"NoSchedule","key":"my-taint.private/taint1","value":"true"}]
```

#### Show all node taints
```shell
$ oc get node -o jsonpath='{range @.items[*]}{.metadata.name}{.spec.taints[*]}{"\n"}'
ip-99-888-77-102.us-eastwest-10.machine.external
ip-99-888-77-113.us-eastwest-10.machine.external
ip-99-888-77-116.us-eastwest-10.machine.external
ip-99-888-77-118.us-eastwest-10.machine.external
ip-99-888-77-122.us-eastwest-10.machine.external{"effect":"NoSchedule","key":"my-taint.private/taint1","value":"true"}
ip-99-888-77-16.us-eastwest-10.machine.external
ip-99-888-77-19.us-eastwest-10.machine.external
ip-99-888-77-40.us-eastwest-10.machine.external{"effect":"NoSchedule","key":"node-role.kubernetes.io/master"}
ip-99-888-77-41.us-eastwest-10.machine.external
ip-99-888-77-42.us-eastwest-10.machine.external
ip-99-888-77-45.us-eastwest-10.machine.external{"effect":"NoSchedule","key":"node-role.kubernetes.io/master"}
ip-99-888-77-49.us-eastwest-10.machine.external
ip-99-888-77-50.us-eastwest-10.machine.external
ip-99-888-77-55.us-eastwest-10.machine.external
ip-99-888-77-71.us-eastwest-10.machine.external
ip-99-888-77-72.us-eastwest-10.machine.external
ip-99-888-77-78.us-eastwest-10.machine.external
ip-99-888-77-82.us-eastwest-10.machine.external{"effect":"NoSchedule","key":"node-role.kubernetes.io/master"}
ip-99-888-77-93.us-eastwest-10.machine.external
ip-99-888-77-94.us-eastwest-10.machine.external
ip-99-888-77-99.us-eastwest-10.machine.external

$ oc get node -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints
NAME                                               TAINTS
ip-99-888-77-102.us-eastwest-10.machine.external   <none>
ip-99-888-77-113.us-eastwest-10.machine.external   <none>
ip-99-888-77-116.us-eastwest-10.machine.external   <none>
ip-99-888-77-122.us-eastwest-10.machine.external   [map[effect:NoSchedule key:my-taint.private/taint1 value:true]]
ip-99-888-77-16.us-eastwest-10.machine.external    <none>
ip-99-888-77-40.us-eastwest-10.machine.external    [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
ip-99-888-77-41.us-eastwest-10.machine.external    <none>
ip-99-888-77-42.us-eastwest-10.machine.external    <none>
ip-99-888-77-45.us-eastwest-10.machine.external    [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
ip-99-888-77-49.us-eastwest-10.machine.external    <none>
ip-99-888-77-50.us-eastwest-10.machine.external    <none>
ip-99-888-77-55.us-eastwest-10.machine.external    <none>
ip-99-888-77-71.us-eastwest-10.machine.external    <none>
ip-99-888-77-82.us-eastwest-10.machine.external    [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
ip-99-888-77-93.us-eastwest-10.machine.external    <none>
```

#### Remove node taint
```shell
$ oc adm taint node ip-99-888-77-122.us-eastwest-10.machine.external my-taint.private/taint1="true":NoSchedule-
node/ip-99-888-77-122.us-eastwest-10.machine.external untainted

$ oc get node ip-99-888-77-122.us-eastwest-10.machine.external -o jsonpath='{.spec.taints[]}{"\n"}'

```