This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
forked from CrunchyData/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
105 lines (86 loc) · 3.05 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package kubeapi
/*
Copyright 2017-2018 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
log "github.com/Sirupsen/logrus"
v1batch "k8s.io/api/batch/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// GetJobs gets a list of jobs using a label selector
func GetJobs(clientset *kubernetes.Clientset, selector, namespace string) (*v1batch.JobList, error) {
lo := meta_v1.ListOptions{LabelSelector: selector}
jobs, err := clientset.Batch().Jobs(namespace).List(lo)
if err != nil {
log.Error(err)
log.Error("error getting Job list selector[" + selector + "]")
}
return jobs, err
}
// GetJob gets a Job by name
func GetJob(clientset *kubernetes.Clientset, name, namespace string) (*v1batch.Job, bool) {
job, err := clientset.Batch().Jobs(namespace).Get(name, meta_v1.GetOptions{})
if kerrors.IsNotFound(err) {
log.Error(err)
return job, false
}
if err != nil {
log.Error(err)
return job, false
}
return job, true
}
// DeleteJob deletes a job
func DeleteJob(clientset *kubernetes.Clientset, jobName, namespace string) error {
log.Debug("deleting Job with Name=" + jobName + " in namespace " + namespace)
delOptions := meta_v1.DeleteOptions{}
var delProp meta_v1.DeletionPropagation
delProp = meta_v1.DeletePropagationForeground
delOptions.PropagationPolicy = &delProp
//delete the job
err := clientset.Batch().Jobs(namespace).Delete(jobName,
&delOptions)
if err != nil {
log.Error("error deleting Job " + jobName + err.Error())
return err
}
log.Info("deleted Job " + jobName)
return err
}
// CreateJob deletes a backup job
func CreateJob(clientset *kubernetes.Clientset, job *v1batch.Job, namespace string) error {
result, err := clientset.Batch().Jobs(namespace).Create(job)
if err != nil {
log.Error("error creating Job " + job.Name + err.Error())
return err
}
log.Info("created Job " + result.Name)
return err
}
// DeleteJobs deletes all jobs that match a selector
func DeleteJobs(clientset *kubernetes.Clientset, selector, namespace string) error {
log.Debug("deleting Jobs with selector=" + selector + " in namespace " + namespace)
//delete the job
delOptions := meta_v1.DeleteOptions{}
var delProp meta_v1.DeletionPropagation
delProp = meta_v1.DeletePropagationForeground
delOptions.PropagationPolicy = &delProp
lo := meta_v1.ListOptions{LabelSelector: selector}
err := clientset.Batch().Jobs(namespace).DeleteCollection(&delOptions, lo)
if err != nil {
log.Error("error deleting Jobs " + selector + err.Error())
return err
}
return err
}