refactor node check
@@ -2,6 +2,7 @@ package nodeCheck
import (
"errors"
+ "os"
"time"
log "github.com/sirupsen/logrus"
@@ -11,14 +12,33 @@ import (
"k8s.io/kubernetes/pkg/api/v1/pod"
)
-// checkNode checks the node's age to make sure it's not less than three minutes old. If so, sleep for one minute
-// and check if kube proxy is ready and running on the node before running the check. This function requires the
-// khcheck to expose the pod's node name information using environment variables.
-func CheckNode(client *kubernetes.Clientset, nodeName string, now time.Time) {
+// WaitForKubeProxy checks the node's age to make sure it's not less than three minutes old. If so, it sleep for
+// one minute and checks if kube proxy is ready and running on the node before running the check.
+func WaitForKubeProxy(client *kubernetes.Clientset, namespace string) {
- node, err := client.CoreV1().Nodes().Get(nodeName, v1.GetOptions{})
+ now := time.Now()
+
+ podName, err := os.Hostname()
+ if err != nil {
+ // Just log the error and return since this check will not work if there's an error getting the podName
+ log.Errorln("Error getting hostname:", err)
+ return
+ }
+
+ log.Infoln("Getting pod:", podName, "in order to get its node information")
+ khPod, err := client.CoreV1().Pods(namespace).Get(podName, v1.GetOptions{})
+ if err != nil {
+ // Just log the error and return since this check will not work if there's an error getting the pod
+ log.Errorln("Error getting pod:", err)
+ return
+ }
+
+ log.Infoln("Pod is on node:", khPod.Spec.NodeName)
+
+ node, err := client.CoreV1().Nodes().Get(khPod.Spec.NodeName, v1.GetOptions{})
if err != nil {
- log.Errorln("Failed to get node:", nodeName, err)
+ // Just log the error and return since this check will not work if there's an error getting the node
+ log.Errorln("Failed to get node:", khPod.Spec.NodeName, err)
return
}
@@ -44,12 +64,10 @@ func CheckNode(client *kubernetes.Clientset, nodeName string, now time.Time) {
// Just log the error.
// TO DO: figure out how to address this. Should the check to skip this run and pass up an error instead?
// If kube-proxy isn't ready and running, there's definitely something wrong with the new node coming up.
- log.Errorln("Timed out checking if kube proxy is ready. Check node:", nodeName,
+ log.Errorln("Timed out checking if kube proxy is ready. Check node:", khPod.Spec.NodeName,
"Check may or may not complete successfully.")
}
- return
}
- return
}
// waitForKubeProxyReady fetches the kube proxy pod every 5 seconds until it's ready and running.
