Skip to content

Commit 75fca96

Browse files
committed
Replace joda-time with Java 8 standard lib
1 parent 494889d commit 75fca96

13 files changed

+84
-100
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.2'
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/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 = Instant.parse(pod.getMetadata().getCreationTimestamp());
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: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@
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.*;
@@ -105,7 +104,7 @@ private Boolean privileged(CreateAgentRequest request) {
105104
}
106105

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

110109
setContainerEnvVariables(elasticAgentPod, request, settings, pluginRequest);
111110
setAnnotations(elasticAgentPod, request);
@@ -158,14 +157,14 @@ KubernetesInstance fromKubernetesPod(Pod elasticAgentPod) {
158157
KubernetesInstance kubernetesInstance;
159158
try {
160159
ObjectMeta metadata = elasticAgentPod.getMetadata();
161-
DateTime createdAt = DateTime.now().withZone(DateTimeZone.UTC);
160+
Instant createdAt = Instant.now();
162161
if (StringUtils.isNotBlank(metadata.getCreationTimestamp())) {
163-
createdAt = new DateTime(getSimpleDateFormat().parse(metadata.getCreationTimestamp())).withZone(DateTimeZone.UTC);
162+
createdAt = Instant.parse(metadata.getCreationTimestamp());
164163
}
165164
String environment = metadata.getLabels().get(ENVIRONMENT_LABEL_KEY);
166165
Long jobId = Long.valueOf(metadata.getLabels().get(JOB_ID_LABEL_KEY));
167166
kubernetesInstance = new KubernetesInstance(createdAt, environment, metadata.getName(), metadata.getAnnotations(), jobId, PodState.fromPod(elasticAgentPod));
168-
} catch (ParseException e) {
167+
} catch (DateTimeParseException e) {
169168
throw new RuntimeException(e);
170169
}
171170
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.fabric8.kubernetes.api.model.Pod;
2222

2323
import java.text.ParseException;
24+
import java.time.Instant;
2425
import java.util.Date;
2526

2627
public class KubernetesPod {
@@ -37,7 +38,7 @@ public KubernetesPod(Pod pod) throws ParseException {
3738
podName = pod.getMetadata().getName();
3839
image = pod.getSpec().getContainers().get(0).getImage();
3940
podIP = pod.getStatus().getPodIP();
40-
creationTimestamp = Util.getSimpleDateFormat().parse(pod.getMetadata().getCreationTimestamp());
41+
creationTimestamp = Date.from(Instant.parse(pod.getMetadata().getCreationTimestamp()));
4142
status = pod.getStatus().getPhase();
4243

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

src/main/java/cd/go/contrib/elasticagent/utils/Util.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import java.io.InputStream;
2727
import java.io.StringReader;
2828
import java.nio.charset.StandardCharsets;
29-
import java.text.SimpleDateFormat;
29+
import java.time.ZoneOffset;
30+
import java.time.format.DateTimeFormatter;
3031
import java.util.Properties;
31-
import java.util.TimeZone;
3232

3333
import static cd.go.contrib.elasticagent.Constants.KUBERNETES_POD_CREATION_TIME_FORMAT;
3434

@@ -38,10 +38,8 @@ public class Util {
3838
.excludeFieldsWithoutExposeAnnotation()
3939
.create();
4040

41-
public static SimpleDateFormat getSimpleDateFormat() {
42-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(KUBERNETES_POD_CREATION_TIME_FORMAT);
43-
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
44-
return simpleDateFormat;
41+
public static DateTimeFormatter getSimpleDateFormat() {
42+
return DateTimeFormatter.ofPattern(KUBERNETES_POD_CREATION_TIME_FORMAT).withZone(ZoneOffset.UTC);
4543
}
4644

4745
public static String readResource(String resourceFile) {

0 commit comments

Comments
 (0)