forked from MNMdiagnostics/nextflow-k8s-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creators.go
266 lines (244 loc) · 6.64 KB
/
creators.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
266
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controllers
import (
"bytes"
"errors"
"text/template"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
batchv1alpha1 "mnmdiagnostics/nextflow-k8s-operator/api/v1alpha1"
)
const (
defaultMountPath = "/workspace"
defaultNextflowImage = "nextflow/nextflow"
defaultNextflowVersion = "22.06.0-edge"
defaultNextflowHome = "/.nextflow"
configPath = "/tmp/nextflow.config"
statusRunning = "Running"
statusSucceeded = "Succeeded"
statusFailed = "Failed"
statusRelaunch = "Relaunch"
)
// Construct a Pod object for Nextflow
func makeNextflowPod(nfLaunch batchv1alpha1.NextflowLaunch, configMapName string) corev1.Pod {
spec := nfLaunch.Spec
// the main NF pod
pod := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: nfLaunch.Name + "-" + generateHash(8),
Namespace: nfLaunch.Namespace,
Labels: spec.Driver.Labels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Image: spec.Nextflow.Image + ":" + spec.Nextflow.Version,
Command: spec.Nextflow.Command,
Args: spec.Nextflow.Args,
Name: nfLaunch.Name + "-" + generateHash(8),
Env: spec.Driver.Env,
Resources: spec.Driver.Resources,
VolumeMounts: []corev1.VolumeMount{
{
Name: "nextflow-config",
MountPath: configPath,
SubPath: "nextflow.config",
},
{
Name: "nextflow-volume",
MountPath: spec.K8s["storageMountPath"],
},
},
}},
Volumes: []corev1.Volume{
{
Name: "nextflow-config",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: configMapName,
},
},
},
},
{
Name: "nextflow-volume",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: spec.K8s["storageClaimName"],
},
},
},
},
RestartPolicy: corev1.RestartPolicyNever,
Tolerations: spec.Driver.Tolerations,
},
}
// optionally attach a secret volume with scm data in it
if spec.Nextflow.ScmSecretName != "" {
pod.Spec.Containers[0].VolumeMounts = append(
pod.Spec.Containers[0].VolumeMounts,
corev1.VolumeMount{
Name: "nextflow-scm",
MountPath: spec.Nextflow.Home + "/scm",
SubPath: "scm",
},
)
pod.Spec.Volumes = append(
pod.Spec.Volumes,
corev1.Volume{
Name: "nextflow-scm",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: spec.Nextflow.ScmSecretName,
},
},
},
)
}
return pod
}
// Construct a Nextflow config file as a ConfigMap
func makeNextflowConfig(nfLaunch batchv1alpha1.NextflowLaunch) corev1.ConfigMap {
configTemplate, _ := template.New("config").
Funcs(template.FuncMap{
"stringsOrMap": stringsOrMap,
"escape": escape,
"groovyValue": groovyValue,
}).
Parse(`
process {
executor = 'k8s'
{{ if .Pod -}}
pod = [
{{ range $opt := .Pod -}}
[
{{- stringsOrMap $opt -}}
],
{{ end }}
]
{{- end }}
}
{{ if .K8s -}}
k8s {
{{- range $par, $value := .K8s }}
{{ escape $par }} = {{ groovyValue $value }}
{{- end }}
}
{{- end }}
{{ if .Params -}}
params {
{{- range $par, $value := .Params }}
{{ escape $par }} = {{ groovyValue $value }}
{{- end }}
}
{{- end }}
{{ if .Env -}}
env {
{{- range $par, $value := .Env }}
{{ escape $par }} = '{{ escape $value }}'
{{- end }}
}
{{- end }}`)
type Options struct {
K8s map[string]string
Params map[string]string
Env map[string]string
Pod []map[string]string
}
values := Options{
K8s: nfLaunch.Spec.K8s,
Params: nfLaunch.Spec.Params,
Env: nfLaunch.Spec.Env,
Pod: nfLaunch.Spec.Pod,
}
var config bytes.Buffer
configTemplate.Execute(&config, values)
return corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: nfLaunch.Name + "-nextflow-config-" + generateHash(8),
Namespace: nfLaunch.Namespace,
},
Data: map[string]string{
"nextflow.config": config.String(),
},
}
}
// Validate launch definition, return an error or nil
func validateLaunch(nfLaunch batchv1alpha1.NextflowLaunch) (batchv1alpha1.NextflowLaunch, error) {
spec := nfLaunch.Spec
// validation
if spec.Pipeline.Source == "" {
return nfLaunch, errors.New("spec.pipeline.source is required")
}
if keyIsEmpty(spec.K8s, "storageClaimName") {
return nfLaunch, errors.New("spec.k8s.storageClaimName is required")
}
// defaults for the essential settings
if keyIsEmpty(spec.K8s, "storageMountPath") {
spec.K8s["storageMountPath"] = defaultMountPath
}
if keyIsEmpty(spec.K8s, "launchDir") {
spec.K8s["launchDir"] = spec.K8s["storageMountPath"] + "/" + nfLaunch.Name
}
if keyIsEmpty(spec.K8s, "workDir") {
spec.K8s["workDir"] = spec.K8s["launchDir"] + "/work"
}
if spec.Nextflow.Image == "" {
spec.Nextflow.Image = defaultNextflowImage
}
if spec.Nextflow.Version == "" {
spec.Nextflow.Version = defaultNextflowVersion
}
profileArg := ""
profileName := ""
if spec.Profile != "" {
profileArg = "-profile"
profileName = escape(spec.Profile)
}
revisionArg := ""
revisionName := ""
if spec.Pipeline.Revision != "" {
revisionArg = "-r"
revisionName = escape(spec.Pipeline.Revision)
}
logArg := ""
logName := ""
if spec.Nextflow.LogPath != "" {
logArg = "-log"
logName = escape(spec.Nextflow.LogPath)
}
if len(spec.Nextflow.Command) == 0 {
spec.Nextflow.Command = []string{
"nextflow",
logArg, logName,
"run",
"-process.executor", "k8s",
"-c", configPath,
"-w", escape(spec.K8s["workDir"]),
profileArg, profileName,
revisionArg, revisionName,
escape(spec.Pipeline.Source),
}
}
if spec.Nextflow.Home == "" {
spec.Nextflow.Home = defaultNextflowHome
} else {
spec.Driver.Env = append(spec.Driver.Env, corev1.EnvVar{
Name: "NXF_HOME",
Value: spec.Nextflow.Home,
})
}
nfLaunch.Spec = spec
return nfLaunch, nil
}