Kubernetes: How can i get namespace which is running more than 3 days?

8/23/2021

Example : I would like to get all namespaces which are running more than 3 days. I have already sorted my namespaces by label and creation timestamp with help of this command: kubectl get namespaces -l provisioner=foo --sort-by=.metadata.creationTimestamp

-- Danil Savchenko
kubernetes

1 Answer

8/23/2021

If you could use shell/bash:

kubectl get ns -o jsonpath='{range .items[*]}{.metadata.name} {.metadata.creationTimestamp}{"\n"}{end}' | while read -r name timestamp; do
        echo "$name" | awk -v current_time=$(date +%s) -v three_days_back=$(date +%s -d "3 day ago") -v ns_time=$(date --date="${timestamp}" +%s) '(current_time - ns_time) >(current_time - three_days_back){print $0}';
done
-- P....
Source: StackOverflow