forked from brigadecore/brigade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
90 lines (79 loc) · 2.61 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
package kube
import (
"bytes"
"fmt"
"io"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/pkg/api/v1"
"github.com/deis/brigade/pkg/brigade"
)
func (s *store) GetJob(id string) (*brigade.Job, error) {
labels := labels.Set{"heritage": "brigade"}
listOption := meta.ListOptions{LabelSelector: labels.AsSelector().String()}
pods, err := s.client.CoreV1().Pods(s.namespace).List(listOption)
if err != nil {
return nil, err
}
if len(pods.Items) < 1 {
return nil, fmt.Errorf("could not find job %s: no pod exists with label %s", id, labels.AsSelector().String())
}
for i := range pods.Items {
job := NewJobFromPod(pods.Items[i])
if job.ID == id {
return job, nil
}
}
return nil, fmt.Errorf("could not find job %s: no pod exists with that ID and label %s", id, labels.AsSelector().String())
}
func (s *store) GetBuildJobs(build *brigade.Build) ([]*brigade.Job, error) {
// Load the pods that ran as part of this build.
lo := meta.ListOptions{LabelSelector: fmt.Sprintf("heritage=brigade,component=job,commit=%s,project=%s", build.Commit, build.ProjectID)}
podList, err := s.client.CoreV1().Pods(s.namespace).List(lo)
if err != nil {
return nil, err
}
jobList := make([]*brigade.Job, len(podList.Items))
for i := range podList.Items {
jobList[i] = NewJobFromPod(podList.Items[i])
}
return jobList, nil
}
func (s *store) GetJobLogStream(job *brigade.Job) (io.ReadCloser, error) {
req := s.client.CoreV1().Pods(s.namespace).GetLogs(job.ID, &v1.PodLogOptions{})
readCloser, err := req.Stream()
if err != nil {
return nil, err
}
return readCloser, nil
}
func (s *store) GetJobLog(job *brigade.Job) (string, error) {
buf := new(bytes.Buffer)
r, err := s.GetJobLogStream(job)
if err != nil {
return "", err
}
defer r.Close()
io.Copy(buf, r)
return buf.String(), nil
}
// NewJobFromPod parses the pod object metadata and deserializes it into a Job.
func NewJobFromPod(pod v1.Pod) *brigade.Job {
job := &brigade.Job{
ID: pod.ObjectMeta.Name,
Name: pod.ObjectMeta.Labels["jobname"],
CreationTime: pod.ObjectMeta.CreationTimestamp.Time,
Image: pod.Spec.Containers[0].Image,
Status: brigade.JobStatus(pod.Status.Phase),
}
if (job.Status != brigade.JobPending) && (job.Status != brigade.JobUnknown) {
job.StartTime = pod.Status.StartTime.Time
}
if len(pod.Status.ContainerStatuses) > 0 {
if pod.Status.ContainerStatuses[0].State.Terminated != nil {
job.EndTime = pod.Status.ContainerStatuses[0].State.Terminated.FinishedAt.Time
job.ExitCode = pod.Status.ContainerStatuses[0].State.Terminated.ExitCode
}
}
return job
}