-
Notifications
You must be signed in to change notification settings - Fork 375
/
job.go
265 lines (240 loc) · 7.96 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package logic
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"strings"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/pipeline/conf"
"github.com/erda-project/erda/modules/pipeline/pkg/task_uuid"
"github.com/erda-project/erda/modules/pipeline/spec"
"github.com/erda-project/erda/pkg/discover"
"github.com/erda-project/erda/pkg/http/httpclient"
"github.com/erda-project/erda/pkg/parser/diceyml"
"github.com/erda-project/erda/pkg/parser/pipelineyml/pipelineymlv1"
"github.com/erda-project/erda/pkg/strutil"
)
// ParseJobHostBindTemplate Analyze the hostPath template and convert it to the cluster info value
func ParseJobHostBindTemplate(hostPath string, clusterInfo map[string]string) (string, error) {
var b bytes.Buffer
if hostPath == "" {
return "", errors.New("hostPath is empty")
}
t, err := template.New("jobBind").
Option("missingkey=error").
Parse(hostPath)
if err != nil {
return "", errors.Errorf("failed to parse bind, hostPath: %s, (%v)", hostPath, err)
}
err = t.Execute(&b, &clusterInfo)
if err != nil {
return "", errors.Errorf("failed to execute bind, hostPath: %s, (%v)", hostPath, err)
}
return b.String(), nil
}
// GenerateK8SVolumes According to job configuration, production volume related configuration
func GenerateK8SVolumes(job *apistructs.JobFromUser) ([]corev1.Volume, []corev1.VolumeMount, []*corev1.PersistentVolumeClaim) {
vols := []corev1.Volume{}
volMounts := []corev1.VolumeMount{}
pvcs := []*corev1.PersistentVolumeClaim{}
for i, v := range job.Volumes {
var pvcid string
if v.ID == nil { // if ID == nil, we need to create a new pvc
sc := WhichStorageClass(v.Storage)
id := fmt.Sprintf("%s-%s-%d", job.Namespace, job.Name, i)
pvcs = append(pvcs, &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: id,
Namespace: job.Namespace,
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: resource.MustParse("10Gi"),
},
},
StorageClassName: &sc,
},
})
pvcid = id
} else {
pvcs = append(pvcs, nil) // append a placeholder
pvcid = *v.ID
}
volName := fmt.Sprintf("vol-%d", i)
vols = append(vols, corev1.Volume{
Name: volName,
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: pvcid,
},
},
})
volMounts = append(volMounts, corev1.VolumeMount{
Name: volName,
MountPath: v.Path,
})
}
return vols, volMounts, pvcs
}
func WhichStorageClass(tp string) string {
switch tp {
case "local":
return "dice-local-volume"
case "nfs":
return "dice-nfs-volume"
default:
return "dice-local-volume"
}
}
func MakeJobName(task *spec.PipelineTask) string {
return strutil.Concat(task.Extra.Namespace, ".", task_uuid.MakeJobID(task))
}
func TransferToSchedulerJob(task *spec.PipelineTask) (job apistructs.JobFromUser, err error) {
defer func() {
if r := recover(); r != nil {
err = errors.Errorf("%v", r)
}
}()
return apistructs.JobFromUser{
Name: task_uuid.MakeJobID(task),
Kind: func() string {
switch task.Type {
case string(pipelineymlv1.RES_TYPE_FLINK):
return string(apistructs.Flink)
case string(pipelineymlv1.RES_TYPE_SPARK):
return string(apistructs.Spark)
default:
return ""
}
}(),
Namespace: task.Extra.Namespace,
ClusterName: func() string {
if len(task.Extra.ClusterName) == 0 {
panic(errors.New("missing cluster name in pipeline task"))
}
return task.Extra.ClusterName
}(),
Image: task.Extra.Image,
Cmd: strings.Join(append([]string{task.Extra.Cmd}, task.Extra.CmdArgs...), " "),
CPU: task.Extra.RuntimeResource.CPU,
Memory: task.Extra.RuntimeResource.Memory,
Binds: task.Extra.Binds,
Volumes: MakeVolume(task),
PreFetcher: task.Extra.PreFetcher,
Env: task.Extra.PublicEnvs,
Labels: task.Extra.Labels,
// flink/spark
Resource: task.Extra.FlinkSparkConf.JarResource,
MainClass: task.Extra.FlinkSparkConf.MainClass,
MainArgs: task.Extra.FlinkSparkConf.MainArgs,
// 重试不依赖 scheduler,由 pipeline engine 自己实现,保证所有 action executor 均适用
Params: task.Extra.Action.Params,
}, nil
}
func MakeVolume(task *spec.PipelineTask) []diceyml.Volume {
diceVolumes := make([]diceyml.Volume, 0)
for _, vo := range task.Extra.Volumes {
if vo.Type == string(spec.StoreTypeDiceVolumeFake) || vo.Type == string(spec.StoreTypeDiceCacheNFS) {
// fake volume,没有实际挂载行为,不传给scheduler
continue
}
diceVolume := diceyml.Volume{
Path: vo.Value,
Storage: func() string {
switch vo.Type {
case string(spec.StoreTypeDiceVolumeNFS):
return "nfs"
case string(spec.StoreTypeDiceVolumeLocal):
return "local"
default:
panic(errors.Errorf("%q has not supported volume type: %s", vo.Name, vo.Type))
}
}(),
}
if vo.Labels != nil {
if id, ok := vo.Labels["ID"]; ok {
diceVolume.ID = &id
goto AppendDiceVolume
}
}
// labels == nil or labels["ID"] not exist
// 如果 id 不存在,说明上一次没有生成 volume,并且是 optional 的,则不创建 diceVolume
if vo.Optional {
continue
}
AppendDiceVolume:
diceVolumes = append(diceVolumes, diceVolume)
}
return diceVolumes
}
func GetBigDataConf(task *spec.PipelineTask) (apistructs.BigdataConf, error) {
conf := apistructs.BigdataConf{
BigdataMetadata: apistructs.BigdataMetadata{
Name: task.Extra.UUID,
Namespace: task.Extra.Namespace,
},
Spec: apistructs.BigdataSpec{},
}
value, ok := task.Extra.Action.Params["bigDataConf"]
if !ok {
return conf, fmt.Errorf("missing big data conf from task: %s", task.Extra.UUID)
}
if err := json.Unmarshal([]byte(value.(string)), &conf.Spec); err != nil {
return conf, fmt.Errorf("unmarshal bigdata config error: %s", err.Error())
}
return conf, nil
}
func GetCLusterInfo(clusterName string) (map[string]string, error) {
var clusterInfoRes struct {
Data map[string]string `json:"data"`
}
var body bytes.Buffer
resp, err := httpclient.New().Get(discover.Scheduler()).
Path(strutil.Concat("/api/clusterinfo/", clusterName)).
Do().Body(&body)
if err != nil {
return nil, errors.Errorf("get cluster info failed, err: %v", err)
}
statusCode := resp.StatusCode()
respBody := body.String()
if err := json.Unmarshal([]byte(respBody), &clusterInfoRes); err != nil {
return nil, errors.Errorf("get cluster info failed, statueCode: %d, err: %v", statusCode, err)
}
return clusterInfoRes.Data, nil
}
// GetPullImagePolicy specify Image Pull Policy with IfNotPresent,Always,Never
func GetPullImagePolicy() corev1.PullPolicy {
var imagePullPolicy corev1.PullPolicy
switch corev1.PullPolicy(conf.SpecifyImagePullPolicy()) {
case corev1.PullAlways:
imagePullPolicy = corev1.PullAlways
case corev1.PullNever:
imagePullPolicy = corev1.PullNever
default:
imagePullPolicy = corev1.PullIfNotPresent
}
return imagePullPolicy
}
// MakeJobLabelSelector return LabelSelector like job-name=pipeline-1.pipeline-task-1
func MakeJobLabelSelector(task *spec.PipelineTask) string {
return fmt.Sprintf("job-name=%s", MakeJobName(task))
}