Skip to content

Commit a10288d

Browse files
committed
Add JobId on KubernetesInstance instance
1 parent f552e15 commit a10288d

File tree

7 files changed

+18
-9
lines changed

7 files changed

+18
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ public class KubernetesInstance {
2727
private final String environment;
2828
private final String name;
2929
private final Map<String, String> properties;
30+
private final Long jobId;
3031

31-
public KubernetesInstance(DateTime createdAt, String environment, String name, Map<String, String> properties) {
32+
public KubernetesInstance(DateTime createdAt, String environment, String name, Map<String, String> properties, Long jobId) {
3233
this.createdAt = createdAt.withZone(DateTimeZone.UTC);
3334
this.environment = environment;
3435
this.name = name;
3536
this.properties = properties;
37+
this.jobId = jobId;
3638
}
3739

3840
public void terminate(KubernetesClient client) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public KubernetesInstance fromKubernetesPod(Pod elasticAgentPod) {
117117
createdAt = new DateTime(getSimpleDateFormat().parse(metadata.getCreationTimestamp())).withZone(DateTimeZone.UTC);
118118
}
119119
String environment = metadata.getLabels().get(Constants.ENVIRONMENT_LABEL_KEY);
120-
kubernetesInstance = new KubernetesInstance(createdAt, environment, metadata.getName(), metadata.getAnnotations());
120+
Long jobId = Long.valueOf(metadata.getLabels().get(Constants.JOB_ID_LABEL_KEY));
121+
kubernetesInstance = new KubernetesInstance(createdAt, environment, metadata.getName(), metadata.getAnnotations(), jobId);
121122
} catch (ParseException e) {
122123
throw new RuntimeException(e);
123124
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public JobIdentifier() {
5757
jobId = null;
5858
}
5959

60-
public JobIdentifier(String pipelineName, long pipelineCounter, String pipelineLabel, String staqeName, String stageCounter, String jobName, long jobId) {
60+
public JobIdentifier(Long jobId) {
61+
this(null, null, null, null, null, null, jobId);
62+
}
63+
64+
public JobIdentifier(String pipelineName, Long pipelineCounter, String pipelineLabel, String staqeName, String stageCounter, String jobName, Long jobId) {
6165
this.pipelineName = pipelineName;
6266
this.pipelineCounter = pipelineCounter;
6367
this.pipelineLabel = pipelineLabel;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void setUp() throws Exception {
6565

6666
@Test
6767
public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance() throws Exception {
68-
KubernetesInstance kubernetesInstance = new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>());
68+
KubernetesInstance kubernetesInstance = new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>(), null);
6969
when(mockKubernetesInstanceFactory.create(mockCreateAgentRequest, mockPluginSettings, mockKubernetesClient, mockPluginRequest, true)).
7070
thenReturn(kubernetesInstance);
7171

@@ -78,7 +78,7 @@ public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance() throw
7878

7979
@Test
8080
public void shouldCreateKubernetesPodAndCacheCreatedInstance() throws Exception {
81-
KubernetesInstance kubernetesInstance = new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>());
81+
KubernetesInstance kubernetesInstance = new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>(), null);
8282
when(mockKubernetesInstanceFactory.create(mockCreateAgentRequest, mockPluginSettings, mockKubernetesClient, mockPluginRequest, false)).
8383
thenReturn(kubernetesInstance);
8484
testProperties.put("SpecifiedUsingPodConfiguration", "false");

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,15 @@ public void testShouldTerminateInstancesThatNeverAutoRegistered() throws Excepti
117117
KubernetesAgentInstances agentInstances = new KubernetesAgentInstances(factory);
118118
HashMap<String, String> properties = new HashMap<>();
119119
properties.put("Image", "foo");
120-
KubernetesInstance container = agentInstances.create(new CreateAgentRequest(null, properties, null, new JobIdentifier()), createSettings(), null);
120+
KubernetesInstance container = agentInstances.create(new CreateAgentRequest(null, properties, null, new JobIdentifier(1L)), createSettings(), null);
121121

122122
agentInstances.clock = new Clock.TestClock().forward(Period.minutes(11));
123123
PluginRequest pluginRequest = mock(PluginRequest.class);
124124

125125
objectMetadata.setName(container.name());
126-
objectMetadata.setLabels(new HashMap<>());
126+
HashMap<String, String> labels = new HashMap<>();
127+
labels.put(Constants.JOB_ID_LABEL_KEY, "1");
128+
objectMetadata.setLabels(labels);
127129
when(pluginRequest.getPluginSettings()).thenReturn(createSettings());
128130
when(pluginRequest.listAgents()).thenReturn(new Agents());
129131
verifyNoMoreInteractions(pluginRequest);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setUp() throws Exception {
6969
agentInstances = new KubernetesAgentInstances(factory);
7070
properties.put("foo", "bar");
7171
properties.put("Image", "gocdcontrib/ubuntu-docker-elastic-agent");
72-
instance = agentInstances.create(new CreateAgentRequest(UUID.randomUUID().toString(), properties, environment, new JobIdentifier()), createSettings(), null);
72+
instance = agentInstances.create(new CreateAgentRequest(UUID.randomUUID().toString(), properties, environment, new JobIdentifier(1L)), createSettings(), null);
7373
}
7474

7575
@Test

src/test/java/cd/go/contrib/elasticagent/requests/CreateAgentRequestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void shouldDeserializeFromJSON() throws Exception {
5757
expectedProperties.put("key2", "value2");
5858
assertThat(request.properties(), Matchers.<Map<String, String>>equalTo(expectedProperties));
5959

60-
JobIdentifier expectedJobIdentifier = new JobIdentifier("test-pipeline", 1, "Test Pipeline", "test-stage", "1", "test-job", 100);
60+
JobIdentifier expectedJobIdentifier = new JobIdentifier("test-pipeline", 1L, "Test Pipeline", "test-stage", "1", "test-job", 100L);
6161
JobIdentifier actualJobIdentifier = request.jobIdentifier();
6262

6363
assertThat(actualJobIdentifier, is(expectedJobIdentifier));

0 commit comments

Comments
 (0)