volcano.sh/volcano@v1.9.0/test/e2e/util/replicaset.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 . "github.com/onsi/gomega" 24 appv1 "k8s.io/api/apps/v1" 25 v1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/labels" 28 "k8s.io/apimachinery/pkg/util/wait" 29 ) 30 31 // CreateReplicaSet creates a new replica set 32 func CreateReplicaSet(ctx *TestContext, name string, rep int32, img string, req v1.ResourceList) *appv1.ReplicaSet { 33 deploymentName := "deployment.k8s.io" 34 deployment := &appv1.ReplicaSet{ 35 ObjectMeta: metav1.ObjectMeta{ 36 Name: name, 37 Namespace: ctx.Namespace, 38 }, 39 Spec: appv1.ReplicaSetSpec{ 40 Replicas: &rep, 41 Selector: &metav1.LabelSelector{ 42 MatchLabels: map[string]string{ 43 deploymentName: name, 44 }, 45 }, 46 Template: v1.PodTemplateSpec{ 47 ObjectMeta: metav1.ObjectMeta{ 48 Labels: map[string]string{deploymentName: name}, 49 }, 50 Spec: v1.PodSpec{ 51 RestartPolicy: v1.RestartPolicyAlways, 52 Containers: []v1.Container{ 53 { 54 Image: img, 55 Name: name, 56 ImagePullPolicy: v1.PullIfNotPresent, 57 Resources: v1.ResourceRequirements{ 58 Requests: req, 59 }, 60 }, 61 }, 62 }, 63 }, 64 }, 65 } 66 67 deployment, err := ctx.Kubeclient.AppsV1().ReplicaSets(ctx.Namespace).Create(context.TODO(), deployment, metav1.CreateOptions{}) 68 Expect(err).NotTo(HaveOccurred(), "failed to create replica sets %s", name) 69 70 return deployment 71 } 72 73 func replicaSetReady(ctx *TestContext, name string) wait.ConditionFunc { 74 return func() (bool, error) { 75 deployment, err := ctx.Kubeclient.AppsV1().ReplicaSets(ctx.Namespace).Get(context.TODO(), name, metav1.GetOptions{}) 76 Expect(err).NotTo(HaveOccurred(), "failed to get replica set %s in namespace %s", name, ctx.Namespace) 77 78 pods, err := ctx.Kubeclient.CoreV1().Pods(ctx.Namespace).List(context.TODO(), metav1.ListOptions{}) 79 Expect(err).NotTo(HaveOccurred(), "failed to list pods in namespace %s", ctx.Namespace) 80 81 labelSelector := labels.SelectorFromSet(deployment.Spec.Selector.MatchLabels) 82 83 readyTaskNum := 0 84 for _, pod := range pods.Items { 85 if !labelSelector.Matches(labels.Set(pod.Labels)) { 86 continue 87 } 88 if pod.Status.Phase == v1.PodRunning || pod.Status.Phase == v1.PodSucceeded { 89 readyTaskNum++ 90 } 91 } 92 93 return *(deployment.Spec.Replicas) == int32(readyTaskNum), nil 94 } 95 } 96 97 func WaitReplicaSetReady(ctx *TestContext, name string) error { 98 return wait.Poll(100*time.Millisecond, FiveMinute, replicaSetReady(ctx, name)) 99 } 100 101 func DeleteReplicaSet(ctx *TestContext, name string) error { 102 foreground := metav1.DeletePropagationForeground 103 return ctx.Kubeclient.AppsV1().ReplicaSets(ctx.Namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ 104 PropagationPolicy: &foreground, 105 }) 106 }