zaki work log

作業ログやら生活ログやらなんやら

[kubectl / oc] get eventなどの一覧表示を時系列で表示してみる(--sort-by)

大抵のオブジェクトには、作成されたタイミングで.metadata.creationTimestampという定義が付与されます。
これを、kubectl/ocの汎用オプションの--sort-byに指定することで、出力の表示順を作成日でソートできます。

使い勝手が良いのは、デフォルトでは時間順に並んでいないeventの表示。

sort-by指定なし

[zaki@okd4-manager okd4.4-preview2]$ oc get event -A 
NAMESPACE           LAST SEEN   TYPE      REASON      OBJECT                         MESSAGE
openshift-console   82s         Warning   Unhealthy   pod/console-566b6ccc9-d8wr8    Readiness probe failed: Get https://10.130.0.37:8443/health: dial tcp 10.130.0.37:8443: connect: connection refused
openshift-console   11m         Warning   Unhealthy   pod/console-566b6ccc9-d8wr8    Liveness probe failed: Get https://10.130.0.37:8443/health: dial tcp 10.130.0.37:8443: connect: connection refused
openshift-console   91m         Normal    Pulled      pod/console-566b6ccc9-d8wr8    Container image "registry.svc.ci.openshift.org/origin/4.4-2020-01-28-022517@sha256:747be724483a632b55ce6aaaabc70bb3d45db4d30f5f3675ac21eb9823c5aae3" already present on machine
openshift-console   6m27s       Warning   BackOff     pod/console-566b6ccc9-d8wr8    Back-off restarting failed container
openshift-console   81s         Warning   Unhealthy   pod/console-566b6ccc9-xm4lz    Readiness probe failed: Get https://10.129.0.32:8443/health: dial tcp 10.129.0.32:8443: connect: connection refused
openshift-console   41m         Warning   Unhealthy   pod/console-566b6ccc9-xm4lz    Liveness probe failed: Get https://10.129.0.32:8443/health: dial tcp 10.129.0.32:8443: connect: connection refused
openshift-console   36m         Normal    Pulled      pod/console-566b6ccc9-xm4lz    Container image "registry.svc.ci.openshift.org/origin/4.4-2020-01-28-022517@sha256:747be724483a632b55ce6aaaabc70bb3d45db4d30f5f3675ac21eb9823c5aae3" already present on machine
openshift-console   6m17s       Warning   BackOff     pod/console-566b6ccc9-xm4lz    Back-off restarting failed container
openshift-console   89s         Warning   Unhealthy   pod/console-75bf97f9b9-7kl6w   Readiness probe failed: Get https://10.128.0.26:8443/health: dial tcp 10.128.0.26:8443: connect: connection refused
openshift-console   6m19s       Warning   BackOff     pod/console-75bf97f9b9-7kl6w   Back-off restarting failed container

creationTimestampでsort-by指定

[zaki@okd4-manager okd4.4-preview2]$ oc get event -A --sort-by=.metadata.creationTimestamp
NAMESPACE           LAST SEEN   TYPE      REASON      OBJECT                         MESSAGE
openshift-console   83s         Warning   Unhealthy   pod/console-566b6ccc9-d8wr8    Readiness probe failed: Get https://10.130.0.37:8443/health: dial tcp 10.130.0.37:8443: connect: connection refused
openshift-console   82s         Warning   Unhealthy   pod/console-566b6ccc9-xm4lz    Readiness probe failed: Get https://10.129.0.32:8443/health: dial tcp 10.129.0.32:8443: connect: connection refused
openshift-console   90s         Warning   Unhealthy   pod/console-75bf97f9b9-7kl6w   Readiness probe failed: Get https://10.128.0.26:8443/health: dial tcp 10.128.0.26:8443: connect: connection refused
openshift-console   41m         Warning   Unhealthy   pod/console-566b6ccc9-xm4lz    Liveness probe failed: Get https://10.129.0.32:8443/health: dial tcp 10.129.0.32:8443: connect: connection refused
openshift-console   6m28s       Warning   BackOff     pod/console-566b6ccc9-d8wr8    Back-off restarting failed container
openshift-console   6m20s       Warning   BackOff     pod/console-75bf97f9b9-7kl6w   Back-off restarting failed container
openshift-console   6m18s       Warning   BackOff     pod/console-566b6ccc9-xm4lz    Back-off restarting failed container
openshift-console   11m         Warning   Unhealthy   pod/console-566b6ccc9-d8wr8    Liveness probe failed: Get https://10.130.0.37:8443/health: dial tcp 10.130.0.37:8443: connect: connection refused
openshift-console   36m         Normal    Pulled      pod/console-566b6ccc9-xm4lz    Container image "registry.svc.ci.openshift.org/origin/4.4-2020-01-28-022517@sha256:747be724483a632b55ce6aaaabc70bb3d45db4d30f5f3675ac21eb9823c5aae3" already present on machine
openshift-console   91m         Normal    Pulled      pod/console-566b6ccc9-d8wr8    Container image "registry.svc.ci.openshift.org/origin/4.4-2020-01-28-022517@sha256:747be724483a632b55ce6aaaabc70bb3d45db4d30f5f3675ac21eb9823c5aae3" already present on machine
[zaki@okd4-manager okd4.4-preview2]$ 

もちろんeventだけでなくpodconfigmapのリソースや、カスタムリソースにも使える。


.lastTimestamp

よくみるとeventは.metadata.creationTimestamp指定だと、LAST SEENの列の時間に対しては作用してないですね。。(おそらく同じ内容のeventが複数回発生すると「作成日時」と「LAST SEEN(最後の出力日時?)」がズレそう)
-oyamlした感じだと、.lastTimestampかなぁ。(これはevent限定の項目)

[zaki@okd4-manager gitlab-auth]$ oc get event -n openshift-console --sort-by .lastTimestamp
LAST SEEN   TYPE      REASON      OBJECT                         MESSAGE
128m        Normal    Pulled      pod/console-566b6ccc9-d8wr8    Container image "registry.svc.ci.openshift.org/origin/4.4-2020-01-28-022517@sha256:747be724483a632b55ce6aaaabc70bb3d45db4d30f5f3675ac21eb9823c5aae3" already present on machine
73m         Normal    Pulled      pod/console-566b6ccc9-xm4lz    Container image "registry.svc.ci.openshift.org/origin/4.4-2020-01-28-022517@sha256:747be724483a632b55ce6aaaabc70bb3d45db4d30f5f3675ac21eb9823c5aae3" already present on machine
48m         Warning   Unhealthy   pod/console-566b6ccc9-d8wr8    Liveness probe failed: Get https://10.130.0.37:8443/health: dial tcp 10.130.0.37:8443: connect: connection refused
13m         Warning   Unhealthy   pod/console-566b6ccc9-xm4lz    Liveness probe failed: Get https://10.129.0.32:8443/health: dial tcp 10.129.0.32:8443: connect: connection refused
8m8s        Warning   BackOff     pod/console-566b6ccc9-d8wr8    Back-off restarting failed container
8m5s        Warning   BackOff     pod/console-566b6ccc9-xm4lz    Back-off restarting failed container
8m1s        Warning   BackOff     pod/console-75bf97f9b9-7kl6w   Back-off restarting failed container
3m10s       Warning   Unhealthy   pod/console-75bf97f9b9-7kl6w   Readiness probe failed: Get https://10.128.0.26:8443/health: dial tcp 10.128.0.26:8443: connect: connection refused
3m3s        Warning   Unhealthy   pod/console-566b6ccc9-d8wr8    Readiness probe failed: Get https://10.130.0.37:8443/health: dial tcp 10.130.0.37:8443: connect: connection refused
3m2s        Warning   Unhealthy   pod/console-566b6ccc9-xm4lz    Readiness probe failed: Get https://10.129.0.32:8443/health: dial tcp 10.129.0.32:8443: connect: connection refused

だいたいそれっぽくなった。
ただしこれ、ネームスペースによっては使えなかった。(なので↑は-n openshift-consoleでネームスペースを限定してる)

[zaki@okd4-manager gitlab-auth]$ oc get event -n openshift-authentication --sort-by .lastTimestamp
F0328 08:57:29.708067    9222 sorter.go:354] Field {.lastTimestamp} in [][][]reflect.Value is an unsortable type: interface, err: unsortable interface: interface

小ネタだと思って書き始めたのに案外難しかった。。