...
1
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
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
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
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
57
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
63
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 }
67
View as plain text