volcano.sh/volcano@v1.9.0/test/e2e/util/queue.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 "time" 22 23 v1 "k8s.io/api/core/v1" 24 25 . "github.com/onsi/ginkgo/v2" 26 . "github.com/onsi/gomega" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/apimachinery/pkg/util/wait" 29 schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1" 30 ) 31 32 type QueueSpec struct { 33 Name string 34 Weight int32 35 GuaranteeResource v1.ResourceList 36 } 37 38 func CreateQueueWithQueueSpec(ctx *TestContext, queueSpec *QueueSpec) { 39 _, err := ctx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), queueSpec.Name, metav1.GetOptions{}) 40 if err != nil { 41 queue := &schedulingv1beta1.Queue{ 42 ObjectMeta: metav1.ObjectMeta{ 43 Name: queueSpec.Name, 44 }, 45 Spec: schedulingv1beta1.QueueSpec{ 46 Weight: queueSpec.Weight, 47 }, 48 } 49 if len(queueSpec.GuaranteeResource) != 0 { 50 queue.Spec.Guarantee.Resource = queueSpec.GuaranteeResource 51 } 52 _, err := ctx.Vcclient.SchedulingV1beta1().Queues().Create(context.TODO(), queue, metav1.CreateOptions{}) 53 Expect(err).NotTo(HaveOccurred(), "failed to create queue %s", queueSpec.Name) 54 } 55 56 // wait for queue state turns to be open 57 time.Sleep(3 * time.Second) 58 } 59 60 // CreateQueue creates Queue with the specified name 61 func CreateQueue(ctx *TestContext, q string, deservedResource v1.ResourceList) { 62 _, err := ctx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), q, metav1.GetOptions{}) 63 if err != nil { 64 _, err := ctx.Vcclient.SchedulingV1beta1().Queues().Create(context.TODO(), &schedulingv1beta1.Queue{ 65 ObjectMeta: metav1.ObjectMeta{ 66 Name: q, 67 }, 68 Spec: schedulingv1beta1.QueueSpec{ 69 Weight: 1, 70 Deserved: deservedResource, 71 }, 72 }, metav1.CreateOptions{}) 73 Expect(err).NotTo(HaveOccurred(), "failed to create queue %s", q) 74 } 75 } 76 77 // CreateQueues create Queues specified in the test context 78 func CreateQueues(ctx *TestContext) { 79 By("Creating Queues") 80 81 for _, queue := range ctx.Queues { 82 CreateQueue(ctx, queue, ctx.DeservedResource[queue]) 83 } 84 85 // wait for all queues state open 86 time.Sleep(3 * time.Second) 87 } 88 89 // DeleteQueue deletes Queue with the specified name 90 func DeleteQueue(ctx *TestContext, q string) { 91 foreground := metav1.DeletePropagationForeground 92 queue, err := ctx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), q, metav1.GetOptions{}) 93 Expect(err).NotTo(HaveOccurred(), "failed to get queue %s", q) 94 95 queue.Status.State = schedulingv1beta1.QueueStateClosed 96 _, err = ctx.Vcclient.SchedulingV1beta1().Queues().UpdateStatus(context.TODO(), queue, metav1.UpdateOptions{}) 97 Expect(err).NotTo(HaveOccurred(), "failed to update status of queue %s", q) 98 err = wait.Poll(100*time.Millisecond, FiveMinute, queueClosed(ctx, q)) 99 Expect(err).NotTo(HaveOccurred(), "failed to wait queue %s closed", q) 100 101 err = ctx.Vcclient.SchedulingV1beta1().Queues().Delete(context.TODO(), q, 102 metav1.DeleteOptions{ 103 PropagationPolicy: &foreground, 104 }) 105 Expect(err).NotTo(HaveOccurred(), "failed to delete queue %s", q) 106 } 107 108 // deleteQueues deletes Queues specified in the test context 109 func deleteQueues(ctx *TestContext) { 110 for _, q := range ctx.Queues { 111 DeleteQueue(ctx, q) 112 } 113 } 114 115 // SeyQueueReclaimable sets the Queue to be reclaimable 116 func SetQueueReclaimable(ctx *TestContext, queues []string, reclaimable bool) { 117 By("Setting Queue reclaimable") 118 119 for _, q := range queues { 120 queue, err := ctx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), q, metav1.GetOptions{}) 121 Expect(err).NotTo(HaveOccurred(), "failed to get queue %s", q) 122 123 queue.Spec.Reclaimable = &reclaimable 124 _, err = ctx.Vcclient.SchedulingV1beta1().Queues().Update(context.TODO(), queue, metav1.UpdateOptions{}) 125 Expect(err).NotTo(HaveOccurred(), "failed to update queue %s", q) 126 } 127 } 128 129 func WaitQueueStatus(condition func() (bool, error)) error { 130 return wait.Poll(100*time.Millisecond, TenMinute, condition) 131 } 132 133 // queueClosed returns whether the Queue is closed 134 func queueClosed(ctx *TestContext, name string) wait.ConditionFunc { 135 return func() (bool, error) { 136 queue, err := ctx.Vcclient.SchedulingV1beta1().Queues().Get(context.TODO(), name, metav1.GetOptions{}) 137 if err != nil { 138 return false, err 139 } 140 141 if queue.Status.State != schedulingv1beta1.QueueStateClosed { 142 return false, nil 143 } 144 145 return true, nil 146 } 147 }