-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkubernetes.go
178 lines (145 loc) · 3.79 KB
/
kubernetes.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
174
175
176
177
178
package worker
import (
"bytes"
"context"
"fmt"
"io"
"text/template"
"time"
"github.com/ohsu-comp-bio/funnel/tes"
v1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
batchv1 "k8s.io/client-go/kubernetes/typed/batch/v1"
"k8s.io/client-go/rest"
)
// KubernetesCommand is responsible for configuring and running a task in a Kubernetes cluster.
type KubernetesCommand struct {
TaskId string
JobId int
StdinFile string
TaskTemplate string
Namespace string
Resources *tes.Resources
Command
}
// Creates a new Kuberntes Job which will run the task.
func (kcmd KubernetesCommand) Run(ctx context.Context) error {
var taskId = kcmd.TaskId
tpl, err := template.New(taskId).Parse(kcmd.TaskTemplate)
if err != nil {
return err
}
var command = kcmd.ShellCommand
if kcmd.StdinFile != "" {
command = append(command, "<", kcmd.StdinFile)
}
var buf bytes.Buffer
err = tpl.Execute(&buf, map[string]interface{}{
"TaskId": taskId,
"JobId": kcmd.JobId,
"Namespace": kcmd.Namespace,
"Image": kcmd.Image,
"Command": command,
"Workdir": kcmd.Workdir,
"Volumes": kcmd.Volumes,
"Cpus": kcmd.Resources.CpuCores,
"RamGb": kcmd.Resources.RamGb,
"DiskGb": kcmd.Resources.DiskGb,
})
if err != nil {
return err
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode(buf.Bytes(), nil, nil)
if err != nil {
return err
}
job, ok := obj.(*v1.Job)
if !ok {
return err
}
clientset, err := getKubernetesClientset()
if err != nil {
return err
}
var client = clientset.BatchV1().Jobs(kcmd.Namespace)
_, err = client.Create(ctx, job, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("creating job: %v", err)
}
waitForJobFinish(ctx, client, metav1.ListOptions{LabelSelector: fmt.Sprintf("job-name=%s-%d", taskId, kcmd.JobId)})
pods, err := clientset.CoreV1().Pods(kcmd.Namespace).List(ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("job-name=%s-%d", taskId, kcmd.JobId)})
if err != nil {
return err
}
for _, v := range pods.Items {
req := clientset.CoreV1().Pods(kcmd.Namespace).GetLogs(v.Name, &corev1.PodLogOptions{})
podLogs, err := req.Stream(ctx)
if err != nil {
return err
}
defer podLogs.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
return err
}
var bytes = buf.Bytes()
_, err = kcmd.Stdout.Write(bytes)
if err != nil {
return err
}
}
return nil
}
// Deletes the job running the task.
func (kcmd KubernetesCommand) Stop() error {
clientset, err := getKubernetesClientset()
if err != nil {
return err
}
jobName := fmt.Sprintf("%s-%d", kcmd.TaskId, kcmd.JobId)
backgroundDeletion := metav1.DeletePropagationBackground
err = clientset.BatchV1().Jobs(kcmd.Namespace).Delete(context.TODO(), jobName, metav1.DeleteOptions{
PropagationPolicy: &backgroundDeletion,
})
if err != nil {
return fmt.Errorf("deleting job: %v", err)
}
return nil
}
// Waits until the job finishes
func waitForJobFinish(ctx context.Context, client batchv1.JobInterface, listOptions metav1.ListOptions) {
ticker := time.NewTicker(10 * time.Second)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
jobs, err := client.List(ctx, listOptions)
if err != nil {
return
}
if len(jobs.Items) == 0 {
// Should not happen
return
}
// There should be always only one job
job := jobs.Items[0]
if job.Status.Succeeded > 0 || job.Status.Failed > 0 {
return
}
}
}
}
func getKubernetesClientset() (*kubernetes.Clientset, error) {
kubeconfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(kubeconfig)
return clientset, err
}