sigs.k8s.io/cluster-api@v1.6.3/controllers/noderefutil/util.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package noderefutil implements noderef utilities.
    18  package noderefutil
    19  
    20  import (
    21  	"time"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  )
    26  
    27  // IsNodeAvailable returns true if the node is ready and minReadySeconds have elapsed or is 0. False otherwise.
    28  func IsNodeAvailable(node *corev1.Node, minReadySeconds int32, now metav1.Time) bool {
    29  	if !IsNodeReady(node) {
    30  		return false
    31  	}
    32  
    33  	if minReadySeconds == 0 {
    34  		return true
    35  	}
    36  
    37  	minReadySecondsDuration := time.Duration(minReadySeconds) * time.Second
    38  	readyCondition := GetReadyCondition(&node.Status)
    39  
    40  	if !readyCondition.LastTransitionTime.IsZero() &&
    41  		readyCondition.LastTransitionTime.Add(minReadySecondsDuration).Before(now.Time) {
    42  		return true
    43  	}
    44  
    45  	return false
    46  }
    47  
    48  // GetReadyCondition extracts the ready condition from the given status and returns that.
    49  // Returns nil and -1 if the condition is not present, and the index of the located condition.
    50  func GetReadyCondition(status *corev1.NodeStatus) *corev1.NodeCondition {
    51  	if status == nil {
    52  		return nil
    53  	}
    54  	for i := range status.Conditions {
    55  		if status.Conditions[i].Type == corev1.NodeReady {
    56  			return &status.Conditions[i]
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  // IsNodeReady returns true if a node is ready; false otherwise.
    63  func IsNodeReady(node *corev1.Node) bool {
    64  	if node == nil {
    65  		return false
    66  	}
    67  	for _, c := range node.Status.Conditions {
    68  		if c.Type == corev1.NodeReady {
    69  			return c.Status == corev1.ConditionTrue
    70  		}
    71  	}
    72  	return false
    73  }
    74  
    75  // IsNodeUnreachable returns true if a node is unreachable.
    76  // Node is considered unreachable when its ready status is "Unknown".
    77  func IsNodeUnreachable(node *corev1.Node) bool {
    78  	if node == nil {
    79  		return false
    80  	}
    81  	for _, c := range node.Status.Conditions {
    82  		if c.Type == corev1.NodeReady {
    83  			return c.Status == corev1.ConditionUnknown
    84  		}
    85  	}
    86  	return false
    87  }