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

Allow to create non-configurable instances programmatically. #191

Merged
merged 1 commit into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.annotation.Nonnull;
import javax.servlet.ServletException;

import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution;
Expand Down Expand Up @@ -149,6 +150,28 @@ public KubernetesCloud(String name) {
super(name);
}

/**
* Copy constructor.
* Allows to create copies of the original kubernetes cloud. Since it's a singleton
* by design, this method also allows specifying a new name.
* @param name Name of the cloud to be created
* @param source Source Kubernetes cloud implementation
* @since 0.13
*/
public KubernetesCloud(@NonNull String name, @NonNull KubernetesCloud source) {
super(name);
this.defaultsProviderTemplate = source.defaultsProviderTemplate;
this.templates.addAll(source.templates);
this.serverUrl = source.serverUrl;
this.skipTlsVerify = source.skipTlsVerify;
this.namespace = source.namespace;
this.jenkinsUrl = source.jenkinsUrl;
this.credentialsId = source.credentialsId;
this.containerCap = source.containerCap;
this.retentionTimeout = source.retentionTimeout;
this.connectTimeout = source.connectTimeout;
}

@Deprecated
public KubernetesCloud(String name, List<? extends PodTemplate> templates, String serverUrl, String namespace,
String jenkinsUrl, String containerCapStr, int connectTimeout, int readTimeout, int retentionTimeout) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.csanchez.jenkins.plugins.kubernetes;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.DescriptorVisibilityFilter;
import hudson.slaves.Cloud;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

public class NonConfigurableKubernetesCloud extends KubernetesCloud {
public NonConfigurableKubernetesCloud(@NonNull String name, @NonNull KubernetesCloud source) {
super(name, source);
}

@Extension
public static class FilterImpl extends DescriptorVisibilityFilter {
@Override
public boolean filter(Object context, Descriptor descriptor) {
return !(descriptor instanceof DescriptorImpl);
}
}

@Extension
public static class DescriptorImpl extends KubernetesCloud.DescriptorImpl {
@Override
public String getDisplayName() {
return Messages.NonConfigurableKubernetesCloud_displayName();
}

@Override
public boolean configure(StaplerRequest request, JSONObject object) throws Descriptor.FormException {
return true;
}

@Override
public Cloud newInstance(StaplerRequest req, JSONObject formData) throws Descriptor.FormException {
if (req != null) {
// We prevent the cloud reconfiguration from the web UI
String cloudName = req.getParameter("cloudName");
return Jenkins.getInstance().getCloud(cloudName);
} else {
throw new IllegalStateException("Expecting req to be non-null");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
offline=Kubernetes agent is going offline
NonConfigurableKubernetesCloud.displayName=Kubernetes (predefined settings)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:c="/lib/credentials">
<f:invisibleEntry title="${%Cloud name}">
<f:readOnlyTextbox name="cloudName" value="${instance.displayName}"/>
</f:invisibleEntry>
</j:jelly>