Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit da23cc6

Browse files
committed
Remove repettive code when deteriminng init container properties
1 parent 637d2f5 commit da23cc6

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,10 @@ Collection<Container> getInitContainers(Map<String, String> kubernetesDeployerPr
648648
}
649649

650650
private @Nullable Container initContainerFromProperties(Map<String, String> kubeProps, String propertyKey) {
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-
651+
String name = getFirstProperty(kubeProps, propertyKey, ".name", ".containerName");
652+
String image = getFirstProperty(kubeProps, propertyKey, ".image", ".imageName");
656653
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");
654+
String commandStr = getFirstProperty(kubeProps, propertyKey, ".command", ".commands");
659655
List<String> commands = StringUtils.hasText(commandStr) ? Arrays.asList(commandStr.split(",")) : Collections.emptyList();
660656
String envString = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".env");
661657
envString = StringUtils.hasText(envString) ? envString : PropertyParserUtils.getDeploymentPropertyValue(kubeProps, propertyKey + ".environmentVariables");
@@ -672,6 +668,16 @@ Collection<Container> getInitContainers(Map<String, String> kubernetesDeployerPr
672668
return null;
673669
}
674670

671+
public static String getFirstProperty(Map<String, String> kubeProps, String baseKey, String... suffixes) {
672+
for (String suffix : suffixes) {
673+
String value = PropertyParserUtils.getDeploymentPropertyValue(kubeProps, baseKey + suffix);
674+
if (StringUtils.hasText(value)) {
675+
return value;
676+
}
677+
}
678+
return null;
679+
}
680+
675681
private Container containerFromProps(InitContainer initContainerProps) {
676682
return new ContainerBuilder()
677683
.withName(initContainerProps.getName())

0 commit comments

Comments
 (0)