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

     1  /*
     2  Copyright 2023 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  	batchv1 "k8s.io/api/batch/v1"
    26  	v1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	"k8s.io/apimachinery/pkg/labels"
    29  	"k8s.io/apimachinery/pkg/util/wait"
    30  )
    31  
    32  // CreateDeployment creates a new deployment
    33  func CreateDeployment(ctx *TestContext, name string, rep int32, img string, req v1.ResourceList) *appv1.Deployment {
    34  	deploymentName := "deployment.k8s.io"
    35  	d := &appv1.Deployment{
    36  		ObjectMeta: metav1.ObjectMeta{
    37  			Name:      name,
    38  			Namespace: ctx.Namespace,
    39  		},
    40  		Spec: appv1.DeploymentSpec{
    41  			Replicas: &rep,
    42  			Selector: &metav1.LabelSelector{
    43  				MatchLabels: map[string]string{
    44  					deploymentName: name,
    45  				},
    46  			},
    47  			Template: v1.PodTemplateSpec{
    48  				ObjectMeta: metav1.ObjectMeta{
    49  					Labels: map[string]string{deploymentName: name},
    50  				},
    51  				Spec: v1.PodSpec{
    52  					SchedulerName: "volcano",
    53  					RestartPolicy: v1.RestartPolicyAlways,
    54  					Containers: []v1.Container{
    55  						{
    56  							Image:           img,
    57  							Name:            name,
    58  							ImagePullPolicy: v1.PullIfNotPresent,
    59  							Resources: v1.ResourceRequirements{
    60  								Requests: req,
    61  							},
    62  						},
    63  					},
    64  				},
    65  			},
    66  		},
    67  	}
    68  
    69  	deployment, err := ctx.Kubeclient.AppsV1().Deployments(ctx.Namespace).Create(context.TODO(), d, metav1.CreateOptions{})
    70  	Expect(err).NotTo(HaveOccurred(), "failed to create deployment %s", name)
    71  
    72  	return deployment
    73  }
    74  
    75  func deploymentReady(ctx *TestContext, name string) wait.ConditionFunc {
    76  	return func() (bool, error) {
    77  		deployment, err := ctx.Kubeclient.AppsV1().Deployments(ctx.Namespace).Get(context.TODO(), name, metav1.GetOptions{})
    78  		Expect(err).NotTo(HaveOccurred(), "failed to get deployment %s in namespace %s", name, ctx.Namespace)
    79  
    80  		pods, err := ctx.Kubeclient.CoreV1().Pods(ctx.Namespace).List(context.TODO(), metav1.ListOptions{})
    81  		Expect(err).NotTo(HaveOccurred(), "failed to list pods in namespace %s", ctx.Namespace)
    82  
    83  		labelSelector := labels.SelectorFromSet(deployment.Spec.Selector.MatchLabels)
    84  
    85  		readyTaskNum := 0
    86  		for _, pod := range pods.Items {
    87  			if !labelSelector.Matches(labels.Set(pod.Labels)) {
    88  				continue
    89  			}
    90  			if pod.DeletionTimestamp != nil {
    91  				return false, nil
    92  			}
    93  			if pod.Status.Phase == v1.PodRunning || pod.Status.Phase == v1.PodSucceeded {
    94  				readyTaskNum++
    95  			}
    96  		}
    97  
    98  		return *(deployment.Spec.Replicas) == int32(readyTaskNum), nil
    99  	}
   100  }
   101  
   102  func WaitDeploymentReady(ctx *TestContext, name string) error {
   103  	return wait.Poll(100*time.Millisecond, FiveMinute, deploymentReady(ctx, name))
   104  }
   105  
   106  func DeleteDeployment(ctx *TestContext, name string) error {
   107  	foreground := metav1.DeletePropagationForeground
   108  	return ctx.Kubeclient.AppsV1().Deployments(ctx.Namespace).Delete(context.TODO(), name, metav1.DeleteOptions{
   109  		PropagationPolicy: &foreground,
   110  	})
   111  }
   112  
   113  // CreateSampleK8sJob creates a new k8s job
   114  func CreateSampleK8sJob(ctx *TestContext, name string, img string, req v1.ResourceList) *batchv1.Job {
   115  	k8sjobname := "job.k8s.io"
   116  	defaultTrue := true
   117  	j := &batchv1.Job{
   118  		ObjectMeta: metav1.ObjectMeta{
   119  			Name: name,
   120  		},
   121  		Spec: batchv1.JobSpec{
   122  			Selector: &metav1.LabelSelector{
   123  				MatchLabels: map[string]string{
   124  					k8sjobname: name,
   125  				},
   126  			},
   127  			ManualSelector: &defaultTrue,
   128  			Template: v1.PodTemplateSpec{
   129  				ObjectMeta: metav1.ObjectMeta{
   130  					Labels: map[string]string{k8sjobname: name},
   131  				},
   132  				Spec: v1.PodSpec{
   133  					SchedulerName: "volcano",
   134  					RestartPolicy: v1.RestartPolicyOnFailure,
   135  					Containers: []v1.Container{
   136  						{
   137  							Image:           img,
   138  							Name:            name,
   139  							Command:         []string{"/bin/sh", "-c", "sleep 10"},
   140  							ImagePullPolicy: v1.PullIfNotPresent,
   141  							Resources: v1.ResourceRequirements{
   142  								Requests: req,
   143  							},
   144  						},
   145  					},
   146  				},
   147  			},
   148  		},
   149  	}
   150  
   151  	jb, err := ctx.Kubeclient.BatchV1().Jobs(ctx.Namespace).Create(context.TODO(), j, metav1.CreateOptions{})
   152  	Expect(err).NotTo(HaveOccurred(), "failed to create k8sjob %s", name)
   153  
   154  	return jb
   155  }
   156  
   157  func k8sjobCompleted(ctx *TestContext, name string) wait.ConditionFunc {
   158  	return func() (bool, error) {
   159  		jb, err := ctx.Kubeclient.BatchV1().Jobs(ctx.Namespace).Get(context.TODO(), name, metav1.GetOptions{})
   160  		Expect(err).NotTo(HaveOccurred(), "failed to get k8sjob %s in namespace %s", name, ctx.Namespace)
   161  
   162  		pods, err := ctx.Kubeclient.CoreV1().Pods(ctx.Namespace).List(context.TODO(), metav1.ListOptions{})
   163  		Expect(err).NotTo(HaveOccurred(), "failed to list pods in namespace %s", ctx.Namespace)
   164  
   165  		labelSelector := labels.SelectorFromSet(jb.Spec.Selector.MatchLabels)
   166  
   167  		for _, pod := range pods.Items {
   168  			if !labelSelector.Matches(labels.Set(pod.Labels)) {
   169  				continue
   170  			}
   171  			if pod.Status.Phase == v1.PodSucceeded {
   172  				return true, nil
   173  			}
   174  		}
   175  
   176  		return false, nil
   177  	}
   178  }
   179  
   180  func Waitk8sJobCompleted(ctx *TestContext, name string) error {
   181  	return wait.Poll(100*time.Millisecond, FiveMinute, k8sjobCompleted(ctx, name))
   182  }
   183  
   184  func DeleteK8sJob(ctx *TestContext, name string) error {
   185  	foreground := metav1.DeletePropagationForeground
   186  	return ctx.Kubeclient.BatchV1().Jobs(ctx.Namespace).Delete(context.TODO(), name, metav1.DeleteOptions{
   187  		PropagationPolicy: &foreground,
   188  	})
   189  }