|
17 | 17 |
|
18 | 18 | import java.util.ArrayList;
|
19 | 19 | import java.util.Arrays;
|
| 20 | +import java.util.Collection; |
20 | 21 | import java.util.Collections;
|
21 | 22 | import java.util.HashMap;
|
22 | 23 | import java.util.List;
|
|
65 | 66 | import org.springframework.cloud.deployer.spi.util.ByteSizeUtils;
|
66 | 67 | import org.springframework.cloud.deployer.spi.util.CommandLineTokenizer;
|
67 | 68 | import org.springframework.core.io.ByteArrayResource;
|
| 69 | +import org.springframework.lang.Nullable; |
68 | 70 | import org.springframework.util.Assert;
|
69 | 71 | import org.springframework.util.CollectionUtils;
|
70 | 72 | import org.springframework.util.StringUtils;
|
|
77 | 79 | * @author Chris Schaefer
|
78 | 80 | * @author Ilayaperumal Gopinathan
|
79 | 81 | * @author Chris Bono
|
| 82 | + * @author Corneil du Plessis |
80 | 83 | */
|
81 | 84 |
|
82 | 85 | class DeploymentPropertiesResolver {
|
@@ -586,44 +589,79 @@ Affinity getAffinityRules(Map<String, String> kubernetesDeployerProperties) {
|
586 | 589 | return affinity;
|
587 | 590 | }
|
588 | 591 |
|
589 |
| - Container getInitContainer(Map<String, String> kubernetesDeployerProperties) { |
| 592 | + Collection<Container> getInitContainers(Map<String, String> kubernetesDeployerProperties) { |
| 593 | + Collection<Container> initContainers = new ArrayList<>(); |
590 | 594 | KubernetesDeployerProperties deployerProperties = bindProperties(kubernetesDeployerProperties,
|
591 | 595 | this.propertyPrefix + ".initContainer", "initContainer");
|
592 | 596 |
|
593 | 597 | // Deployment prop passed in for entire '.initContainer'
|
594 | 598 | InitContainer initContainerProps = deployerProperties.getInitContainer();
|
595 | 599 | if (initContainerProps != null) {
|
596 |
| - return containerFromProps(initContainerProps); |
| 600 | + initContainers.add(containerFromProps(initContainerProps)); |
| 601 | + } else { |
| 602 | + String propertyKey = this.propertyPrefix + ".initContainer"; |
| 603 | + Container container = initContainerFromProperties(kubernetesDeployerProperties, propertyKey); |
| 604 | + if (container != null) { |
| 605 | + initContainers.add(container); |
| 606 | + } else { |
| 607 | + initContainerProps = this.properties.getInitContainer(); |
| 608 | + if (initContainerProps != null) { |
| 609 | + initContainers.add(containerFromProps(initContainerProps)); |
| 610 | + } |
| 611 | + } |
597 | 612 | }
|
| 613 | + KubernetesDeployerProperties initContainerDeployerProperties = bindProperties(kubernetesDeployerProperties, |
| 614 | + this.propertyPrefix + ".initContainers", "initContainers"); |
| 615 | + for (InitContainer initContainer : initContainerDeployerProperties.getInitContainers()) { |
| 616 | + initContainers.add(containerFromProps(initContainer)); |
| 617 | + } |
| 618 | + if(initContainerDeployerProperties.getInitContainers().isEmpty()) { |
| 619 | + for (int i = 0; ; i++) { |
| 620 | + String propertyKey = this.propertyPrefix + ".initContainers[" + i + "]"; |
| 621 | + // Get properties using binding |
| 622 | + KubernetesDeployerProperties kubeProps = bindProperties(kubernetesDeployerProperties, propertyKey, "initContainer"); |
| 623 | + if (kubeProps.getInitContainer() != null) { |
| 624 | + initContainers.add(containerFromProps(kubeProps.getInitContainer())); |
| 625 | + } else { |
| 626 | + // Get properties using FQN |
| 627 | + Container initContainer = initContainerFromProperties(kubernetesDeployerProperties, propertyKey); |
| 628 | + if (initContainer != null) { |
| 629 | + initContainers.add(initContainer); |
| 630 | + } else { |
| 631 | + // Use default is configured |
| 632 | + if (properties.getInitContainers().size() > i) { |
| 633 | + initContainers.add(containerFromProps(properties.getInitContainers().get(i))); |
| 634 | + } |
| 635 | + break; |
| 636 | + } |
| 637 | + } |
| 638 | + } |
| 639 | + } |
| 640 | + if (!properties.getInitContainers().isEmpty()) { |
| 641 | + // Add remaining defaults. |
| 642 | + for (int i = initContainers.size(); i < properties.getInitContainers().size(); i++) { |
| 643 | + initContainers.add(containerFromProps(properties.getInitContainers().get(i))); |
| 644 | + } |
| 645 | + } |
| 646 | + return initContainers; |
| 647 | + } |
598 | 648 |
|
599 |
| - // Deployment props passed in for specific '.initContainer.<property>' |
600 |
| - String containerName = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties, |
601 |
| - this.propertyPrefix + ".initContainer.containerName"); |
602 |
| - String imageName = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties, |
603 |
| - this.propertyPrefix + ".initContainer.imageName"); |
604 |
| - |
| 649 | + private @Nullable Container initContainerFromProperties(Map<String, String> kubeProps, String propertyKey) { |
| 650 | + String containerName = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".containerName"); |
| 651 | + String imageName = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".imageName"); |
605 | 652 | if (StringUtils.hasText(containerName) && StringUtils.hasText(imageName)) {
|
606 |
| - String commandsStr = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties, |
607 |
| - this.propertyPrefix + ".initContainer.commands"); |
608 |
| - List<String> commands = StringUtils.hasText(commandsStr) ? Arrays.stream(commandsStr.split(",")).collect(Collectors.toList()) : Collections.emptyList(); |
609 |
| - String envString = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties, |
610 |
| - this.propertyPrefix + ".initContainer.environmentVariables"); |
611 |
| - List<VolumeMount> vms = this.getInitContainerVolumeMounts(kubernetesDeployerProperties); |
| 653 | + String commandsStr = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".commands"); |
| 654 | + List<String> commands = StringUtils.hasText(commandsStr) ? Arrays.asList(commandsStr.split(",")) : Collections.emptyList(); |
| 655 | + String envString = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + "environmentVariables"); |
| 656 | + List<VolumeMount> vms = this.getInitContainerVolumeMounts(kubeProps, propertyKey); |
612 | 657 | return new ContainerBuilder()
|
613 | 658 | .withName(containerName)
|
614 | 659 | .withImage(imageName)
|
615 | 660 | .withCommand(commands)
|
616 |
| - .withEnv(toEnvironmentVariables((envString != null)? envString.split(","): new String[0])) |
| 661 | + .withEnv(toEnvironmentVariables((envString != null) ? envString.split(",") : new String[0])) |
617 | 662 | .addAllToVolumeMounts(vms)
|
618 | 663 | .build();
|
619 | 664 | }
|
620 |
| - |
621 |
| - // Default is global initContainer |
622 |
| - initContainerProps = this.properties.getInitContainer(); |
623 |
| - if (initContainerProps != null) { |
624 |
| - return containerFromProps(initContainerProps); |
625 |
| - } |
626 |
| - |
627 | 665 | return null;
|
628 | 666 | }
|
629 | 667 |
|
@@ -836,9 +874,11 @@ List<VolumeMount> getVolumeMounts(Map<String, String> deploymentProperties) {
|
836 | 874 | * @param deploymentProperties the deployment properties from {@link AppDeploymentRequest}
|
837 | 875 | * @return the configured volume mounts
|
838 | 876 | */
|
839 |
| - private List<VolumeMount> getInitContainerVolumeMounts(Map<String, String> deploymentProperties) { |
840 |
| - return this.getVolumeMounts(PropertyParserUtils.getDeploymentPropertyValue(deploymentProperties, |
841 |
| - this.propertyPrefix + ".initContainer.volumeMounts")); |
| 877 | + private List<VolumeMount> getInitContainerVolumeMounts(Map<String, String> deploymentProperties, String propertyKey) { |
| 878 | + return this.getVolumeMounts(PropertyParserUtils.getDeploymentPropertyValue( |
| 879 | + deploymentProperties, |
| 880 | + propertyKey + ".volumeMounts") |
| 881 | + ); |
842 | 882 | }
|
843 | 883 |
|
844 | 884 | private List<VolumeMount> getVolumeMounts(String propertyValue) {
|
|
0 commit comments