k8s.io/kubernetes@v1.29.3/test/integration/volumescheduling/util.go (about)

     1  /*
     2  Copyright 2019 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 volumescheduling
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/util/wait"
    26  	clientset "k8s.io/client-go/kubernetes"
    27  	podutil "k8s.io/kubernetes/pkg/api/v1/pod"
    28  )
    29  
    30  // waitForPodToScheduleWithTimeout waits for a pod to get scheduled and returns
    31  // an error if it does not scheduled within the given timeout.
    32  func waitForPodToScheduleWithTimeout(cs clientset.Interface, pod *v1.Pod, timeout time.Duration) error {
    33  	return wait.Poll(100*time.Millisecond, timeout, podScheduled(cs, pod.Namespace, pod.Name))
    34  }
    35  
    36  // waitForPodToSchedule waits for a pod to get scheduled and returns an error if
    37  // it does not get scheduled within the timeout duration (30 seconds).
    38  func waitForPodToSchedule(cs clientset.Interface, pod *v1.Pod) error {
    39  	return waitForPodToScheduleWithTimeout(cs, pod, 30*time.Second)
    40  }
    41  
    42  // waitForPodUnscheduleWithTimeout waits for a pod to fail scheduling and returns
    43  // an error if it does not become unschedulable within the given timeout.
    44  func waitForPodUnschedulableWithTimeout(cs clientset.Interface, pod *v1.Pod, timeout time.Duration) error {
    45  	return wait.Poll(100*time.Millisecond, timeout, podUnschedulable(cs, pod.Namespace, pod.Name))
    46  }
    47  
    48  // waitForPodUnschedule waits for a pod to fail scheduling and returns
    49  // an error if it does not become unschedulable within the timeout duration (30 seconds).
    50  func waitForPodUnschedulable(cs clientset.Interface, pod *v1.Pod) error {
    51  	return waitForPodUnschedulableWithTimeout(cs, pod, 30*time.Second)
    52  }
    53  
    54  // podScheduled returns true if a node is assigned to the given pod.
    55  func podScheduled(c clientset.Interface, podNamespace, podName string) wait.ConditionFunc {
    56  	return func() (bool, error) {
    57  		pod, err := c.CoreV1().Pods(podNamespace).Get(context.TODO(), podName, metav1.GetOptions{})
    58  		if err != nil {
    59  			// This could be a connection error so we want to retry.
    60  			return false, nil
    61  		}
    62  		if pod.Spec.NodeName == "" {
    63  			return false, nil
    64  		}
    65  		return true, nil
    66  	}
    67  }
    68  
    69  // podUnschedulable returns a condition function that returns true if the given pod
    70  // gets unschedulable status.
    71  func podUnschedulable(c clientset.Interface, podNamespace, podName string) wait.ConditionFunc {
    72  	return func() (bool, error) {
    73  		pod, err := c.CoreV1().Pods(podNamespace).Get(context.TODO(), podName, metav1.GetOptions{})
    74  		if err != nil {
    75  			// This could be a connection error so we want to retry.
    76  			return false, nil
    77  		}
    78  		_, cond := podutil.GetPodCondition(&pod.Status, v1.PodScheduled)
    79  		return cond != nil && cond.Status == v1.ConditionFalse &&
    80  			cond.Reason == v1.PodReasonUnschedulable, nil
    81  	}
    82  }