Skip to content

Commit 001b438

Browse files
committed
Fix code warnings
1 parent f0e7436 commit 001b438

File tree

54 files changed

+470
-483
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+470
-483
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.ArrayList;
2525
import java.util.Collection;
2626
import java.util.List;
27+
import java.util.Objects;
2728

2829
import static cd.go.contrib.elasticagent.utils.Util.GSON;
2930

@@ -105,7 +106,7 @@ public boolean equals(Object o) {
105106

106107
Agent agent = (Agent) o;
107108

108-
if (agentId != null ? !agentId.equals(agent.agentId) : agent.agentId != null) return false;
109+
if (!Objects.equals(agentId, agent.agentId)) return false;
109110
if (agentState != agent.agentState) return false;
110111
if (buildState != agent.buildState) return false;
111112
return configState == agent.configState;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public interface AgentInstances<T> {
3636
* @param pluginRequest the plugin request object
3737
* @param consoleLogAppender
3838
*/
39-
T create(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest, ConsoleLogAppender consoleLogAppender) throws Exception;
39+
T create(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest, ConsoleLogAppender consoleLogAppender);
4040

4141
/**
4242
* This message is sent when the plugin needs to terminate the agent instance.
4343
*
4444
* @param agentId the elastic agent id
4545
* @param settings the plugin settings object
4646
*/
47-
void terminate(String agentId, PluginSettings settings) throws Exception;
47+
void terminate(String agentId, PluginSettings settings);
4848

4949
/**
5050
* This message is sent from the {@link ServerPingRequestExecutor}
@@ -54,7 +54,7 @@ public interface AgentInstances<T> {
5454
* @param settings the plugin settings object
5555
* @param agents the list of all the agents
5656
*/
57-
void terminateUnregisteredInstances(PluginSettings settings, Agents agents) throws Exception;
57+
void terminateUnregisteredInstances(PluginSettings settings, Agents agents);
5858

5959
/**
6060
* This message is sent from the {@link ServerPingRequestExecutor}
@@ -75,7 +75,7 @@ public interface AgentInstances<T> {
7575
*
7676
* @param pluginSettings the plugin settings object
7777
*/
78-
void refreshAll(PluginSettings pluginSettings) throws Exception;
78+
void refreshAll(PluginSettings pluginSettings);
7979

8080
/**
8181
* This

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface Clock {
2525

2626
class TestClock implements Clock {
2727

28-
Instant time = null;
28+
Instant time;
2929

3030
public TestClock(Instant time) {
3131
this.time = time;

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
5959
final Integer maxAllowedContainers = settings.getMaxPendingPods();
6060
synchronized (instances) {
6161
refreshAll(settings);
62-
doWithLockOnSemaphore(new SetupSemaphore(maxAllowedContainers, instances, semaphore));
62+
new SetupSemaphore(maxAllowedContainers, instances, semaphore).run();
6363
consoleLogAppender.accept("Waiting to create agent pod.");
6464
if (semaphore.tryAcquire()) {
6565
return createKubernetesInstance(request, settings, pluginRequest, consoleLogAppender);
@@ -72,12 +72,6 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
7272
}
7373
}
7474

75-
private void doWithLockOnSemaphore(Runnable runnable) {
76-
synchronized (semaphore) {
77-
runnable.run();
78-
}
79-
}
80-
8175
private KubernetesInstance createKubernetesInstance(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest, ConsoleLogAppender consoleLogAppender) {
8276
JobIdentifier jobIdentifier = request.jobIdentifier();
8377
if (isAgentCreatedForJob(jobIdentifier.getJobId())) {
@@ -120,7 +114,7 @@ public void terminate(String agentId, PluginSettings settings) {
120114
}
121115

122116
@Override
123-
public void terminateUnregisteredInstances(PluginSettings settings, Agents agents) throws Exception {
117+
public void terminateUnregisteredInstances(PluginSettings settings, Agents agents) {
124118
KubernetesAgentInstances toTerminate = unregisteredAfterTimeout(settings, agents);
125119
if (toTerminate.instances.isEmpty()) {
126120
return;
@@ -195,7 +189,7 @@ public void register(KubernetesInstance instance) {
195189
instances.put(instance.name(), instance);
196190
}
197191

198-
private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings settings, Agents knownAgents) throws Exception {
192+
private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings settings, Agents knownAgents) {
199193
Duration period = settings.getAutoRegisterPeriod();
200194
KubernetesAgentInstances unregisteredInstances = new KubernetesAgentInstances();
201195
try (KubernetesClientFactory.CachedClient client = factory.client(settings)) {

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,12 @@ public class KubernetesInstanceFactory {
5050
public KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, KubernetesClient client, PluginRequest pluginRequest) {
5151
String podSpecType = request.properties().get(POD_SPEC_TYPE.getKey());
5252
if (podSpecType != null) {
53-
switch (podSpecType) {
54-
case "properties":
55-
return createUsingProperties(request, settings, client, pluginRequest);
56-
case "remote":
57-
return createUsingRemoteFile(request, settings, client, pluginRequest);
58-
case "yaml":
59-
return createUsingPodYaml(request, settings, client, pluginRequest);
60-
default:
61-
throw new IllegalArgumentException(String.format("Unsupported value for `PodSpecType`: %s", podSpecType));
62-
}
53+
return switch (podSpecType) {
54+
case "properties" -> createUsingProperties(request, settings, client, pluginRequest);
55+
case "remote" -> createUsingRemoteFile(request, settings, client, pluginRequest);
56+
case "yaml" -> createUsingPodYaml(request, settings, client, pluginRequest);
57+
default -> throw new IllegalArgumentException(String.format("Unsupported value for `PodSpecType`: %s", podSpecType));
58+
};
6359
}
6460
else {
6561
if (Boolean.parseBoolean(request.properties().get(SPECIFIED_USING_POD_CONFIGURATION.getKey()))) {
@@ -85,7 +81,7 @@ private KubernetesInstance createUsingProperties(CreateAgentRequest request, Plu
8581
podMetadata.setName(containerName);
8682

8783
PodSpec podSpec = new PodSpec();
88-
podSpec.setContainers(Arrays.asList(container));
84+
podSpec.setContainers(List.of(container));
8985

9086
Pod elasticAgentPod = new Pod("v1", "Pod", podMetadata, podSpec, new PodStatus());
9187

@@ -265,13 +261,13 @@ else if ("yaml".equalsIgnoreCase(fileType)) {
265261
Path podSpecFile = Path.of(String.format("pod_spec_%s", UUID.randomUUID()));
266262
try (InputStream downloadStream = new URL(fileToDownload).openStream()){
267263
Files.copy(downloadStream, podSpecFile);
268-
LOG.debug(format("Finished downloading %s to %s", fileToDownload, podSpecFile));
264+
LOG.debug(format("Finished downloading {0} to {1}", fileToDownload, podSpecFile));
269265
String spec = Files.readString(podSpecFile, UTF_8);
270266
String templatedPodSpec = getTemplatedPodSpec(spec);
271267
elasticAgentPod = mapper.readValue(templatedPodSpec, Pod.class);
272268
setPodNameIfNecessary(elasticAgentPod, spec);
273269
Files.deleteIfExists(podSpecFile);
274-
LOG.debug(format("Deleted %s", podSpecFile));
270+
LOG.debug(format("Deleted {0}", podSpecFile));
275271
} catch (IOException e) {
276272
//ignore error here, handle this inside validate profile!
277273
LOG.error(e.getMessage());

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) {
117117
}
118118
}
119119

120-
private void refreshInstancesForAllClusters(List<ClusterProfileProperties> listOfClusterProfileProperties) throws Exception {
120+
private void refreshInstancesForAllClusters(List<ClusterProfileProperties> listOfClusterProfileProperties) {
121121
for (ClusterProfileProperties clusterProfileProperties : listOfClusterProfileProperties) {
122122
refreshInstancesForCluster(clusterProfileProperties);
123123
}
124124
}
125125

126-
private AgentInstances<KubernetesInstance> getAgentInstancesFor(ClusterProfileProperties clusterProfileProperties) throws Exception {
126+
private AgentInstances<KubernetesInstance> getAgentInstancesFor(ClusterProfileProperties clusterProfileProperties) {
127127
KubernetesAgentInstances agentInstances = clusterSpecificAgentInstances.get(clusterProfileProperties.uuid());
128128

129129
//initialize agent instances if those are null
@@ -135,7 +135,7 @@ private AgentInstances<KubernetesInstance> getAgentInstancesFor(ClusterProfilePr
135135
return agentInstances;
136136
}
137137

138-
private void refreshInstancesForCluster(ClusterProfileProperties clusterProfileProperties) throws Exception {
138+
private void refreshInstancesForCluster(ClusterProfileProperties clusterProfileProperties) {
139139
KubernetesAgentInstances kubernetesInstances = clusterSpecificAgentInstances.getOrDefault(clusterProfileProperties.uuid(), new KubernetesAgentInstances());
140140
kubernetesInstances.refreshAll(clusterProfileProperties);
141141

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.time.Duration;
2525
import java.time.temporal.ChronoUnit;
26+
import java.util.Objects;
2627

2728
import static cd.go.contrib.elasticagent.utils.Util.IntTypeAdapter;
2829
import static cd.go.contrib.elasticagent.utils.Util.isBlank;
@@ -135,25 +136,23 @@ private <T> T getOrDefault(T t, T defaultValue) {
135136
@Override
136137
public boolean equals(Object o) {
137138
if (this == o) return true;
138-
if (!(o instanceof PluginSettings)) return false;
139+
if (!(o instanceof PluginSettings that)) return false;
139140

140-
PluginSettings that = (PluginSettings) o;
141-
142-
if (goServerUrl != null ? !goServerUrl.equals(that.goServerUrl) : that.goServerUrl != null)
141+
if (!Objects.equals(goServerUrl, that.goServerUrl))
143142
return false;
144-
if (autoRegisterTimeout != null ? !autoRegisterTimeout.equals(that.autoRegisterTimeout) : that.autoRegisterTimeout != null)
143+
if (!Objects.equals(autoRegisterTimeout, that.autoRegisterTimeout))
145144
return false;
146-
if (maxPendingPods != null ? !maxPendingPods.equals(that.maxPendingPods) : that.maxPendingPods != null)
145+
if (!Objects.equals(maxPendingPods, that.maxPendingPods))
147146
return false;
148-
if (clusterUrl != null ? !clusterUrl.equals(that.clusterUrl) : that.clusterUrl != null)
147+
if (!Objects.equals(clusterUrl, that.clusterUrl))
149148
return false;
150-
if (securityToken != null ? !securityToken.equals(that.securityToken) : that.securityToken != null)
149+
if (!Objects.equals(securityToken, that.securityToken))
151150
return false;
152-
if (clusterCACertData != null ? !clusterCACertData.equals(that.clusterCACertData) : that.clusterCACertData != null)
151+
if (!Objects.equals(clusterCACertData, that.clusterCACertData))
153152
return false;
154-
if (clusterRequestTimeout != null ? !clusterRequestTimeout.equals(that.clusterRequestTimeout) : that.clusterRequestTimeout != null)
153+
if (!Objects.equals(clusterRequestTimeout, that.clusterRequestTimeout))
155154
return false;
156-
return namespace != null ? namespace.equals(that.namespace) : that.namespace == null;
155+
return Objects.equals(namespace, that.namespace);
157156
}
158157

159158
@Override

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ public SetupSemaphore(Integer maxAllowedPendingPods, Map<String, KubernetesInsta
3434

3535
@Override
3636
public void run() {
37-
List<KubernetesInstance> pendingInstances = getPendingInstances(instances);
38-
int totalPendingPods = pendingInstances.size();
39-
int availablePermits = maxAllowedPendingPods - totalPendingPods;
37+
synchronized (semaphore) {
38+
List<KubernetesInstance> pendingInstances = getPendingInstances(instances);
39+
int totalPendingPods = pendingInstances.size();
40+
int availablePermits = maxAllowedPendingPods - totalPendingPods;
4041

41-
if (availablePermits <= 0) {
42-
// no more capacity available.
43-
semaphore.drainPermits();
44-
} else {
45-
int semaphoreValueDifference = availablePermits - semaphore.availablePermits();
46-
if (semaphoreValueDifference > 0) {
47-
semaphore.release(semaphoreValueDifference);
48-
} else if (semaphoreValueDifference < 0) {
49-
semaphore.tryAcquire(Math.abs(semaphoreValueDifference));
42+
if (availablePermits <= 0) {
43+
// no more capacity available.
44+
semaphore.drainPermits();
45+
} else {
46+
int semaphoreValueDifference = availablePermits - semaphore.availablePermits();
47+
if (semaphoreValueDifference > 0) {
48+
semaphore.release(semaphoreValueDifference);
49+
} else if (semaphoreValueDifference < 0) {
50+
semaphore.tryAcquire(Math.abs(semaphoreValueDifference));
51+
}
5052
}
5153
}
5254
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public ClusterProfileValidateRequestExecutor(ClusterProfileValidateRequest clust
4141
}
4242

4343
@Override
44-
public GoPluginApiResponse execute() throws Exception {
44+
public GoPluginApiResponse execute() {
4545
ArrayList<Map<String, String>> result = new ArrayList<>();
4646

4747
List<String> knownFields = new ArrayList<>();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2222
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
2323

24-
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
25-
import static java.text.MessageFormat.format;
26-
2724
import java.time.Instant;
2825
import java.time.LocalDateTime;
2926
import java.time.ZoneOffset;
3027
import java.time.format.DateTimeFormatter;
3128

29+
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
30+
import static java.text.MessageFormat.format;
31+
3232
public class CreateAgentRequestExecutor implements RequestExecutor {
3333
private static final DateTimeFormatter MESSAGE_PREFIX_FORMATTER = DateTimeFormatter.ofPattern("'##|'HH:mm:ss.SSS '[go]'");
3434
private static final DateTimeFormatter UTC_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss +00:00");
@@ -43,7 +43,7 @@ public CreateAgentRequestExecutor(CreateAgentRequest request, AgentInstances<Kub
4343
}
4444

4545
@Override
46-
public GoPluginApiResponse execute() throws Exception {
46+
public GoPluginApiResponse execute() {
4747
LOG.debug(format("[Create Agent] creating elastic agent for profile {0} in cluster {1}", request.properties(), request.clusterProfileProperties()));
4848
ConsoleLogAppender consoleLogAppender = text -> {
4949
final String message = String.format("%s %s\n", MESSAGE_PREFIX_FORMATTER.format(LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC)), text);

0 commit comments

Comments
 (0)