k8s.io/kubernetes@v1.29.3/test/utils/conditions.go (about)

     1  /*
     2  Copyright 2016 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 utils
    18  
    19  import (
    20  	"fmt"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	podutil "k8s.io/kubernetes/pkg/api/v1/pod"
    24  )
    25  
    26  type ContainerFailures struct {
    27  	status   *v1.ContainerStateTerminated
    28  	Restarts int
    29  }
    30  
    31  // PodRunningReady checks whether pod p's phase is running and it has a ready
    32  // condition of status true.
    33  func PodRunningReady(p *v1.Pod) (bool, error) {
    34  	// Check the phase is running.
    35  	if p.Status.Phase != v1.PodRunning {
    36  		return false, fmt.Errorf("want pod '%s' on '%s' to be '%v' but was '%v'",
    37  			p.ObjectMeta.Name, p.Spec.NodeName, v1.PodRunning, p.Status.Phase)
    38  	}
    39  	// Check the ready condition is true.
    40  	if !podutil.IsPodReady(p) {
    41  		return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
    42  			p.ObjectMeta.Name, p.Spec.NodeName, v1.PodReady, v1.ConditionTrue, p.Status.Conditions)
    43  	}
    44  	return true, nil
    45  }
    46  
    47  func PodRunningReadyOrSucceeded(p *v1.Pod) (bool, error) {
    48  	// Check if the phase is succeeded.
    49  	if p.Status.Phase == v1.PodSucceeded {
    50  		return true, nil
    51  	}
    52  	return PodRunningReady(p)
    53  }
    54  
    55  func PodSucceeded(p *v1.Pod) (bool, error) {
    56  	return p.Status.Phase == v1.PodSucceeded, nil
    57  }
    58  
    59  // FailedContainers inspects all containers in a pod and returns failure
    60  // information for containers that have failed or been restarted.
    61  // A map is returned where the key is the containerID and the value is a
    62  // struct containing the restart and failure information
    63  func FailedContainers(pod *v1.Pod) map[string]ContainerFailures {
    64  	var state ContainerFailures
    65  	states := make(map[string]ContainerFailures)
    66  
    67  	statuses := pod.Status.ContainerStatuses
    68  	if len(statuses) == 0 {
    69  		return nil
    70  	}
    71  	for _, status := range statuses {
    72  		if status.State.Terminated != nil {
    73  			states[status.ContainerID] = ContainerFailures{status: status.State.Terminated}
    74  		} else if status.LastTerminationState.Terminated != nil {
    75  			states[status.ContainerID] = ContainerFailures{status: status.LastTerminationState.Terminated}
    76  		}
    77  		if status.RestartCount > 0 {
    78  			var ok bool
    79  			if state, ok = states[status.ContainerID]; !ok {
    80  				state = ContainerFailures{}
    81  			}
    82  			state.Restarts = int(status.RestartCount)
    83  			states[status.ContainerID] = state
    84  		}
    85  	}
    86  
    87  	return states
    88  }
    89  
    90  // TerminatedContainers inspects all containers in a pod and returns a map
    91  // of "container name: termination reason", for all currently terminated
    92  // containers.
    93  func TerminatedContainers(pod *v1.Pod) map[string]string {
    94  	states := make(map[string]string)
    95  	statuses := pod.Status.ContainerStatuses
    96  	if len(statuses) == 0 {
    97  		return states
    98  	}
    99  	for _, status := range statuses {
   100  		if status.State.Terminated != nil {
   101  			states[status.Name] = status.State.Terminated.Reason
   102  		}
   103  	}
   104  	return states
   105  }
   106  
   107  // PodNotReady checks whether pod p's has a ready condition of status false.
   108  func PodNotReady(p *v1.Pod) (bool, error) {
   109  	// Check the ready condition is false.
   110  	if podutil.IsPodReady(p) {
   111  		return false, fmt.Errorf("pod '%s' on '%s' didn't have condition {%v %v}; conditions: %v",
   112  			p.ObjectMeta.Name, p.Spec.NodeName, v1.PodReady, v1.ConditionFalse, p.Status.Conditions)
   113  	}
   114  	return true, nil
   115  }