k8s.io/kubernetes@v1.29.3/test/e2e/framework/job/rest.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 job
    18  
    19  import (
    20  	"context"
    21  
    22  	batchv1 "k8s.io/api/batch/v1"
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/labels"
    26  	clientset "k8s.io/client-go/kubernetes"
    27  )
    28  
    29  // GetJob uses c to get the Job in namespace ns named name. If the returned error is nil, the returned Job is valid.
    30  func GetJob(ctx context.Context, c clientset.Interface, ns, name string) (*batchv1.Job, error) {
    31  	return c.BatchV1().Jobs(ns).Get(ctx, name, metav1.GetOptions{})
    32  }
    33  
    34  // GetAllRunningJobPods returns a list of all running Pods belonging to a Job.
    35  func GetAllRunningJobPods(ctx context.Context, c clientset.Interface, ns, jobName string) ([]v1.Pod, error) {
    36  	if podList, err := GetJobPods(ctx, c, ns, jobName); err != nil {
    37  		return nil, err
    38  	} else {
    39  		pods := []v1.Pod{}
    40  		for _, pod := range podList.Items {
    41  			if pod.Status.Phase == v1.PodRunning {
    42  				pods = append(pods, pod)
    43  			}
    44  		}
    45  		return pods, nil
    46  	}
    47  }
    48  
    49  // GetJobPods returns a list of Pods belonging to a Job.
    50  func GetJobPods(ctx context.Context, c clientset.Interface, ns, jobName string) (*v1.PodList, error) {
    51  	label := labels.SelectorFromSet(labels.Set(map[string]string{JobSelectorKey: jobName}))
    52  	options := metav1.ListOptions{LabelSelector: label.String()}
    53  	return c.CoreV1().Pods(ns).List(ctx, options)
    54  }
    55  
    56  // CreateJob uses c to create job in namespace ns. If the returned error is nil, the returned Job is valid and has
    57  // been created.
    58  func CreateJob(ctx context.Context, c clientset.Interface, ns string, job *batchv1.Job) (*batchv1.Job, error) {
    59  	return c.BatchV1().Jobs(ns).Create(ctx, job, metav1.CreateOptions{})
    60  }
    61  
    62  // UpdateJob uses c to update a job in namespace ns. If the returned error is
    63  // nil, the returned Job is valid and has been updated.
    64  func UpdateJob(ctx context.Context, c clientset.Interface, ns string, job *batchv1.Job) (*batchv1.Job, error) {
    65  	return c.BatchV1().Jobs(ns).Update(ctx, job, metav1.UpdateOptions{})
    66  }