/* Copyright 2016 The Kubernetes Authors. Modifications Copyright 2022 Amazon.com, Inc. or its affiliates. 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. */ package utils import ( v1 "k8s.io/api/core/v1" ) func IsRunningAndReady(pod *v1.Pod) bool { return pod.Status.Phase == v1.PodRunning && IsPodReady(pod) } func IsTerminating(pod *v1.Pod) bool { return pod.DeletionTimestamp != nil } // Methods below were copied/adapted from k8s directly because k8s.io/kubernetes // is not intended to be consumed as a module: https://github.com/kubernetes/kubernetes/issues/79384 // // Source file: // https://github.com/kubernetes/kubernetes/blob/d7123a65248e25/pkg/api/v1/pod/util.go // IsPodReady returns true if a pod is ready; false otherwise. func IsPodReady(pod *v1.Pod) bool { return IsPodReadyConditionTrue(pod.Status) } // IsPodReadyConditionTrue returns true if a pod is ready; false otherwise. func IsPodReadyConditionTrue(status v1.PodStatus) bool { condition := GetPodReadyCondition(status) return condition != nil && condition.Status == v1.ConditionTrue } // GetPodReadyCondition extracts the pod ready condition from the given status and returns that. // Returns nil if the condition is not present. func GetPodReadyCondition(status v1.PodStatus) *v1.PodCondition { _, condition := GetPodCondition(&status, v1.PodReady) return condition } // GetPodCondition extracts the provided condition from the given status and returns that. // Returns nil and -1 if the condition is not present, and the index of the located condition. func GetPodCondition(status *v1.PodStatus, conditionType v1.PodConditionType) (int, *v1.PodCondition) { if status == nil { return -1, nil } return GetPodConditionFromList(status.Conditions, conditionType) } // GetPodConditionFromList extracts the provided condition from the given list of condition and // returns the index of the condition and the condition. Returns -1 and nil if the condition is not present. func GetPodConditionFromList(conditions []v1.PodCondition, conditionType v1.PodConditionType) (int, *v1.PodCondition) { if conditions == nil { return -1, nil } for i := range conditions { if conditions[i].Type == conditionType { return i, &conditions[i] } } return -1, nil }