Skip to content

Commit ebb760e

Browse files
authored
Merge pull request #18 from GaneshSPatil/agent-status-report-page
Agent status report page
2 parents 76a7db0 + b5261d7 commit ebb760e

24 files changed

+1027
-20
lines changed

build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ tasks.withType(Jar) { jarTask ->
9797
}
9898
}
9999

100+
100101
manifest {
101102
attributes(
102103
'Go-Version': project.pluginDesc.goCdVersion,
@@ -108,3 +109,9 @@ tasks.withType(Jar) { jarTask ->
108109
)
109110
}
110111
}
112+
113+
task deploy(type: Copy) {
114+
delete '/Users/ganeshpatil/projects/gocd/server/plugins/external/*.jar.*'
115+
from jar
116+
into '/Users/ganeshpatil/projects/gocd/server/plugins/external'
117+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public interface Constants {
4848
String JOB_ID_LABEL_KEY = "Elastic-Agent-Job-Id";
4949
String JOB_IDENTIFIER_LABEL_KEY = "Elastic-Agent-Job-Identifier";
5050

51-
String KUBERNETES_NAMESPACE_KEY = "default";
51+
String KUBERNETES_NAMESPACE = "default";
5252
String KUBERNETES_POD_KIND_LABEL_KEY = "kind";
5353
String KUBERNETES_POD_KIND_LABEL_VALUE = "kubernetes-elastic-agent";
5454
String KUBERNETES_POD_NAME = "kubernetes-elastic-agent";

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Agents instancesCreatedAfterTimeout(PluginSettings settings, Agents agent
150150
public void refreshAll(PluginRequest pluginRequest) throws Exception {
151151
LOG.debug("[Refresh Instances]. Syncing k8s elastic agent pod information");
152152
KubernetesClient client = factory.kubernetes(pluginRequest.getPluginSettings());
153-
PodList list = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE_KEY).list();
153+
PodList list = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).list();
154154

155155
for (Pod pod : list.getItems()) {
156156
Map<String, String> podLabels = pod.getMetadata().getLabels();
@@ -180,7 +180,7 @@ private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings setting
180180
if (knownAgents.containsAgentWithId(instanceName)) {
181181
continue;
182182
}
183-
Pod pod = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE_KEY).withName(instanceName).get();
183+
Pod pod = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).withName(instanceName).get();
184184
Date createdAt = getSimpleDateFormat().parse(pod.getMetadata().getCreationTimestamp());
185185
DateTime dateTimeCreated = new DateTime(createdAt);
186186

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public KubernetesInstance(DateTime createdAt, String environment, String name, M
4040
}
4141

4242
public void terminate(KubernetesClient client) {
43-
client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE_KEY).withName(name).delete();
43+
client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).withName(name).delete();
4444
}
4545

4646
public String name() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static void setAnnotations(Pod pod, CreateAgentRequest request) {
118118

119119
private KubernetesInstance createKubernetesPod(KubernetesClient client, Pod elasticAgentPod) {
120120
LOG.info(String.format("[Create Agent] Creating K8s pod with spec:%s", elasticAgentPod.toString()));
121-
Pod pod = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE_KEY).create(elasticAgentPod);
121+
Pod pod = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).create(elasticAgentPod);
122122
return fromKubernetesPod(pod);
123123
}
124124

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
package cd.go.contrib.elasticagent;
1818

1919
import cd.go.contrib.elasticagent.executors.*;
20-
import cd.go.contrib.elasticagent.requests.CreateAgentRequest;
21-
import cd.go.contrib.elasticagent.requests.ProfileValidateRequest;
22-
import cd.go.contrib.elasticagent.requests.ShouldAssignWorkRequest;
23-
import cd.go.contrib.elasticagent.requests.ValidatePluginSettings;
20+
import cd.go.contrib.elasticagent.requests.*;
2421
import com.thoughtworks.go.plugin.api.GoApplicationAccessor;
2522
import com.thoughtworks.go.plugin.api.GoPlugin;
2623
import com.thoughtworks.go.plugin.api.GoPluginIdentifier;
@@ -76,6 +73,8 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) throws UnhandledRe
7673
return new ServerPingRequestExecutor(agentInstances, pluginRequest).execute();
7774
case REQUEST_STATUS_REPORT:
7875
return new StatusReportExecutor(pluginRequest).execute();
76+
case REQUEST_ELASTIC_AGENT_STATUS_REPORT:
77+
return AgentStatusReportRequest.fromJSON(request.requestBody()).executor(pluginRequest).execute();
7978
default:
8079
throw new UnhandledRequestTypeException(request.requestName());
8180
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public enum Request {
3535
PLUGIN_SETTINGS_VALIDATE_CONFIGURATION(Constants.GO_PLUGIN_SETTINGS_PREFIX + ".validate-configuration"),
3636

3737
REQUEST_STATUS_REPORT(Constants.ELASTIC_AGENT_REQUEST_PREFIX + ".status-report"),
38+
REQUEST_ELASTIC_AGENT_STATUS_REPORT(Constants.ELASTIC_AGENT_REQUEST_PREFIX + ".agent-status-report"),
3839
REQUEST_GET_CAPABILITIES(Constants.ELASTIC_AGENT_REQUEST_PREFIX + ".get-capabilities");
3940

4041
private final String requestName;

src/main/java/cd/go/contrib/elasticagent/builders/PluginStatusReportViewBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class PluginStatusReportViewBuilder {
3131
private static PluginStatusReportViewBuilder builder;
3232
private final Configuration configuration;
3333

34-
private PluginStatusReportViewBuilder() throws IOException {
34+
private PluginStatusReportViewBuilder() {
3535
configuration = new Configuration(Configuration.VERSION_2_3_23);
3636
configuration.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));
3737
configuration.setDefaultEncoding("UTF-8");
@@ -44,13 +44,13 @@ public Template getTemplate(String template) throws IOException {
4444
return configuration.getTemplate(template);
4545
}
4646

47-
public String build(Template template, KubernetesCluster cluster) throws IOException, TemplateException {
47+
public String build(Template template, Object cluster) throws IOException, TemplateException {
4848
Writer writer = new StringWriter();
4949
template.process(cluster, writer);
5050
return writer.toString();
5151
}
5252

53-
public static PluginStatusReportViewBuilder instance() throws IOException {
53+
public static PluginStatusReportViewBuilder instance() {
5454
if (builder == null) {
5555
builder = new PluginStatusReportViewBuilder();
5656
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package cd.go.contrib.elasticagent.executors;
2+
3+
import cd.go.contrib.elasticagent.Constants;
4+
import cd.go.contrib.elasticagent.KubernetesClientFactory;
5+
import cd.go.contrib.elasticagent.PluginRequest;
6+
import cd.go.contrib.elasticagent.builders.PluginStatusReportViewBuilder;
7+
import cd.go.contrib.elasticagent.model.JobIdentifier;
8+
import cd.go.contrib.elasticagent.model.reports.agent.KubernetesElasticAgent;
9+
import cd.go.contrib.elasticagent.requests.AgentStatusReportRequest;
10+
import com.google.gson.JsonObject;
11+
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
12+
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
13+
import freemarker.template.Template;
14+
import io.fabric8.kubernetes.api.model.Pod;
15+
import io.fabric8.kubernetes.client.KubernetesClient;
16+
import org.apache.commons.lang3.StringUtils;
17+
18+
import java.util.List;
19+
20+
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
21+
22+
public class AgentStatusReportExecutor {
23+
private final AgentStatusReportRequest request;
24+
private final PluginRequest pluginRequest;
25+
private final KubernetesClientFactory factory;
26+
private final PluginStatusReportViewBuilder statusReportViewBuilder;
27+
28+
public AgentStatusReportExecutor(AgentStatusReportRequest request, PluginRequest pluginRequest) {
29+
this(request, pluginRequest, KubernetesClientFactory.instance(), PluginStatusReportViewBuilder.instance());
30+
}
31+
32+
public AgentStatusReportExecutor(AgentStatusReportRequest request, PluginRequest pluginRequest, KubernetesClientFactory kubernetesClientFactory, PluginStatusReportViewBuilder builder) {
33+
this.request = request;
34+
this.pluginRequest = pluginRequest;
35+
this.factory = kubernetesClientFactory;
36+
this.statusReportViewBuilder = builder;
37+
}
38+
39+
public GoPluginApiResponse execute() throws Exception {
40+
String elasticAgentId = request.getElasticAgentId();
41+
JobIdentifier jobIdentifier = request.getJobIdentifier();
42+
LOG.info(String.format("[status-report] Generating status report for agent: %s with job: %s", elasticAgentId, jobIdentifier));
43+
KubernetesClient client = factory.kubernetes(pluginRequest.getPluginSettings());
44+
45+
try {
46+
Pod pod;
47+
if (StringUtils.isNotBlank(elasticAgentId)) {
48+
pod = findPodUsingElasticAgentId(elasticAgentId, client);
49+
} else {
50+
pod = findPodUsingJobIdentifier(jobIdentifier, client);
51+
}
52+
53+
KubernetesElasticAgent elasticAgent = KubernetesElasticAgent.fromPod(client, pod, elasticAgentId, jobIdentifier);
54+
55+
final String statusReportView = statusReportViewBuilder.build(statusReportViewBuilder.getTemplate("agent-status-report.template.ftlh"), elasticAgent);
56+
57+
JsonObject responseJSON = new JsonObject();
58+
responseJSON.addProperty("view", statusReportView);
59+
60+
return DefaultGoPluginApiResponse.success(responseJSON.toString());
61+
} catch (Exception e) {
62+
final String statusReportView = statusReportViewBuilder.build(statusReportViewBuilder.getTemplate("error.template.ftlh"), e);
63+
64+
JsonObject responseJSON = new JsonObject();
65+
responseJSON.addProperty("view", statusReportView);
66+
67+
return DefaultGoPluginApiResponse.success(responseJSON.toString());
68+
}
69+
}
70+
71+
private Pod findPodUsingJobIdentifier(JobIdentifier jobIdentifier, KubernetesClient client) {
72+
try {
73+
return client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE)
74+
.withLabel(Constants.JOB_ID_LABEL_KEY, String.valueOf(jobIdentifier.getJobId()))
75+
.list().getItems().get(0);
76+
} catch (Exception e) {
77+
throw new RuntimeException(String.format("Can not find a running Pod for the provided job identifier '%s'", jobIdentifier));
78+
}
79+
}
80+
81+
private Pod findPodUsingElasticAgentId(String elasticAgentId, KubernetesClient client) {
82+
List<Pod> pods = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).list().getItems();
83+
for (Pod pod : pods) {
84+
if (pod.getMetadata().getName().equals(elasticAgentId)) {
85+
return pod;
86+
}
87+
}
88+
89+
throw new RuntimeException(String.format("Can not find a running Pod for the provided elastic agent id '%s'", elasticAgentId));
90+
}
91+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030

3131
public class KubernetesCluster {
3232
private final List<KubernetesNode> nodes;
33+
private final String pluginId;
3334

3435
public KubernetesCluster(KubernetesClient client) throws ParseException {
36+
pluginId = Constants.PLUGIN_ID;
3537
nodes = client.nodes().list().getItems().stream().map(node -> new KubernetesNode(node)).collect(toList());
3638
LOG.info("Running kubernetes nodes " + nodes.size());
3739
fetchPods(client);
@@ -40,7 +42,7 @@ public KubernetesCluster(KubernetesClient client) throws ParseException {
4042
private void fetchPods(KubernetesClient dockerClient) throws ParseException {
4143
final Map<String, KubernetesNode> dockerNodeMap = nodes.stream().distinct().collect(toMap(KubernetesNode::getName, node -> node));
4244

43-
final List<Pod> pods = dockerClient.pods().inNamespace(Constants.KUBERNETES_NAMESPACE_KEY)
45+
final List<Pod> pods = dockerClient.pods().inNamespace(Constants.KUBERNETES_NAMESPACE)
4446
.withLabel(Constants.CREATED_BY_LABEL_KEY, Constants.PLUGIN_ID)
4547
.list().getItems();
4648

@@ -58,4 +60,8 @@ private void fetchPods(KubernetesClient dockerClient) throws ParseException {
5860
public List<KubernetesNode> getNodes() {
5961
return nodes;
6062
}
63+
64+
public String getPluginId() {
65+
return pluginId;
66+
}
6167
}

0 commit comments

Comments
 (0)