-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
PodTemplate.java
888 lines (728 loc) · 27.9 KB
/
PodTemplate.java
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
package org.csanchez.jenkins.plugins.kubernetes;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.apache.commons.lang.StringUtils;
import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.PodRetention;
import org.csanchez.jenkins.plugins.kubernetes.pod.yaml.YamlMergeStrategy;
import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume;
import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.DescriptorVisibilityFilter;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.Saveable;
import hudson.model.labels.LabelAtom;
import hudson.slaves.NodeProperty;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import jenkins.model.Jenkins;
/**
* Kubernetes Pod Template
*
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class PodTemplate extends AbstractDescribableImpl<PodTemplate> implements Serializable, Saveable {
private static final long serialVersionUID = 3285310269140845583L;
private static final String FALLBACK_ARGUMENTS = "${computer.jnlpmac} ${computer.name}";
private static final String DEFAULT_ID = "jenkins/slave-default";
private static final Logger LOGGER = Logger.getLogger(PodTemplate.class.getName());
/**
* Connection timeout expiration in seconds, default to 100 seconds
*/
public static final Integer DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT = Integer
.getInteger(PodTemplate.class.getName() + ".connectionTimeout", 100);
private String inheritFrom;
private String name;
private String namespace;
private String image;
private boolean privileged;
private boolean capOnlyOnAlivePods;
private boolean alwaysPullImage;
private String command;
private String args;
private String remoteFs;
private int instanceCap = Integer.MAX_VALUE;
private int slaveConnectTimeout = DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT;
private int idleMinutes;
private int activeDeadlineSeconds;
private String label;
private String serviceAccount;
private String nodeSelector;
private Node.Mode nodeUsageMode = Node.Mode.EXCLUSIVE;
private String resourceRequestCpu;
private String resourceRequestMemory;
private String resourceLimitCpu;
private String resourceLimitMemory;
private boolean customWorkspaceVolumeEnabled;
private WorkspaceVolume workspaceVolume;
private final List<PodVolume> volumes = new ArrayList<PodVolume>();
private List<ContainerTemplate> containers = new ArrayList<ContainerTemplate>();
private List<TemplateEnvVar> envVars = new ArrayList<>();
private List<PodAnnotation> annotations = new ArrayList<PodAnnotation>();
private List<PodImagePullSecret> imagePullSecrets = new ArrayList<PodImagePullSecret>();
private PodTemplateToolLocation nodeProperties;
/**
* @deprecated Stored as a list of yaml fragments
*/
@Deprecated
private transient String yaml;
private List<String> yamls = new ArrayList<>();
public YamlMergeStrategy getYamlMergeStrategy() {
return yamlMergeStrategy;
}
@DataBoundSetter
public void setYamlMergeStrategy(YamlMergeStrategy yamlMergeStrategy) {
this.yamlMergeStrategy = yamlMergeStrategy;
}
private YamlMergeStrategy yamlMergeStrategy = YamlMergeStrategy.defaultStrategy();
public Pod getYamlsPod() {
return yamlMergeStrategy.merge(getYamls());
}
private Boolean showRawYaml;
@CheckForNull
private PodRetention podRetention = PodRetention.getPodTemplateDefault();
@DataBoundConstructor
public PodTemplate() {
}
public PodTemplate(PodTemplate from) {
this.setAnnotations(from.getAnnotations());
this.setContainers(from.getContainers());
this.setImagePullSecrets(from.getImagePullSecrets());
this.setInstanceCap(from.getInstanceCap());
this.setLabel(from.getLabel());
this.setName(from.getName());
this.setNamespace(from.getNamespace());
this.setInheritFrom(from.getInheritFrom());
this.setNodeSelector(from.getNodeSelector());
this.setNodeUsageMode(from.getNodeUsageMode());
this.setServiceAccount(from.getServiceAccount());
this.setSlaveConnectTimeout(from.getSlaveConnectTimeout());
this.setActiveDeadlineSeconds(from.getActiveDeadlineSeconds());
this.setVolumes(from.getVolumes());
this.setWorkspaceVolume(from.getWorkspaceVolume());
this.setYamls(from.getYamls());
this.setShowRawYaml(from.isShowRawYaml());
this.setNodeProperties(from.getNodeProperties());
this.setPodRetention(from.getPodRetention());
}
@Deprecated
public PodTemplate(String image, List<? extends PodVolume> volumes) {
this(null, image, volumes);
}
@Deprecated
PodTemplate(String name, String image, List<? extends PodVolume> volumes) {
this(name, volumes, Collections.emptyList());
if (image != null) {
getContainers().add(new ContainerTemplate(name, image));
}
}
@Restricted(NoExternalUse.class) // testing only
PodTemplate(String name, List<? extends PodVolume> volumes, List<? extends ContainerTemplate> containers) {
this.name = name;
this.volumes.addAll(volumes);
this.containers.addAll(containers);
}
private Optional<ContainerTemplate> getFirstContainer() {
return Optional.ofNullable(getContainers().isEmpty() ? null : getContainers().get(0));
}
public String getInheritFrom() {
return inheritFrom;
}
@DataBoundSetter
public void setInheritFrom(String inheritFrom) {
this.inheritFrom = inheritFrom;
}
@DataBoundSetter
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getNamespace() {
return namespace;
}
@DataBoundSetter
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Deprecated
public String getImage() {
return getFirstContainer().map(ContainerTemplate::getImage).orElse(null);
}
@Deprecated
@DataBoundSetter
public void setCommand(String command) {
getFirstContainer().ifPresent((i) -> i.setCommand(command));
}
@Deprecated
public String getCommand() {
return getFirstContainer().map(ContainerTemplate::getCommand).orElse(null);
}
@Deprecated
@DataBoundSetter
public void setArgs(String args) {
getFirstContainer().ifPresent((i) -> i.setArgs(args));
}
@Deprecated
public String getArgs() {
return getFirstContainer().map(ContainerTemplate::getArgs).orElse(null);
}
public String getDisplayName() {
return "Kubernetes Pod Template";
}
@DataBoundSetter
@Deprecated
public void setRemoteFs(String remoteFs) {
getFirstContainer().ifPresent((i) -> i.setWorkingDir(remoteFs));
}
@Deprecated
public String getRemoteFs() {
return getFirstContainer().map(ContainerTemplate::getWorkingDir).orElse(null);
}
public void setInstanceCap(int instanceCap) {
if (instanceCap < 0) {
this.instanceCap = Integer.MAX_VALUE;
} else {
this.instanceCap = instanceCap;
}
}
public int getInstanceCap() {
return instanceCap;
}
public void setSlaveConnectTimeout(int slaveConnectTimeout) {
if (slaveConnectTimeout <= 0) {
LOGGER.log(Level.WARNING, "Agent -> Jenkins connection timeout " +
"cannot be <= 0. Falling back to the default value: " +
DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT);
this.slaveConnectTimeout = DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT;
} else {
this.slaveConnectTimeout = slaveConnectTimeout;
}
}
public int getSlaveConnectTimeout() {
if (slaveConnectTimeout == 0)
return DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT;
return slaveConnectTimeout;
}
@DataBoundSetter
public void setInstanceCapStr(String instanceCapStr) {
if (StringUtils.isBlank(instanceCapStr)) {
setInstanceCap(Integer.MAX_VALUE);
} else {
setInstanceCap(Integer.parseInt(instanceCapStr));
}
}
public String getInstanceCapStr() {
if (getInstanceCap() == Integer.MAX_VALUE) {
return "";
} else {
return String.valueOf(instanceCap);
}
}
@DataBoundSetter
public void setSlaveConnectTimeoutStr(String slaveConnectTimeoutStr) {
if (StringUtils.isBlank(slaveConnectTimeoutStr)) {
setSlaveConnectTimeout(DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT);
} else {
setSlaveConnectTimeout(Integer.parseInt(slaveConnectTimeoutStr));
}
}
public String getSlaveConnectTimeoutStr() {
return String.valueOf(slaveConnectTimeout);
}
public void setIdleMinutes(int i) {
this.idleMinutes = i;
}
public int getIdleMinutes() {
return idleMinutes;
}
public void setActiveDeadlineSeconds(int i) {
this.activeDeadlineSeconds = i;
}
public int getActiveDeadlineSeconds() {
return activeDeadlineSeconds;
}
@DataBoundSetter
public void setIdleMinutesStr(String idleMinutes) {
if (StringUtils.isBlank(idleMinutes)) {
setIdleMinutes(0);
} else {
setIdleMinutes(Integer.parseInt(idleMinutes));
}
}
public String getIdleMinutesStr() {
if (getIdleMinutes() == 0) {
return "";
} else {
return String.valueOf(idleMinutes);
}
}
@DataBoundSetter
public void setActiveDeadlineSecondsStr(String activeDeadlineSeconds) {
if (StringUtils.isBlank(activeDeadlineSeconds)) {
setActiveDeadlineSeconds(0);
} else {
setActiveDeadlineSeconds(Integer.parseInt(activeDeadlineSeconds));
}
}
public String getActiveDeadlineSecondsStr() {
if (getActiveDeadlineSeconds() == 0) {
return "";
} else {
return String.valueOf(activeDeadlineSeconds);
}
}
public Set<LabelAtom> getLabelSet() {
return Label.parse(label);
}
public Map<String, String> getLabelsMap() {
Set<LabelAtom> labelSet = getLabelSet();
ImmutableMap.Builder<String, String> builder = ImmutableMap.<String, String> builder();
if (!labelSet.isEmpty()) {
for (LabelAtom label : labelSet) {
builder.put(label == null ? DEFAULT_ID : "jenkins/" + label.getName(), "true");
}
}
return builder.build();
}
@DataBoundSetter
public void setLabel(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
@DataBoundSetter
public void setNodeSelector(String nodeSelector) {
this.nodeSelector = nodeSelector;
}
public String getNodeSelector() {
return nodeSelector;
}
@DataBoundSetter
public void setNodeUsageMode(Node.Mode nodeUsageMode) {
this.nodeUsageMode = nodeUsageMode;
}
@DataBoundSetter
public void setNodeUsageMode(String nodeUsageMode) {
this.nodeUsageMode = Node.Mode.valueOf(nodeUsageMode);
}
public Node.Mode getNodeUsageMode() {
return nodeUsageMode;
}
@Deprecated
@DataBoundSetter
public void setPrivileged(boolean privileged) {
getFirstContainer().ifPresent((i) -> i.setPrivileged(privileged));
}
@Deprecated
public boolean isPrivileged() {
return getFirstContainer().map(ContainerTemplate::isPrivileged).orElse(false);
}
public String getServiceAccount() {
return serviceAccount;
}
@DataBoundSetter
public void setServiceAccount(String serviceAccount) {
this.serviceAccount = Util.fixEmpty(serviceAccount);
}
@Deprecated
@DataBoundSetter
public void setAlwaysPullImage(boolean alwaysPullImage) {
getFirstContainer().ifPresent((i) -> i.setAlwaysPullImage(alwaysPullImage));
}
@Deprecated
public boolean isAlwaysPullImage() {
return getFirstContainer().map(ContainerTemplate::isAlwaysPullImage).orElse(false);
}
@DataBoundSetter
@Deprecated
public void setCapOnlyOnAlivePods(boolean capOnlyOnAlivePods) {
this.capOnlyOnAlivePods = capOnlyOnAlivePods;
}
@Deprecated
public boolean isCapOnlyOnAlivePods() {
return capOnlyOnAlivePods;
}
public List<TemplateEnvVar> getEnvVars() {
if (envVars == null) {
return Collections.emptyList();
}
return envVars;
}
public void addEnvVars(List<TemplateEnvVar> envVars) {
if (envVars != null) {
this.envVars.addAll(envVars);
}
}
@DataBoundSetter
public void setEnvVars(List<TemplateEnvVar> envVars) {
if (envVars != null) {
this.envVars.clear();
this.addEnvVars(envVars);
}
}
public List<PodAnnotation> getAnnotations() {
if (annotations == null) {
return Collections.emptyList();
}
return annotations;
}
public void addAnnotations(List<PodAnnotation> annotations) {
this.annotations.addAll(annotations);
}
@DataBoundSetter
public void setAnnotations(List<PodAnnotation> annotations) {
if (annotations != null) {
this.annotations = new ArrayList<PodAnnotation>();
this.addAnnotations(annotations);
}
}
public List<PodImagePullSecret> getImagePullSecrets() {
return imagePullSecrets == null ? Collections.emptyList() : imagePullSecrets;
}
public void addImagePullSecrets(List<PodImagePullSecret> imagePullSecrets) {
this.imagePullSecrets.addAll(imagePullSecrets);
}
@DataBoundSetter
public void setImagePullSecrets(List<PodImagePullSecret> imagePullSecrets) {
if (imagePullSecrets != null) {
this.imagePullSecrets.clear();
this.addImagePullSecrets(imagePullSecrets);
}
}
@DataBoundSetter
public void setNodeProperties(List<? extends NodeProperty<?>> properties) {
this.getNodeProperties().clear();
this.getNodeProperties().addAll(properties);
}
@Nonnull
public PodTemplateToolLocation getNodeProperties(){
if( this.nodeProperties == null)
this.nodeProperties = new PodTemplateToolLocation(this);
return nodeProperties;
}
@Deprecated
public String getResourceRequestMemory() {
return getFirstContainer().map(ContainerTemplate::getResourceRequestMemory).orElse(null);
}
@Deprecated
@DataBoundSetter
public void setResourceRequestMemory(String resourceRequestMemory) {
getFirstContainer().ifPresent((i) -> i.setResourceRequestMemory(resourceRequestMemory));
}
@Deprecated
public String getResourceLimitCpu() {
return getFirstContainer().map(ContainerTemplate::getResourceLimitCpu).orElse(null);
}
@Deprecated
@DataBoundSetter
public void setResourceLimitCpu(String resourceLimitCpu) {
getFirstContainer().ifPresent((i) -> i.setResourceLimitCpu(resourceLimitCpu));
}
@Deprecated
public String getResourceLimitMemory() {
return getFirstContainer().map(ContainerTemplate::getResourceLimitMemory).orElse(null);
}
@Deprecated
@DataBoundSetter
public void setResourceLimitMemory(String resourceLimitMemory) {
getFirstContainer().ifPresent((i) -> i.setResourceLimitMemory(resourceLimitMemory));
}
@Deprecated
public String getResourceRequestCpu() {
return getFirstContainer().map(ContainerTemplate::getResourceRequestCpu).orElse(null);
}
@Deprecated
@DataBoundSetter
public void setResourceRequestCpu(String resourceRequestCpu) {
getFirstContainer().ifPresent((i) -> i.setResourceRequestCpu(resourceRequestCpu));
}
@DataBoundSetter
public void setVolumes(@Nonnull List<PodVolume> items) {
synchronized (this.volumes) {
this.volumes.clear();
this.volumes.addAll(items);
}
}
@Nonnull
public List<PodVolume> getVolumes() {
if (volumes == null) {
return Collections.emptyList();
}
return volumes;
}
public boolean isCustomWorkspaceVolumeEnabled() {
return customWorkspaceVolumeEnabled;
}
@DataBoundSetter
public void setCustomWorkspaceVolumeEnabled(boolean customWorkspaceVolumeEnabled) {
this.customWorkspaceVolumeEnabled = customWorkspaceVolumeEnabled;
}
public WorkspaceVolume getWorkspaceVolume() {
return workspaceVolume;
}
@DataBoundSetter
public void setWorkspaceVolume(WorkspaceVolume workspaceVolume) {
this.workspaceVolume = workspaceVolume;
}
@DataBoundSetter
public void setContainers(@Nonnull List<ContainerTemplate> items) {
synchronized (this.containers) {
this.containers.clear();
this.containers.addAll(items);
}
}
@Nonnull
public List<ContainerTemplate> getContainers() {
if (containers == null) {
return Collections.emptyList();
}
return containers;
}
/**
* @return The first yaml fragment for this pod template
*/
@Restricted(NoExternalUse.class) // Tests and UI
public String getYaml() {
return yamls == null || yamls.isEmpty() ? null : yamls.get(0);
}
@DataBoundSetter
public void setYaml(String yaml) {
String trimmed = Util.fixEmpty(yaml);
if (trimmed != null) {
this.yamls = Collections.singletonList(yaml);
} else {
this.yamls = Collections.emptyList();
}
}
@Nonnull
public List<String> getYamls() {
if (yamls ==null) {
return Collections.emptyList();
}
return yamls;
}
public void setYamls(List<String> yamls) {
if (yamls != null) {
List<String> ys = new ArrayList<>();
for (String y : yamls) {
String trimmed = Util.fixEmpty(y);
if (trimmed != null) {
ys.add(trimmed);
}
}
this.yamls = ys;
} else {
this.yamls = Collections.emptyList();
}
}
public PodRetention getPodRetention() {
return podRetention;
}
@DataBoundSetter
public void setPodRetention(PodRetention podRetention) {
if (podRetention == null) {
podRetention = PodRetention.getPodTemplateDefault();
}
this.podRetention = podRetention;
}
protected Object readResolve() {
if (containers == null) {
// upgrading from 0.8
containers = new ArrayList<>();
ContainerTemplate containerTemplate = new ContainerTemplate(KubernetesCloud.JNLP_NAME, this.image);
containerTemplate.setCommand(command);
containerTemplate.setArgs(Strings.isNullOrEmpty(args) ? FALLBACK_ARGUMENTS : args);
containerTemplate.setPrivileged(privileged);
containerTemplate.setAlwaysPullImage(alwaysPullImage);
containerTemplate.setEnvVars(envVars);
containerTemplate.setResourceRequestMemory(resourceRequestMemory);
containerTemplate.setResourceLimitCpu(resourceLimitCpu);
containerTemplate.setResourceLimitMemory(resourceLimitMemory);
containerTemplate.setResourceRequestCpu(resourceRequestCpu);
containerTemplate.setWorkingDir(remoteFs);
containers.add(containerTemplate);
}
if (podRetention == null) {
// https://issues.jenkins-ci.org/browse/JENKINS-53260
// various legacy paths for injecting pod templates can
// bypass the defaulting paths and the
// value can still be null, so check for it here so
// as to not blow up things like termination path
podRetention = PodRetention.getPodTemplateDefault();
}
if (annotations == null) {
annotations = new ArrayList<>();
}
if (yamls == null) {
yamls = new ArrayList<>();
}
if (yaml != null) {
yamls.add(yaml);
yaml = null;
}
// JENKINS-57116 remove empty items from yamls
if (!yamls.isEmpty() && StringUtils.isBlank(yamls.get(0))) {
setYamls(yamls);
}
if (showRawYaml == null) {
showRawYaml = Boolean.TRUE;
}
if (yamlMergeStrategy == null) {
yamlMergeStrategy = YamlMergeStrategy.defaultStrategy();
}
return this;
}
@Deprecated
public Pod build(KubernetesClient client, KubernetesSlave slave) {
return build(slave);
}
/**
* Build a Pod object from a PodTemplate
*
* @param slave
*/
public Pod build(KubernetesSlave slave) {
return new PodTemplateBuilder(this).withSlave(slave).build();
}
/**
* @deprecated Use {@code Serialization.asYaml(build(KubernetesSlave))} instead.
*/
@Deprecated
public String getDescriptionForLogging() {
return String.format("Agent specification [%s] (%s): %n%s",
getDisplayName(),
getLabel(),
getContainersDescriptionForLogging());
}
boolean isShowRawYamlSet() {
return showRawYaml != null;
}
public boolean isShowRawYaml() {
return isShowRawYamlSet() ? showRawYaml.booleanValue() : true;
}
@DataBoundSetter
public void setShowRawYaml(boolean showRawYaml) {
this.showRawYaml = Boolean.valueOf(showRawYaml);
}
private String getContainersDescriptionForLogging() {
List<ContainerTemplate> containers = getContainers();
StringBuilder sb = new StringBuilder();
for (ContainerTemplate ct : containers) {
sb.append("* [").append(ct.getName()).append("] ").append(ct.getImage());
StringBuilder optional = new StringBuilder();
optionalField(optional, "resourceRequestCpu", ct.getResourceRequestCpu());
optionalField(optional, "resourceRequestMemory", ct.getResourceRequestMemory());
optionalField(optional, "resourceLimitCpu", ct.getResourceLimitCpu());
optionalField(optional, "resourceLimitMemory", ct.getResourceLimitMemory());
if (optional.length() > 0) {
sb.append("(").append(optional).append(")");
}
sb.append("\n");
}
if (isShowRawYaml()) {
for (String yaml : getYamls()) {
sb.append("yaml:\n")
.append(yaml)
.append("\n");
}
}
return sb.toString();
}
private void optionalField(StringBuilder builder, String label, String value) {
if (StringUtils.isNotBlank(value)) {
if (builder.length() > 0) {
builder.append(", ");
}
builder.append(label).append(": ").append(value);
}
}
/**
* Empty implementation of Saveable interface. This interface is used for DescribableList implementation
*/
@Override
public void save() { }
@Extension
public static class DescriptorImpl extends Descriptor<PodTemplate> {
@Override
public String getDisplayName() {
return "Kubernetes Pod Template";
}
@SuppressWarnings("unused") // Used by jelly
@Restricted(DoNotUse.class) // Used by jelly
public List<? extends Descriptor> getEnvVarsDescriptors() {
return DescriptorVisibilityFilter.apply(null, Jenkins.getInstance().getDescriptorList(TemplateEnvVar.class));
}
@SuppressWarnings("unused") // Used by jelly
@Restricted(DoNotUse.class) // Used by jelly
public Descriptor getDefaultPodRetention() {
Jenkins jenkins = Jenkins.getInstanceOrNull();
if (jenkins == null) {
return null;
}
return jenkins.getDescriptor(PodRetention.getPodTemplateDefault().getClass());
}
@SuppressWarnings("unused") // Used by jelly
@Restricted(DoNotUse.class) // Used by jelly
public YamlMergeStrategy getDefaultYamlMergeStrategy() {
return YamlMergeStrategy.defaultStrategy();
}
}
@Override
public String toString() {
return "PodTemplate{" +
(inheritFrom == null ? "" : "inheritFrom='" + inheritFrom + '\'') +
(name == null ? "" : ", name='" + name + '\'') +
(namespace == null ? "" : ", namespace='" + namespace + '\'') +
(image == null ? "" : ", image='" + image + '\'') +
(!privileged ? "" : ", privileged=" + privileged) +
(!alwaysPullImage ? "" : ", alwaysPullImage=" + alwaysPullImage) +
(command == null ? "" : ", command='" + command + '\'') +
(args == null ? "" : ", args='" + args + '\'') +
(remoteFs == null ? "" : ", remoteFs='" + remoteFs + '\'') +
(instanceCap == Integer.MAX_VALUE ? "" : ", instanceCap=" + instanceCap) +
(slaveConnectTimeout == DEFAULT_SLAVE_JENKINS_CONNECTION_TIMEOUT ? "" : ", slaveConnectTimeout=" + slaveConnectTimeout) +
(idleMinutes == 0 ? "" : ", idleMinutes=" + idleMinutes) +
(activeDeadlineSeconds == 0 ? "" : ", activeDeadlineSeconds=" + activeDeadlineSeconds) +
(label == null ? "" : ", label='" + label + '\'') +
(serviceAccount == null ? "" : ", serviceAccount='" + serviceAccount + '\'') +
(nodeSelector == null ? "" : ", nodeSelector='" + nodeSelector + '\'') +
(nodeUsageMode == null ? "" : ", nodeUsageMode=" + nodeUsageMode) +
(resourceRequestCpu == null ? "" : ", resourceRequestCpu='" + resourceRequestCpu + '\'') +
(resourceRequestMemory == null ? "" : ", resourceRequestMemory='" + resourceRequestMemory + '\'') +
(resourceLimitCpu == null ? "" : ", resourceLimitCpu='" + resourceLimitCpu + '\'') +
(resourceLimitMemory == null ? "" : ", resourceLimitMemory='" + resourceLimitMemory + '\'') +
(!customWorkspaceVolumeEnabled ? "" : ", customWorkspaceVolumeEnabled=" + customWorkspaceVolumeEnabled) +
(workspaceVolume == null ? "" : ", workspaceVolume=" + workspaceVolume) +
(volumes == null || volumes.isEmpty() ? "" : ", volumes=" + volumes) +
(containers == null || containers.isEmpty() ? "" : ", containers=" + containers) +
(envVars == null || envVars.isEmpty() ? "" : ", envVars=" + envVars) +
(annotations == null || annotations.isEmpty() ? "" : ", annotations=" + annotations) +
(imagePullSecrets == null || imagePullSecrets.isEmpty() ? "" : ", imagePullSecrets=" + imagePullSecrets) +
(nodeProperties == null || nodeProperties.isEmpty() ? "" : ", nodeProperties=" + nodeProperties) +
(yamls == null || yamls.isEmpty() ? "" : ", yamls=" + yamls) +
'}';
}
}