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-49594] Implement extension points to contribute pod template… #288

Merged
merged 2 commits into from
Feb 19, 2018
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 @@ -188,9 +188,7 @@ public List<PodTemplate> getTemplates() {
*/
@Nonnull
public List<PodTemplate> getAllTemplates() {
List<PodTemplate> podTemplates = new ArrayList<>(PodTemplateMap.get().getTemplates(this));
podTemplates.addAll(templates);
return Collections.unmodifiableList(podTemplates);
return PodTemplateSource.getAll(this);
}

@DataBoundSetter
Expand Down Expand Up @@ -397,9 +395,7 @@ public synchronized Collection<NodeProvisioner.PlannedNode> provision(@CheckForN

List<NodeProvisioner.PlannedNode> r = new ArrayList<NodeProvisioner.PlannedNode>();

ArrayList<PodTemplate> templates = getMatchingTemplates(label);

for (PodTemplate t: templates) {
for (PodTemplate t: getTemplatesFor(label)) {
LOGGER.log(Level.INFO, "Template: " + t.getDisplayName());
for (int i = 1; i <= excessWorkload; i++) {
if (!addProvisionedSlave(t, label)) {
Expand Down Expand Up @@ -491,16 +487,20 @@ public PodTemplate getTemplate(@CheckForNull Label label) {
* Gets all PodTemplates that have the matching {@link Label}.
* @param label label to look for in templates
* @return list of matching templates
* @deprecated Use {@link #getTemplatesFor(Label)} instead.
*/
@Deprecated
public ArrayList<PodTemplate> getMatchingTemplates(@CheckForNull Label label) {
ArrayList<PodTemplate> podList = new ArrayList<PodTemplate>();
List<PodTemplate> podTemplates = getAllTemplates();
for (PodTemplate t : podTemplates) {
if ((label == null && t.getNodeUsageMode() == Node.Mode.NORMAL) || (label != null && label.matches(t.getLabelSet()))) {
podList.add(t);
}
}
return podList;
return new ArrayList<>(getTemplatesFor(label));
}

/**
* Gets all PodTemplates that have the matching {@link Label}.
* @param label label to look for in templates
* @return list of matching templates
*/
public List<PodTemplate> getTemplatesFor(@CheckForNull Label label) {
return PodTemplateFilter.applyAll(this, getAllTemplates(), label);
}

/**
Expand Down Expand Up @@ -635,4 +635,12 @@ private Object readResolve() {
return this;
}

@Extension
public static class PodTemplateSourceImpl extends PodTemplateSource {
@Nonnull
@Override
public List<PodTemplate> getList(@Nonnull KubernetesCloud cloud) {
return cloud.getTemplates();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.csanchez.jenkins.plugins.kubernetes;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Label;

/**
* Filters a pod template according to criteria.
*/
public abstract class PodTemplateFilter implements ExtensionPoint {
/**
* Returns a list of all implementations of {@link PodTemplateFilter}.
* @return a list of all implementations of {@link PodTemplateFilter}.
*/
public static ExtensionList<PodTemplateFilter> all() {
return ExtensionList.lookup(PodTemplateFilter.class);
}

/**
* Pass the given pod templates list into all filters implementations.
*
* @param cloud The cloud instance the pod templates are getting considered for
* @param podTemplates The initial list of pod templates
* @param label The label that was requested for provisioning
* @return The pod template list after filtering
*/
public static List<PodTemplate> applyAll(@Nonnull KubernetesCloud cloud, @Nonnull List<PodTemplate> podTemplates, @CheckForNull Label label) {
List<PodTemplate> result = new ArrayList<>();
for (PodTemplate t : podTemplates) {
PodTemplate output = null;
for (PodTemplateFilter f : all()) {
output = f.transform(cloud, t, label);
if (output == null) {
break;
}
}
if (output != null) {
result.add(output);
}
}
return result;
}

/**
* Transforms a pod template definition.
*
* @param cloud The {@link KubernetesCloud} instance the {@link PodTemplate} instances will be scheduled into.
* @param podTemplate The input pod template to process.
* @param label The label that was requested for provisioning
* @return A new pod template after transformation. It can be null if the filter denies access to the given pod template.
*/
@CheckForNull
protected abstract PodTemplate transform(@Nonnull KubernetesCloud cloud, @Nonnull PodTemplate podTemplate, @CheckForNull Label label);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.csanchez.jenkins.plugins.kubernetes;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import hudson.Extension;
import hudson.model.Label;
import hudson.model.Node;

/**
* Implementation of {@link PodTemplateFilter} filtering pod templates matching the right label.
*/
@Extension
public class PodTemplateLabelFilter extends PodTemplateFilter {
@Override
protected PodTemplate transform(@Nonnull KubernetesCloud cloud, @Nonnull PodTemplate podTemplate, @CheckForNull Label label) {
if ((label == null && podTemplate.getNodeUsageMode() == Node.Mode.NORMAL) || (label != null && label.matches(podTemplate.getLabelSet()))) {
return podTemplate;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.csanchez.jenkins.plugins.kubernetes;

import static java.util.stream.Collectors.toList;

import java.util.Collection;
import java.util.List;

import javax.annotation.Nonnull;

import hudson.ExtensionList;
import hudson.ExtensionPoint;

/**
* A source of pod templates.
*/
public abstract class PodTemplateSource implements ExtensionPoint {
public static List<PodTemplate> getAll(@Nonnull KubernetesCloud cloud) {
return ExtensionList.lookup(PodTemplateSource.class)
.stream()
.map(s -> s.getList(cloud))
.flatMap(Collection::stream)
.collect(toList());
}

/**
* The list of {@link PodTemplate} contributed by this implementation.
* @return The list of {@link PodTemplate} contributed by this implementation.
* @param cloud
*/
@Nonnull
protected abstract List<PodTemplate> getList(@Nonnull KubernetesCloud cloud);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplateSource;

import hudson.Extension;
import hudson.ExtensionList;
Expand Down Expand Up @@ -62,4 +63,14 @@ public void removeTemplate(@Nonnull KubernetesCloud cloud, @Nonnull PodTemplate
getOrCreateTemplateList(cloud).remove(podTemplate);
}

@Extension
public static class PodTemplateSourceImpl extends PodTemplateSource {

@Nonnull
@Override
public List<PodTemplate> getList(@Nonnull KubernetesCloud cloud) {
return PodTemplateMap.get().getTemplates(cloud);
}
}

}