forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
173 lines (156 loc) · 4.37 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package job
import (
"context"
"encoding/json"
"fmt"
"log"
"os/exec"
"regexp"
"time"
"github.com/Azure/acs-engine/test/e2e/kubernetes/pod"
"github.com/Azure/acs-engine/test/e2e/kubernetes/util"
)
// List is a container that holds all jobs returned from doing a kubectl get jobs
type List struct {
Jobs []Job `json:"items"`
}
// Job is used to parse data from kubectl get jobs
type Job struct {
Metadata pod.Metadata `json:"metadata"`
Spec Spec `json:"spec"`
Status Status `json:"status"`
}
// Spec holds job spec metadata
type Spec struct {
Completions int `json:"completions"`
Parallelism int `json:"parallelism"`
}
// Status holds job status information
type Status struct {
Active int `json:"active"`
Succeeded int `json:"succeeded"`
}
// CreateJobFromFile will create a Job from file with a name
func CreateJobFromFile(filename, name, namespace string) (*Job, error) {
cmd := exec.Command("kubectl", "create", "-f", filename)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error trying to create Job %s:%s\n", name, string(out))
return nil, err
}
job, err := Get(name, namespace)
if err != nil {
log.Printf("Error while trying to fetch Job %s:%s\n", name, err)
return nil, err
}
return job, nil
}
// GetAll will return all jobs in a given namespace
func GetAll(namespace string) (*List, error) {
cmd := exec.Command("kubectl", "get", "jobs", "-n", namespace, "-o", "json")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
jl := List{}
err = json.Unmarshal(out, &jl)
if err != nil {
log.Printf("Error unmarshalling jobs json:%s\n", err)
return nil, err
}
return &jl, nil
}
// Get will return a job with a given name and namespace
func Get(jobName, namespace string) (*Job, error) {
cmd := exec.Command("kubectl", "get", "jobs", jobName, "-n", namespace, "-o", "json")
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
j := Job{}
err = json.Unmarshal(out, &j)
if err != nil {
log.Printf("Error unmarshalling jobs json:%s\n", err)
return nil, err
}
return &j, nil
}
// AreAllJobsCompleted will return true if all jobs with a common prefix in a given namespace are in a Completed State
func AreAllJobsCompleted(jobPrefix, namespace string) (bool, error) {
jl, err := GetAll(namespace)
if err != nil {
return false, err
}
var status []bool
for _, job := range jl.Jobs {
matched, err := regexp.MatchString(jobPrefix, job.Metadata.Name)
if err != nil {
log.Printf("Error trying to match job name:%s\n", err)
return false, err
}
if matched {
if job.Status.Active > 0 {
status = append(status, false)
} else if job.Status.Succeeded == job.Spec.Completions {
status = append(status, true)
}
}
}
if len(status) == 0 {
return false, nil
}
for _, s := range status {
if !s {
return false, nil
}
}
return true, nil
}
// WaitOnReady is used when you dont have a handle on a job but want to wait until its in a Succeeded state.
func WaitOnReady(jobPrefix, namespace string, sleep, duration time.Duration) (bool, error) {
readyCh := make(chan bool, 1)
errCh := make(chan error)
ctx, cancel := context.WithTimeout(context.Background(), duration)
defer cancel()
go func() {
for {
select {
case <-ctx.Done():
errCh <- fmt.Errorf("Timeout exceeded (%s) while waiting for Jobs (%s) to complete in namespace (%s)", duration.String(), jobPrefix, namespace)
default:
ready, _ := AreAllJobsCompleted(jobPrefix, namespace)
if ready {
readyCh <- true
} else {
time.Sleep(sleep)
}
}
}
}()
for {
select {
case err := <-errCh:
return false, err
case ready := <-readyCh:
return ready, nil
}
}
}
// WaitOnReady will call the static method WaitOnReady passing in p.Metadata.Name and p.Metadata.Namespace
func (j *Job) WaitOnReady(sleep, duration time.Duration) (bool, error) {
return WaitOnReady(j.Metadata.Name, j.Metadata.Namespace, sleep, duration)
}
// Delete will delete a Job in a given namespace
func (j *Job) Delete() error {
cmd := exec.Command("kubectl", "delete", "job", "-n", j.Metadata.Namespace, j.Metadata.Name)
util.PrintCommand(cmd)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Error while trying to delete Job %s in namespace %s:%s\n", j.Metadata.Namespace, j.Metadata.Name, string(out))
return err
}
return nil
}