Using Kubernetes client-go how to check programatically if Node is "Ready"?

8/4/2019

It looks like I might need to traverse the v1.Node->NodeStatus->Conditions[] slice and sort by transition time and find if the most recent timed condition is NodeConditionType == "Ready". I am wondering if there is a better way or if that approach is flawed?

-- Philip Lombardi
client-go
kubernetes

2 Answers

8/4/2019

In my situation, I enable TaintBasedEvictions and TaintNodesByCondition feature gates, and k8s node-controller will add some taints (e.g. node.kubernetes.io/not-ready, node.kubernetes.io/unreachable) automatically on node, I only need to watch node, and check taint.

-- menya
Source: StackOverflow

8/4/2019

You are looking in the right place, but Conditions may not work exactly the way your question implies they do. Conditions shouldn't be seen as time-based events, but rather current states. To quote the API conventions documentation:

Conditions represent the latest available observations of an object's state.

As such, it isn't necessary to look for the latest Condition, but rather the condition for the type of state that you are interested in observing. There should only be one whose NodeConditionType is Ready, but you will need to check the .Status field on NodeCondition to confirm if its value is True, False or Unknown.

-- bosgood
Source: StackOverflow