-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjob.go
136 lines (124 loc) · 2.9 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
package k8s
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type JobLister interface {
List(ctx context.Context, opts metav1.ListOptions) (*batchv1.JobList, error)
}
type JobDeleter interface {
Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
}
func NewJob(
uid uuid.UUID,
endpoint string,
numPods,
numRequests,
numConcurrent uint,
headers []string,
) *batchv1.Job {
headersStr := strings.Join(headers, ",")
parallelism := int32(numPods)
completions := int32(numPods)
jb := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: jobName(uid, 1),
Labels: map[string]string{
"created-by": "megaboom",
},
},
Spec: batchv1.JobSpec{
Parallelism: ¶llelism,
Completions: &completions,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"created-by": "megaboom",
},
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: "megaboom-runner",
Image: "ghcr.io/arschles/hey:latest",
Command: []string{
"hey",
"-c",
strconv.Itoa(int(numConcurrent)),
"-n",
strconv.Itoa(int(numRequests)),
endpoint,
},
ImagePullPolicy: corev1.PullAlways,
Env: []corev1.EnvVar{
{
Name: "MEGABOOM_HEADERS",
Value: headersStr,
},
},
},
},
},
},
},
}
AddUIDToJob(jb, uid)
AddEndpointToJob(jb, endpoint)
return jb
}
func DeleteJob(
ctx context.Context,
cl JobDeleter,
uid uuid.UUID,
) error {
return cl.Delete(ctx, jobName(uid, 1), metav1.DeleteOptions{
GracePeriodSeconds: i64Ptr(0),
})
}
func jobName(uid uuid.UUID, jobNum int) string {
return fmt.Sprintf("megaboom-job-%s-%d", uid.String(), jobNum)
}
func i64Ptr(i int64) *int64 {
return &i
}
func AddUIDToJob(j *batchv1.Job, uid uuid.UUID) {
j.ObjectMeta.Labels["uid"] = uid.String()
j.Spec.Template.Spec.Containers[0].Env = append(
j.Spec.Template.Spec.Containers[0].Env,
corev1.EnvVar{
Name: "MEGABOOM_UID",
Value: uid.String(),
},
)
}
func GetUIDFromJob(j *batchv1.Job) (uuid.UUID, error) {
uid, ok := j.ObjectMeta.Labels["uid"]
if !ok {
return uuid.Nil, fmt.Errorf("job %s has no uid label", j.Name)
}
return uuid.Parse(uid)
}
func AddEndpointToJob(j *batchv1.Job, endpoint string) {
j.ObjectMeta.Labels["endpoint"] = endpoint
j.Spec.Template.Spec.Containers[0].Env = append(
j.Spec.Template.Spec.Containers[0].Env,
corev1.EnvVar{
Name: "MEGABOOM_ENDPOINT",
Value: endpoint,
},
)
}
func GetEndpointFromJob(j *batchv1.Job) (string, error) {
endpoint, ok := j.ObjectMeta.Labels["endpoint"]
if !ok {
return "", fmt.Errorf("job %s has no endpoint label", j.Name)
}
return endpoint, nil
}