|
28 | 28 | import javax.annotation.Nonnull;
|
29 | 29 |
|
30 | 30 | 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; |
35 | 31 | import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar;
|
36 | 32 | import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume;
|
37 | 33 | import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume;
|
|
44 | 40 | import hudson.Util;
|
45 | 41 | import hudson.model.Label;
|
46 | 42 | import hudson.model.Node;
|
47 |
| -import hudson.tools.ToolLocationNodeProperty; |
48 | 43 | import io.fabric8.kubernetes.api.model.Container;
|
49 | 44 | import io.fabric8.kubernetes.api.model.ContainerBuilder;
|
50 | 45 | import io.fabric8.kubernetes.api.model.EnvFromSource;
|
@@ -185,6 +180,22 @@ private static Map<String, Quantity> combineResources(Container parent, Containe
|
185 | 180 | );
|
186 | 181 | }
|
187 | 182 |
|
| 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 | + |
188 | 199 | /**
|
189 | 200 | * Combines a Pod with its parent.
|
190 | 201 | * @param parent The parent Pod (nullable).
|
@@ -222,9 +233,7 @@ public static Pod combine(Pod parent, Pod template) {
|
222 | 233 | .collect(toMap(c -> c.getName(), c -> combine(parentContainers.get(c.getName()), c))));
|
223 | 234 |
|
224 | 235 | // 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()); |
228 | 237 |
|
229 | 238 | // Tolerations
|
230 | 239 | List<Toleration> combinedTolerations = Lists.newLinkedList();
|
@@ -268,6 +277,12 @@ public static Pod combine(Pod parent, Pod template) {
|
268 | 277 | return pod;
|
269 | 278 | }
|
270 | 279 |
|
| 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 | + |
271 | 286 | /**
|
272 | 287 | * Combines a {@link PodTemplate} with its parent.
|
273 | 288 | * @param parent The parent container template (nullable).
|
@@ -353,7 +368,9 @@ public static PodTemplate combine(PodTemplate parent, PodTemplate template) {
|
353 | 368 | template.isCustomWorkspaceVolumeEnabled() : parent.isCustomWorkspaceVolumeEnabled());
|
354 | 369 | podTemplate.setPodRetention(template.getPodRetention());
|
355 | 370 |
|
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); |
357 | 374 |
|
358 | 375 | LOGGER.log(Level.FINEST, "Pod templates combined: {0}", podTemplate);
|
359 | 376 | return podTemplate;
|
@@ -503,6 +520,14 @@ public static Pod parseFromYaml(String yaml) {
|
503 | 520 | }
|
504 | 521 | }
|
505 | 522 |
|
| 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 | + |
506 | 531 | public static Collection<String> validateYamlContainerNames(String yaml) {
|
507 | 532 | if (StringUtils.isBlank(yaml)) {
|
508 | 533 | return Collections.emptyList();
|
|
0 commit comments