-
Notifications
You must be signed in to change notification settings - Fork 2
/
job.go
48 lines (40 loc) · 1.29 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package kube
import (
"context"
"github.com/equinor/radix-operator/pkg/apis/utils/slice"
batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
// ListJobs Lists jobs from cache or from cluster
func (kubeutil *Kube) ListJobs(namespace string) ([]*batchv1.Job, error) {
if kubeutil.JobLister != nil {
jobs, err := kubeutil.JobLister.Jobs(namespace).List(labels.NewSelector())
if err != nil {
return nil, err
}
return jobs, nil
} else {
list, err := kubeutil.kubeClient.BatchV1().Jobs(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
jobs := slice.PointersOf(list.Items).([]*batchv1.Job)
return jobs, nil
}
}
// ListJobsWithSelector List jobs with selector
func (kubeutil *Kube) ListJobsWithSelector(namespace, labelSelectorString string) ([]*batchv1.Job, error) {
if kubeutil.JobLister != nil {
selector, err := labels.Parse(labelSelectorString)
if err != nil {
return nil, err
}
return kubeutil.JobLister.Jobs(namespace).List(selector)
}
list, err := kubeutil.kubeClient.BatchV1().Jobs(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelectorString})
if err != nil {
return nil, err
}
return slice.PointersOf(list.Items).([]*batchv1.Job), nil
}