I need to print only specific fields of Kubernetes Events, sorted by a specific field.
This is to help me gather telemetry and analytics about my namespace
How could I do that?
Following command does it.
It prints the events sorted by timestamp of creation.
It also users go-template
to filter out specific fields of the kubernetes-event
object.
kubectl get events --sort-by='.metadata.creationTimestamp' -o 'go-template={{range .items}}{{.involvedObject.name}}{{"\t"}}{{.involvedObject.kind}}{{"\t"}}{{.message}}{{"\t"}}{{.reason}}{{"\t"}}{{.type}}{{"\t"}}{{.firstTimestamp}}{{"\n"}}{{end}}'
kubectl get events --sort-by='.lastTimestamp'
If you don't mind seeing the output as JSON:
kubectl get event -o json | jq '.items |= sort_by(.lastTimestamp)'
This requires jq.