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 the user to configure a pod template that will be used for prov… #133

Merged
merged 1 commit into from
Feb 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 @@ -112,6 +112,8 @@ public class KubernetesCloud extends Cloud {
/** Default timeout for idle workers that don't correctly indicate exit. */
private static final int DEFAULT_RETENTION_TIMEOUT_MINUTES = 5;

private String defaultsProviderTemplate;

private List<PodTemplate> templates = new ArrayList<PodTemplate>();
private String serverUrl;
@CheckForNull
Expand Down Expand Up @@ -166,6 +168,15 @@ public void setRetentionTimeout(int retentionTimeout) {
this.retentionTimeout = retentionTimeout;
}

public String getDefaultsProviderTemplate() {
return defaultsProviderTemplate;
}

@DataBoundSetter
public void setDefaultsProviderTemplate(String defaultsProviderTemplate) {
this.defaultsProviderTemplate = defaultsProviderTemplate;
}

public List<PodTemplate> getTemplates() {
return templates;
}
Expand Down Expand Up @@ -390,7 +401,7 @@ private Container createContainer(KubernetesSlave slave, ContainerTemplate conta


private Pod getPodTemplate(KubernetesSlave slave, @CheckForNull Label label) {
final PodTemplate template = PodTemplateUtils.unwrap(getTemplate(label), templates);
final PodTemplate template = PodTemplateUtils.unwrap(getTemplate(label), defaultsProviderTemplate, templates);
if (template == null) {
return null;
}
Expand Down Expand Up @@ -761,7 +772,7 @@ public boolean canProvision(@CheckForNull Label label) {
* @return the template
*/
public PodTemplate getTemplate(@CheckForNull Label label) {
return PodTemplateUtils.getTemplate(label, templates);
return PodTemplateUtils.getTemplateByLabel(label, templates);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -149,21 +151,34 @@ public static PodTemplate combine(PodTemplate parent, PodTemplate template) {

/**
* Unwraps the hierarchy of the PodTemplate.
* @param template
*
* @param template The template to unwrap.
* @param defaultProviderTemplate The name of the template that provides the default values.
* @param allTemplates A collection of all the known templates
* @return
*/
static PodTemplate unwrap(PodTemplate template, Collection<PodTemplate> allTemplates) {
static PodTemplate unwrap(PodTemplate template, String defaultProviderTemplate, Collection<PodTemplate> allTemplates) {
if (template == null) {
return null;
}

if (Strings.isNullOrEmpty(template.getInheritFrom())) {
StringBuilder sb = new StringBuilder();
if (!Strings.isNullOrEmpty(defaultProviderTemplate)) {
sb.append(defaultProviderTemplate).append(" ");

}
if (!Strings.isNullOrEmpty(template.getInheritFrom())) {
sb.append(template.getInheritFrom()).append(" ");
}
String inheritFrom = sb.toString();

if (Strings.isNullOrEmpty(inheritFrom)) {
return template;
} else {
String[] parentLabels = template.getInheritFrom().split("[ ]+");
String[] parentNames = inheritFrom.split("[ ]+");
PodTemplate parent = null;
for (String label : parentLabels) {
PodTemplate next = getTemplate(Label.get(label), allTemplates);
for (String name : parentNames) {
PodTemplate next = getTemplateByName(name, allTemplates);
if (next != null) {
parent = combine(parent, unwrap(next, allTemplates));
}
Expand All @@ -172,7 +187,25 @@ static PodTemplate unwrap(PodTemplate template, Collection<PodTemplate> allTempl
}
}

public static PodTemplate getTemplate(@CheckForNull Label label, Collection<PodTemplate> templates) {
/**
* Unwraps the hierarchy of the PodTemplate.
*
* @param template The template to unwrap.
* @param allTemplates A collection of all the known templates
* @return
*/
static PodTemplate unwrap(PodTemplate template, Collection<PodTemplate> allTemplates) {
return unwrap(template, null, allTemplates);
}


/**
* Gets the {@link PodTemplate} by {@link Label}.
* @param label The label.
* @param templates The list of all templates.
* @return The first pod template from the collection that has a matching label.
*/
public static PodTemplate getTemplateByLabel(@CheckForNull Label label, Collection<PodTemplate> templates) {
for (PodTemplate t : templates) {
if (label == null || label.matches(t.getLabelSet())) {
return t;
Expand All @@ -181,6 +214,21 @@ public static PodTemplate getTemplate(@CheckForNull Label label, Collection<PodT
return null;
}

/**
* Gets the {@link PodTemplate} by name.
* @param name The name.
* @param templates The list of all templates.
* @return The first pod template from the collection that has a matching name.
*/
public static PodTemplate getTemplateByName(@CheckForNull String name, Collection<PodTemplate> templates) {
for (PodTemplate t : templates) {
if (name != null && name.equals(t.getName())) {
return t;
}
}
return null;
}

/**
* Substitutes a placeholder with a value found in the environment.
* @param s The placeholder. Should be use the format: ${placeholder}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
</f:entry>
</f:advanced>

<f:entry title="${%Defaults Provider Template Name}" field="defaultsProviderTemplate">
<f:textbox default=""/>
</f:entry>

<f:entry title="${%Images}" description="${%List of Images to be launched as slaves}">
<f:repeatableHeteroProperty field="templates" hasHeader="true" addCaption="Add Pod Template"
deleteCaption="Delete Template" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
The name of the pod template to use for providing default values.
When a value is specified and there is a pod template with a matching name, the template will be
used as a parent to all other pod templates.
</div>