Skip to content

Commit 835f493

Browse files
committed
[JENKINS-56082] Merge pod templates definitions from yaml
1 parent 7b0977f commit 835f493

File tree

6 files changed

+233
-43
lines changed

6 files changed

+233
-43
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplate.java

+52-6
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ public class PodTemplate extends AbstractDescribableImpl<PodTemplate> implements
122122

123123
private PodTemplateToolLocation nodeProperties;
124124

125-
private String yaml;
125+
/**
126+
* @deprecated Stored as a list of yaml fragments
127+
*/
128+
@Deprecated
129+
private transient String yaml;
130+
131+
private List<String> yamls;
126132

127133
@CheckForNull
128134
private PodRetention podRetention = PodRetention.getPodTemplateDefault();
@@ -147,7 +153,7 @@ public PodTemplate(PodTemplate from) {
147153
this.setActiveDeadlineSeconds(from.getActiveDeadlineSeconds());
148154
this.setVolumes(from.getVolumes());
149155
this.setWorkspaceVolume(from.getWorkspaceVolume());
150-
this.setYaml(from.getYaml());
156+
this.setYamls(from.getYamls());
151157
this.setNodeProperties(from.getNodeProperties());
152158
this.setPodRetention(from.getPodRetention());
153159
}
@@ -608,13 +614,45 @@ public List<ContainerTemplate> getContainers() {
608614
return containers;
609615
}
610616

617+
/**
618+
* @deprecated Use getYamls instead.
619+
* @return
620+
*/
621+
@Deprecated
611622
public String getYaml() {
612623
return yaml;
613624
}
614625

615626
@DataBoundSetter
616627
public void setYaml(String yaml) {
617-
this.yaml = yaml;
628+
String trimmed = Util.fixEmpty(yaml);
629+
if (trimmed != null) {
630+
this.yamls = Collections.singletonList(yaml);
631+
} else {
632+
this.yamls = Collections.emptyList();
633+
}
634+
}
635+
636+
public List<String> getYamls() {
637+
if (yamls ==null) {
638+
return Collections.emptyList();
639+
}
640+
return yamls;
641+
}
642+
643+
public void setYamls(List<String> yamls) {
644+
if (yamls != null) {
645+
List<String> ys = new ArrayList<>();
646+
for (String y : yamls) {
647+
String trimmed = Util.fixEmpty(y);
648+
if (trimmed != null) {
649+
ys.add(trimmed);
650+
}
651+
}
652+
this.yamls = ys;
653+
} else {
654+
this.yamls = Collections.emptyList();
655+
}
618656
}
619657

620658
public PodRetention getPodRetention() {
@@ -660,6 +698,14 @@ protected Object readResolve() {
660698
annotations = new ArrayList<>();
661699
}
662700

701+
if (yamls == null) {
702+
yamls = new ArrayList<>();
703+
}
704+
705+
if (yaml != null) {
706+
yamls.add(yaml);
707+
yaml = null;
708+
}
663709
return this;
664710
}
665711

@@ -695,10 +741,10 @@ private String getContainersDescriptionForLogging() {
695741
}
696742
sb.append("\n");
697743
}
698-
if (StringUtils.isNotBlank(getYaml())) {
744+
for (String yaml : getYamls()) {
699745
sb.append("yaml:\n")
700-
.append(getYaml())
701-
.append("\n");
746+
.append(yaml)
747+
.append("\n");
702748
}
703749
return sb.toString();
704750
}

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateBuilder.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,10 @@ public Pod build() {
196196
}
197197

198198
builder.withContainers(containers.values().toArray(new Container[containers.size()]));
199-
Pod pod = builder.endSpec().build();
200199

201-
// merge with the yaml
202-
String yaml = template.getYaml();
203-
if (!StringUtils.isBlank(yaml)) {
204-
Pod podFromYaml = parseFromYaml(yaml);
205-
pod = combine(podFromYaml, pod);
206-
}
200+
// merge with the yaml fragments
201+
Pod yamlPods = combine(template.getYamls().stream().map(PodTemplateUtils::parseFromYaml).collect(Collectors.toList()));
202+
Pod pod = combine(yamlPods, builder.endSpec().build());
207203

208204
// Apply defaults
209205

src/main/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtils.java

+34-9
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
import javax.annotation.Nonnull;
2929

3030
import org.apache.commons.lang.StringUtils;
31-
import hudson.slaves.NodeProperty;
32-
import hudson.slaves.NodePropertyDescriptor;
33-
import hudson.util.DescribableList;
34-
import jenkins.model.Jenkins;
3531
import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar;
3632
import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume;
3733
import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume;
@@ -44,7 +40,6 @@
4440
import hudson.Util;
4541
import hudson.model.Label;
4642
import hudson.model.Node;
47-
import hudson.tools.ToolLocationNodeProperty;
4843
import io.fabric8.kubernetes.api.model.Container;
4944
import io.fabric8.kubernetes.api.model.ContainerBuilder;
5045
import io.fabric8.kubernetes.api.model.EnvFromSource;
@@ -185,6 +180,22 @@ private static Map<String, Quantity> combineResources(Container parent, Containe
185180
);
186181
}
187182

183+
/**
184+
* Combines all given pods together in order.
185+
* @param pods the pods to combine
186+
*/
187+
public static Pod combine(List<Pod> pods) {
188+
Pod result = null;
189+
for (Pod p: pods) {
190+
if (result != null) {
191+
result = combine(result, p);
192+
} else {
193+
result = p;
194+
}
195+
}
196+
return result;
197+
}
198+
188199
/**
189200
* Combines a Pod with its parent.
190201
* @param parent The parent Pod (nullable).
@@ -222,9 +233,7 @@ public static Pod combine(Pod parent, Pod template) {
222233
.collect(toMap(c -> c.getName(), c -> combine(parentContainers.get(c.getName()), c))));
223234

224235
// Volumes
225-
List<Volume> combinedVolumes = Lists.newLinkedList();
226-
Optional.ofNullable(parent.getSpec().getVolumes()).ifPresent(combinedVolumes::addAll);
227-
Optional.ofNullable(template.getSpec().getVolumes()).ifPresent(combinedVolumes::addAll);
236+
List<Volume> combinedVolumes = combineVolumes(parent.getSpec().getVolumes(), template.getSpec().getVolumes());
228237

229238
// Tolerations
230239
List<Toleration> combinedTolerations = Lists.newLinkedList();
@@ -268,6 +277,12 @@ public static Pod combine(Pod parent, Pod template) {
268277
return pod;
269278
}
270279

280+
private static List<Volume> combineVolumes(@Nonnull List<Volume> volumes1, @Nonnull List<Volume> volumes2) {
281+
Map<String, Volume> volumesByName = volumes1.stream().collect(Collectors.toMap(Volume::getName, Function.identity()));
282+
volumes2.forEach(v -> volumesByName.put(v.getName(), v));
283+
return new ArrayList<>(volumesByName.values());
284+
}
285+
271286
/**
272287
* Combines a {@link PodTemplate} with its parent.
273288
* @param parent The parent container template (nullable).
@@ -353,7 +368,9 @@ public static PodTemplate combine(PodTemplate parent, PodTemplate template) {
353368
template.isCustomWorkspaceVolumeEnabled() : parent.isCustomWorkspaceVolumeEnabled());
354369
podTemplate.setPodRetention(template.getPodRetention());
355370

356-
podTemplate.setYaml(template.getYaml() == null ? parent.getYaml() : template.getYaml());
371+
List<String> yamls = new ArrayList<>(parent.getYamls());
372+
yamls.addAll(template.getYamls());
373+
podTemplate.setYamls(yamls);
357374

358375
LOGGER.log(Level.FINEST, "Pod templates combined: {0}", podTemplate);
359376
return podTemplate;
@@ -503,6 +520,14 @@ public static Pod parseFromYaml(String yaml) {
503520
}
504521
}
505522

523+
public static Collection<String> validateYamlContainerNames(List<String> yamls) {
524+
Collection<String> errors = new ArrayList<>();
525+
for (String yaml : yamls) {
526+
errors.addAll(validateYamlContainerNames(yaml));
527+
}
528+
return errors;
529+
}
530+
506531
public static Collection<String> validateYamlContainerNames(String yaml) {
507532
if (StringUtils.isBlank(yaml)) {
508533
return Collections.emptyList();

src/main/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStepExecution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public boolean start() throws Exception {
113113
throw new AbortException(Messages.RFC1123_error(container.getName()));
114114
}
115115
}
116-
Collection<String> errors = PodTemplateUtils.validateYamlContainerNames(newTemplate.getYaml());
116+
Collection<String> errors = PodTemplateUtils.validateYamlContainerNames(newTemplate.getYamls());
117117
if (!errors.isEmpty()) {
118118
throw new AbortException(Messages.RFC1123_error(String.join(", ", errors)));
119119
}

0 commit comments

Comments
 (0)