Skip to content

Commit f47525b

Browse files
authored
Merge pull request #340 from brandonvin/drop-joda-time
Replace joda-time with java.time
2 parents 5ad3f6e + 7afdabc commit f47525b

15 files changed

+103
-130
lines changed

build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
apply plugin: 'java'
19-
apply from: "https://raw.githubusercontent.com/gocd/gocd-plugin-gradle-task-helpers/master/helper.gradle?_=${(int) (new Date().toInstant().epochSecond / 60)}"
19+
apply from: "https://raw.githubusercontent.com/gocd/gocd-plugin-gradle-task-helpers/master/helper.gradle?_=${(int) (Instant.now().epochSecond / 60)}"
2020

2121
gocdPlugin {
2222
id = 'cd.go.contrib.elasticagent.kubernetes'
@@ -65,7 +65,6 @@ dependencies {
6565
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
6666
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
6767
implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
68-
implementation group: 'joda-time', name: 'joda-time', version: '2.12.5'
6968
implementation group: 'io.fabric8', name: 'kubernetes-client', version: '5.12.4'
7069
implementation group: 'com.github.spullara.mustache.java', name: 'compiler', version: '0.9.10'
7170
implementation group: 'org.freemarker', name: 'freemarker', version: '2.3.32'

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

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,33 @@
1616

1717
package cd.go.contrib.elasticagent;
1818

19-
import org.joda.time.DateTime;
20-
import org.joda.time.Period;
19+
import java.time.Instant;
2120

2221
public interface Clock {
23-
Clock DEFAULT = DateTime::new;
22+
Clock DEFAULT = Instant::now;
2423

25-
DateTime now();
24+
Instant now();
2625

2726
class TestClock implements Clock {
2827

29-
DateTime time = null;
28+
Instant time = null;
3029

31-
public TestClock(DateTime time) {
30+
public TestClock(Instant time) {
3231
this.time = time;
3332
}
3433

3534
public TestClock() {
36-
this(new DateTime());
35+
this(Instant.now());
3736
}
3837

3938
@Override
40-
public DateTime now() {
39+
public Instant now() {
4140
return time;
4241
}
4342

44-
public TestClock reset() {
45-
time = new DateTime();
46-
return this;
47-
}
48-
49-
public TestClock set(DateTime time) {
43+
public TestClock set(Instant time) {
5044
this.time = time;
5145
return this;
5246
}
53-
54-
public TestClock rewind(Period period) {
55-
this.time = this.time.minus(period);
56-
return this;
57-
}
58-
59-
public TestClock forward(Period period) {
60-
this.time = this.time.plus(period);
61-
return this;
62-
}
6347
}
6448
}

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

Lines changed: 2 additions & 1 deletion
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,7 +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";
56-
String KUBERNETES_POD_CREATION_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
57+
DateTimeFormatter KUBERNETES_POD_CREATION_TIME_FORMAT = DateTimeFormatter.ISO_INSTANT;
5758

5859
String POD_POSTFIX = "POD_POSTFIX";
5960
String CONTAINER_POSTFIX = "CONTAINER_POSTFIX";

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@
2222
import io.fabric8.kubernetes.api.model.PodList;
2323
import io.fabric8.kubernetes.client.KubernetesClient;
2424
import org.apache.commons.lang3.StringUtils;
25-
import org.joda.time.DateTime;
26-
import org.joda.time.Period;
2725

2826
import java.net.SocketTimeoutException;
27+
import java.time.Duration;
28+
import java.time.Instant;
2929
import java.util.ArrayList;
30-
import java.util.Date;
3130
import java.util.Map;
3231
import java.util.concurrent.ConcurrentHashMap;
3332
import java.util.concurrent.Semaphore;
3433

3534
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
36-
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
3735
import static java.text.MessageFormat.format;
3836

3937
public class KubernetesAgentInstances implements AgentInstances<KubernetesInstance> {
@@ -196,7 +194,7 @@ public void register(KubernetesInstance instance) {
196194
}
197195

198196
private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings settings, Agents knownAgents) throws Exception {
199-
Period period = settings.getAutoRegisterPeriod();
197+
Duration period = settings.getAutoRegisterPeriod();
200198
KubernetesAgentInstances unregisteredInstances = new KubernetesAgentInstances();
201199
KubernetesClient client = factory.client(settings);
202200

@@ -211,10 +209,9 @@ private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings setting
211209
continue;
212210
}
213211

214-
Date createdAt = getSimpleDateFormat().parse(pod.getMetadata().getCreationTimestamp());
215-
DateTime dateTimeCreated = new DateTime(createdAt);
212+
Instant createdAt = Constants.KUBERNETES_POD_CREATION_TIME_FORMAT.parse(pod.getMetadata().getCreationTimestamp(), Instant::from);
216213

217-
if (clock.now().isAfter(dateTimeCreated.plus(period))) {
214+
if (clock.now().isAfter(createdAt.plus(period))) {
218215
unregisteredInstances.register(kubernetesInstanceFactory.fromKubernetesPod(pod));
219216
}
220217
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public synchronized KubernetesClient client(PluginSettings clusterProfileConfigu
5858
LOG.debug(format("Creating a new client because {0}.", (client == null) ? "client is null" : "cluster profile configurations has changed"));
5959
this.clusterProfileConfigurations = clusterProfileConfigurations;
6060
this.client = createClientFor(clusterProfileConfigurations);
61-
this.clientCreatedTime = this.clock.now().getMillis();
61+
this.clientCreatedTime = this.clock.now().toEpochMilli();
6262
LOG.debug("New client is created.");
6363

6464
return this.client;
6565
}
6666

6767
private void clearOutClientOnTimer() {
68-
long currentTime = this.clock.now().getMillis();
68+
long currentTime = this.clock.now().toEpochMilli();
6969
long differenceInMinutes = TimeUnit.MILLISECONDS.toMinutes(currentTime - this.clientCreatedTime);
7070
if (differenceInMinutes > getKubernetesClientRecycleInterval()) {
7171
LOG.info("Recycling kubernetes client on timer...");

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

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

1919
import io.fabric8.kubernetes.client.KubernetesClient;
20-
import org.joda.time.DateTime;
21-
import org.joda.time.DateTimeZone;
2220

21+
import java.time.Instant;
2322
import java.util.Map;
2423

2524
public class KubernetesInstance {
26-
private final DateTime createdAt;
25+
private final Instant createdAt;
2726
private final String environment;
2827
private final String name;
2928
private final Map<String, String> properties;
3029
private final Long jobId;
3130
private final PodState state;
3231

33-
public KubernetesInstance(DateTime createdAt, String environment, String name, Map<String, String> properties, Long jobId, PodState state) {
34-
this.createdAt = createdAt.withZone(DateTimeZone.UTC);
32+
public KubernetesInstance(Instant createdAt, String environment, String name, Map<String, String> properties, Long jobId, PodState state) {
33+
this.createdAt = createdAt;
3534
this.environment = environment;
3635
this.name = name;
3736
this.properties = properties;
@@ -47,7 +46,7 @@ public String name() {
4746
return name;
4847
}
4948

50-
public DateTime createdAt() {
49+
public Instant createdAt() {
5150
return createdAt;
5251
}
5352

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@
2828
import io.fabric8.kubernetes.client.KubernetesClient;
2929
import org.apache.commons.io.FileUtils;
3030
import org.apache.commons.lang3.StringUtils;
31-
import org.joda.time.DateTime;
32-
import org.joda.time.DateTimeZone;
3331

3432
import java.io.File;
3533
import java.io.IOException;
3634
import java.io.StringReader;
3735
import java.io.StringWriter;
3836
import java.net.URL;
39-
import java.text.ParseException;
37+
import java.time.Instant;
38+
import java.time.format.DateTimeParseException;
4039
import java.util.*;
4140

4241
import static cd.go.contrib.elasticagent.Constants.*;
4342
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
4443
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.*;
45-
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
4644
import static java.lang.String.valueOf;
4745
import static java.nio.charset.StandardCharsets.UTF_8;
4846
import static java.text.MessageFormat.format;
@@ -64,7 +62,7 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
6462
}
6563
}
6664
else {
67-
if (Boolean.valueOf(request.properties().get(SPECIFIED_USING_POD_CONFIGURATION.getKey()))) {
65+
if (Boolean.parseBoolean(request.properties().get(SPECIFIED_USING_POD_CONFIGURATION.getKey()))) {
6866
return createUsingPodYaml(request, settings, client, pluginRequest);
6967
} else {
7068
return createUsingProperties(request, settings, client, pluginRequest);
@@ -105,7 +103,7 @@ private Boolean privileged(CreateAgentRequest request) {
105103
}
106104

107105
private void setGoCDMetadata(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest, Pod elasticAgentPod) {
108-
elasticAgentPod.getMetadata().setCreationTimestamp(getSimpleDateFormat().format(new Date()));
106+
elasticAgentPod.getMetadata().setCreationTimestamp(KUBERNETES_POD_CREATION_TIME_FORMAT.format(Instant.now()));
109107

110108
setContainerEnvVariables(elasticAgentPod, request, settings, pluginRequest);
111109
setAnnotations(elasticAgentPod, request);
@@ -158,14 +156,14 @@ KubernetesInstance fromKubernetesPod(Pod elasticAgentPod) {
158156
KubernetesInstance kubernetesInstance;
159157
try {
160158
ObjectMeta metadata = elasticAgentPod.getMetadata();
161-
DateTime createdAt = DateTime.now().withZone(DateTimeZone.UTC);
159+
Instant createdAt = Instant.now();
162160
if (StringUtils.isNotBlank(metadata.getCreationTimestamp())) {
163-
createdAt = new DateTime(getSimpleDateFormat().parse(metadata.getCreationTimestamp())).withZone(DateTimeZone.UTC);
161+
createdAt = Constants.KUBERNETES_POD_CREATION_TIME_FORMAT.parse(metadata.getCreationTimestamp(), Instant::from);
164162
}
165163
String environment = metadata.getLabels().get(ENVIRONMENT_LABEL_KEY);
166164
Long jobId = Long.valueOf(metadata.getLabels().get(JOB_ID_LABEL_KEY));
167165
kubernetesInstance = new KubernetesInstance(createdAt, environment, metadata.getName(), metadata.getAnnotations(), jobId, PodState.fromPod(elasticAgentPod));
168-
} catch (ParseException e) {
166+
} catch (DateTimeParseException e) {
169167
throw new RuntimeException(e);
170168
}
171169
return kubernetesInstance;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import com.google.gson.annotations.Expose;
2222
import com.google.gson.annotations.SerializedName;
2323
import org.apache.commons.lang3.StringUtils;
24-
import org.joda.time.Period;
24+
import java.time.Duration;
25+
import java.time.temporal.ChronoUnit;
2526

2627
import static cd.go.contrib.elasticagent.utils.Util.IntTypeAdapter;
2728

@@ -54,7 +55,7 @@ public class PluginSettings {
5455
@SerializedName("namespace")
5556
private String namespace;
5657

57-
private Period autoRegisterPeriod;
58+
private Duration autoRegisterPeriod;
5859

5960
public PluginSettings() {
6061
}
@@ -72,9 +73,9 @@ public static PluginSettings fromJSON(String json) {
7273
return gson.fromJson(json, PluginSettings.class);
7374
}
7475

75-
public Period getAutoRegisterPeriod() {
76+
public Duration getAutoRegisterPeriod() {
7677
if (this.autoRegisterPeriod == null) {
77-
this.autoRegisterPeriod = new Period().withMinutes(getAutoRegisterTimeout());
78+
this.autoRegisterPeriod = Duration.of(getAutoRegisterTimeout(), ChronoUnit.MINUTES);
7879
}
7980
return this.autoRegisterPeriod;
8081
}

src/main/java/cd/go/contrib/elasticagent/executors/CreateAgentRequestExecutor.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
import cd.go.contrib.elasticagent.requests.CreateAgentRequest;
2121
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2222
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
23-
import org.joda.time.DateTime;
24-
import org.joda.time.LocalTime;
25-
import org.joda.time.format.DateTimeFormat;
26-
import org.joda.time.format.DateTimeFormatter;
2723

2824
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
2925
import static java.text.MessageFormat.format;
3026

27+
import java.time.Instant;
28+
import java.time.LocalDateTime;
29+
import java.time.ZoneOffset;
30+
import java.time.format.DateTimeFormatter;
31+
3132
public class CreateAgentRequestExecutor implements RequestExecutor {
32-
private static final DateTimeFormatter MESSAGE_PREFIX_FORMATTER = DateTimeFormat.forPattern("'##|'HH:mm:ss.SSS '[go]'");
33+
private static final DateTimeFormatter MESSAGE_PREFIX_FORMATTER = DateTimeFormatter.ofPattern("'##|'HH:mm:ss.SSS '[go]'");
34+
private static final DateTimeFormatter UTC_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss +00:00");
3335
private final AgentInstances<KubernetesInstance> agentInstances;
3436
private final PluginRequest pluginRequest;
3537
private final CreateAgentRequest request;
@@ -44,10 +46,11 @@ public CreateAgentRequestExecutor(CreateAgentRequest request, AgentInstances<Kub
4446
public GoPluginApiResponse execute() throws Exception {
4547
LOG.debug(format("[Create Agent] creating elastic agent for profile {0} in cluster {1}", request.properties(), request.clusterProfileProperties()));
4648
ConsoleLogAppender consoleLogAppender = text -> {
47-
final String message = String.format("%s %s\n", LocalTime.now().toString(MESSAGE_PREFIX_FORMATTER), text);
49+
final String message = String.format("%s %s\n", MESSAGE_PREFIX_FORMATTER.format(LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC)), text);
4850
pluginRequest.appendToConsoleLog(request.jobIdentifier(), message);
4951
};
50-
consoleLogAppender.accept(format("Received request to create a pod for job {0} in cluster {1} at {2}", request.jobIdentifier(), request.clusterProfileProperties().getClusterUrl(), new DateTime().toString("yyyy-MM-dd HH:mm:ss ZZ")));
52+
LocalDateTime localNow = LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC);
53+
consoleLogAppender.accept(format("Received request to create a pod for job {0} in cluster {1} at {2}", request.jobIdentifier(), request.clusterProfileProperties().getClusterUrl(), UTC_FORMAT.format(localNow)));
5154
try {
5255
agentInstances.create(request, request.clusterProfileProperties(), pluginRequest, consoleLogAppender);
5356
} catch (Exception e) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +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;
22+
import java.time.Instant;
2423
import java.util.Date;
2524

2625
public class KubernetesPod {
2726
private final String podName;
28-
private String nodeName;
27+
private final String nodeName;
2928
private final String image;
3029
private final Date creationTimestamp;
3130
private final String podIP;
3231
private final String status;
33-
private JobIdentifier jobIdentifier;
32+
private final JobIdentifier jobIdentifier;
3433

35-
public KubernetesPod(Pod pod) throws ParseException {
34+
public KubernetesPod(Pod pod) {
3635
jobIdentifier = JobIdentifier.fromJson(pod.getMetadata().getAnnotations().get(Constants.JOB_IDENTIFIER_LABEL_KEY));
3736
podName = pod.getMetadata().getName();
3837
image = pod.getSpec().getContainers().get(0).getImage();
3938
podIP = pod.getStatus().getPodIP();
40-
creationTimestamp = Util.getSimpleDateFormat().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));
4141
status = pod.getStatus().getPhase();
4242

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

0 commit comments

Comments
 (0)