Skip to content

Commit 7afdabc

Browse files
committed
Revert to use of a fixed date format
Probably better not to depend on toString() which generally isn't considered to have a stable format across types or time.
1 parent b0eea5f commit 7afdabc

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

src/main/java/cd/go/contrib/elasticagent/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import cd.go.contrib.elasticagent.utils.Util;
2020
import com.thoughtworks.go.plugin.api.GoPluginIdentifier;
2121

22+
import java.time.format.DateTimeFormatter;
2223
import java.util.Collections;
2324

2425
public interface Constants {
@@ -53,6 +54,7 @@ public interface Constants {
5354
String KUBERNETES_POD_KIND_LABEL_KEY = "kind";
5455
String KUBERNETES_POD_KIND_LABEL_VALUE = "kubernetes-elastic-agent";
5556
String KUBERNETES_POD_NAME_PREFIX = "k8s-ea";
57+
DateTimeFormatter KUBERNETES_POD_CREATION_TIME_FORMAT = DateTimeFormatter.ISO_INSTANT;
5658

5759
String POD_POSTFIX = "POD_POSTFIX";
5860
String CONTAINER_POSTFIX = "CONTAINER_POSTFIX";

src/main/java/cd/go/contrib/elasticagent/KubernetesAgentInstances.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings setting
209209
continue;
210210
}
211211

212-
Instant createdAt = Instant.parse(pod.getMetadata().getCreationTimestamp());
212+
Instant createdAt = Constants.KUBERNETES_POD_CREATION_TIME_FORMAT.parse(pod.getMetadata().getCreationTimestamp(), Instant::from);
213213

214214
if (clock.now().isAfter(createdAt.plus(period))) {
215215
unregisteredInstances.register(kubernetesInstanceFactory.fromKubernetesPod(pod));

src/main/java/cd/go/contrib/elasticagent/KubernetesInstanceFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
6262
}
6363
}
6464
else {
65-
if (Boolean.valueOf(request.properties().get(SPECIFIED_USING_POD_CONFIGURATION.getKey()))) {
65+
if (Boolean.parseBoolean(request.properties().get(SPECIFIED_USING_POD_CONFIGURATION.getKey()))) {
6666
return createUsingPodYaml(request, settings, client, pluginRequest);
6767
} else {
6868
return createUsingProperties(request, settings, client, pluginRequest);
@@ -103,7 +103,7 @@ private Boolean privileged(CreateAgentRequest request) {
103103
}
104104

105105
private void setGoCDMetadata(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest, Pod elasticAgentPod) {
106-
elasticAgentPod.getMetadata().setCreationTimestamp(Instant.now().toString());
106+
elasticAgentPod.getMetadata().setCreationTimestamp(KUBERNETES_POD_CREATION_TIME_FORMAT.format(Instant.now()));
107107

108108
setContainerEnvVariables(elasticAgentPod, request, settings, pluginRequest);
109109
setAnnotations(elasticAgentPod, request);
@@ -158,7 +158,7 @@ KubernetesInstance fromKubernetesPod(Pod elasticAgentPod) {
158158
ObjectMeta metadata = elasticAgentPod.getMetadata();
159159
Instant createdAt = Instant.now();
160160
if (StringUtils.isNotBlank(metadata.getCreationTimestamp())) {
161-
createdAt = Instant.parse(metadata.getCreationTimestamp());
161+
createdAt = Constants.KUBERNETES_POD_CREATION_TIME_FORMAT.parse(metadata.getCreationTimestamp(), Instant::from);
162162
}
163163
String environment = metadata.getLabels().get(ENVIRONMENT_LABEL_KEY);
164164
Long jobId = Long.valueOf(metadata.getLabels().get(JOB_ID_LABEL_KEY));

src/main/java/cd/go/contrib/elasticagent/model/KubernetesPod.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,27 @@
1717
package cd.go.contrib.elasticagent.model;
1818

1919
import cd.go.contrib.elasticagent.Constants;
20-
import cd.go.contrib.elasticagent.utils.Util;
2120
import io.fabric8.kubernetes.api.model.Pod;
2221

23-
import java.text.ParseException;
2422
import java.time.Instant;
2523
import java.util.Date;
2624

2725
public class KubernetesPod {
2826
private final String podName;
29-
private String nodeName;
27+
private final String nodeName;
3028
private final String image;
3129
private final Date creationTimestamp;
3230
private final String podIP;
3331
private final String status;
34-
private JobIdentifier jobIdentifier;
32+
private final JobIdentifier jobIdentifier;
3533

36-
public KubernetesPod(Pod pod) throws ParseException {
34+
public KubernetesPod(Pod pod) {
3735
jobIdentifier = JobIdentifier.fromJson(pod.getMetadata().getAnnotations().get(Constants.JOB_IDENTIFIER_LABEL_KEY));
3836
podName = pod.getMetadata().getName();
3937
image = pod.getSpec().getContainers().get(0).getImage();
4038
podIP = pod.getStatus().getPodIP();
41-
creationTimestamp = Date.from(Instant.parse(pod.getMetadata().getCreationTimestamp()));
39+
final CharSequence text = pod.getMetadata().getCreationTimestamp();
40+
creationTimestamp = Date.from(Constants.KUBERNETES_POD_CREATION_TIME_FORMAT.parse(text, Instant::from));
4241
status = pod.getStatus().getPhase();
4342

4443
nodeName = pod.getSpec().getNodeName();

src/test/java/cd/go/contrib/elasticagent/builders/PluginStatusReportViewBuilderTest.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@
3535
import static org.mockito.Mockito.when;
3636

3737
public class PluginStatusReportViewBuilderTest {
38-
@Test
39-
public void shouldBuildStatusReportHtmlWithAgentStatusReportLink() throws IOException, TemplateException {
40-
KubernetesPod pod = mock(KubernetesPod.class);
41-
when(pod.getJobIdentifier()).thenReturn(new JobIdentifier(3243546575676657L));
42-
when(pod.getCreationTimestamp()).thenReturn(new Date());
38+
@Test
39+
public void shouldBuildStatusReportHtmlWithAgentStatusReportLink() throws IOException, TemplateException {
40+
KubernetesPod pod = mock(KubernetesPod.class);
41+
when(pod.getJobIdentifier()).thenReturn(new JobIdentifier(3243546575676657L));
42+
when(pod.getCreationTimestamp()).thenReturn(new Date());
4343

44-
KubernetesNode node = mock(KubernetesNode.class);
45-
when(node.getPods()).thenReturn(singletonList(pod));
44+
KubernetesNode node = mock(KubernetesNode.class);
45+
when(node.getPods()).thenReturn(singletonList(pod));
4646

47-
KubernetesCluster cluster = mock(KubernetesCluster.class);
48-
when(cluster.getNodes()).thenReturn(singletonList(node));
49-
when(cluster.getPluginId()).thenReturn("cd.go.contrib.elastic.agent.kubernetes");
50-
PluginStatusReportViewBuilder builder = PluginStatusReportViewBuilder.instance();
47+
KubernetesCluster cluster = mock(KubernetesCluster.class);
48+
when(cluster.getNodes()).thenReturn(singletonList(node));
49+
when(cluster.getPluginId()).thenReturn("cd.go.contrib.elastic.agent.kubernetes");
50+
PluginStatusReportViewBuilder builder = PluginStatusReportViewBuilder.instance();
5151

52-
String build = builder.build(builder.getTemplate("status-report.template.ftlh"), cluster);
52+
String build = builder.build(builder.getTemplate("status-report.template.ftlh"), cluster);
5353

54-
Document document = Jsoup.parse(build);
54+
Document document = Jsoup.parse(build);
5555

56-
Element link = document.selectFirst("tbody tr td a");
57-
System.out.println(link);
56+
Element link = document.selectFirst("tbody tr td a");
57+
System.out.println(link);
5858

59-
assertThat(link.attr("href")).isEqualTo("/go/admin/status_reports/cd.go.contrib.elastic.agent.kubernetes/agent/?job_id=3243546575676657");
60-
}
59+
assertThat(link.attr("href")).isEqualTo("/go/admin/status_reports/cd.go.contrib.elastic.agent.kubernetes/agent/?job_id=3243546575676657");
60+
}
6161

6262
}

src/test/java/cd/go/contrib/elasticagent/executors/ServerPingRequestExecutorTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
import java.time.Instant;
3333
import java.util.*;
3434

35-
import static java.time.temporal.ChronoUnit.MINUTES;
36-
3735
import static cd.go.contrib.elasticagent.Constants.JOB_ID_LABEL_KEY;
36+
import static java.time.temporal.ChronoUnit.MINUTES;
3837
import static org.junit.jupiter.api.Assertions.assertFalse;
3938
import static org.junit.jupiter.api.Assertions.assertTrue;
4039
import static org.mockito.Mockito.*;
@@ -67,7 +66,7 @@ public void setUp() {
6766
when(podResource.get()).thenReturn(mockedPod);
6867

6968
objectMetadata = new ObjectMeta();
70-
objectMetadata.setCreationTimestamp(Instant.now().toString());
69+
objectMetadata.setCreationTimestamp(Constants.KUBERNETES_POD_CREATION_TIME_FORMAT.format(Instant.now()));
7170

7271
when(mockedPod.getMetadata()).thenReturn(objectMetadata);
7372

@@ -205,7 +204,7 @@ public void testShouldTerminateUnregisteredInstances_forSingleCluster() throws E
205204
objectMetadata = new ObjectMeta();
206205
objectMetadata.setLabels(Collections.singletonMap(JOB_ID_LABEL_KEY, "20"));
207206
objectMetadata.setName(unregisteredAgentId1);
208-
objectMetadata.setCreationTimestamp(Instant.now().minus(20, MINUTES).toString());
207+
objectMetadata.setCreationTimestamp(Constants.KUBERNETES_POD_CREATION_TIME_FORMAT.format(Instant.now().minus(20, MINUTES)));
209208

210209
when(mockedPod.getMetadata()).thenReturn(objectMetadata);
211210

0 commit comments

Comments
 (0)