github.com/vtuson/helm@v2.8.2+incompatible/pkg/helm/portforwarder/pod.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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 portforwarder
    18  
    19  import (
    20  	"k8s.io/api/core/v1"
    21  )
    22  
    23  // These functions are adapted from the "kubernetes" repository's file
    24  //
    25  //    kubernetes/pkg/api/v1/pod/util.go
    26  //
    27  // where they rely upon the API types specific to that repository. Here we recast them to operate
    28  // upon the type from the "client-go" repository instead.
    29  
    30  // isPodReady returns true if a pod is ready; false otherwise.
    31  func isPodReady(pod *v1.Pod) bool {
    32  	return isPodReadyConditionTrue(pod.Status)
    33  }
    34  
    35  // isPodReady retruns true if a pod is ready; false otherwise.
    36  func isPodReadyConditionTrue(status v1.PodStatus) bool {
    37  	condition := getPodReadyCondition(status)
    38  	return condition != nil && condition.Status == v1.ConditionTrue
    39  }
    40  
    41  // getPodReadyCondition extracts the pod ready condition from the given status and returns that.
    42  // Returns nil if the condition is not present.
    43  func getPodReadyCondition(status v1.PodStatus) *v1.PodCondition {
    44  	_, condition := getPodCondition(&status, v1.PodReady)
    45  	return condition
    46  }
    47  
    48  // getPodCondition extracts the provided 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 getPodCondition(status *v1.PodStatus, conditionType v1.PodConditionType) (int, *v1.PodCondition) {
    51  	if status == nil {
    52  		return -1, nil
    53  	}
    54  	for i := range status.Conditions {
    55  		if status.Conditions[i].Type == conditionType {
    56  			return i, &status.Conditions[i]
    57  		}
    58  	}
    59  	return -1, nil
    60  }