Skip to content

Commit 5e83c8e

Browse files
committed
Includes job identfiers in the check to see if work can be assigned to an agent
1 parent 8fbae5f commit 5e83c8e

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,11 @@
1818

1919
import cd.go.contrib.elasticagent.AgentInstances;
2020
import cd.go.contrib.elasticagent.KubernetesInstance;
21-
import cd.go.contrib.elasticagent.KubernetesInstanceFactory;
2221
import cd.go.contrib.elasticagent.RequestExecutor;
2322
import cd.go.contrib.elasticagent.requests.ShouldAssignWorkRequest;
2423
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
2524
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
2625

27-
import java.util.HashMap;
28-
import java.util.Map;
29-
30-
import static cd.go.contrib.elasticagent.KubernetesPlugin.LOG;
31-
import static org.apache.commons.lang3.StringUtils.stripToEmpty;
32-
3326
public class ShouldAssignWorkRequestExecutor implements RequestExecutor {
3427
private final AgentInstances<KubernetesInstance> agentInstances;
3528
private final ShouldAssignWorkRequest request;
@@ -41,22 +34,13 @@ public ShouldAssignWorkRequestExecutor(ShouldAssignWorkRequest request, AgentIns
4134

4235
@Override
4336
public GoPluginApiResponse execute() {
44-
KubernetesInstance instance = agentInstances.find(request.agent().elasticAgentId());
37+
KubernetesInstance pod = agentInstances.find(request.agent().elasticAgentId());
4538

46-
if (instance == null) {
39+
if (pod == null) {
4740
return DefaultGoPluginApiResponse.success("false");
4841
}
4942

50-
boolean environmentMatches = stripToEmpty(request.environment()).equalsIgnoreCase(stripToEmpty(instance.environment()));
51-
52-
53-
Map<String, String> containerProperties = instance.getInstanceProperties() == null ? new HashMap<>() : instance.getInstanceProperties();
54-
Map<String, String> requestProperties = request.properties() == null ? new HashMap<>() : request.properties();
55-
56-
boolean propertiesMatch = requestProperties.equals(containerProperties);
57-
58-
if (environmentMatches && propertiesMatch) {
59-
LOG.debug(String.format("[Should Assign Work] Assigning job[%s] to agent[%s]", request.properties(), request.agent().elasticAgentId()));
43+
if (request.jobIdentifier().getJobId().equals(pod.jobId())) {
6044
return DefaultGoPluginApiResponse.success("true");
6145
}
6246

src/main/java/cd/go/contrib/elasticagent/requests/ShouldAssignWorkRequest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import cd.go.contrib.elasticagent.Request;
2222
import cd.go.contrib.elasticagent.RequestExecutor;
2323
import cd.go.contrib.elasticagent.executors.ShouldAssignWorkRequestExecutor;
24+
import cd.go.contrib.elasticagent.model.JobIdentifier;
2425
import com.google.gson.annotations.Expose;
2526
import com.google.gson.annotations.SerializedName;
2627

@@ -35,20 +36,27 @@ public class ShouldAssignWorkRequest {
3536
@Expose
3637
@SerializedName("agent")
3738
private Agent agent;
39+
3840
@Expose
3941
@SerializedName("environment")
4042
private String environment;
43+
4144
@Expose
4245
@SerializedName("properties")
4346
private Map<String, String> properties;
4447

45-
public ShouldAssignWorkRequest(Agent agent, String environment, Map<String, String> properties) {
48+
@Expose
49+
@SerializedName("job_identifier")
50+
private JobIdentifier jobIdentifier;
51+
52+
public ShouldAssignWorkRequest() {
53+
}
54+
55+
public ShouldAssignWorkRequest(Agent agent, String environment, Map<String, String> properties, JobIdentifier jobIdentifier) {
4656
this.agent = agent;
4757
this.environment = environment;
4858
this.properties = properties;
49-
}
50-
51-
public ShouldAssignWorkRequest() {
59+
this.jobIdentifier = jobIdentifier;
5260
}
5361

5462
public static ShouldAssignWorkRequest fromJSON(String json) {
@@ -70,4 +78,8 @@ public Map<String, String> properties() {
7078
public RequestExecutor executor(AgentInstances agentInstances) {
7179
return new ShouldAssignWorkRequestExecutor(this, agentInstances);
7280
}
81+
82+
public JobIdentifier jobIdentifier() {
83+
return jobIdentifier;
84+
}
7385
}

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import static org.mockito.MockitoAnnotations.initMocks;
4646

4747
public class ShouldAssignWorkRequestExecutorTest extends BaseTest {
48-
private final String environment = "production";
4948
@Mock
5049
KubernetesClientFactory factory;
5150
private AgentInstances<KubernetesInstance> agentInstances;
@@ -57,6 +56,7 @@ public class ShouldAssignWorkRequestExecutorTest extends BaseTest {
5756
private MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockedOperation;
5857
@Mock
5958
private NonNamespaceOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockedNamespaceOperation;
59+
private String environment = "QA";
6060

6161
@Before
6262
public void setUp() throws Exception {
@@ -69,28 +69,23 @@ 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(1L)), createSettings(), null);
72+
instance = agentInstances.create(new CreateAgentRequest(UUID.randomUUID().toString(), properties, environment, new JobIdentifier(100L)), createSettings(), null);
7373
}
7474

7575
@Test
76-
public void shouldAssignWorkToContainerWithMatchingEnvironmentNameAndProperties() throws Exception {
77-
ShouldAssignWorkRequest request = new ShouldAssignWorkRequest(new Agent(instance.name(), null, null, null), environment, properties);
76+
public void shouldAssignWorkWhenJobIdMatchesPodId() throws Exception {
77+
JobIdentifier jobIdentifier = new JobIdentifier("test-pipeline", 1L, "Test Pipeline", "test-stage", "1", "test-job", 100L);
78+
ShouldAssignWorkRequest request = new ShouldAssignWorkRequest(new Agent(instance.name(), null, null, null), environment, properties, jobIdentifier);
7879
GoPluginApiResponse response = new ShouldAssignWorkRequestExecutor(request, agentInstances).execute();
7980
assertThat(response.responseCode(), is(200));
8081
assertThat(response.responseBody(), is("true"));
8182
}
8283

8384
@Test
84-
public void shouldNotAssignWorkToContainerWithDifferentEnvironmentName() throws Exception {
85-
ShouldAssignWorkRequest request = new ShouldAssignWorkRequest(new Agent(instance.name(), null, null, null), "FooEnv", properties);
86-
GoPluginApiResponse response = new ShouldAssignWorkRequestExecutor(request, agentInstances).execute();
87-
assertThat(response.responseCode(), is(200));
88-
assertThat(response.responseBody(), is("false"));
89-
}
90-
91-
@Test
92-
public void shouldNotAssignWorkToContainerWithDifferentProperties() throws Exception {
93-
ShouldAssignWorkRequest request = new ShouldAssignWorkRequest(new Agent(instance.name(), null, null, null), environment, null);
85+
public void shouldNotAssignWorkWhenJobIdDiffersFromPodId() throws Exception {
86+
long mismatchingJobId = 200L;
87+
JobIdentifier jobIdentifier = new JobIdentifier("test-pipeline", 1L, "Test Pipeline", "test-stage", "1", "test-job", mismatchingJobId);
88+
ShouldAssignWorkRequest request = new ShouldAssignWorkRequest(new Agent(instance.name(), null, null, null), "FooEnv", properties, jobIdentifier);
9489
GoPluginApiResponse response = new ShouldAssignWorkRequestExecutor(request, agentInstances).execute();
9590
assertThat(response.responseCode(), is(200));
9691
assertThat(response.responseBody(), is("false"));

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package cd.go.contrib.elasticagent.requests;
1818

1919
import cd.go.contrib.elasticagent.Agent;
20+
import cd.go.contrib.elasticagent.model.JobIdentifier;
2021
import org.hamcrest.Matchers;
2122
import org.junit.Test;
2223

2324
import java.util.HashMap;
2425
import java.util.Map;
2526

2627
import static org.hamcrest.Matchers.equalTo;
28+
import static org.hamcrest.Matchers.is;
2729
import static org.junit.Assert.assertThat;
2830

2931
public class ShouldAssignWorkRequestTest {
@@ -40,6 +42,15 @@ public void shouldDeserializeFromJSON() throws Exception {
4042
" },\n" +
4143
" \"properties\": {\n" +
4244
" \"property_name\": \"property_value\"\n" +
45+
" },\n" +
46+
" \"job_identifier\": {\n" +
47+
" \"pipeline_name\": \"test-pipeline\",\n" +
48+
" \"pipeline_counter\": 1,\n" +
49+
" \"pipeline_label\": \"Test Pipeline\",\n" +
50+
" \"stage_name\": \"test-stage\",\n" +
51+
" \"stage_counter\": \"1\",\n" +
52+
" \"job_name\": \"test-job\",\n" +
53+
" \"job_id\": 100\n" +
4354
" }\n" +
4455
"}";
4556

@@ -49,5 +60,10 @@ public void shouldDeserializeFromJSON() throws Exception {
4960
HashMap<String, String> expectedProperties = new HashMap<>();
5061
expectedProperties.put("property_name", "property_value");
5162
assertThat(request.properties(), Matchers.<Map<String, String>>equalTo(expectedProperties));
63+
64+
JobIdentifier expectedJobIdentifier = new JobIdentifier("test-pipeline", 1L, "Test Pipeline", "test-stage", "1", "test-job", 100L);
65+
JobIdentifier actualJobIdentifier = request.jobIdentifier();
66+
67+
assertThat(actualJobIdentifier, is(expectedJobIdentifier));
5268
}
5369
}

0 commit comments

Comments
 (0)