forked from armadaproject/jenkins-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmadaPodTemplateStep.java
More file actions
executable file
·543 lines (439 loc) · 15.1 KB
/
ArmadaPodTemplateStep.java
File metadata and controls
executable file
·543 lines (439 loc) · 15.1 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package io.armadaproject.jenkins.plugin.pipeline;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.Extension;
import hudson.Util;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.slaves.Cloud;
import hudson.util.ListBoxModel;
import io.armadaproject.jenkins.plugin.ContainerTemplate;
import io.armadaproject.jenkins.plugin.ArmadaCloud;
import io.armadaproject.jenkins.plugin.PodAnnotation;
import io.armadaproject.jenkins.plugin.PodTemplate;
import io.armadaproject.jenkins.plugin.pod.retention.PodRetention;
import io.armadaproject.jenkins.plugin.pod.yaml.YamlMergeStrategy;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import io.armadaproject.jenkins.plugin.model.TemplateEnvVar;
import io.armadaproject.jenkins.plugin.volumes.PodVolume;
import io.armadaproject.jenkins.plugin.volumes.workspace.WorkspaceVolume;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
public class ArmadaPodTemplateStep extends Step implements Serializable {
private static final long serialVersionUID = 5588861066775717487L;
@CheckForNull
private String cloud;
@CheckForNull
private String inheritFrom;
@CheckForNull
private String label;
@CheckForNull
private String name;
@CheckForNull
private String namespace;
private List<ContainerTemplate> containers = new ArrayList<>();
private List<TemplateEnvVar> envVars = new ArrayList<>();
private List<PodVolume> volumes = new ArrayList<PodVolume>();
@CheckForNull
private WorkspaceVolume workspaceVolume;
private List<PodAnnotation> annotations = new ArrayList<>();
private List<String> imagePullSecrets = new ArrayList<>();
private Integer instanceCap = Integer.MAX_VALUE;
private int idleMinutes;
private int slaveConnectTimeout = PodTemplate.DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT;
private int activeDeadlineSeconds;
private Boolean hostNetwork;
@CheckForNull
private String serviceAccount;
@CheckForNull
private String schedulerName;
@CheckForNull
private String nodeSelector;
private Node.Mode nodeUsageMode = Node.Mode.EXCLUSIVE;
private String workingDir = ContainerTemplate.DEFAULT_WORKING_DIR;
@CheckForNull
private String yaml;
private YamlMergeStrategy yamlMergeStrategy;
@CheckForNull
private Boolean inheritYamlMergeStrategy;
@CheckForNull
private PodRetention podRetention;
private Boolean showRawYaml;
@CheckForNull
private String runAsUser;
@CheckForNull
private String runAsGroup;
@CheckForNull
private String supplementalGroups;
@CheckForNull
private String agentContainer;
private boolean agentInjection;
@DataBoundConstructor
public ArmadaPodTemplateStep() {}
public String getLabel() {
return label;
}
@DataBoundSetter
public void setLabel(@CheckForNull String label) {
this.label = Util.fixEmpty(label);
}
@CheckForNull
public String getName() {
return name;
}
@DataBoundSetter
public void setName(@CheckForNull String name) {
this.name = Util.fixEmpty(name);
}
@CheckForNull
public String getNamespace() {
return namespace;
}
@DataBoundSetter
public void setNamespace(@CheckForNull String namespace) {
this.namespace = Util.fixEmpty(namespace);
}
@CheckForNull
public String getCloud() {
return cloud;
}
@DataBoundSetter
public void setCloud(@CheckForNull String cloud) {
this.cloud = Util.fixEmpty(cloud);
}
@CheckForNull
public String getInheritFrom() {
return inheritFrom;
}
@DataBoundSetter
public void setInheritFrom(@CheckForNull String inheritFrom) {
if (DescriptorImpl.defaultInheritFrom.equals(inheritFrom)) {
this.inheritFrom = null;
} else {
this.inheritFrom = inheritFrom;
}
}
public boolean isAgentInjection() {
return agentInjection;
}
@DataBoundSetter
public void setAgentInjection(boolean agentInjection) {
this.agentInjection = agentInjection;
}
@CheckForNull
public String getAgentContainer() {
return agentContainer;
}
@DataBoundSetter
public void setAgentContainer(@CheckForNull String agentContainer) {
this.agentContainer = Util.fixEmpty(agentContainer);
}
public List<ContainerTemplate> getContainers() {
return containers;
}
@DataBoundSetter
public void setContainers(List<ContainerTemplate> containers) {
this.containers = containers;
}
public List<TemplateEnvVar> getEnvVars() {
return envVars == null ? Collections.emptyList() : envVars;
}
@DataBoundSetter
public void setEnvVars(List<TemplateEnvVar> envVars) {
if (envVars != null) {
this.envVars.clear();
this.envVars.addAll(envVars);
}
}
@CheckForNull
public YamlMergeStrategy getYamlMergeStrategy() {
return yamlMergeStrategy;
}
@DataBoundSetter
public void setYamlMergeStrategy(YamlMergeStrategy yamlMergeStrategy) {
this.yamlMergeStrategy = yamlMergeStrategy;
}
public List<PodVolume> getVolumes() {
return volumes;
}
@DataBoundSetter
public void setVolumes(List<PodVolume> volumes) {
this.volumes = volumes;
}
@CheckForNull
public WorkspaceVolume getWorkspaceVolume() {
return workspaceVolume == null ? DescriptorImpl.defaultWorkspaceVolume : this.workspaceVolume;
}
@DataBoundSetter
public void setWorkspaceVolume(@CheckForNull WorkspaceVolume workspaceVolume) {
this.workspaceVolume =
(workspaceVolume == null || workspaceVolume
.equals(DescriptorImpl.defaultWorkspaceVolume)) ? null : workspaceVolume;
}
public Integer getInstanceCap() {
return instanceCap;
}
@DataBoundSetter
public void setInstanceCap(@CheckForNull Integer instanceCap) {
if (instanceCap == null || instanceCap.intValue() <= 0) {
this.instanceCap = Integer.MAX_VALUE;
} else {
this.instanceCap = instanceCap;
}
}
public int getIdleMinutes() {
return idleMinutes;
}
@DataBoundSetter
public void setIdleMinutes(@CheckForNull int idleMinutes) {
this.idleMinutes = idleMinutes;
}
@CheckForNull
public int getSlaveConnectTimeout() {
return slaveConnectTimeout;
}
@DataBoundSetter
public void setSlaveConnectTimeout(@CheckForNull int slaveConnectTimeout) {
this.slaveConnectTimeout = slaveConnectTimeout;
}
@CheckForNull
public int getActiveDeadlineSeconds() {
return activeDeadlineSeconds;
}
@DataBoundSetter
public void setActiveDeadlineSeconds(@CheckForNull int activeDeadlineSeconds) {
this.activeDeadlineSeconds = activeDeadlineSeconds;
}
public Boolean getHostNetwork() {
return hostNetwork;
}
@DataBoundSetter
public void setHostNetwork(boolean hostNetwork) {
this.hostNetwork = hostNetwork;
}
@CheckForNull
public String getServiceAccount() {
return serviceAccount;
}
@DataBoundSetter
public void setServiceAccount(@CheckForNull String serviceAccount) {
this.serviceAccount = Util.fixEmpty(serviceAccount);
}
@CheckForNull
public String getSchedulerName() {
return schedulerName;
}
@DataBoundSetter
public void setSchedulerName(@CheckForNull String schedulerName) {
this.schedulerName = Util.fixEmpty(schedulerName);
}
@CheckForNull
public String getNodeSelector() {
return nodeSelector;
}
@DataBoundSetter
public void setNodeSelector(@CheckForNull String nodeSelector) {
this.nodeSelector = Util.fixEmpty(nodeSelector);
}
public Node.Mode getNodeUsageMode() {
return nodeUsageMode;
}
public void setNodeUsageMode(Node.Mode nodeUsageMode) {
this.nodeUsageMode = nodeUsageMode;
}
@DataBoundSetter
public void setNodeUsageMode(String nodeUsageMode) {
this.nodeUsageMode = Node.Mode.valueOf(nodeUsageMode);
}
public String getWorkingDir() {
return workingDir;
}
@DataBoundSetter
public void setWorkingDir(String workingDir) {
this.workingDir = workingDir;
}
@Override
public StepExecution start(StepContext context) throws Exception {
return new PodTemplateStepExecution(this, context);
}
public List<PodAnnotation> getAnnotations() {
return annotations;
}
@DataBoundSetter
public void setAnnotations(List<PodAnnotation> annotations) {
this.annotations = annotations;
}
public List<String> getImagePullSecrets() {
return imagePullSecrets == null ? Collections.emptyList() : imagePullSecrets;
}
@DataBoundSetter
public void setImagePullSecrets(List<String> imagePullSecrets) {
if (imagePullSecrets != null) {
this.imagePullSecrets.clear();
this.imagePullSecrets.addAll(imagePullSecrets);
}
}
@CheckForNull
public String getYaml() {
return yaml;
}
@DataBoundSetter
public void setYaml(@CheckForNull String yaml) {
this.yaml = Util.fixEmpty(yaml);
}
@CheckForNull
public PodRetention getPodRetention() {
return this.podRetention == null ? DescriptorImpl.defaultPodRetention : this.podRetention;
}
@DataBoundSetter
public void setPodRetention(@CheckForNull PodRetention podRetention) {
this.podRetention =
(podRetention == null || podRetention
.equals(DescriptorImpl.defaultPodRetention)) ? null : podRetention;
}
public boolean isInheritYamlMergeStrategy() {
return Optional.ofNullable(inheritYamlMergeStrategy).orElse(false);
}
@DataBoundSetter
public void setInheritYamlMergeStrategy(boolean inheritYamlMergeStrategy) {
this.inheritYamlMergeStrategy = inheritYamlMergeStrategy;
}
boolean isShowRawYamlSet() {
return showRawYaml != null;
}
public boolean isShowRawYaml() {
return isShowRawYamlSet() ? showRawYaml.booleanValue() : true;
}
@DataBoundSetter
public void setShowRawYaml(boolean showRawYaml) {
this.showRawYaml = Boolean.valueOf(showRawYaml);
}
public String getRunAsUser() {
return this.runAsUser;
}
@DataBoundSetter
public void setRunAsUser(String runAsUser) {
this.runAsUser = runAsUser;
}
public String getRunAsGroup() {
return this.runAsGroup;
}
@DataBoundSetter
public void setRunAsGroup(String runAsGroup) {
this.runAsGroup = runAsGroup;
}
@CheckForNull
public String getSupplementalGroups() {
return supplementalGroups;
}
@DataBoundSetter
public void setSupplementalGroups(@CheckForNull String supplementalGroups) {
this.supplementalGroups = Util.fixEmpty(supplementalGroups);
}
@Extension
public static class DescriptorImpl extends StepDescriptor {
static final String[] POD_TEMPLATE_FIELDS = {
"name",
"namespace",
"inheritFrom",
"containers",
"envVars",
"volumes",
"annotations",
"yaml",
"showRawYaml",
"instanceCap",
"podRetention",
"supplementalGroups",
"idleMinutes",
"activeDeadlineSeconds",
"serviceAccount",
"nodeSelector",
"workingDir",
"workspaceVolume",
"agentContainer",
"agentInjection"
};
public DescriptorImpl() {
for (String field : POD_TEMPLATE_FIELDS) {
addHelpFileRedirect(field, PodTemplate.class, field);
}
}
@SuppressWarnings("unused") // by stapler/jelly
public ListBoxModel doFillCloudItems() {
ListBoxModel result = new ListBoxModel();
result.add("—any—", "");
if (!Jenkins.get().hasPermission(Jenkins.MANAGE)) {
return result;
}
Jenkins.get().clouds.getAll(ArmadaCloud.class)
.forEach(cloud -> result.add(cloud.name));
return result;
}
@SuppressWarnings("unused") // by stapler/jelly
public ListBoxModel doFillInheritFromItems(@QueryParameter("cloud") String cloudName) {
cloudName = Util.fixEmpty(cloudName);
ListBoxModel result = new ListBoxModel();
result.add("—Default inheritance—", "<default>");
result.add("—Disable inheritance—", " ");
if (!Jenkins.get().hasPermission(Jenkins.MANAGE)) {
return result;
}
Cloud cloud;
if (cloudName == null) {
cloud = Jenkins.get().clouds.get(ArmadaCloud.class);
} else {
cloud = Jenkins.get().getCloud(cloudName);
}
if (cloud instanceof ArmadaCloud) {
List<PodTemplate> templates = ((ArmadaCloud) cloud).getTemplates();
result.addAll(templates.stream()
.filter(template -> StringUtils.isNotEmpty(template.getName()))
.map(PodTemplate::getName)
.map(ListBoxModel.Option::new)
.collect(Collectors.toList()));
}
return result;
}
@Override
public String getFunctionName() {
return "armadaPodTemplate";
}
@Override
public String getDisplayName() {
return "Define a armadaPodTemplate to use in the kubernetes plugin";
}
@Override
public boolean takesImplicitBlockArgument() {
return true;
}
@Override
public Set<? extends Class<?>> getRequiredContext() {
return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(Run.class, TaskListener.class)));
}
@SuppressWarnings("unused") // jelly
public String getWorkingDir() {
return ContainerTemplate.DEFAULT_WORKING_DIR;
}
public static final Integer defaultInstanceCap = Integer.MAX_VALUE;
public static final PodRetention defaultPodRetention = PodRetention.getPodTemplateDefault();
public static final WorkspaceVolume defaultWorkspaceVolume = WorkspaceVolume.getDefault();
/** Only used for snippet generation. */
public static final String defaultInheritFrom = "<default>";
}
}