volcano.sh/volcano@v1.9.0/test/e2e/util/podgroup.go (about)

     1  /*
     2  Copyright 2021 The Volcano 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 util
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"time"
    24  
    25  	. "github.com/onsi/gomega"
    26  	v1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/util/wait"
    29  
    30  	schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    31  )
    32  
    33  // CreatePodGroup creates a PodGroup with the specified name in the namespace
    34  func CreatePodGroup(ctx *TestContext, pg string, namespace string) *schedulingv1beta1.PodGroup {
    35  	podGroup, err := ctx.Vcclient.SchedulingV1beta1().PodGroups(namespace).Create(context.TODO(), &schedulingv1beta1.PodGroup{
    36  		ObjectMeta: metav1.ObjectMeta{
    37  			Namespace: namespace,
    38  			Name:      pg,
    39  		},
    40  		Spec: schedulingv1beta1.PodGroupSpec{
    41  			MinResources: &v1.ResourceList{},
    42  		},
    43  	}, metav1.CreateOptions{})
    44  	Expect(err).NotTo(HaveOccurred(), "failed to create pod group %s", pg)
    45  	return podGroup
    46  }
    47  
    48  // DeletePodGroup deletes a PodGroup with the specified name in the namespace
    49  func DeletePodGroup(ctx *TestContext, pg string, namespace string) {
    50  	_, err := ctx.Vcclient.SchedulingV1beta1().PodGroups(namespace).Get(context.TODO(), pg, metav1.GetOptions{})
    51  	Expect(err).NotTo(HaveOccurred(), "failed to get pod group %s", pg)
    52  	err = ctx.Vcclient.SchedulingV1beta1().PodGroups(namespace).Delete(context.TODO(), pg, metav1.DeleteOptions{})
    53  	Expect(err).NotTo(HaveOccurred(), "failed to delete pod group %s", pg)
    54  }
    55  
    56  // WaitPodGroupPhase waits for the PodGroup to be the specified state
    57  func WaitPodGroupPhase(ctx *TestContext, podGroup *schedulingv1beta1.PodGroup, state schedulingv1beta1.PodGroupPhase) error {
    58  	var additionalError error
    59  	err := wait.Poll(100*time.Millisecond, FiveMinute, func() (bool, error) {
    60  		podGroup, err := ctx.Vcclient.SchedulingV1beta1().PodGroups(podGroup.Namespace).Get(context.TODO(), podGroup.Name, metav1.GetOptions{})
    61  		Expect(err).NotTo(HaveOccurred(), "failed to get pod group %s in namespace %s", podGroup.Name, podGroup.Namespace)
    62  		expected := podGroup.Status.Phase == state
    63  		if !expected {
    64  			additionalError = fmt.Errorf("expected podGroup '%s' phase in %s, actual got %s", podGroup.Name,
    65  				state, podGroup.Status.Phase)
    66  		}
    67  		return expected, nil
    68  	})
    69  	if err != nil && strings.Contains(err.Error(), TimeOutMessage) {
    70  		return fmt.Errorf("[Wait time out]: %s", additionalError)
    71  	}
    72  	return err
    73  }
    74  
    75  // PodGroupIsReady returns whether the status of PodGroup is ready
    76  func PodGroupIsReady(ctx *TestContext, namespace string) (bool, error) {
    77  	pgs, err := ctx.Vcclient.SchedulingV1beta1().PodGroups(namespace).List(context.TODO(), metav1.ListOptions{})
    78  	if err != nil {
    79  		return false, err
    80  	}
    81  	if pgs != nil && len(pgs.Items) == 0 {
    82  		return false, fmt.Errorf("pod group not found")
    83  	}
    84  
    85  	for _, pg := range pgs.Items {
    86  		if pg.Status.Phase != schedulingv1beta1.PodGroupPending {
    87  			return true, nil
    88  		}
    89  	}
    90  
    91  	return false, fmt.Errorf("pod group phase is Pending")
    92  }