Skip to content

Commit 5343d5b

Browse files
arvindsvvarshavaradarajan
authored andcommitted
Add more messages, improve existing messages
1 parent c670bde commit 5343d5b

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
6363
synchronized (instances) {
6464
refreshAll(settings);
6565
doWithLockOnSemaphore(new SetupSemaphore(maxAllowedContainers, instances, semaphore));
66-
66+
consoleLogAppender.accept("Waiting to create agent pod.");
6767
if (semaphore.tryAcquire()) {
6868
return createKubernetesInstance(request, settings, pluginRequest, consoleLogAppender);
6969
} else {
70-
LOG.warn(format("[Create Agent Request] The number of pending kubernetes pods is currently at the maximum permissible limit ({0}). Total kubernetes pods ({1}). Not creating any more containers.", maxAllowedContainers, instances.size()));
71-
consoleLogAppender.accept(format("[Create Agent Request] The number of pending kubernetes pods is currently at the maximum permissible limit ({0}). Total kubernetes pods ({1}). Not creating any more containers.", maxAllowedContainers, instances.size()));
70+
String message = format("[Create Agent Request] The number of pending kubernetes pods is currently at the maximum permissible limit ({0}). Total kubernetes pods ({1}). Not creating any more containers.", maxAllowedContainers, instances.size());
71+
LOG.warn(message);
72+
consoleLogAppender.accept(message);
7273
return null;
7374
}
7475
}
@@ -83,16 +84,17 @@ private void doWithLockOnSemaphore(Runnable runnable) {
8384
private KubernetesInstance createKubernetesInstance(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest, ConsoleLogAppender consoleLogAppender) {
8485
JobIdentifier jobIdentifier = request.jobIdentifier();
8586
if (isAgentCreatedForJob(jobIdentifier.getJobId())) {
86-
LOG.warn(format("[Create Agent Request] Request for creating an agent for Job Identifier [{0}] has already been scheduled. Skipping current request.", jobIdentifier));
87-
consoleLogAppender.accept(format("[Create Agent Request] Request for creating an agent for Job Identifier [{0}] has already been scheduled. Skipping current request.", jobIdentifier));
87+
String message = format("[Create Agent Request] Request for creating an agent for Job Identifier [{0}] has already been scheduled. Skipping current request.", jobIdentifier);
88+
LOG.warn(message);
89+
consoleLogAppender.accept(message);
8890
return null;
8991
}
9092

9193
KubernetesClient client = factory.client(settings);
9294
KubernetesInstance instance = kubernetesInstanceFactory.create(request, settings, client, pluginRequest);
93-
consoleLogAppender.accept(String.format("Created pod: %s for job %s", instance.name(), jobIdentifier));
95+
consoleLogAppender.accept(format("Creating pod: %s", instance.name()));
9496
register(instance);
95-
consoleLogAppender.accept(String.format("Registered agent: %s for job %s", instance.name(), jobIdentifier));
97+
consoleLogAppender.accept(format("Agent pod %s created. Waiting for it to register to the GoCD server.", instance.name()));
9698

9799
return instance;
98100
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public GoPluginApiResponse execute() throws Exception {
4848
pluginRequest.appendToConsoleLog(request.jobIdentifier(), message);
4949
};
5050
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")));
51-
agentInstances.create(request, request.clusterProfileProperties(), pluginRequest, consoleLogAppender);
51+
try {
52+
agentInstances.create(request, request.clusterProfileProperties(), pluginRequest, consoleLogAppender);
53+
} catch (Exception e) {
54+
consoleLogAppender.accept(format("Failed to create agent pod: %s", e.getMessage()));
55+
throw e;
56+
}
57+
5258
return new DefaultGoPluginApiResponse(200);
5359
}
5460

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

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

2424
import java.util.HashMap;
2525

26+
import static org.junit.Assert.fail;
2627
import static org.mockito.Mockito.*;
2728

2829
public class CreateAgentRequestExecutorTest {
@@ -40,4 +41,26 @@ public void shouldAskDockerContainersToCreateAnAgent() throws Exception {
4041
verify(pluginRequest).appendToConsoleLog(eq(jobIdentifier), contains("Received request to create a pod for job"));
4142
verify(agentInstances).create(eq(request), eq(request.clusterProfileProperties()), eq(pluginRequest), any(ConsoleLogAppender.class));
4243
}
44+
45+
@Test
46+
public void shouldLogErrorMessageToConsoleIfAgentCreateFails() throws Exception {
47+
final HashMap<String, String> elasticAgentProfileProperties = new HashMap<>();
48+
elasticAgentProfileProperties.put("Image", "image1");
49+
ClusterProfileProperties clusterProfileProperties = new ClusterProfileProperties("http://go-server", "http://k8ssvc.url", "");
50+
final JobIdentifier jobIdentifier = new JobIdentifier("p1", 1L, "l1", "s1", "1", "j1", 1L);
51+
CreateAgentRequest request = new CreateAgentRequest("key1", elasticAgentProfileProperties, "env1", jobIdentifier, clusterProfileProperties);
52+
AgentInstances<KubernetesInstance> agentInstances = mock(KubernetesAgentInstances.class);
53+
PluginRequest pluginRequest = mock(PluginRequest.class);
54+
55+
when(agentInstances.create(any(), any(), any(), any())).thenThrow(new RuntimeException("Ouch!"));
56+
57+
try {
58+
new CreateAgentRequestExecutor(request, agentInstances, pluginRequest).execute();
59+
fail("Should have thrown an exception.");
60+
} catch (Exception e) {
61+
// This is expected. Ignore.
62+
}
63+
verify(pluginRequest).appendToConsoleLog(any(), contains("Received request to create a pod for job"));
64+
verify(pluginRequest).appendToConsoleLog(any(), contains("Failed to create agent pod"));
65+
}
4366
}

0 commit comments

Comments
 (0)