Skip to content

Commit 0fe2ec7

Browse files
authored
Fix NullPointerException in server-ping request (#14)
* On Server Ping request, to find the gocd-agent-container creation time, pod creation timestamp is used, not setting pod creation timestamp throws a nullPointerException * Specify pod creation timestamp for both pods created via configuration spec or via configuration yaml
1 parent ea08006 commit 0fe2ec7

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ private KubernetesInstance create(CreateAgentRequest request, PluginSettings set
6868

6969
Pod elasticAgentPod = new Pod("v1", "Pod", podMetadata, podSpec, new PodStatus());
7070

71+
setGoCDMetadata(request, settings, pluginRequest, elasticAgentPod);
72+
73+
return createKubernetesPod(client, elasticAgentPod);
74+
}
75+
76+
private void setGoCDMetadata(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest, Pod elasticAgentPod) {
77+
elasticAgentPod.getMetadata().setCreationTimestamp(getSimpleDateFormat().format(new Date()));
78+
7179
setContainerEnvVariables(elasticAgentPod, request, settings, pluginRequest);
7280
setAnnotations(elasticAgentPod, request);
7381
setLabels(elasticAgentPod, request);
74-
75-
return createKubernetesPod(client, elasticAgentPod);
7682
}
7783

7884
private ResourceRequirements getPodResources(CreateAgentRequest request) {
@@ -210,12 +216,7 @@ private KubernetesInstance createUsingPodYaml(CreateAgentRequest request, Plugin
210216
LOG.error(e.getMessage());
211217
}
212218

213-
elasticAgentPod.getMetadata().setCreationTimestamp(getSimpleDateFormat().format(new Date()));
214-
215-
setContainerEnvVariables(elasticAgentPod, request, settings, pluginRequest);
216-
setAnnotations(elasticAgentPod, request);
217-
setLabels(elasticAgentPod, request);
218-
219+
setGoCDMetadata(request, settings, pluginRequest, elasticAgentPod);
219220
return createKubernetesPod(client, elasticAgentPod);
220221
}
221222

src/test/java/cd/go/contrib/elasticagent/KubernetesAgentInstancesIntegrationTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ public void shouldCreateKubernetesPodWithPodMetadata() throws Exception {
136136
assertThat(elasticAgentPod.getMetadata().getName(), is(instance.name()));
137137
}
138138

139+
@Test
140+
public void shouldCreateKubernetesPodWithTimeStamp() throws Exception {
141+
ArgumentCaptor<Pod> argumentCaptor = ArgumentCaptor.forClass(Pod.class);
142+
kubernetesAgentInstances.create(createAgentRequest, settings, mockedPluginRequest);
143+
verify(pods).create(argumentCaptor.capture());
144+
145+
Pod elasticAgentPod = argumentCaptor.getValue();
146+
147+
assertNotNull(elasticAgentPod.getMetadata());
148+
assertNotNull(elasticAgentPod.getMetadata().getCreationTimestamp());
149+
}
150+
139151
@Test
140152
public void shouldCreateKubernetesPodWithGoCDElasticAgentContainerContainingEnvironmentVariables() throws Exception {
141153
ArgumentCaptor<Pod> argumentCaptor = ArgumentCaptor.forClass(Pod.class);
@@ -238,6 +250,20 @@ public void usingPodYamlConfigurations_shouldCreateKubernetesPodWithPodMetadata(
238250
assertThat(elasticAgentPod.getMetadata().getName(), is(instance.name()));
239251
}
240252

253+
@Test
254+
public void usingPodYamlConfigurations_shouldCreateKubernetesPodWithTimestamp() throws Exception {
255+
createAgentRequest = CreateAgentRequestMother.createAgentRequestUsingPodYaml();
256+
257+
ArgumentCaptor<Pod> argumentCaptor = ArgumentCaptor.forClass(Pod.class);
258+
kubernetesAgentInstances.create(createAgentRequest, settings, mockedPluginRequest);
259+
verify(pods).create(argumentCaptor.capture());
260+
261+
Pod elasticAgentPod = argumentCaptor.getValue();
262+
263+
assertNotNull(elasticAgentPod.getMetadata());
264+
assertNotNull(elasticAgentPod.getMetadata().getCreationTimestamp());
265+
}
266+
241267
@Test
242268
public void usingPodYamlConfigurations_shouldCreateKubernetesPodWithGoCDElasticAgentContainerContainingEnvironmentVariables() throws Exception {
243269
createAgentRequest = CreateAgentRequestMother.createAgentRequestUsingPodYaml();

0 commit comments

Comments
 (0)