Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-72261] allow custom implementation for removing/update pod templates #1465

Merged
merged 11 commits into from
Nov 8, 2023
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();
}