Skip to content

Commit dfb7916

Browse files
authored
Merge pull request #21 from bdpiparva/minor-fixes-agent-status-reports
Added job identifier to agent status report page.
2 parents b48a5da + b4dec20 commit dfb7916

File tree

7 files changed

+474
-330
lines changed

7 files changed

+474
-330
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public interface Constants {
5151
String KUBERNETES_NAMESPACE = "default";
5252
String KUBERNETES_POD_KIND_LABEL_KEY = "kind";
5353
String KUBERNETES_POD_KIND_LABEL_VALUE = "kubernetes-elastic-agent";
54-
String KUBERNETES_POD_NAME = "kubernetes-elastic-agent";
54+
String KUBERNETES_POD_NAME_PREFIX = "k8s-ea";
5555
String KUBERNETES_POD_CREATION_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
5656

5757
String POD_POSTFIX = "POD_POSTFIX";

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535
import java.text.ParseException;
3636
import java.util.*;
3737

38+
import static cd.go.contrib.elasticagent.Constants.*;
3839
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
3940
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.POD_CONFIGURATION;
4041
import static cd.go.contrib.elasticagent.executors.GetProfileMetadataExecutor.PRIVILEGED;
4142
import static cd.go.contrib.elasticagent.utils.Util.GSON;
4243
import static cd.go.contrib.elasticagent.utils.Util.getSimpleDateFormat;
44+
import static java.lang.String.format;
45+
import static java.lang.String.valueOf;
4346
import static org.apache.commons.lang3.StringUtils.isBlank;
4447

4548
public class KubernetesInstanceFactory {
@@ -52,7 +55,7 @@ public KubernetesInstance create(CreateAgentRequest request, PluginSettings sett
5255
}
5356

5457
private KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, KubernetesClient client, PluginRequest pluginRequest) {
55-
String containerName = Constants.KUBERNETES_POD_NAME + UUID.randomUUID().toString();
58+
String containerName = format("%s-%s", KUBERNETES_POD_NAME_PREFIX, UUID.randomUUID().toString());
5659

5760
Container container = new Container();
5861
container.setName(containerName);
@@ -98,13 +101,13 @@ private ResourceRequirements getPodResources(CreateAgentRequest request) {
98101
String maxMemory = request.properties().get("MaxMemory");
99102
if (StringUtils.isNotBlank(maxMemory)) {
100103
Size mem = Size.parse(maxMemory);
101-
LOG.debug(String.format("[Create Agent] Setting memory resource limit on k8s pod:%s", new Quantity(String.valueOf(mem.toMegabytes()), "M")));
102-
limits.put("memory", new Quantity(String.valueOf(mem.toBytes())));
104+
LOG.debug(format("[Create Agent] Setting memory resource limit on k8s pod:%s", new Quantity(valueOf(mem.toMegabytes()), "M")));
105+
limits.put("memory", new Quantity(valueOf(mem.toBytes())));
103106
}
104107

105108
String maxCPU = request.properties().get("MaxCPU");
106109
if (StringUtils.isNotBlank(maxCPU)) {
107-
LOG.debug(String.format("[Create Agent] Setting cpu resource limit on k8s pod:%s", new Quantity(maxCPU)));
110+
LOG.debug(format("[Create Agent] Setting cpu resource limit on k8s pod:%s", new Quantity(maxCPU)));
108111
limits.put("cpu", new Quantity(maxCPU));
109112
}
110113

@@ -122,13 +125,13 @@ private static void setLabels(Pod pod, CreateAgentRequest request) {
122125
private static void setAnnotations(Pod pod, CreateAgentRequest request) {
123126
Map<String, String> existingAnnotations = (pod.getMetadata().getAnnotations() != null) ? pod.getMetadata().getAnnotations() : new HashMap<>();
124127
existingAnnotations.putAll(request.properties());
125-
existingAnnotations.put(Constants.JOB_IDENTIFIER_LABEL_KEY, GSON.toJson(request.jobIdentifier()));
128+
existingAnnotations.put(JOB_IDENTIFIER_LABEL_KEY, GSON.toJson(request.jobIdentifier()));
126129
pod.getMetadata().setAnnotations(existingAnnotations);
127130
}
128131

129132
private KubernetesInstance createKubernetesPod(KubernetesClient client, Pod elasticAgentPod) {
130-
LOG.info(String.format("[Create Agent] Creating K8s pod with spec:%s", elasticAgentPod.toString()));
131-
Pod pod = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).create(elasticAgentPod);
133+
LOG.info(format("[Create Agent] Creating K8s pod with spec:%s", elasticAgentPod.toString()));
134+
Pod pod = client.pods().inNamespace(KUBERNETES_NAMESPACE).create(elasticAgentPod);
132135
return fromKubernetesPod(pod);
133136
}
134137

@@ -140,8 +143,8 @@ public KubernetesInstance fromKubernetesPod(Pod elasticAgentPod) {
140143
if (StringUtils.isNotBlank(metadata.getCreationTimestamp())) {
141144
createdAt = new DateTime(getSimpleDateFormat().parse(metadata.getCreationTimestamp())).withZone(DateTimeZone.UTC);
142145
}
143-
String environment = metadata.getLabels().get(Constants.ENVIRONMENT_LABEL_KEY);
144-
Long jobId = Long.valueOf(metadata.getLabels().get(Constants.JOB_ID_LABEL_KEY));
146+
String environment = metadata.getLabels().get(ENVIRONMENT_LABEL_KEY);
147+
Long jobId = Long.valueOf(metadata.getLabels().get(JOB_ID_LABEL_KEY));
145148
kubernetesInstance = new KubernetesInstance(createdAt, environment, metadata.getName(), metadata.getAnnotations(), jobId, PodState.fromPod(elasticAgentPod));
146149
} catch (ParseException e) {
147150
throw new RuntimeException(e);
@@ -183,14 +186,14 @@ private static Collection<? extends EnvVar> parseEnvironments(String environment
183186
private static HashMap<String, String> labelsFrom(CreateAgentRequest request) {
184187
HashMap<String, String> labels = new HashMap<>();
185188

186-
labels.put(Constants.CREATED_BY_LABEL_KEY, Constants.PLUGIN_ID);
187-
labels.put(Constants.JOB_ID_LABEL_KEY, String.valueOf(request.jobIdentifier().getJobId()));
189+
labels.put(CREATED_BY_LABEL_KEY, PLUGIN_ID);
190+
labels.put(JOB_ID_LABEL_KEY, valueOf(request.jobIdentifier().getJobId()));
188191

189192
if (StringUtils.isNotBlank(request.environment())) {
190-
labels.put(Constants.ENVIRONMENT_LABEL_KEY, request.environment());
193+
labels.put(ENVIRONMENT_LABEL_KEY, request.environment());
191194
}
192195

193-
labels.put(Constants.KUBERNETES_POD_KIND_LABEL_KEY, Constants.KUBERNETES_POD_KIND_LABEL_VALUE);
196+
labels.put(KUBERNETES_POD_KIND_LABEL_KEY, KUBERNETES_POD_KIND_LABEL_VALUE);
194197

195198
return labels;
196199
}
@@ -235,10 +238,10 @@ public static String getTemplatizedPodYamlString(String podYaml) {
235238

236239
public static Map<String, String> getJinJavaContext() {
237240
HashMap<String, String> context = new HashMap<>();
238-
context.put(Constants.POD_POSTFIX, UUID.randomUUID().toString());
239-
context.put(Constants.CONTAINER_POSTFIX, UUID.randomUUID().toString());
240-
context.put(Constants.GOCD_AGENT_IMAGE, "gocd/gocd-agent-alpine-3.5");
241-
context.put(Constants.LATEST_VERSION, "v17.10.0");
241+
context.put(POD_POSTFIX, UUID.randomUUID().toString());
242+
context.put(CONTAINER_POSTFIX, UUID.randomUUID().toString());
243+
context.put(GOCD_AGENT_IMAGE, "gocd/gocd-agent-alpine-3.5");
244+
context.put(LATEST_VERSION, "v17.10.0");
242245
return context;
243246
}
244247
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.google.gson.JsonObject;
1111
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
1212
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
13-
import freemarker.template.Template;
1413
import io.fabric8.kubernetes.api.model.Pod;
1514
import io.fabric8.kubernetes.client.KubernetesClient;
1615
import org.apache.commons.lang3.StringUtils;
@@ -50,7 +49,7 @@ public GoPluginApiResponse execute() throws Exception {
5049
pod = findPodUsingJobIdentifier(jobIdentifier, client);
5150
}
5251

53-
KubernetesElasticAgent elasticAgent = KubernetesElasticAgent.fromPod(client, pod, elasticAgentId, jobIdentifier);
52+
KubernetesElasticAgent elasticAgent = KubernetesElasticAgent.fromPod(client, pod, jobIdentifier);
5453

5554
final String statusReportView = statusReportViewBuilder.build(statusReportViewBuilder.getTemplate("agent-status-report.template.ftlh"), elasticAgent);
5655

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

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,77 @@ public class JobIdentifier {
2626

2727
@Expose
2828
@SerializedName("pipeline_counter")
29-
private final Long pipelineCounter;
29+
private Long pipelineCounter;
3030

3131
@Expose
3232
@SerializedName("pipeline_label")
33-
private final String pipelineLabel;
33+
private String pipelineLabel;
3434

3535
@Expose
3636
@SerializedName("stage_name")
37-
private final String staqeName;
37+
private String stageName;
3838

3939
@Expose
4040
@SerializedName("stage_counter")
41-
private final String stageCounter;
41+
private String stageCounter;
4242

4343
@Expose
4444
@SerializedName("job_name")
45-
private final String jobName;
45+
private String jobName;
4646

4747
@Expose
4848
@SerializedName("job_id")
49-
private final Long jobId;
49+
private Long jobId;
5050

5151
public JobIdentifier() {
52-
pipelineCounter = null;
53-
pipelineLabel = null;
54-
staqeName = null;
55-
stageCounter = null;
56-
jobName = null;
57-
jobId = null;
5852
}
5953

6054
public JobIdentifier(Long jobId) {
6155
this(null, null, null, null, null, null, jobId);
6256
}
6357

64-
public JobIdentifier(String pipelineName, Long pipelineCounter, String pipelineLabel, String staqeName, String stageCounter, String jobName, Long jobId) {
58+
public JobIdentifier(String pipelineName, Long pipelineCounter, String pipelineLabel, String stageName, String stageCounter, String jobName, Long jobId) {
6559
this.pipelineName = pipelineName;
6660
this.pipelineCounter = pipelineCounter;
6761
this.pipelineLabel = pipelineLabel;
68-
this.staqeName = staqeName;
62+
this.stageName = stageName;
6963
this.stageCounter = stageCounter;
7064
this.jobName = jobName;
7165
this.jobId = jobId;
7266
}
7367

68+
public String getPipelineName() {
69+
return pipelineName;
70+
}
71+
72+
public Long getPipelineCounter() {
73+
return pipelineCounter;
74+
}
75+
76+
public String getPipelineLabel() {
77+
return pipelineLabel;
78+
}
79+
80+
public String getStageName() {
81+
return stageName;
82+
}
83+
84+
public String getStageCounter() {
85+
return stageCounter;
86+
}
87+
88+
public String getJobName() {
89+
return jobName;
90+
}
91+
92+
public Long getJobId() {
93+
return jobId;
94+
}
95+
96+
public String representation() {
97+
return pipelineName + "/" + pipelineCounter + "/" + stageName + "/" + stageCounter + "/" + jobName;
98+
}
99+
74100
@Override
75101
public boolean equals(Object o) {
76102
if (this == o) return true;
@@ -83,7 +109,7 @@ public boolean equals(Object o) {
83109
if (pipelineName != null ? !pipelineName.equals(that.pipelineName) : that.pipelineName != null) return false;
84110
if (pipelineLabel != null ? !pipelineLabel.equals(that.pipelineLabel) : that.pipelineLabel != null)
85111
return false;
86-
if (staqeName != null ? !staqeName.equals(that.staqeName) : that.staqeName != null) return false;
112+
if (stageName != null ? !stageName.equals(that.stageName) : that.stageName != null) return false;
87113
if (stageCounter != null ? !stageCounter.equals(that.stageCounter) : that.stageCounter != null) return false;
88114
return jobName != null ? jobName.equals(that.jobName) : that.jobName == null;
89115
}
@@ -93,7 +119,7 @@ public int hashCode() {
93119
int result = pipelineName != null ? pipelineName.hashCode() : 0;
94120
result = 31 * result + (int) (pipelineCounter ^ (pipelineCounter >>> 32));
95121
result = 31 * result + (pipelineLabel != null ? pipelineLabel.hashCode() : 0);
96-
result = 31 * result + (staqeName != null ? staqeName.hashCode() : 0);
122+
result = 31 * result + (stageName != null ? stageName.hashCode() : 0);
97123
result = 31 * result + (stageCounter != null ? stageCounter.hashCode() : 0);
98124
result = 31 * result + (jobName != null ? jobName.hashCode() : 0);
99125
result = 31 * result + (int) (jobId ^ (jobId >>> 32));
@@ -106,22 +132,10 @@ public String toString() {
106132
"pipelineName='" + pipelineName + '\'' +
107133
", pipelineCounter=" + pipelineCounter +
108134
", pipelineLabel='" + pipelineLabel + '\'' +
109-
", staqeName='" + staqeName + '\'' +
135+
", staqeName='" + stageName + '\'' +
110136
", stageCounter='" + stageCounter + '\'' +
111137
", jobName='" + jobName + '\'' +
112138
", jobId=" + jobId +
113139
'}';
114140
}
115-
116-
public Long getJobId() {
117-
return jobId;
118-
}
119-
120-
public String getJobName() {
121-
return jobName;
122-
}
123-
124-
public String representation() {
125-
return pipelineName + "/" + pipelineCounter + "/" + staqeName + "/" + stageCounter + "/" + jobName;
126-
}
127141
}

src/main/java/cd/go/contrib/elasticagent/model/reports/agent/KubernetesElasticAgent.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111

1212
import java.util.ArrayList;
1313

14+
import static cd.go.contrib.elasticagent.utils.Util.GSON;
15+
1416
public class KubernetesElasticAgent {
17+
private JobIdentifier jobIdentifier;
1518
private KubernetesPodDetails podDetails;
1619
private GoCDContainerDetails agentDetails;
1720
private String elasticAgentId;
1821
private ArrayList<KubernetesPodEvent> events;
1922
private String logs;
2023
private String configuration;
2124

22-
public static KubernetesElasticAgent fromPod(KubernetesClient client, Pod pod, String elasticAgentId, JobIdentifier jobIdentifier) {
25+
public static KubernetesElasticAgent fromPod(KubernetesClient client, Pod pod, JobIdentifier jobIdentifier) {
2326
KubernetesElasticAgent agent = new KubernetesElasticAgent();
24-
agent.elasticAgentId = elasticAgentId;
27+
agent.jobIdentifier = getJobIdentifier(pod, jobIdentifier);
28+
agent.elasticAgentId = pod.getMetadata().getName();
2529
agent.podDetails = KubernetesPodDetails.fromPod(pod);
2630
agent.agentDetails = GoCDContainerDetails.fromContainer(pod.getSpec().getContainers().get(0), pod.getStatus().getContainerStatuses().get(0));
2731
agent.events = getAllEventsForPod(pod, client);
@@ -30,6 +34,15 @@ public static KubernetesElasticAgent fromPod(KubernetesClient client, Pod pod, S
3034
return agent;
3135
}
3236

37+
private static JobIdentifier getJobIdentifier(Pod pod, JobIdentifier jobIdentifier) {
38+
if (jobIdentifier != null) {
39+
return jobIdentifier;
40+
}
41+
42+
final String json = pod.getMetadata().getAnnotations().get(Constants.JOB_IDENTIFIER_LABEL_KEY);
43+
return GSON.fromJson(json, JobIdentifier.class);
44+
}
45+
3346
public ArrayList<KubernetesPodEvent> getEvents() {
3447
return events;
3548
}
@@ -54,6 +67,10 @@ public String getElasticAgentId() {
5467
return elasticAgentId;
5568
}
5669

70+
public JobIdentifier getJobIdentifier() {
71+
return jobIdentifier;
72+
}
73+
5774
private static ArrayList<KubernetesPodEvent> getAllEventsForPod(Pod pod, KubernetesClient client) {
5875
ArrayList<KubernetesPodEvent> events = new ArrayList<>();
5976

0 commit comments

Comments
 (0)