Skip to content

Commit 487d544

Browse files
committed
feat: Allow override of scheduler name spec field
1 parent 55e01ea commit 487d544

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public class PodTemplate extends AbstractDescribableImpl<PodTemplate> implements
124124

125125
private String serviceAccount;
126126

127+
private String schedulerName;
128+
127129
private String nodeSelector;
128130

129131
private Node.Mode nodeUsageMode;
@@ -567,6 +569,15 @@ public void setServiceAccount(String serviceAccount) {
567569
this.serviceAccount = Util.fixEmpty(serviceAccount);
568570
}
569571

572+
public String getSchedulerName() {
573+
return schedulerName;
574+
}
575+
576+
@DataBoundSetter
577+
public void setSchedulerName(String schedulerName) {
578+
this.schedulerName = Util.fixEmpty(schedulerName);
579+
}
580+
570581
@Deprecated
571582
@DataBoundSetter
572583
public void setAlwaysPullImage(boolean alwaysPullImage) {
@@ -1018,6 +1029,7 @@ public String toString() {
10181029
(activeDeadlineSeconds == 0 ? "" : ", activeDeadlineSeconds=" + activeDeadlineSeconds) +
10191030
(label == null ? "" : ", label='" + label + '\'') +
10201031
(serviceAccount == null ? "" : ", serviceAccount='" + serviceAccount + '\'') +
1032+
(schedulerName == null ? "" : ", schedulerName='" + schedulerName + '\'') +
10211033
(nodeSelector == null ? "" : ", nodeSelector='" + nodeSelector + '\'') +
10221034
(nodeUsageMode == null ? "" : ", nodeUsageMode=" + nodeUsageMode) +
10231035
(resourceRequestCpu == null ? "" : ", resourceRequestCpu='" + resourceRequestCpu + '\'') +

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ public Pod build() {
215215
if (template.getServiceAccount() != null) {
216216
builder.withServiceAccountName(substituteEnv(template.getServiceAccount()));
217217
}
218+
if (template.getSchedulerName() != null) {
219+
builder.withSchedulerName(substituteEnv(template.getSchedulerName()));
220+
}
218221

219222
List<LocalObjectReference> imagePullSecrets = template.getImagePullSecrets().stream()
220223
.map((x) -> x.toLocalObjectReference()).collect(Collectors.toList());

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ public static Pod combine(Pod parent, Pod template) {
250250
String serviceAccountName = Strings.isNullOrEmpty(template.getSpec().getServiceAccountName())
251251
? parent.getSpec().getServiceAccountName()
252252
: template.getSpec().getServiceAccountName();
253+
String schedulerName = Strings.isNullOrEmpty(template.getSpec().getSchedulerName())
254+
? parent.getSpec().getSchedulerName()
255+
: template.getSpec().getSchedulerName();
253256

254257
Boolean hostNetwork = template.getSpec().getHostNetwork() != null
255258
? template.getSpec().getHostNetwork()
@@ -298,6 +301,7 @@ public static Pod combine(Pod parent, Pod template) {
298301
.withNodeSelector(nodeSelector) //
299302
.withServiceAccount(serviceAccount) //
300303
.withServiceAccountName(serviceAccountName) //
304+
.withSchedulerName(schedulerName)
301305
.withHostNetwork(hostNetwork) //
302306
.withContainers(combinedContainers) //
303307
.withInitContainers(combinedInitContainers) //
@@ -371,6 +375,7 @@ public static PodTemplate combine(PodTemplate parent, PodTemplate template) {
371375
String label = template.getLabel();
372376
String nodeSelector = Strings.isNullOrEmpty(template.getNodeSelector()) ? parent.getNodeSelector() : template.getNodeSelector();
373377
String serviceAccount = Strings.isNullOrEmpty(template.getServiceAccount()) ? parent.getServiceAccount() : template.getServiceAccount();
378+
String schedulerName = Strings.isNullOrEmpty(template.getSchedulerName()) ? parent.getSchedulerName() : template.getSchedulerName();
374379
Node.Mode nodeUsageMode = template.getNodeUsageMode() == null ? parent.getNodeUsageMode() : template.getNodeUsageMode();
375380

376381
Set<PodAnnotation> podAnnotations = new LinkedHashSet<>();
@@ -406,6 +411,7 @@ public static PodTemplate combine(PodTemplate parent, PodTemplate template) {
406411
podTemplate.setLabel(label);
407412
podTemplate.setNodeSelector(nodeSelector);
408413
podTemplate.setServiceAccount(serviceAccount);
414+
podTemplate.setSchedulerName(schedulerName);
409415
podTemplate.setEnvVars(combineEnvVars(parent, template));
410416
podTemplate.setContainers(new ArrayList<>(combinedContainers.values()));
411417
podTemplate.setWorkspaceVolume(workspaceVolume);
@@ -434,6 +440,9 @@ public static PodTemplate combine(PodTemplate parent, PodTemplate template) {
434440
podTemplate.setServiceAccount(!Strings.isNullOrEmpty(template.getServiceAccount()) ?
435441
template.getServiceAccount() : parent.getServiceAccount());
436442

443+
podTemplate.setSchedulerName(!Strings.isNullOrEmpty(template.getSchedulerName()) ?
444+
template.getSchedulerName() : parent.getSchedulerName());
445+
437446
podTemplate.setPodRetention(template.getPodRetention());
438447
podTemplate.setShowRawYaml(template.isShowRawYamlSet() ? template.isShowRawYaml() : parent.isShowRawYaml());
439448

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesDeclarativeAgent.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class KubernetesDeclarativeAgent extends DeclarativeAgent<KubernetesDecla
5353
@CheckForNull
5454
private String serviceAccount;
5555
@CheckForNull
56+
private String schedulerName;
57+
@CheckForNull
5658
private String nodeSelector;
5759
@CheckForNull
5860
private String namespace;
@@ -163,6 +165,15 @@ public void setServiceAccount(String serviceAccount) {
163165
this.serviceAccount = serviceAccount;
164166
}
165167

168+
public String getSchedulerName() {
169+
return schedulerName;
170+
}
171+
172+
@DataBoundSetter
173+
public void setSchedulerName(String schedulerName) {
174+
this.schedulerName = schedulerName;
175+
}
176+
166177
public String getNodeSelector() {
167178
return nodeSelector;
168179
}

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStep.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public class PodTemplateStep extends Step implements Serializable {
7878
@CheckForNull
7979
private String serviceAccount;
8080

81+
@CheckForNull
82+
private String schedulerName;
83+
8184
@CheckForNull
8285
private String nodeSelector;
8386

@@ -269,6 +272,14 @@ public void setServiceAccount(@CheckForNull String serviceAccount) {
269272
this.serviceAccount = Util.fixEmpty(serviceAccount);
270273
}
271274

275+
@CheckForNull
276+
public String getSchedulerName() { return schedulerName; }
277+
278+
@DataBoundSetter
279+
public void setSchedulerName(@CheckForNull String schedulerName) {
280+
this.schedulerName = Util.fixEmpty(schedulerName);
281+
}
282+
272283
@CheckForNull
273284
public String getNodeSelector() {
274285
return nodeSelector;

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStepExecution.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public boolean start() throws Exception {
107107
newTemplate.setNodeSelector(step.getNodeSelector());
108108
newTemplate.setNodeUsageMode(step.getNodeUsageMode());
109109
newTemplate.setServiceAccount(step.getServiceAccount());
110+
newTemplate.setSchedulerName(step.getSchedulerName());
110111
newTemplate.setRunAsUser(step.getRunAsUser());
111112
newTemplate.setRunAsGroup(step.getRunAsGroup());
112113
if (step.getHostNetwork() != null) {

src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilderTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,31 @@ public void yamlOverrideHostNetwork() {
635635
assertTrue(pod.getSpec().getHostNetwork());
636636
}
637637

638+
@Test
639+
public void yamlOverrideSchedulerName() {
640+
PodTemplate parent = new PodTemplate();
641+
parent.setYaml(
642+
"apiVersion: v1\n" +
643+
"kind: Pod\n" +
644+
"metadata:\n" +
645+
" labels:\n" +
646+
" some-label: some-label-value\n" +
647+
"spec:\n" +
648+
" schedulerName: default-scheduler\n"
649+
);
650+
651+
PodTemplate child = new PodTemplate();
652+
child.setYaml(
653+
"spec:\n" +
654+
" schedulerName: custom-scheduler\n"
655+
);
656+
child.setInheritFrom("parent");
657+
child.setYamlMergeStrategy(merge());
658+
PodTemplate result = combine(parent, child);
659+
Pod pod = new PodTemplateBuilder(result, slave).build();
660+
assertEquals("custom-scheduler", pod.getSpec().getSchedulerName());
661+
}
662+
638663
@Test
639664
public void yamlOverrideSecurityContext() {
640665
PodTemplate parent = new PodTemplate();

0 commit comments

Comments
 (0)