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