Skip to content

Commit 758a0c6

Browse files
authored
Merge pull request #1465 from scherler/JENKINS-72261
2 parents 7df85e7 + 9e979cc commit 758a0c6

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
*
8484
* @author Carlos Sanchez [email protected]
8585
*/
86-
public class KubernetesCloud extends Cloud {
86+
public class KubernetesCloud extends Cloud implements PodTemplateGroup {
8787
public static final int DEFAULT_MAX_REQUESTS_PER_HOST = 32;
8888
public static final Integer DEFAULT_WAIT_FOR_POD_SEC = 600;
8989

@@ -591,6 +591,12 @@ public Collection<NodeProvisioner.PlannedNode> provision(@NonNull final Cloud.Cl
591591
return Collections.emptyList();
592592
}
593593

594+
@Override
595+
public void replaceTemplate(PodTemplate oldTemplate, PodTemplate newTemplate){
596+
this.removeTemplate(oldTemplate);
597+
this.addTemplate(newTemplate);
598+
}
599+
594600
@Override
595601
public boolean canProvision(@NonNull Cloud.CloudState state) {
596602
return getTemplate(state.getLabel()) != null;
@@ -660,10 +666,16 @@ public void addTemplate(PodTemplate t) {
660666
*
661667
* @param t docker template
662668
*/
669+
@Override
663670
public void removeTemplate(PodTemplate t) {
664671
this.templates.remove(t);
665672
}
666673

674+
@Override
675+
public String getPodTemplateGroupUrl() {
676+
return "../../templates";
677+
}
678+
667679
/**
668680
* Add a dynamic pod template. Won't be displayed in UI, and persisted separately from the cloud instance.
669681
* @param t the template to add

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -642,31 +642,30 @@ public void addEnvVars(List<TemplateEnvVar> envVars) {
642642
* Deletes the template.
643643
*/
644644
@POST
645-
public HttpResponse doDoDelete(@AncestorInPath KubernetesCloud kubernetesCloud) throws IOException {
645+
public HttpResponse doDoDelete(@AncestorInPath PodTemplateGroup owner) throws IOException {
646646
Jenkins j = Jenkins.get();
647647
j.checkPermission(Jenkins.ADMINISTER);
648-
if (kubernetesCloud == null) {
648+
if (owner == null) {
649649
throw new IllegalStateException("Cloud could not be found");
650650
}
651-
kubernetesCloud.removeTemplate(this);
651+
owner.removeTemplate(this);
652652
j.save();
653653
// take the user back.
654-
return new HttpRedirect("../../templates");
654+
return new HttpRedirect(owner.getPodTemplateGroupUrl());
655655
}
656656

657657
@POST
658-
public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath KubernetesCloud kubernetesCloud) throws IOException, ServletException, Descriptor.FormException {
658+
public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath PodTemplateGroup owner) throws IOException, ServletException, Descriptor.FormException {
659659
Jenkins j = Jenkins.get();
660660
j.checkPermission(Jenkins.ADMINISTER);
661-
if (kubernetesCloud == null) {
661+
if (owner == null) {
662662
throw new IllegalStateException("Cloud could not be found");
663663
}
664-
kubernetesCloud.removeTemplate(this);
665664
PodTemplate newTemplate = reconfigure(req, req.getSubmittedForm());
666-
kubernetesCloud.addTemplate(newTemplate);
665+
owner.replaceTemplate(this, newTemplate);
667666
j.save();
668667
// take the user back.
669-
return FormApply.success("../../templates");
668+
return FormApply.success(owner.getPodTemplateGroupUrl());
670669
}
671670

672671
private PodTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.csanchez.jenkins.plugins.kubernetes;
2+
/**
3+
* A group of pod templates that can be saved together.
4+
*/
5+
public interface PodTemplateGroup {
6+
/**
7+
* Replaces the old template with the new template.
8+
* @param oldTemplate the old template to replace
9+
* @param newTemplate the new template to replace with
10+
*/
11+
void replaceTemplate(PodTemplate oldTemplate, PodTemplate newTemplate);
12+
/**
13+
* Removes the template from the group.
14+
* @param podTemplate the template to remove
15+
*/
16+
void removeTemplate(PodTemplate podTemplate);
17+
/**
18+
* @return the URL to redirect to after the template is saved.
19+
*/
20+
String getPodTemplateGroupUrl();
21+
}

0 commit comments

Comments
 (0)