diff --git a/pkg/utils/kubeclient/node.go b/pkg/utils/kubeclient/node.go index 2d594ba2554..65954de38d9 100644 --- a/pkg/utils/kubeclient/node.go +++ b/pkg/utils/kubeclient/node.go @@ -41,13 +41,11 @@ func GetNode(client client.Reader, name string) (node *corev1.Node, err error) { // IsReady checks if the node is ready // If the node is ready,it returns True.Otherwise,it returns False. -func IsReady(node corev1.Node) (ready bool) { - ready = true +func IsReady(node corev1.Node) bool { for _, condition := range node.Status.Conditions { - if condition.Type == corev1.NodeReady && condition.Status != corev1.ConditionTrue { - ready = false - break + if condition.Type == corev1.NodeReady { + return condition.Status == corev1.ConditionTrue } } - return ready + return false } diff --git a/pkg/utils/kubeclient/node_test.go b/pkg/utils/kubeclient/node_test.go index 2f2a0d4b894..82a757e3fe2 100644 --- a/pkg/utils/kubeclient/node_test.go +++ b/pkg/utils/kubeclient/node_test.go @@ -124,4 +124,77 @@ var _ = Describe("IsReady", func() { Expect(result).To(BeFalse()) }) }) + + Context("when node has no NodeReady condition", func() { + It("should return false when conditions are empty", func() { + node := corev1.Node{ + ObjectMeta: metav1.ObjectMeta{Name: "test-empty"}, + Status: corev1.NodeStatus{ + Conditions: []corev1.NodeCondition{}, + }, + } + result := IsReady(node) + Expect(result).To(BeFalse()) + }) + + It("should return false when other conditions are present but NodeReady is missing", func() { + node := corev1.Node{ + ObjectMeta: metav1.ObjectMeta{Name: "test3"}, + Status: corev1.NodeStatus{ + Conditions: []corev1.NodeCondition{ + { + Type: corev1.NodeMemoryPressure, + Status: corev1.ConditionFalse, + }, + }, + }, + } + result := IsReady(node) + Expect(result).To(BeFalse()) + }) + }) + + Context("when node ready condition is unknown", func() { + It("should return false when NodeReady condition status is Unknown", func() { + node := corev1.Node{ + ObjectMeta: metav1.ObjectMeta{Name: "test4"}, + Status: corev1.NodeStatus{ + Conditions: []corev1.NodeCondition{ + { + Type: corev1.NodeReady, + Status: corev1.ConditionUnknown, + }, + }, + }, + } + result := IsReady(node) + Expect(result).To(BeFalse()) + }) + }) + + Context("when one NodeReady condition is false among other conditions", func() { + It("should return false when NodeReady is False among other conditions", func() { + node := corev1.Node{ + ObjectMeta: metav1.ObjectMeta{Name: "test5"}, + Status: corev1.NodeStatus{ + Conditions: []corev1.NodeCondition{ + { + Type: corev1.NodeDiskPressure, + Status: corev1.ConditionFalse, + }, + { + Type: corev1.NodeReady, + Status: corev1.ConditionFalse, + }, + { + Type: corev1.NodePIDPressure, + Status: corev1.ConditionFalse, + }, + }, + }, + } + result := IsReady(node) + Expect(result).To(BeFalse()) + }) + }) })