diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtension.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtension.java index fd0da42cc45..334d1b121b3 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtension.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtension.java @@ -16,7 +16,6 @@ package com.thoughtworks.go.plugin.access.elastic; -import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.domain.JobIdentifier; import com.thoughtworks.go.plugin.access.ExtensionsRegistry; import com.thoughtworks.go.plugin.access.PluginRequestHelper; @@ -49,13 +48,13 @@ public ElasticAgentExtension(PluginManager pluginManager, ExtensionsRegistry ext super(pluginManager, extensionsRegistry, new PluginRequestHelper(pluginManager, SUPPORTED_VERSIONS, ELASTIC_AGENT_EXTENSION), ELASTIC_AGENT_EXTENSION); elasticAgentExtensionMap.put(ElasticAgentExtensionV4.VERSION, new ElasticAgentExtensionV4(pluginRequestHelper)); elasticAgentExtensionMap.put(ElasticAgentExtensionV5.VERSION, new ElasticAgentExtensionV5(pluginRequestHelper)); - + registerHandler(ElasticAgentExtensionV4.VERSION, new PluginSettingsJsonMessageHandler1_0()); registerHandler(ElasticAgentExtensionV5.VERSION, new PluginSettingsJsonMessageHandler1_0()); } - public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map configuration, final Map clusterProfileConfiguration, JobIdentifier jobIdentifier) { + public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map configuration, final Map clusterProfileConfiguration, JobIdentifier jobIdentifier) { getVersionedElasticAgentExtension(pluginId).createAgent(pluginId, autoRegisterKey, environment, configuration, clusterProfileConfiguration, jobIdentifier); } @@ -63,8 +62,8 @@ public void serverPing(final String pluginId) { getVersionedElasticAgentExtension(pluginId).serverPing(pluginId); } - public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map configuration, JobIdentifier identifier) { - return getVersionedElasticAgentExtension(pluginId).shouldAssignWork(pluginId, agent, environment, configuration, identifier); + public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map configuration, Map clusterProfileProperties, JobIdentifier identifier) { + return getVersionedElasticAgentExtension(pluginId).shouldAssignWork(pluginId, agent, environment, configuration, clusterProfileProperties, identifier); } List getProfileMetadata(String pluginId) { diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentPluginRegistry.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentPluginRegistry.java index 25238b65a78..278fafa6cc0 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentPluginRegistry.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentPluginRegistry.java @@ -51,10 +51,10 @@ public void serverPing(String pluginId) { LOGGER.debug("Done processing server ping for plugin {}", pluginId); } - public boolean shouldAssignWork(PluginDescriptor plugin, AgentMetadata agent, String environment, Map configuration, JobIdentifier identifier) { - LOGGER.debug("Processing should assign work for plugin: {} with agent: {} with environment: {} with configuration: {}", plugin.id(), agent, environment, configuration); - boolean result = extension.shouldAssignWork(plugin.id(), agent, environment, configuration, identifier); - LOGGER.debug("Done processing should assign work (result: {}) for plugin: {} with agent: {} with environment: {} with configuration {}", result, plugin.id(), agent, environment, configuration); + public boolean shouldAssignWork(PluginDescriptor plugin, AgentMetadata agent, String environment, Map configuration, Map clusterProfileProperties, JobIdentifier identifier) { + LOGGER.debug("Processing should assign work for plugin: {} with agent: {} with environment: {} with configuration: {} in cluster: {}", plugin.id(), agent, environment, configuration, clusterProfileProperties); + boolean result = extension.shouldAssignWork(plugin.id(), agent, environment, configuration, clusterProfileProperties, identifier); + LOGGER.debug("Done processing should assign work (result: {}) for plugin: {} with agent: {} with environment: {} with configuration {} in cluster: {}", result, plugin.id(), agent, environment, configuration, clusterProfileProperties); return result; } diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/VersionedElasticAgentExtension.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/VersionedElasticAgentExtension.java index 365b3bfdbae..43f5cf6fe4e 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/VersionedElasticAgentExtension.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/VersionedElasticAgentExtension.java @@ -46,7 +46,7 @@ public interface VersionedElasticAgentExtension { void serverPing(String pluginId); - boolean shouldAssignWork(String pluginId, AgentMetadata agent, String environment, Map configuration, JobIdentifier identifier); + boolean shouldAssignWork(String pluginId, AgentMetadata agent, String environment, Map configuration, Map clusterProfileProperties, JobIdentifier identifier); String getPluginStatusReport(String pluginId); diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v4/ElasticAgentExtensionV4.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v4/ElasticAgentExtensionV4.java index 7a4ca95caf7..49288afc1f4 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v4/ElasticAgentExtensionV4.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v4/ElasticAgentExtensionV4.java @@ -129,7 +129,7 @@ public void serverPing(final String pluginId) { } @Override - public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map configuration, JobIdentifier identifier) { + public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map configuration, Map clusterProfileProperties, JobIdentifier identifier) { return pluginRequestHelper.submitRequest(pluginId, REQUEST_SHOULD_ASSIGN_WORK, new DefaultPluginInteractionCallback() { @Override public String requestBody(String resolvedExtensionVersion) { diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionConverterV5.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionConverterV5.java index 1c2ce84d2b7..ebe653da191 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionConverterV5.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionConverterV5.java @@ -19,7 +19,6 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; -import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.domain.JobIdentifier; import com.thoughtworks.go.plugin.access.common.handler.JSONResultMessageHandler; import com.thoughtworks.go.plugin.access.common.models.ImageDeserializer; @@ -49,10 +48,11 @@ String createAgentRequestBody(String autoRegisterKey, String environment, Map configuration, JobIdentifier identifier) { + String shouldAssignWorkRequestBody(AgentMetadata elasticAgent, String environment, Map configuration, Map clusterProfileProperties, JobIdentifier identifier) { JsonObject jsonObject = new JsonObject(); - jsonObject.add("properties", mapToJsonObject(configuration)); jsonObject.addProperty("environment", environment); + jsonObject.add("elastic_agent_profile_properties", mapToJsonObject(configuration)); + jsonObject.add("cluster_profile_properties", mapToJsonObject(clusterProfileProperties)); jsonObject.add("agent", agentMetadataConverterV5.toDTO(elasticAgent).toJSON()); jsonObject.add("job_identifier", jobIdentifierJson(identifier)); return GSON.toJson(jsonObject); diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionV5.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionV5.java index fd6de992552..b4368f4df73 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionV5.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionV5.java @@ -146,11 +146,11 @@ public void serverPing(final String pluginId) { } @Override - public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map configuration, JobIdentifier identifier) { + public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map configuration, Map clusterProfileProperties, JobIdentifier identifier) { return pluginRequestHelper.submitRequest(pluginId, REQUEST_SHOULD_ASSIGN_WORK, new DefaultPluginInteractionCallback() { @Override public String requestBody(String resolvedExtensionVersion) { - return elasticAgentExtensionConverterV5.shouldAssignWorkRequestBody(agent, environment, configuration, identifier); + return elasticAgentExtensionConverterV5.shouldAssignWorkRequestBody(agent, environment, configuration,clusterProfileProperties, identifier); } @Override diff --git a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV4Test.java b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV4Test.java index 11806ab61ad..331ad426671 100644 --- a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV4Test.java +++ b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV4Test.java @@ -211,7 +211,7 @@ public void shouldMakeShouldAssignWorkCall() { final AgentMetadata agentMetadata = new AgentMetadata("foo-agent-id", "Idle", "Idle", "Enabled"); when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success("true")); - final boolean shouldAssignWork = extensionV4.shouldAssignWork(PLUGIN_ID, agentMetadata, "test-env", profile, new JobIdentifier()); + final boolean shouldAssignWork = extensionV4.shouldAssignWork(PLUGIN_ID, agentMetadata, "test-env", profile, null, new JobIdentifier()); assertTrue(shouldAssignWork); diff --git a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV5Test.java b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV5Test.java index 8b471ff285d..b93a8c801f5 100644 --- a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV5Test.java +++ b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV5Test.java @@ -259,18 +259,21 @@ public void shouldSendServerPing() { @Test public void shouldMakeShouldAssignWorkCall() { - final Map profile = Collections.singletonMap("ServerURL", "https://example.com/go"); + final Map profile = Collections.singletonMap("Image", "alpine:latest"); + final Map clusterProfileProperties = Collections.singletonMap("ServerURL", "https://example.com/go"); final AgentMetadata agentMetadata = new AgentMetadata("foo-agent-id", "Idle", "Idle", "Enabled"); when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success("true")); - - final boolean shouldAssignWork = extensionV5.shouldAssignWork(PLUGIN_ID, agentMetadata, "test-env", profile, new JobIdentifier()); + final boolean shouldAssignWork = extensionV5.shouldAssignWork(PLUGIN_ID, agentMetadata, "test-env", profile, clusterProfileProperties, new JobIdentifier()); assertTrue(shouldAssignWork); String expectedRequestBody = "{\n" + - " \"properties\": {\n" + + " \"cluster_profile_properties\": {\n" + " \"ServerURL\": \"https://example.com/go\"\n" + " },\n" + + " \"elastic_agent_profile_properties\": {\n" + + " \"Image\": \"alpine:latest\"\n" + + " },\n" + " \"environment\": \"test-env\",\n" + " \"agent\": {\n" + " \"agent_id\": \"foo-agent-id\",\n" + diff --git a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentPluginRegistryTest.java b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentPluginRegistryTest.java index b2204bf2b72..f36d2a8c00f 100644 --- a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentPluginRegistryTest.java +++ b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentPluginRegistryTest.java @@ -79,12 +79,12 @@ public void shouldTalkToExtensionToExecuteServerPingCall() { public void shouldTalkToExtensionToExecuteShouldAssignWorkCall() { final String environment = "test-env"; final JobIdentifier jobIdentifier = new JobIdentifier(); - final Map configuration = Collections.singletonMap("GoServerURL", "foo"); + final Map configuration = Collections.singletonMap("Image", "alpine:latest"); + final Map clusterProfileProperties = Collections.singletonMap("GoServerURL", "foo"); final AgentMetadata agentMetadata = new AgentMetadata("som-id", "Idle", "Idle", "Enabled"); - elasticAgentPluginRegistry.shouldAssignWork(pluginDescriptor, agentMetadata, environment, configuration, jobIdentifier); - - verify(elasticAgentExtension, times(1)).shouldAssignWork(PLUGIN_ID, agentMetadata, environment, configuration, jobIdentifier); + elasticAgentPluginRegistry.shouldAssignWork(pluginDescriptor, agentMetadata, environment, configuration, clusterProfileProperties, jobIdentifier); + verify(elasticAgentExtension, times(1)).shouldAssignWork(PLUGIN_ID, agentMetadata, environment, configuration, clusterProfileProperties, jobIdentifier); verifyNoMoreInteractions(elasticAgentExtension); } diff --git a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionConverterV5Test.java b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionConverterV5Test.java index ce4ce391074..e669bfc3af0 100644 --- a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionConverterV5Test.java +++ b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionConverterV5Test.java @@ -92,7 +92,10 @@ public void shouldJSONizeCreateAgentRequestBody() throws Exception { public void shouldJSONizeShouldAssignWorkRequestBody() throws Exception { HashMap configuration = new HashMap<>(); configuration.put("property_name", "property_value"); - String actual = new ElasticAgentExtensionConverterV5().shouldAssignWorkRequestBody(elasticAgent(), "prod", configuration, jobIdentifier); + HashMap clusterProfileProperties = new HashMap<>(); + clusterProfileProperties.put("property_name", "property_value"); + + String actual = new ElasticAgentExtensionConverterV5().shouldAssignWorkRequestBody(elasticAgent(), "prod", configuration, clusterProfileProperties, jobIdentifier); String expected = "{" + " \"environment\":\"prod\"," + " \"agent\":{" + @@ -101,7 +104,10 @@ public void shouldJSONizeShouldAssignWorkRequestBody() throws Exception { " \"build_state\":\"Idle\"," + " \"config_state\":\"Enabled\"" + " }," + - " \"properties\":{" + + " \"elastic_agent_profile_properties\":{" + + " \"property_name\":\"property_value\"" + + " }," + + " \"cluster_profile_properties\":{" + " \"property_name\":\"property_value\"" + " }," + " \"job_identifier\": {\n" + diff --git a/server/src/main/java/com/thoughtworks/go/server/service/ElasticAgentPluginService.java b/server/src/main/java/com/thoughtworks/go/server/service/ElasticAgentPluginService.java index df8f1861f1f..c4bd0789d95 100644 --- a/server/src/main/java/com/thoughtworks/go/server/service/ElasticAgentPluginService.java +++ b/server/src/main/java/com/thoughtworks/go/server/service/ElasticAgentPluginService.java @@ -184,6 +184,8 @@ private Predicate isElasticAgent() { } public boolean shouldAssignWork(ElasticAgentMetadata metadata, String environment, ElasticProfile elasticProfile, JobIdentifier identifier) { + ClusterProfile clusterProfile = clusterProfilesService.findProfile(elasticProfile.getClusterProfileId()); + Map clusterProfileProperties = clusterProfile != null ? clusterProfile.getConfigurationAsMap(true) : Collections.EMPTY_MAP; GoPluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptorFor(metadata.elasticPluginId()); Map configuration = elasticProfile.getConfigurationAsMap(true); @@ -191,7 +193,7 @@ public boolean shouldAssignWork(ElasticAgentMetadata metadata, String environmen return false; } - return elasticAgentPluginRegistry.shouldAssignWork(pluginDescriptor, toAgentMetadata(metadata), environment, configuration, identifier); + return elasticAgentPluginRegistry.shouldAssignWork(pluginDescriptor, toAgentMetadata(metadata), environment, configuration, clusterProfileProperties, identifier); } public String getPluginStatusReport(String pluginId) { diff --git a/server/src/test-fast/java/com/thoughtworks/go/server/service/ElasticAgentPluginServiceTest.java b/server/src/test-fast/java/com/thoughtworks/go/server/service/ElasticAgentPluginServiceTest.java index e7d31969ca3..665e11fcdb4 100644 --- a/server/src/test-fast/java/com/thoughtworks/go/server/service/ElasticAgentPluginServiceTest.java +++ b/server/src/test-fast/java/com/thoughtworks/go/server/service/ElasticAgentPluginServiceTest.java @@ -254,7 +254,7 @@ void shouldAssignJobToAnAgentIfThePluginMatchesForTheAgentAndJob_AndThePluginAgr ElasticAgentMetadata agentMetadata = new ElasticAgentMetadata(uuid, uuid, elasticPluginId, AgentRuntimeStatus.Idle, AgentConfigStatus.Enabled); ElasticProfile elasticProfile = new ElasticProfile("1", elasticPluginId); - when(registry.shouldAssignWork(any(), any(), any(), any(), any())).thenReturn(true); + when(registry.shouldAssignWork(any(), any(), any(), any(), any(), any())).thenReturn(true); assertThat(service.shouldAssignWork(agentMetadata, null, elasticProfile, null)).isTrue(); } @@ -264,7 +264,7 @@ void shouldNotAssignJobToAnAgentIfThePluginMatchesForTheAgentAndJob_ButThePlugin String elasticPluginId = "plugin-1"; ElasticAgentMetadata agentMetadata = new ElasticAgentMetadata(uuid, uuid, elasticPluginId, AgentRuntimeStatus.Idle, AgentConfigStatus.Enabled); ElasticProfile elasticProfile = new ElasticProfile("1", elasticPluginId); - when(registry.shouldAssignWork(any(), any(), any(), any(), any())).thenReturn(false); + when(registry.shouldAssignWork(any(), any(), any(), any(), any(), any())).thenReturn(false); assertThat(service.shouldAssignWork(agentMetadata, null, elasticProfile, null)).isFalse(); }