Skip to content

Commit

Permalink
Adding platform timeout configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Albertoimpl committed Sep 26, 2024
1 parent 2d927d4 commit b7da779
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.cloud.appbroker.deployer.BrokeredService;
import org.springframework.cloud.appbroker.deployer.BrokeredServices;
import org.springframework.cloud.appbroker.deployer.DeployerClient;
import org.springframework.cloud.appbroker.deployer.cloudfoundry.CloudFoundryTargetProperties;
import org.springframework.cloud.appbroker.extensions.parameters.BackingApplicationsParametersTransformationService;
import org.springframework.cloud.appbroker.extensions.parameters.BackingServicesParametersTransformationService;
import org.springframework.cloud.appbroker.extensions.parameters.EnvironmentMappingParametersTransformerFactory;
Expand Down Expand Up @@ -217,7 +218,9 @@ private ApplicationContextRunner configuredContext() {

"spring.cloud.appbroker.deployer.cloudfoundry.api-host=https://api.example.local",
"spring.cloud.appbroker.deployer.cloudfoundry.username=user",
"spring.cloud.appbroker.deployer.cloudfoundry.password=secret"
"spring.cloud.appbroker.deployer.cloudfoundry.password=secret",
"spring.cloud.appbroker.deployer.cloudfoundry.staging-timeout=3",
"spring.cloud.appbroker.deployer.cloudfoundry.deployment-timeout=4"
);
}

Expand Down Expand Up @@ -255,6 +258,12 @@ private void assertBeansCreated(AssertableApplicationContext context) {
}

private void assertPropertiesLoaded(AssertableApplicationContext context) {
CloudFoundryTargetProperties cloudFoundryTargetProperties = context.getBean(CloudFoundryTargetProperties.class);
assertThat(cloudFoundryTargetProperties.getUsername()).isEqualTo("user");
assertThat(cloudFoundryTargetProperties.getPassword()).isEqualTo("secret");
assertThat(cloudFoundryTargetProperties.getDeploymentTimeout()).isEqualTo(4L);
assertThat(cloudFoundryTargetProperties.getStagingTimeout()).isEqualTo(3L);

BrokeredServices brokeredServices = context.getBean(BrokeredServices.class);
assertThat(brokeredServices).hasSize(2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

//TODO: refactor this class
@SuppressWarnings({"PMD.GodClass", "PMD.CyclomaticComplexity", "PMD.ExcessiveClassLength"})
public class CloudFoundryAppDeployer implements AppDeployer, ResourceLoaderAware {

private static final Duration DEFAULT_PLATFORM_OPERATION_DURATION = Duration.ofMinutes(10);

private static final Logger LOG = LoggerFactory.getLogger(CloudFoundryAppDeployer.class);

private static final String REQUEST_LOG_TEMPLATE = "request={}";
Expand Down Expand Up @@ -454,7 +455,7 @@ private Mono<GetDeploymentResponse> waitForDeploymentDeployed(String deploymentI
.deploymentId(deploymentId)
.build())
.filter(this::deploymentFinished)
.repeatWhenEmpty(getExponentialBackOff())
.repeatWhenEmpty(getExponentialBackOff(getDeploymentTimeout()))
.doOnRequest(l -> LOG.debug("Waiting for deployment to complete. deploymentId={}", deploymentId))
.doOnSuccess(response -> {
LOG.info("Success waiting for deployment to complete. deploymentId={}", deploymentId);
Expand Down Expand Up @@ -503,7 +504,7 @@ private Mono<GetBuildResponse> waitForBuildStaged(String buildId) {
.buildId(buildId)
.build())
.filter(p -> p.getState().equals(BuildState.STAGED))
.repeatWhenEmpty(getExponentialBackOff())
.repeatWhenEmpty(getExponentialBackOff(getStagingTimeout()))
.doOnRequest(l -> LOG.debug("Waiting for build to stage. buildId={}", buildId))
.doOnSuccess(response -> {
LOG.info("Success waiting for build to stage. buildId={}", buildId);
Expand Down Expand Up @@ -535,7 +536,7 @@ private Mono<GetPackageResponse> waitForPackageReady(String packageId) {
.packages()
.get(GetPackageRequest.builder().packageId(packageId).build())
.filter(p -> p.getState().equals(PackageState.READY))
.repeatWhenEmpty(getExponentialBackOff())
.repeatWhenEmpty(getExponentialBackOff(DEFAULT_PLATFORM_OPERATION_DURATION))
.doOnRequest(l -> LOG.debug("Waiting for package ready. packageId={}", packageId))
.doOnSuccess(response -> {
LOG.info("Success waiting for package ready. packageId={}", packageId);
Expand Down Expand Up @@ -679,8 +680,22 @@ private Mono<CreatePackageResponse> createPackageForApplication(String applicati
applicationId, e.getMessage()), e));
}

private Function<Flux<Long>, Publisher<?>> getExponentialBackOff() {
return DelayUtils.exponentialBackOff(Duration.ofSeconds(2), Duration.ofMinutes(5), Duration.ofMinutes(10));
private Duration getStagingTimeout() {
if (targetProperties.getStagingTimeout() == null) {
return DEFAULT_PLATFORM_OPERATION_DURATION;
}
return Duration.ofMinutes(targetProperties.getStagingTimeout());
}

private Duration getDeploymentTimeout() {
if (targetProperties.getDeploymentTimeout() == null) {
return DEFAULT_PLATFORM_OPERATION_DURATION;
}
return Duration.ofMinutes(targetProperties.getDeploymentTimeout());
}

private Function<Flux<Long>, Publisher<?>> getExponentialBackOff(Duration timeout) {
return DelayUtils.exponentialBackOff(Duration.ofSeconds(2), Duration.ofMinutes(5), timeout);
}

private Mono<Void> pushApplication(DeployApplicationRequest request, Map<String, String> deploymentProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class CloudFoundryTargetProperties {

private String identityZoneSubdomain;

private Long deploymentTimeout;

private Long stagingTimeout;

public String getApiHost() {
return apiHost;
}
Expand Down Expand Up @@ -136,6 +140,22 @@ public ProxyConfiguration getProxyConfiguration() {
return null;
}

public Long getDeploymentTimeout() {
return deploymentTimeout;
}

public void setDeploymentTimeout(Long deploymentTimeout) {
this.deploymentTimeout = deploymentTimeout;
}

public Long getStagingTimeout() {
return stagingTimeout;
}

public void setStagingTimeout(Long stagingTimeout) {
this.stagingTimeout = stagingTimeout;
}

private static String parseApiHost(String api) {
final URI uri = URI.create(api);
return uri.getHost() == null ? api : uri.getHost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ spring:
*client-secret: EXAMPLE_SECRET*
*default-org: test*
*default-space: development*
*staging-timeout: 15*
*deployment-timeout: 10*
----
====

Expand Down

0 comments on commit b7da779

Please sign in to comment.