Skip to content

Commit

Permalink
Merge pull request #1465 from scherler/JENKINS-72261
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlatombe authored Nov 8, 2023
2 parents 7df85e7 + 9e979cc commit 758a0c6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
*
* @author Carlos Sanchez carlos@apache.org
*/
public class KubernetesCloud extends Cloud {
public class KubernetesCloud extends Cloud implements PodTemplateGroup {
public static final int DEFAULT_MAX_REQUESTS_PER_HOST = 32;
public static final Integer DEFAULT_WAIT_FOR_POD_SEC = 600;

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

@Override
public void replaceTemplate(PodTemplate oldTemplate, PodTemplate newTemplate){
this.removeTemplate(oldTemplate);
this.addTemplate(newTemplate);
}

@Override
public boolean canProvision(@NonNull Cloud.CloudState state) {
return getTemplate(state.getLabel()) != null;
Expand Down Expand Up @@ -660,10 +666,16 @@ public void addTemplate(PodTemplate t) {
*
* @param t docker template
*/
@Override
public void removeTemplate(PodTemplate t) {
this.templates.remove(t);
}

@Override
public String getPodTemplateGroupUrl() {
return "../../templates";
}

/**
* Add a dynamic pod template. Won't be displayed in UI, and persisted separately from the cloud instance.
* @param t the template to add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,31 +642,30 @@ public void addEnvVars(List<TemplateEnvVar> envVars) {
* Deletes the template.
*/
@POST
public HttpResponse doDoDelete(@AncestorInPath KubernetesCloud kubernetesCloud) throws IOException {
public HttpResponse doDoDelete(@AncestorInPath PodTemplateGroup owner) throws IOException {
Jenkins j = Jenkins.get();
j.checkPermission(Jenkins.ADMINISTER);
if (kubernetesCloud == null) {
if (owner == null) {
throw new IllegalStateException("Cloud could not be found");
}
kubernetesCloud.removeTemplate(this);
owner.removeTemplate(this);
j.save();
// take the user back.
return new HttpRedirect("../../templates");
return new HttpRedirect(owner.getPodTemplateGroupUrl());
}

@POST
public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath KubernetesCloud kubernetesCloud) throws IOException, ServletException, Descriptor.FormException {
public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath PodTemplateGroup owner) throws IOException, ServletException, Descriptor.FormException {
Jenkins j = Jenkins.get();
j.checkPermission(Jenkins.ADMINISTER);
if (kubernetesCloud == null) {
if (owner == null) {
throw new IllegalStateException("Cloud could not be found");
}
kubernetesCloud.removeTemplate(this);
PodTemplate newTemplate = reconfigure(req, req.getSubmittedForm());
kubernetesCloud.addTemplate(newTemplate);
owner.replaceTemplate(this, newTemplate);
j.save();
// take the user back.
return FormApply.success("../../templates");
return FormApply.success(owner.getPodTemplateGroupUrl());
}

private PodTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.csanchez.jenkins.plugins.kubernetes;
/**
* A group of pod templates that can be saved together.
*/
public interface PodTemplateGroup {
/**
* Replaces the old template with the new template.
* @param oldTemplate the old template to replace
* @param newTemplate the new template to replace with
*/
void replaceTemplate(PodTemplate oldTemplate, PodTemplate newTemplate);
/**
* Removes the template from the group.
* @param podTemplate the template to remove
*/
void removeTemplate(PodTemplate podTemplate);
/**
* @return the URL to redirect to after the template is saved.
*/
String getPodTemplateGroupUrl();
}

0 comments on commit 758a0c6

Please sign in to comment.