From 6abafceac337e33f6a2e5a8b1a7f9b77c2d2af51 Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Mon, 30 Oct 2023 12:40:42 +0100 Subject: [PATCH 1/9] [interface] create interface for podtemplates Signed-off-by: Thorsten Scherler --- .../plugins/kubernetes/PodTemplate.java | 22 ++++++++++++++----- .../plugins/kubernetes/PodTemplateGroup.java | 0 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java index 120b0ffeb6..4a39a23969 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java @@ -62,7 +62,7 @@ * * @author Nicolas De Loof */ -public class PodTemplate extends AbstractDescribableImpl implements Serializable, Saveable { +public class PodTemplate extends AbstractDescribableImpl implements Serializable, Saveable, PodTemplateGroup { private static final long serialVersionUID = 3285310269140845583L; @@ -648,12 +648,23 @@ public HttpResponse doDoDelete(@AncestorInPath KubernetesCloud kubernetesCloud) if (kubernetesCloud == null) { throw new IllegalStateException("Cloud could not be found"); } - kubernetesCloud.removeTemplate(this); + removeTemplate(kubernetesCloud,this); j.save(); // take the user back. return new HttpRedirect("../../templates"); } + @Override + public void replaceTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate){ + removeTemplate(kubernetesCloud, podTemplate); + kubernetesCloud.addTemplate(podTemplate); + } + + @Override + public void removeTemplate(KubernetesCloud kubernetesCloud, PodTemplateGroup podTemplate){ + kubernetesCloud.removeTemplate(this); + } + @POST public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath KubernetesCloud kubernetesCloud) throws IOException, ServletException, Descriptor.FormException { Jenkins j = Jenkins.get(); @@ -661,15 +672,14 @@ public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath Kubernete if (kubernetesCloud == null) { throw new IllegalStateException("Cloud could not be found"); } - kubernetesCloud.removeTemplate(this); - PodTemplate newTemplate = reconfigure(req, req.getSubmittedForm()); - kubernetesCloud.addTemplate(newTemplate); + PodTemplateGroup newTemplate = reconfigure(req, req.getSubmittedForm()); + removeTemplate(kubernetesCloud, newTemplate); j.save(); // take the user back. return FormApply.success("../../templates"); } - private PodTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { + private PodTemplateGroup reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { if (form == null) { return null; } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java new file mode 100644 index 0000000000..e69de29bb2 From b98b5d605c6ad411fe3c42c11d0a21c4baed9727 Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Mon, 30 Oct 2023 12:46:26 +0100 Subject: [PATCH 2/9] [interface] create interface for podtemplates Signed-off-by: Thorsten Scherler --- .../jenkins/plugins/kubernetes/KubernetesCloud.java | 4 ++-- .../jenkins/plugins/kubernetes/PodTemplateGroup.java | 9 +++++++++ .../jenkins/plugins/kubernetes/KubernetesCloudTest.java | 2 +- .../jenkins/plugins/kubernetes/KubernetesSlaveTest.java | 2 +- .../jenkins/plugins/kubernetes/PodTemplateUtilsTest.java | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java index 23c1a33105..2cdf56a4df 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -608,7 +608,7 @@ public PodTemplate getTemplate(@CheckForNull Label label) { @SuppressWarnings("unused ") // stapler @CheckForNull - public PodTemplate getTemplate(@NonNull String id) { + public PodTemplateGroup getTemplate(@NonNull String id) { return getTemplateById(id); } @@ -660,7 +660,7 @@ public void addTemplate(PodTemplate t) { * * @param t docker template */ - public void removeTemplate(PodTemplate t) { + public void removeTemplate(PodTemplateGroup t) { this.templates.remove(t); } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java index e69de29bb2..d221829c88 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java @@ -0,0 +1,9 @@ +package org.csanchez.jenkins.plugins.kubernetes; + +public interface PodTemplateGroup { + + void replaceTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate); + + void removeTemplate(KubernetesCloud kubernetesCloud, PodTemplateGroup podTemplate); + +} \ No newline at end of file diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java index f840033990..9c5af5b067 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java @@ -95,7 +95,7 @@ public void testInheritance() { PodTemplate withNewMavenVersion = new PodTemplate(); withNewMavenVersion.setContainers(Arrays.asList(maven2)); - PodTemplate result = PodTemplateUtils.combine(parent, withNewMavenVersion); + PodTemplateGroup result = PodTemplateUtils.combine(parent, withNewMavenVersion); } @Test(expected = IllegalStateException.class) diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java index a41ed0a2a9..aa004e66d3 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java @@ -123,7 +123,7 @@ public T getExpectedResult() { return this.expectedResult; } - public PodTemplate getPodTemplate() { + public PodTemplateGroup getPodTemplate() { return this.podTemplate; } diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java index e4912d68cb..5be4bd2b24 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java @@ -106,7 +106,7 @@ public void shouldReturnPodTemplateWhenParentIsNull() { PodTemplate template = new PodTemplate(); template.setName("template"); template.setServiceAccount("sa1"); - PodTemplate result = combine(null, template); + PodTemplateGroup result = combine(null, template); assertEquals(result, template); } From 76245b6c8e04d82d0d7c50a286c00a499ae5a4c3 Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Mon, 30 Oct 2023 13:45:41 +0100 Subject: [PATCH 3/9] [interface] fix interface Signed-off-by: Thorsten Scherler --- .../jenkins/plugins/kubernetes/PodTemplate.java | 14 +++++++------- .../plugins/kubernetes/PodTemplateGroup.java | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java index 4a39a23969..0d2e1da676 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java @@ -655,14 +655,14 @@ public HttpResponse doDoDelete(@AncestorInPath KubernetesCloud kubernetesCloud) } @Override - public void replaceTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate){ + public void replaceTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate, PodTemplate newTemplate){ removeTemplate(kubernetesCloud, podTemplate); - kubernetesCloud.addTemplate(podTemplate); + kubernetesCloud.addTemplate(newTemplate); } @Override - public void removeTemplate(KubernetesCloud kubernetesCloud, PodTemplateGroup podTemplate){ - kubernetesCloud.removeTemplate(this); + public void removeTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate){ + kubernetesCloud.removeTemplate(podTemplate); } @POST @@ -672,14 +672,14 @@ public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath Kubernete if (kubernetesCloud == null) { throw new IllegalStateException("Cloud could not be found"); } - PodTemplateGroup newTemplate = reconfigure(req, req.getSubmittedForm()); - removeTemplate(kubernetesCloud, newTemplate); + PodTemplate newTemplate = reconfigure(req, req.getSubmittedForm()); + replaceTemplate(kubernetesCloud, this, newTemplate); j.save(); // take the user back. return FormApply.success("../../templates"); } - private PodTemplateGroup reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { + private PodTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { if (form == null) { return null; } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java index d221829c88..f3a83d0104 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java @@ -2,8 +2,8 @@ public interface PodTemplateGroup { - void replaceTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate); + void replaceTemplate(KubernetesCloud kubernetesCloud, PodTemplate oldTemplate, PodTemplate newTemplate); - void removeTemplate(KubernetesCloud kubernetesCloud, PodTemplateGroup podTemplate); + void removeTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate); } \ No newline at end of file From b9f6302b4c9bd85c2ea38cefe69222968a69b794 Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Tue, 31 Oct 2023 11:14:22 +0100 Subject: [PATCH 4/9] [interface] revert IDE refactoring Signed-off-by: Thorsten Scherler --- .../csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java | 4 ++-- .../jenkins/plugins/kubernetes/KubernetesCloudTest.java | 2 +- .../jenkins/plugins/kubernetes/KubernetesSlaveTest.java | 2 +- .../jenkins/plugins/kubernetes/PodTemplateUtilsTest.java | 3 +-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java index 2cdf56a4df..23c1a33105 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -608,7 +608,7 @@ public PodTemplate getTemplate(@CheckForNull Label label) { @SuppressWarnings("unused ") // stapler @CheckForNull - public PodTemplateGroup getTemplate(@NonNull String id) { + public PodTemplate getTemplate(@NonNull String id) { return getTemplateById(id); } @@ -660,7 +660,7 @@ public void addTemplate(PodTemplate t) { * * @param t docker template */ - public void removeTemplate(PodTemplateGroup t) { + public void removeTemplate(PodTemplate t) { this.templates.remove(t); } diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java index 9c5af5b067..f840033990 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java @@ -95,7 +95,7 @@ public void testInheritance() { PodTemplate withNewMavenVersion = new PodTemplate(); withNewMavenVersion.setContainers(Arrays.asList(maven2)); - PodTemplateGroup result = PodTemplateUtils.combine(parent, withNewMavenVersion); + PodTemplate result = PodTemplateUtils.combine(parent, withNewMavenVersion); } @Test(expected = IllegalStateException.class) diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java index aa004e66d3..a41ed0a2a9 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java @@ -123,7 +123,7 @@ public T getExpectedResult() { return this.expectedResult; } - public PodTemplateGroup getPodTemplate() { + public PodTemplate getPodTemplate() { return this.podTemplate; } diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java index 5be4bd2b24..a02b23f55e 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java @@ -58,7 +58,6 @@ import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.api.model.PodBuilder; -import io.fabric8.kubernetes.api.model.PodFluent.SpecNested; import io.fabric8.kubernetes.api.model.PodSpec; import io.fabric8.kubernetes.api.model.Quantity; import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder; @@ -106,7 +105,7 @@ public void shouldReturnPodTemplateWhenParentIsNull() { PodTemplate template = new PodTemplate(); template.setName("template"); template.setServiceAccount("sa1"); - PodTemplateGroup result = combine(null, template); + PodTemplate result = combine(null, template); assertEquals(result, template); } From 8fb203a2a3b3be2ade0b3c508b3350f507f5a43e Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Tue, 31 Oct 2023 13:01:46 +0100 Subject: [PATCH 5/9] [interface] implement interface in cloud Signed-off-by: Thorsten Scherler --- .../plugins/kubernetes/KubernetesCloud.java | 9 +++++++- .../plugins/kubernetes/PodTemplate.java | 21 +++++-------------- .../plugins/kubernetes/PodTemplateGroup.java | 4 ++-- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java index 23c1a33105..e3c2639b08 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -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; @@ -591,6 +591,12 @@ public Collection 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; @@ -660,6 +666,7 @@ public void addTemplate(PodTemplate t) { * * @param t docker template */ + @Override public void removeTemplate(PodTemplate t) { this.templates.remove(t); } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java index 0d2e1da676..84800cad62 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java @@ -62,7 +62,7 @@ * * @author Nicolas De Loof */ -public class PodTemplate extends AbstractDescribableImpl implements Serializable, Saveable, PodTemplateGroup { +public class PodTemplate extends AbstractDescribableImpl implements Serializable, Saveable { private static final long serialVersionUID = 3285310269140845583L; @@ -642,38 +642,27 @@ public void addEnvVars(List envVars) { * Deletes the template. */ @POST - public HttpResponse doDoDelete(@AncestorInPath KubernetesCloud kubernetesCloud) throws IOException { + public HttpResponse doDoDelete(@AncestorInPath PodTemplateGroup kubernetesCloud) throws IOException { Jenkins j = Jenkins.get(); j.checkPermission(Jenkins.ADMINISTER); if (kubernetesCloud == null) { throw new IllegalStateException("Cloud could not be found"); } - removeTemplate(kubernetesCloud,this); + kubernetesCloud.removeTemplate(this); j.save(); // take the user back. return new HttpRedirect("../../templates"); } - @Override - public void replaceTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate, PodTemplate newTemplate){ - removeTemplate(kubernetesCloud, podTemplate); - kubernetesCloud.addTemplate(newTemplate); - } - - @Override - public void removeTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate){ - kubernetesCloud.removeTemplate(podTemplate); - } - @POST - public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath KubernetesCloud kubernetesCloud) throws IOException, ServletException, Descriptor.FormException { + public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath PodTemplateGroup kubernetesCloud) throws IOException, ServletException, Descriptor.FormException { Jenkins j = Jenkins.get(); j.checkPermission(Jenkins.ADMINISTER); if (kubernetesCloud == null) { throw new IllegalStateException("Cloud could not be found"); } PodTemplate newTemplate = reconfigure(req, req.getSubmittedForm()); - replaceTemplate(kubernetesCloud, this, newTemplate); + kubernetesCloud.replaceTemplate(this, newTemplate); j.save(); // take the user back. return FormApply.success("../../templates"); diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java index f3a83d0104..b14d76ad74 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java @@ -2,8 +2,8 @@ public interface PodTemplateGroup { - void replaceTemplate(KubernetesCloud kubernetesCloud, PodTemplate oldTemplate, PodTemplate newTemplate); + void replaceTemplate(PodTemplate oldTemplate, PodTemplate newTemplate); - void removeTemplate(KubernetesCloud kubernetesCloud, PodTemplate podTemplate); + void removeTemplate(PodTemplate podTemplate); } \ No newline at end of file From ba826adbdf5cda771d4a43452a33e80c0034a7fc Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Tue, 31 Oct 2023 13:20:07 +0100 Subject: [PATCH 6/9] [interface] add redirect Signed-off-by: Thorsten Scherler --- .../plugins/kubernetes/KubernetesCloud.java | 5 +++++ .../jenkins/plugins/kubernetes/PodTemplate.java | 4 ++-- .../plugins/kubernetes/PodTemplateGroup.java | 17 ++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java index e3c2639b08..eb8be8ebfc 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -671,6 +671,11 @@ public void removeTemplate(PodTemplate t) { this.templates.remove(t); } + @Override + public String redirectUrl() { + 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 diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java index 84800cad62..b637092c9a 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java @@ -651,7 +651,7 @@ public HttpResponse doDoDelete(@AncestorInPath PodTemplateGroup kubernetesCloud) kubernetesCloud.removeTemplate(this); j.save(); // take the user back. - return new HttpRedirect("../../templates"); + return new HttpRedirect(kubernetesCloud.redirectUrl()); } @POST @@ -665,7 +665,7 @@ public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath PodTempla kubernetesCloud.replaceTemplate(this, newTemplate); j.save(); // take the user back. - return FormApply.success("../../templates"); + return FormApply.success(kubernetesCloud.redirectUrl()); } private PodTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java index b14d76ad74..fbc6004c34 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java @@ -1,9 +1,20 @@ package org.csanchez.jenkins.plugins.kubernetes; 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); - + /** + * Returns the URL to redirect to after the template is saved. + * @return the URL to redirect to after the template is saved. + */ + String redirectUrl(); } \ No newline at end of file From 5277a3380fe0aca20a557d09427de124e188c079 Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Mon, 6 Nov 2023 20:45:46 +0100 Subject: [PATCH 7/9] [JENKINS-72261] trigger rebuild Signed-off-by: Thorsten Scherler --- .../csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java index fbc6004c34..2e840aaaaa 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java @@ -17,4 +17,4 @@ public interface PodTemplateGroup { * @return the URL to redirect to after the template is saved. */ String redirectUrl(); -} \ No newline at end of file +} From 000affc2e5df314c1866ca5853997d94ece9fc5c Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Wed, 8 Nov 2023 09:27:00 +0100 Subject: [PATCH 8/9] Apply suggestions from code review Co-authored-by: Vincent Latombe --- .../plugins/kubernetes/PodTemplate.java | 4 ++-- .../plugins/kubernetes/PodTemplateGroup.java | 21 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java index b637092c9a..94b2926039 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java @@ -642,7 +642,7 @@ public void addEnvVars(List envVars) { * Deletes the template. */ @POST - public HttpResponse doDoDelete(@AncestorInPath PodTemplateGroup kubernetesCloud) throws IOException { + public HttpResponse doDoDelete(@AncestorInPath PodTemplateGroup owner) throws IOException { Jenkins j = Jenkins.get(); j.checkPermission(Jenkins.ADMINISTER); if (kubernetesCloud == null) { @@ -655,7 +655,7 @@ public HttpResponse doDoDelete(@AncestorInPath PodTemplateGroup kubernetesCloud) } @POST - public HttpResponse doConfigSubmit(StaplerRequest req, @AncestorInPath PodTemplateGroup 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) { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java index 2e840aaaaa..7661ab00e7 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java @@ -1,20 +1,19 @@ package org.csanchez.jenkins.plugins.kubernetes; 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 - */ + /** + * 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 - */ + /** + * Removes the template from the group. + * @param podTemplate the template to remove + */ void removeTemplate(PodTemplate podTemplate); /** - * Returns the URL to redirect to after the template is saved. * @return the URL to redirect to after the template is saved. */ - String redirectUrl(); + String getPodTemplateGroupUrl(); } From 9e979cca6b247cc37365923994234c77ad22af64 Mon Sep 17 00:00:00 2001 From: Thorsten Scherler Date: Wed, 8 Nov 2023 09:51:07 +0100 Subject: [PATCH 9/9] [JENKINS-72261] fix merge and apply suggestions Signed-off-by: Thorsten Scherler --- .../jenkins/plugins/kubernetes/KubernetesCloud.java | 2 +- .../jenkins/plugins/kubernetes/PodTemplate.java | 12 ++++++------ .../jenkins/plugins/kubernetes/PodTemplateGroup.java | 4 +++- .../plugins/kubernetes/PodTemplateUtilsTest.java | 1 + 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java index eb8be8ebfc..376d0043b8 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java @@ -672,7 +672,7 @@ public void removeTemplate(PodTemplate t) { } @Override - public String redirectUrl() { + public String getPodTemplateGroupUrl() { return "../../templates"; } diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java index 94b2926039..4ea8e1fe7c 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java @@ -645,27 +645,27 @@ public void addEnvVars(List envVars) { 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(kubernetesCloud.redirectUrl()); + return new HttpRedirect(owner.getPodTemplateGroupUrl()); } @POST 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"); } PodTemplate newTemplate = reconfigure(req, req.getSubmittedForm()); - kubernetesCloud.replaceTemplate(this, newTemplate); + owner.replaceTemplate(this, newTemplate); j.save(); // take the user back. - return FormApply.success(kubernetesCloud.redirectUrl()); + return FormApply.success(owner.getPodTemplateGroupUrl()); } private PodTemplate reconfigure(@NonNull final StaplerRequest req, JSONObject form) throws Descriptor.FormException { diff --git a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java index 7661ab00e7..7ce876ea7f 100644 --- a/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java +++ b/src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateGroup.java @@ -1,5 +1,7 @@ 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. diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java index a02b23f55e..e4912d68cb 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java @@ -58,6 +58,7 @@ import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.api.model.PodBuilder; +import io.fabric8.kubernetes.api.model.PodFluent.SpecNested; import io.fabric8.kubernetes.api.model.PodSpec; import io.fabric8.kubernetes.api.model.Quantity; import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;