mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(codedeploy): use service clock for lifecycle timestamps (#8557)
* fix(codedeploy): use service clock for lifecycle timestamps
This commit is contained in:
1 parent
93da7a6c26
commit
e47747845e
2 files changed
+78
-28
No files matched your search
@@ -35,7 +35,6 @@ import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -176,7 +175,7 @@ public class CodeDeployService {
|
||||
if (store == null) {
|
||||
continue;
|
||||
}
|
||||
double now = EpochSeconds.fromInstant(Instant.now());
|
||||
double now = EpochSeconds.fromInstant(clock.instant());
|
||||
boolean changed = false;
|
||||
for (String name : BUILT_IN_CONFIG_NAMES) {
|
||||
if (!store.containsKey(name)) {
|
||||
@@ -266,7 +265,7 @@ public class CodeDeployService {
|
||||
private Map<String, DeploymentConfig> deploymentConfigsFor(String region) {
|
||||
return deploymentConfigs.computeIfAbsent(region, r -> {
|
||||
ConcurrentHashMap<String, DeploymentConfig> store = new ConcurrentHashMap<>();
|
||||
double now = EpochSeconds.fromInstant(Instant.now());
|
||||
double now = EpochSeconds.fromInstant(clock.instant());
|
||||
for (String name : BUILT_IN_CONFIG_NAMES) {
|
||||
store.put(name, buildBuiltInConfig(name, now));
|
||||
}
|
||||
@@ -341,7 +340,7 @@ public class CodeDeployService {
|
||||
Application app = new Application();
|
||||
app.setApplicationId(UUID.randomUUID().toString());
|
||||
app.setApplicationName(name);
|
||||
app.setCreateTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
app.setCreateTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
app.setLinkedToGitHub(false);
|
||||
app.setComputePlatform(computePlatform != null ? computePlatform : "Server");
|
||||
store.put(name, app);
|
||||
@@ -546,7 +545,7 @@ public class CodeDeployService {
|
||||
cfg.setDeploymentConfigId(UUID.randomUUID().toString());
|
||||
cfg.setDeploymentConfigName(name);
|
||||
cfg.setMinimumHealthyHosts(minimumHealthyHosts);
|
||||
cfg.setCreateTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
cfg.setCreateTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
cfg.setComputePlatform(computePlatform != null ? computePlatform : "Server");
|
||||
cfg.setTrafficRoutingConfig(trafficRoutingConfig);
|
||||
cfg.setZonalConfig(zonalConfig);
|
||||
@@ -616,7 +615,7 @@ public class CodeDeployService {
|
||||
String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName();
|
||||
|
||||
String deploymentId = generateDeploymentId();
|
||||
double now = EpochSeconds.fromInstant(Instant.now());
|
||||
double now = EpochSeconds.fromInstant(clock.instant());
|
||||
|
||||
Deployment deployment = new Deployment();
|
||||
deployment.setDeploymentId(deploymentId);
|
||||
@@ -749,7 +748,7 @@ public class CodeDeployService {
|
||||
"instance/" + instanceName).toString());
|
||||
inst.setIamSessionArn(iamSessionArn);
|
||||
inst.setIamUserArn(iamUserArn);
|
||||
inst.setRegisterTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
inst.setRegisterTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
inst.setRegistrationStatus("Registered");
|
||||
store.put(instanceName, inst);
|
||||
persistRegion(onPremisesInstances, region);
|
||||
@@ -758,7 +757,7 @@ public class CodeDeployService {
|
||||
|
||||
public void deregisterOnPremisesInstance(String region, String instanceName) {
|
||||
OnPremisesInstance inst = requireOnPremisesInstance(region, instanceName);
|
||||
inst.setDeregisterTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
inst.setDeregisterTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
inst.setRegistrationStatus("Deregistered");
|
||||
persistRegion(onPremisesInstances, region);
|
||||
}
|
||||
@@ -820,7 +819,7 @@ public class CodeDeployService {
|
||||
String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName();
|
||||
|
||||
String deploymentId = generateDeploymentId();
|
||||
double now = EpochSeconds.fromInstant(Instant.now());
|
||||
double now = EpochSeconds.fromInstant(clock.instant());
|
||||
|
||||
Deployment deployment = new Deployment();
|
||||
deployment.setDeploymentId(deploymentId);
|
||||
@@ -880,7 +879,7 @@ public class CodeDeployService {
|
||||
String deploymentId = deployment.getDeploymentId();
|
||||
try {
|
||||
deployment.setStatus("InProgress");
|
||||
deployment.setStartTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setStartTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
instanceTargetMaps.forEach(m -> updateTargetStatus(m, "InProgress"));
|
||||
|
||||
boolean anyFailed = false;
|
||||
@@ -888,7 +887,7 @@ public class CodeDeployService {
|
||||
if (stopFlag.get()) {
|
||||
instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Skipped"));
|
||||
deployment.setStatus("Stopped");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
return;
|
||||
}
|
||||
String instanceId = instanceIds.get(i);
|
||||
@@ -908,18 +907,18 @@ public class CodeDeployService {
|
||||
} else {
|
||||
deployment.setStatus("Succeeded");
|
||||
}
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Skipped"));
|
||||
deployment.setStatus("Stopped");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Server deployment {0} failed: {1}", deploymentId, e.getMessage());
|
||||
instanceTargetMaps.forEach(m -> updateTargetStatus(m, "Failed"));
|
||||
deployment.setStatus("Failed");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
deployment.setErrorInformation(Map.of("code", "DeploymentFailed",
|
||||
"message", e.getMessage() != null ? e.getMessage() : "Unknown error"));
|
||||
} finally {
|
||||
@@ -1287,7 +1286,7 @@ public class CodeDeployService {
|
||||
String effectiveConfig = configName != null ? configName : group.getDeploymentConfigName();
|
||||
|
||||
String deploymentId = generateDeploymentId();
|
||||
double now = EpochSeconds.fromInstant(Instant.now());
|
||||
double now = EpochSeconds.fromInstant(clock.instant());
|
||||
|
||||
Deployment deployment = new Deployment();
|
||||
deployment.setDeploymentId(deploymentId);
|
||||
@@ -1387,7 +1386,7 @@ public class CodeDeployService {
|
||||
TaskSet blueTaskSet = null;
|
||||
try {
|
||||
deployment.setStatus("InProgress");
|
||||
deployment.setStartTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setStartTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
updateEcsTargetStatus(ecsTargetMap, "InProgress");
|
||||
|
||||
if (stopFlag.get()) { finishEcsStopped(deployment, ecsTargetMap); return; }
|
||||
@@ -1504,7 +1503,7 @@ public class CodeDeployService {
|
||||
|
||||
updateEcsTargetStatus(ecsTargetMap, "Succeeded");
|
||||
deployment.setStatus("Succeeded");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
@@ -1595,20 +1594,20 @@ public class CodeDeployService {
|
||||
|
||||
private void updateEcsTargetStatus(Map<String, Object> ecsTargetMap, String status) {
|
||||
ecsTargetMap.put("status", status);
|
||||
ecsTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now()));
|
||||
ecsTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(clock.instant()));
|
||||
}
|
||||
|
||||
private void finishEcsStopped(Deployment deployment, Map<String, Object> ecsTargetMap) {
|
||||
updateEcsTargetStatus(ecsTargetMap, "Skipped");
|
||||
deployment.setStatus("Stopped");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
}
|
||||
|
||||
private void finishEcsFailed(Deployment deployment, Map<String, Object> ecsTargetMap,
|
||||
String errorCode, String message) {
|
||||
updateEcsTargetStatus(ecsTargetMap, "Failed");
|
||||
deployment.setStatus("Failed");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
deployment.setErrorInformation(Map.of("code", errorCode, "message", message != null ? message : ""));
|
||||
}
|
||||
|
||||
@@ -1647,7 +1646,7 @@ public class CodeDeployService {
|
||||
String deploymentId = deployment.getDeploymentId();
|
||||
try {
|
||||
deployment.setStatus("InProgress");
|
||||
deployment.setStartTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setStartTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
updateTargetStatus(lambdaTargetMap, "InProgress");
|
||||
|
||||
if (stopFlag.get()) { finishStopped(deployment, lambdaTargetMap); return; }
|
||||
@@ -1676,7 +1675,7 @@ public class CodeDeployService {
|
||||
|
||||
updateTargetStatus(lambdaTargetMap, "Succeeded");
|
||||
deployment.setStatus("Succeeded");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
@@ -1822,18 +1821,18 @@ public class CodeDeployService {
|
||||
private Map<String, Object> addLifecycleEvent(Map<String, Object> lambdaTargetMap, String name) {
|
||||
Map<String, Object> event = new ConcurrentHashMap<>();
|
||||
event.put("lifecycleEventName", name);
|
||||
event.put("startTime", EpochSeconds.fromInstant(Instant.now()));
|
||||
event.put("startTime", EpochSeconds.fromInstant(clock.instant()));
|
||||
event.put("status", "InProgress");
|
||||
List<Map<String, Object>> events = (List<Map<String, Object>>) lambdaTargetMap.get("lifecycleEvents");
|
||||
if (events != null) {
|
||||
events.add(event);
|
||||
}
|
||||
lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now()));
|
||||
lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(clock.instant()));
|
||||
return event;
|
||||
}
|
||||
|
||||
private void finishLifecycleEvent(Map<String, Object> event, String status) {
|
||||
event.put("endTime", EpochSeconds.fromInstant(Instant.now()));
|
||||
event.put("endTime", EpochSeconds.fromInstant(clock.instant()));
|
||||
event.put("status", status);
|
||||
}
|
||||
|
||||
@@ -1845,20 +1844,20 @@ public class CodeDeployService {
|
||||
|
||||
private void updateTargetStatus(Map<String, Object> lambdaTargetMap, String status) {
|
||||
lambdaTargetMap.put("status", status);
|
||||
lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(Instant.now()));
|
||||
lambdaTargetMap.put("lastUpdatedAt", EpochSeconds.fromInstant(clock.instant()));
|
||||
}
|
||||
|
||||
private void finishStopped(Deployment deployment, Map<String, Object> lambdaTargetMap) {
|
||||
updateTargetStatus(lambdaTargetMap, "Skipped");
|
||||
deployment.setStatus("Stopped");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
}
|
||||
|
||||
private void finishFailed(Deployment deployment, Map<String, Object> lambdaTargetMap,
|
||||
String errorCode, String message) {
|
||||
updateTargetStatus(lambdaTargetMap, "Failed");
|
||||
deployment.setStatus("Failed");
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(Instant.now()));
|
||||
deployment.setCompleteTime(EpochSeconds.fromInstant(clock.instant()));
|
||||
deployment.setErrorInformation(Map.of("code", errorCode, "message", message != null ? message : ""));
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.floci.aws.services.codedeploy;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.EpochSeconds;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.services.codedeploy.model.Deployment;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
@@ -21,6 +22,8 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -74,6 +77,54 @@ class CodeDeployServiceTest {
|
||||
return svc;
|
||||
}
|
||||
|
||||
@Test
|
||||
void applicationAndConfigurationUseTheDeploymentClock() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC);
|
||||
CodeDeployService target = newService(Duration.ofHours(1), clock);
|
||||
double created = EpochSeconds.fromInstant(clock.instant());
|
||||
|
||||
assertEquals(created, target.createApplication(REGION, "clock-app", "Server", null).getCreateTime());
|
||||
assertEquals(created, target.createDeploymentConfig(REGION, "clock-config",
|
||||
Map.of("type", "HOST_COUNT", "value", 1), "Server", null, null).getCreateTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void onPremisesRegistrationAndDeregistrationUseTheDeploymentClock() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC);
|
||||
CodeDeployService target = newService(Duration.ofHours(1), clock);
|
||||
double registered = EpochSeconds.fromInstant(clock.instant());
|
||||
assertEquals(registered, target.registerOnPremisesInstance(REGION, "clock-host", null, null)
|
||||
.getRegisterTime());
|
||||
|
||||
target.deregisterOnPremisesInstance(REGION, "clock-host");
|
||||
|
||||
assertEquals(registered,
|
||||
target.getOnPremisesInstance(REGION, "clock-host").getDeregisterTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serverDeploymentCreationAndCompletionUseTheDeploymentClock() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2026-01-01T00:00:00Z"), ZoneOffset.UTC);
|
||||
CodeDeployService target = newService(Duration.ofHours(1), clock);
|
||||
double created = EpochSeconds.fromInstant(clock.instant());
|
||||
|
||||
target.registerOnPremisesInstance(REGION, "clock-deployment-host", null, null);
|
||||
when(ssmCommandService.isInstanceRegistered("clock-deployment-host", REGION)).thenReturn(true);
|
||||
when(ssmCommandService.sendCommandToInstance(eq("clock-deployment-host"),
|
||||
anyString(), anyMap(), eq(60), eq(REGION))).thenReturn("clock-command");
|
||||
when(ssmCommandService.getCommandInvocationStatus("clock-command", "clock-deployment-host", REGION))
|
||||
.thenReturn("Success");
|
||||
String id = createServerDeployment(target, "clock-deployment", "clock-group", 60);
|
||||
await().atMost(Duration.ofSeconds(5)).until(() ->
|
||||
"Succeeded".equals(target.getDeployment(REGION, id).getStatus())
|
||||
&& target.getDeployment(REGION, id).getCompleteTime() != null);
|
||||
Deployment deployment = target.getDeployment(REGION, id);
|
||||
|
||||
assertEquals(created, deployment.getCreateTime());
|
||||
assertEquals(created, deployment.getStartTime());
|
||||
assertEquals(created, deployment.getCompleteTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void onPremisesTagFilterTypesMatchOnlyRequestedTags() {
|
||||
for (String name : List.of("red-team", "blue-team", "red-env", "untagged")) {
|
||||
|
||||
Reference in new issue
Block a user