node_ready.go 2.8 KB
Newer Older
M
Medya Gh 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2020 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

17
// Package kverify verifies a running Kubernetes cluster is healthy
M
Medya Gh 已提交
18 19 20
package kverify

import (
P
Predrag Rogic 已提交
21
	"context"
M
Medya Gh 已提交
22 23 24
	"fmt"
	"time"

25
	core "k8s.io/api/core/v1"
M
Medya Gh 已提交
26 27 28
	meta "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/kubernetes"
29
	"k8s.io/klog/v2"
M
Medya Gh 已提交
30 31 32
	kconst "k8s.io/kubernetes/cmd/kubeadm/app/constants"
)

33 34 35
// WaitNodeCondition waits for specified condition of node name.
func WaitNodeCondition(cs *kubernetes.Clientset, name string, condition core.NodeConditionType, timeout time.Duration) error {
	klog.Infof("waiting up to %v for node %q to be %q ...", timeout, name, condition)
M
Medya Gh 已提交
36 37
	start := time.Now()
	defer func() {
38
		klog.Infof("duration metric: took %v waiting for node %q to be %q ...", time.Since(start), name, condition)
M
Medya Gh 已提交
39
	}()
40 41 42

	lap := time.Now()
	checkCondition := func() (bool, error) {
M
Medya Gh 已提交
43
		if time.Since(start) > timeout {
44
			return false, fmt.Errorf("timed out waiting %v for node %q to be %q (will not retry!)", timeout, name, condition)
M
Medya Gh 已提交
45 46
		}

47 48 49 50 51 52 53 54
		status, reason := nodeConditionStatus(cs, name, condition)
		if status == core.ConditionTrue {
			klog.Info(reason)
			return true, nil
		}
		if status == core.ConditionUnknown {
			klog.Info(reason)
			return false, fmt.Errorf(reason)
M
Medya Gh 已提交
55
		}
56 57 58 59 60 61
		// reduce log spam
		if time.Since(lap) > (2 * time.Second) {
			klog.Info(reason)
			lap = time.Now()
		}
		return false, nil
M
Medya Gh 已提交
62
	}
63 64
	if err := wait.PollImmediate(kconst.APICallRetryInterval, kconst.DefaultControlPlaneTimeout, checkCondition); err != nil {
		return fmt.Errorf("waitNodeCondition: %w", err)
M
Medya Gh 已提交
65
	}
66

M
Medya Gh 已提交
67 68
	return nil
}
69 70 71

// nodeConditionStatus returns if node is in specified condition and verbose reason.
func nodeConditionStatus(cs *kubernetes.Clientset, name string, condition core.NodeConditionType) (status core.ConditionStatus, reason string) {
P
Predrag Rogic 已提交
72
	node, err := cs.CoreV1().Nodes().Get(context.Background(), name, meta.GetOptions{})
73 74 75 76 77 78 79 80 81 82 83 84 85
	if err != nil {
		return core.ConditionUnknown, fmt.Sprintf("error getting node %q: %v", name, err)
	}

	for _, c := range node.Status.Conditions {
		if c.Type == condition {
			return c.Status, fmt.Sprintf("node %q has status %q:%q", node.Name, condition, c.Status)
		}
	}

	// assume transient condition
	return core.ConditionFalse, fmt.Sprintf("node %q doesn't have %q status: %+v", node.Name, condition, node.Status)
}