Skip to content

Commit d0c51d4

Browse files
authored
Merge pull request #30 from bdpiparva/status-report-improvements
Status report improvements
2 parents 2d6e880 + b1db2e7 commit d0c51d4

22 files changed

+332
-197
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ processResources {
7474
filesMatching('plugin.properties') {
7575
expand project.pluginDesc
7676
}
77+
78+
filesMatching('version.properties') {
79+
expand project.pluginDesc
80+
}
7781
}
7882
}
7983

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
4040
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.POD_CONFIGURATION;
4141
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.PRIVILEGED;
42-
import static cd.go.contrib.elasticagent.utils.Util.GSON;
4342
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
4443
import static java.lang.String.valueOf;
4544
import static java.text.MessageFormat.format;
@@ -125,7 +124,7 @@ private static void setLabels(Pod pod, CreateAgentRequest request) {
125124
private static void setAnnotations(Pod pod, CreateAgentRequest request) {
126125
Map<String, String> existingAnnotations = (pod.getMetadata().getAnnotations() != null) ? pod.getMetadata().getAnnotations() : new HashMap<>();
127126
existingAnnotations.putAll(request.properties());
128-
existingAnnotations.put(JOB_IDENTIFIER_LABEL_KEY, GSON.toJson(request.jobIdentifier()));
127+
existingAnnotations.put(JOB_IDENTIFIER_LABEL_KEY, request.jobIdentifier().toJson());
129128
pod.getMetadata().setAnnotations(existingAnnotations);
130129
}
131130

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ThoughtWorks, Inc.
2+
* Copyright 2018 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,11 +18,14 @@
1818

1919
import cd.go.contrib.elasticagent.executors.*;
2020
import cd.go.contrib.elasticagent.requests.*;
21+
import cd.go.contrib.elasticagent.utils.Util;
2122
import com.thoughtworks.go.plugin.api.GoApplicationAccessor;
2223
import com.thoughtworks.go.plugin.api.GoPlugin;
2324
import com.thoughtworks.go.plugin.api.GoPluginIdentifier;
2425
import com.thoughtworks.go.plugin.api.annotation.Extension;
26+
import com.thoughtworks.go.plugin.api.annotation.Load;
2527
import com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException;
28+
import com.thoughtworks.go.plugin.api.info.PluginContext;
2629
import com.thoughtworks.go.plugin.api.logging.Logger;
2730
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
2831
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
@@ -43,8 +46,13 @@ public void initializeGoApplicationAccessor(GoApplicationAccessor accessor) {
4346
agentInstances = new KubernetesAgentInstances();
4447
}
4548

49+
@Load
50+
public void onLoad(PluginContext ctx) {
51+
LOG.info("Loading plugin " + Util.pluginId() + " version " + Util.fullVersion());
52+
}
53+
4654
@Override
47-
public GoPluginApiResponse handle(GoPluginApiRequest request) throws UnhandledRequestTypeException {
55+
public GoPluginApiResponse handle(GoPluginApiRequest request) {
4856
try {
4957
switch (Request.fromString(request.requestName())) {
5058
case REQUEST_GET_CAPABILITIES:

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1+
/*
2+
* Copyright 2018 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package cd.go.contrib.elasticagent.executors;
218

319
import cd.go.contrib.elasticagent.Constants;
420
import cd.go.contrib.elasticagent.KubernetesClientFactory;
521
import cd.go.contrib.elasticagent.PluginRequest;
622
import cd.go.contrib.elasticagent.builders.PluginStatusReportViewBuilder;
723
import cd.go.contrib.elasticagent.model.JobIdentifier;
8-
import cd.go.contrib.elasticagent.model.reports.StatusReportGenerationErrorHandler;
9-
import cd.go.contrib.elasticagent.model.reports.StatusReportGenerationException;
1024
import cd.go.contrib.elasticagent.model.reports.agent.KubernetesElasticAgent;
25+
import cd.go.contrib.elasticagent.reports.StatusReportGenerationErrorHandler;
26+
import cd.go.contrib.elasticagent.reports.StatusReportGenerationException;
1127
import cd.go.contrib.elasticagent.requests.AgentStatusReportRequest;
1228
import com.google.gson.JsonObject;
1329
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
@@ -71,7 +87,7 @@ private Pod findPodUsingJobIdentifier(JobIdentifier jobIdentifier, KubernetesCli
7187
.withLabel(Constants.JOB_ID_LABEL_KEY, String.valueOf(jobIdentifier.getJobId()))
7288
.list().getItems().get(0);
7389
} catch (Exception e) {
74-
throw new StatusReportGenerationException(format("Can not find a running Pod for the provided job identifier: {0}.", jobIdentifier.representation()));
90+
throw StatusReportGenerationException.noRunningPod(jobIdentifier);
7591
}
7692
}
7793

@@ -83,6 +99,6 @@ private Pod findPodUsingElasticAgentId(String elasticAgentId, KubernetesClient c
8399
}
84100
}
85101

86-
throw new StatusReportGenerationException(format("Can not find a running Pod for the provided elastic agent id: {0}.", elasticAgentId));
102+
throw StatusReportGenerationException.noRunningPod(elasticAgentId);
87103
}
88104
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ThoughtWorks, Inc.
2+
* Copyright 2018 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@
2626

2727
public class GetCapabilitiesExecutor implements RequestExecutor {
2828
public GoPluginApiResponse execute() {
29-
String capabilities = new Capabilities(true).toJSON();
29+
String capabilities = new Capabilities(true, true).toJSON();
3030
LOG.debug("Plugin capabilities:" + capabilities);
3131
return new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, capabilities);
3232
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
/*
3-
* Copyright 2017 ThoughtWorks, Inc.
3+
* Copyright 2018 ThoughtWorks, Inc.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -24,6 +24,9 @@
2424
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2525
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
2626

27+
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
28+
import static java.text.MessageFormat.format;
29+
2730
public class ShouldAssignWorkRequestExecutor implements RequestExecutor {
2831
private final AgentInstances<KubernetesInstance> agentInstances;
2932
private final ShouldAssignWorkRequest request;
@@ -42,9 +45,11 @@ public GoPluginApiResponse execute() {
4245
}
4346

4447
if (request.jobIdentifier().getJobId().equals(pod.jobId())) {
48+
LOG.debug(format("[should-assign-work] Job with identifier {0} can be assigned to an agent {1}.", request.jobIdentifier(), pod.name()));
4549
return DefaultGoPluginApiResponse.success("true");
4650
}
4751

52+
LOG.error(format("[should-assign-work] Job with identifier {0} can not be assigned to an agent {1}.", request.jobIdentifier(), pod.name()));
4853
return DefaultGoPluginApiResponse.success("false");
4954
}
5055
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ThoughtWorks, Inc.
2+
* Copyright 2018 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
2020
import cd.go.contrib.elasticagent.PluginRequest;
2121
import cd.go.contrib.elasticagent.builders.PluginStatusReportViewBuilder;
2222
import cd.go.contrib.elasticagent.model.KubernetesCluster;
23-
import cd.go.contrib.elasticagent.model.reports.StatusReportGenerationErrorHandler;
23+
import cd.go.contrib.elasticagent.reports.StatusReportGenerationErrorHandler;
2424
import com.google.gson.JsonObject;
2525
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2626
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
@@ -47,7 +47,7 @@ public StatusReportExecutor(PluginRequest pluginRequest, KubernetesClientFactory
4747

4848
public GoPluginApiResponse execute() {
4949
try {
50-
LOG.info("[status-report] Generating status report");
50+
LOG.info("[status-report] Generating status report.");
5151
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
5252
final KubernetesCluster kubernetesCluster = new KubernetesCluster(client);
5353
final Template template = statusReportViewBuilder.getTemplate("status-report.template.ftlh");

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ThoughtWorks, Inc.
2+
* Copyright 2018 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,8 +26,13 @@ public class Capabilities {
2626
@SerializedName("supports_status_report")
2727
private boolean supportsStatusReport;
2828

29-
public Capabilities(boolean supportsStatusReport) {
29+
@Expose
30+
@SerializedName("supports_agent_status_report")
31+
private boolean supportsAgentStatusReport;
32+
33+
public Capabilities(boolean supportsStatusReport, boolean supportsAgentStatusReport) {
3034
this.supportsStatusReport = supportsStatusReport;
35+
this.supportsAgentStatusReport = supportsAgentStatusReport;
3136
}
3237

3338
public static Capabilities fromJSON(String json) {

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

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ThoughtWorks, Inc.
2+
* Copyright 2018 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,36 +19,40 @@
1919
import com.google.gson.annotations.Expose;
2020
import com.google.gson.annotations.SerializedName;
2121

22+
import static cd.go.contrib.elasticagent.utils.Util.GSON;
23+
import static java.text.MessageFormat.format;
24+
2225
public class JobIdentifier {
2326
@Expose
2427
@SerializedName("pipeline_name")
2528
private String pipelineName;
2629

2730
@Expose
2831
@SerializedName("pipeline_counter")
29-
private Long pipelineCounter;
32+
private final Long pipelineCounter;
3033

3134
@Expose
3235
@SerializedName("pipeline_label")
33-
private String pipelineLabel;
36+
private final String pipelineLabel;
3437

3538
@Expose
3639
@SerializedName("stage_name")
37-
private String stageName;
40+
private final String stageName;
3841

3942
@Expose
4043
@SerializedName("stage_counter")
41-
private String stageCounter;
44+
private final String stageCounter;
4245

4346
@Expose
4447
@SerializedName("job_name")
45-
private String jobName;
48+
private final String jobName;
4649

4750
@Expose
4851
@SerializedName("job_id")
49-
private Long jobId;
52+
private final Long jobId;
5053

5154
public JobIdentifier() {
55+
this(null, null, null, null, null, null, null);
5256
}
5357

5458
public JobIdentifier(Long jobId) {
@@ -93,8 +97,33 @@ public Long getJobId() {
9397
return jobId;
9498
}
9599

96-
public String representation() {
97-
return pipelineName + "/" + pipelineCounter + "/" + stageName + "/" + stageCounter + "/" + jobName;
100+
public String getRepresentation() {
101+
return String.format("%s/%s/%s/%s/%s", pipelineName, pipelineCounter, stageName, stageCounter, jobName);
102+
}
103+
104+
public String getPipelineHistoryPageLink() {
105+
return String.format("/go/tab/pipeline/history/%s", pipelineName);
106+
}
107+
108+
public String getVsmPageLink() {
109+
return String.format("/go/pipelines/value_stream_map/%s/%s", pipelineName, pipelineCounter);
110+
}
111+
112+
public String getStageDetailsPageLink() {
113+
return String.format("/go/pipelines/%s/%s/%s/%s", pipelineName, pipelineCounter, stageName, stageCounter);
114+
}
115+
116+
public String getJobDetailsPageLink() {
117+
return String.format("/go/tab/build/detail/%s", getRepresentation());
118+
}
119+
120+
public String toJson() {
121+
return GSON.toJson(this);
122+
}
123+
124+
@Override
125+
public String toString() {
126+
return format("JobIdentifier'{'pipelineName=''{0}'', pipelineCounter={1}, pipelineLabel=''{2}'', stageName=''{3}'', stageCounter=''{4}'', jobName=''{5}'', jobId={6}'}'", pipelineName, pipelineCounter, pipelineLabel, stageName, stageCounter, jobName, jobId);
98127
}
99128

100129
@Override
@@ -104,38 +133,31 @@ public boolean equals(Object o) {
104133

105134
JobIdentifier that = (JobIdentifier) o;
106135

107-
if (pipelineCounter != that.pipelineCounter) return false;
108-
if (jobId != that.jobId) return false;
109136
if (pipelineName != null ? !pipelineName.equals(that.pipelineName) : that.pipelineName != null) return false;
137+
if (pipelineCounter != null ? !pipelineCounter.equals(that.pipelineCounter) : that.pipelineCounter != null)
138+
return false;
110139
if (pipelineLabel != null ? !pipelineLabel.equals(that.pipelineLabel) : that.pipelineLabel != null)
111140
return false;
112141
if (stageName != null ? !stageName.equals(that.stageName) : that.stageName != null) return false;
113142
if (stageCounter != null ? !stageCounter.equals(that.stageCounter) : that.stageCounter != null) return false;
114-
return jobName != null ? jobName.equals(that.jobName) : that.jobName == null;
143+
if (jobName != null ? !jobName.equals(that.jobName) : that.jobName != null) return false;
144+
return jobId != null ? jobId.equals(that.jobId) : that.jobId == null;
115145
}
116146

117147
@Override
118148
public int hashCode() {
119149
int result = pipelineName != null ? pipelineName.hashCode() : 0;
120-
result = 31 * result + (int) (pipelineCounter ^ (pipelineCounter >>> 32));
150+
result = 31 * result + (pipelineCounter != null ? pipelineCounter.hashCode() : 0);
121151
result = 31 * result + (pipelineLabel != null ? pipelineLabel.hashCode() : 0);
122152
result = 31 * result + (stageName != null ? stageName.hashCode() : 0);
123153
result = 31 * result + (stageCounter != null ? stageCounter.hashCode() : 0);
124154
result = 31 * result + (jobName != null ? jobName.hashCode() : 0);
125-
result = 31 * result + (int) (jobId ^ (jobId >>> 32));
155+
result = 31 * result + (jobId != null ? jobId.hashCode() : 0);
126156
return result;
127157
}
128158

129-
@Override
130-
public String toString() {
131-
return "JobIdentifier{" +
132-
"pipelineName='" + pipelineName + '\'' +
133-
", pipelineCounter=" + pipelineCounter +
134-
", pipelineLabel='" + pipelineLabel + '\'' +
135-
", staqeName='" + stageName + '\'' +
136-
", stageCounter='" + stageCounter + '\'' +
137-
", jobName='" + jobName + '\'' +
138-
", jobId=" + jobId +
139-
'}';
159+
public static JobIdentifier fromJson(String json) {
160+
return GSON.fromJson(json, JobIdentifier.class);
140161
}
162+
141163
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ThoughtWorks, Inc.
2+
* Copyright 2018 ThoughtWorks, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ public class KubernetesPod {
3333
private JobIdentifier jobIdentifier;
3434

3535
public KubernetesPod(Pod pod) throws ParseException {
36-
jobIdentifier = Util.GSON.fromJson(pod.getMetadata().getAnnotations().get(Constants.JOB_IDENTIFIER_LABEL_KEY), JobIdentifier.class);
36+
jobIdentifier = JobIdentifier.fromJson(pod.getMetadata().getAnnotations().get(Constants.JOB_IDENTIFIER_LABEL_KEY));
3737
podName = pod.getMetadata().getName();
3838
image = pod.getSpec().getContainers().get(0).getImage();
3939
podIP = pod.getStatus().getPodIP();
@@ -68,8 +68,8 @@ public String getStatus() {
6868
}
6969

7070
public String getJobInformation() {
71-
if(jobIdentifier != null) {
72-
return jobIdentifier.representation();
71+
if (jobIdentifier != null) {
72+
return jobIdentifier.getRepresentation();
7373
}
7474
return "No Job Information Available!";
7575
}

0 commit comments

Comments
 (0)