Kubectl command to check if namespace is ready

2/18/2019

I would like to know if there is a command in kubernetes that returns true if all resources in a namespace have the ready status and false otherwise.

Something similar to this (ficticious) command:

kubectl get namespace <namespace-name> readiness

If there is not one such command, any help guiding me in the direction of how to retrieve this information (if all resources are ready in a given namespace) is appreciated.

-- user2074945
kubernetes

2 Answers

2/18/2019

There is no such command. try the below command to check all running pods

kubectl get po -n <namespace> | grep 'Running\|Completed'

below command to check the pods that are failed,terminated, error etc.

kubectl get po -n <namespace> | grep -v Running |grep -v Completed
-- P Ekambaram
Source: StackOverflow

2/18/2019

With the following sh scirpt, it is possible to check if all pods in a given namespace are running:

allRunning() {
    podStatus=$(kubectl get pods -n <namespace> -o=jsonpath='{range .items[*]}{.status.conditions[?(@.type=="ContainersReady")].status}{"\n"}{end}')
    for elem in $podStatus
    do
        echo $elem
        if [ $elem != "Running" ]
        then
            return 0
        fi
    done
    return 1
}
allRunning
allAreRunning=$

if [ $allAreRunning == 1 ] 
then
    echo "all are running"
else
    echo "not ready"
fi

EDIT 1: As suggested in the comments, pods do not seem to be the correct resource type to check readiness against. Thus, I suggest the following command for querying readiness which is based on deployment availability:

kubectl get deployments -o=jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Available")].status}{"\n"}{end}'
-- user2074945
Source: StackOverflow