Skip to content

Commit 637d2f5

Browse files
committed
Rename key fields in the InitContainer
Resolves issue spring-attic#477
1 parent 06f4723 commit 637d2f5

File tree

5 files changed

+101
-45
lines changed

5 files changed

+101
-45
lines changed

spring-cloud-deployer-kubernetes/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/DefaultContainerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ public Container create(ContainerConfiguration containerConfiguration) {
170170

171171
ContainerBuilder container = new ContainerBuilder();
172172
container.withName(containerConfiguration.getAppId()).withImage(image).withEnv(envVars).withEnvFrom(envFromSources)
173-
.withArgs(appArgs).withVolumeMounts(deploymentPropertiesResolver.getVolumeMounts(deploymentProperties));
173+
.withArgs(appArgs).withImagePullPolicy(deploymentPropertiesResolver.getImagePullPolicy(deploymentProperties))
174+
.withVolumeMounts(deploymentPropertiesResolver.getVolumeMounts(deploymentProperties));
174175

175176
Set<Integer> ports = new HashSet<>();
176177

spring-cloud-deployer-kubernetes/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/DeploymentPropertiesResolver.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ Map<String, String> getNodeSelectors(Map<String, String> deploymentProperties) {
380380
return nodeSelectors;
381381
}
382382

383+
String getImagePullPolicy(Map<String, String> kubernetesDeployerProperties) {
384+
ImagePullPolicy imagePullPolicy = deduceImagePullPolicy(kubernetesDeployerProperties);
385+
return imagePullPolicy != null ? imagePullPolicy.name() : null;
386+
}
387+
383388
String getImagePullSecret(Map<String, String> kubernetesDeployerProperties) {
384389
String imagePullSecret = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties,
385390
this.propertyPrefix + ".imagePullSecret", "");
@@ -643,16 +648,22 @@ Collection<Container> getInitContainers(Map<String, String> kubernetesDeployerPr
643648
}
644649

645650
private @Nullable Container initContainerFromProperties(Map<String, String> kubeProps, String propertyKey) {
646-
String containerName = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".containerName");
647-
String imageName = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".imageName");
648-
if (StringUtils.hasText(containerName) && StringUtils.hasText(imageName)) {
649-
String commandsStr = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".commands");
650-
List<String> commands = StringUtils.hasText(commandsStr) ? Arrays.asList(commandsStr.split(",")) : Collections.emptyList();
651-
String envString = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".environmentVariables");
651+
String name = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".name");
652+
name = StringUtils.hasText(name) ? name : PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".containerName");
653+
String image = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".image");
654+
image = StringUtils.hasText(image) ? image : PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".imageName");
655+
656+
if (StringUtils.hasText(name) && StringUtils.hasText(image)) {
657+
String commandStr = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".command");
658+
commandStr = StringUtils.hasText(commandStr) ? commandStr : PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".commands");
659+
List<String> commands = StringUtils.hasText(commandStr) ? Arrays.asList(commandStr.split(",")) : Collections.emptyList();
660+
String envString = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".env");
661+
envString = StringUtils.hasText(envString) ? envString : PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".environmentVariables");
662+
652663
List<VolumeMount> vms = this.getInitContainerVolumeMounts(kubeProps, propertyKey);
653664
return new ContainerBuilder()
654-
.withName(containerName)
655-
.withImage(imageName)
665+
.withName(name)
666+
.withImage(image)
656667
.withCommand(commands)
657668
.withEnv(toEnvironmentVariables((envString != null) ? envString.split(",") : new String[0]))
658669
.addAllToVolumeMounts(vms)
@@ -663,9 +674,9 @@ Collection<Container> getInitContainers(Map<String, String> kubernetesDeployerPr
663674

664675
private Container containerFromProps(InitContainer initContainerProps) {
665676
return new ContainerBuilder()
666-
.withName(initContainerProps.getContainerName())
667-
.withImage(initContainerProps.getImageName())
668-
.withCommand(initContainerProps.getCommands())
677+
.withName(initContainerProps.getName())
678+
.withImage(initContainerProps.getImage())
679+
.withCommand(initContainerProps.getCommand())
669680
.withArgs(initContainerProps.getArgs())
670681
.withEnv(toEnvironmentVariables(initContainerProps.getEnvironmentVariables()))
671682
.addAllToVolumeMounts(Optional.ofNullable(initContainerProps.getVolumeMounts()).orElse(Collections.emptyList()))

spring-cloud-deployer-kubernetes/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesDeployerProperties.java

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import com.fasterxml.jackson.annotation.JsonAlias;
23+
import com.fasterxml.jackson.annotation.JsonProperty;
2224
import io.fabric8.kubernetes.api.model.NodeAffinity;
2325
import io.fabric8.kubernetes.api.model.PodAffinity;
2426
import io.fabric8.kubernetes.api.model.PodAntiAffinity;
@@ -980,42 +982,72 @@ static class Container extends io.fabric8.kubernetes.api.model.Container {
980982
}
981983

982984
public static class ContainerProperties {
983-
private String imageName;
984-
985-
private String containerName;
986-
987-
private List<String> commands;
988-
private List<String> args;
989-
private List<VolumeMount> volumeMounts;
985+
@JsonAlias("imageName")
986+
@JsonProperty("image")
987+
private String image;
988+
@JsonAlias("containerName")
989+
@JsonProperty("name")
990+
private String name;
991+
@JsonProperty("command")
992+
@JsonAlias("commands")
993+
private List<String> command;
994+
private List<String> args;
995+
private List<VolumeMount> volumeMounts;
990996

991997
/**
992998
* Environment variables to set for any deployed init container.
993999
*/
9941000
private String[] environmentVariables = new String[]{};
9951001

996-
public String getImageName() {
997-
return imageName;
998-
}
1002+
public String getName() {
1003+
return name;
1004+
}
9991005

1000-
public void setImageName(String imageName) {
1001-
this.imageName = imageName;
1002-
}
1006+
public void setName(String name) {
1007+
this.name = name;
1008+
}
1009+
@Deprecated
1010+
public String getImageName() {
1011+
return getImage();
1012+
}
1013+
@Deprecated
1014+
public void setImageName(String image) {
1015+
setImage(image);
1016+
}
1017+
public String getImage() {
1018+
return image;
1019+
}
10031020

1004-
public String getContainerName() {
1005-
return containerName;
1006-
}
1021+
public void setImage(String image) {
1022+
this.image = image;
1023+
}
10071024

1008-
public void setContainerName(String containerName) {
1009-
this.containerName = containerName;
1010-
}
1025+
@Deprecated
1026+
public String getContainerName() {
1027+
return getName();
1028+
}
10111029

1012-
public List<String> getCommands() {
1013-
return commands;
1014-
}
1030+
@Deprecated
1031+
public void setContainerName(String containerName) {
1032+
setName(containerName);
1033+
}
10151034

1016-
public void setCommands(List<String> commands) {
1017-
this.commands = commands;
1018-
}
1035+
public List<String> getCommand() {
1036+
return command;
1037+
}
1038+
1039+
public void setCommand(List<String> command) {
1040+
this.command = command;
1041+
}
1042+
@Deprecated
1043+
public List<String> getCommands() {
1044+
return getCommand();
1045+
}
1046+
1047+
@Deprecated
1048+
public void setCommands(List<String> commands) {
1049+
setCommand(commands);
1050+
}
10191051

10201052
public List<String> getArgs() {
10211053
return args;

spring-cloud-deployer-kubernetes/src/test/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAppDeployerIntegrationIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,9 +1327,9 @@ public void testCreateInitContainerWithEnvVariables() {
13271327
public void initContainerFromGlobalProps() {
13281328
// Set up a global initContainer (it should be chosen)
13291329
KubernetesDeployerProperties.InitContainer globalInitContainerProps = new KubernetesDeployerProperties.InitContainer();
1330-
globalInitContainerProps.setContainerName("test-global");
1331-
globalInitContainerProps.setImageName("busybox:latest");
1332-
globalInitContainerProps.setCommands(Arrays.asList("sh", "-c", "echo hello-global"));
1330+
globalInitContainerProps.setName("test-global");
1331+
globalInitContainerProps.setImage("busybox:latest");
1332+
globalInitContainerProps.setCommand(Arrays.asList("sh", "-c", "echo hello-global"));
13331333

13341334
KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
13351335
kubernetesDeployerProperties.setInitContainer(globalInitContainerProps);

spring-cloud-deployer-kubernetes/src/test/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAppDeployerTests.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -931,15 +931,21 @@ public void testMultipleInitContainerProperties() {
931931
props.put("spring.cloud.deployer.kubernetes.initContainers[1].imageName", "busybox:2");
932932
props.put("spring.cloud.deployer.kubernetes.initContainers[1].containerName", "bb_s2");
933933
props.put("spring.cloud.deployer.kubernetes.initContainers[1].commands", "sh,-c,script2.sh");
934-
props.put("spring.cloud.deployer.kubernetes.initContainers[2]", "{ \"imageName\": \"busybox:3\", \"containerName\": \"bb_s3\", \"args\": [\"-c\", \"script3.sh\"], \"volumeMounts\": [{\"mountPath\": \"/data\", \"name\": \"s3vol\", \"readOnly\": true}] }");
934+
props.put("spring.cloud.deployer.kubernetes.initContainers[1].environmentVariables", "foo=baz");
935935

936-
AppDefinition definition = new AppDefinition("app-test", null);
936+
props.put("spring.cloud.deployer.kubernetes.initContainers[2]", "{ \"image\": \"busybox:3\", \"name\": \"bb_s3\", \"args\": [\"-c\", \"script3.sh\"], \"volumeMounts\": [{\"mountPath\": \"/data\", \"name\": \"s3vol\", \"readOnly\": true}] }");
937+
props.put("spring.cloud.deployer.kubernetes.initContainers[3].image", "busybox:2");
938+
props.put("spring.cloud.deployer.kubernetes.initContainers[3].name", "bb_s4");
939+
props.put("spring.cloud.deployer.kubernetes.initContainers[3].command", "sh,-c,script4.sh");
940+
props.put("spring.cloud.deployer.kubernetes.initContainers[3].env", "foo=bar");
941+
942+
AppDefinition definition = new AppDefinition("app-test", null);
937943
AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition, getResource(), props);
938944

939945
deployer = k8sAppDeployer(new KubernetesDeployerProperties());
940946
PodSpec podSpec = deployer.createPodSpec(appDeploymentRequest);
941947
assertThat(podSpec.getInitContainers()).isNotEmpty();
942-
assertThat(podSpec.getInitContainers().size()).isEqualTo(3);
948+
assertThat(podSpec.getInitContainers().size()).isEqualTo(4);
943949
Container container0 = podSpec.getInitContainers().get(0);
944950
assertThat(container0.getImage()).isEqualTo("busybox:1");
945951
assertThat(container0.getName()).isEqualTo("bb_s1");
@@ -948,15 +954,21 @@ public void testMultipleInitContainerProperties() {
948954
assertThat(container1.getImage()).isEqualTo("busybox:2");
949955
assertThat(container1.getName()).isEqualTo("bb_s2");
950956
assertThat(container1.getCommand()).containsExactly("sh", "-c", "script2.sh");
951-
Container container2 = podSpec.getInitContainers().get(2);
957+
assertThat(container1.getEnv()).isEqualTo(Collections.singletonList(new EnvVar("foo","baz", null)));
958+
Container container2 = podSpec.getInitContainers().get(2);
952959
assertThat(container2.getImage()).isEqualTo("busybox:3");
953960
assertThat(container2.getName()).isEqualTo("bb_s3");
954961
assertThat(container2.getArgs()).containsExactly("-c", "script3.sh");
955962
assertThat(container2.getVolumeMounts()).isNotEmpty();
956963
assertThat(container2.getVolumeMounts().get(0).getName()).isEqualTo("s3vol");
957964
assertThat(container2.getVolumeMounts().get(0).getMountPath()).isEqualTo("/data");
958965
assertThat(container2.getVolumeMounts().get(0).getReadOnly()).isTrue();
959-
}
966+
Container container3 = podSpec.getInitContainers().get(3);
967+
assertThat(container3.getImage()).isEqualTo("busybox:2");
968+
assertThat(container3.getName()).isEqualTo("bb_s4");
969+
assertThat(container3.getCommand()).containsExactly("sh", "-c", "script4.sh");
970+
assertThat(container3.getEnv()).isEqualTo(Collections.singletonList(new EnvVar("foo","bar", null)));
971+
}
960972

961973
@Test
962974
public void testNodeAffinityProperty() {

0 commit comments

Comments
 (0)