Skip to content

Commit

Permalink
Pass along cluster profile properties to should assign work request (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
GaneshSPatil committed Mar 18, 2019
1 parent 767167a commit 9ecbf11
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,22 +48,22 @@ 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<String, String> configuration, final Map<String, String> clusterProfileConfiguration, JobIdentifier jobIdentifier) {
public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map<String, String> configuration, final Map<String, String> clusterProfileConfiguration, JobIdentifier jobIdentifier) {
getVersionedElasticAgentExtension(pluginId).createAgent(pluginId, autoRegisterKey, environment, configuration, clusterProfileConfiguration, jobIdentifier);
}

public void serverPing(final String pluginId) {
getVersionedElasticAgentExtension(pluginId).serverPing(pluginId);
}

public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map<String, String> 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<String, String> configuration, Map<String, String> clusterProfileProperties, JobIdentifier identifier) {
return getVersionedElasticAgentExtension(pluginId).shouldAssignWork(pluginId, agent, environment, configuration, clusterProfileProperties, identifier);
}

List<PluginConfiguration> getProfileMetadata(String pluginId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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<String, String> configuration, Map<String, String> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public interface VersionedElasticAgentExtension {

void serverPing(String pluginId);

boolean shouldAssignWork(String pluginId, AgentMetadata agent, String environment, Map<String, String> configuration, JobIdentifier identifier);
boolean shouldAssignWork(String pluginId, AgentMetadata agent, String environment, Map<String, String> configuration, Map<String, String> clusterProfileProperties, JobIdentifier identifier);

String getPluginStatusReport(String pluginId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void serverPing(final String pluginId) {
}

@Override
public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map<String, String> configuration, JobIdentifier identifier) {
public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map<String, String> configuration, Map<String, String> clusterProfileProperties, JobIdentifier identifier) {
return pluginRequestHelper.submitRequest(pluginId, REQUEST_SHOULD_ASSIGN_WORK, new DefaultPluginInteractionCallback<Boolean>() {
@Override
public String requestBody(String resolvedExtensionVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,10 +48,11 @@ String createAgentRequestBody(String autoRegisterKey, String environment, Map<St
return GSON.toJson(jsonObject);
}

String shouldAssignWorkRequestBody(AgentMetadata elasticAgent, String environment, Map<String, String> configuration, JobIdentifier identifier) {
String shouldAssignWorkRequestBody(AgentMetadata elasticAgent, String environment, Map<String, String> configuration, Map<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ public void serverPing(final String pluginId) {
}

@Override
public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map<String, String> configuration, JobIdentifier identifier) {
public boolean shouldAssignWork(String pluginId, final AgentMetadata agent, final String environment, final Map<String, String> configuration, Map<String, String> clusterProfileProperties, JobIdentifier identifier) {
return pluginRequestHelper.submitRequest(pluginId, REQUEST_SHOULD_ASSIGN_WORK, new DefaultPluginInteractionCallback<Boolean>() {
@Override
public String requestBody(String resolvedExtensionVersion) {
return elasticAgentExtensionConverterV5.shouldAssignWorkRequestBody(agent, environment, configuration, identifier);
return elasticAgentExtensionConverterV5.shouldAssignWorkRequestBody(agent, environment, configuration,clusterProfileProperties, identifier);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,21 @@ public void shouldSendServerPing() {

@Test
public void shouldMakeShouldAssignWorkCall() {
final Map<String, String> profile = Collections.singletonMap("ServerURL", "https://example.com/go");
final Map<String, String> profile = Collections.singletonMap("Image", "alpine:latest");
final Map<String, String> 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" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public void shouldTalkToExtensionToExecuteServerPingCall() {
public void shouldTalkToExtensionToExecuteShouldAssignWorkCall() {
final String environment = "test-env";
final JobIdentifier jobIdentifier = new JobIdentifier();
final Map<String, String> configuration = Collections.singletonMap("GoServerURL", "foo");
final Map<String, String> configuration = Collections.singletonMap("Image", "alpine:latest");
final Map<String, String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ public void shouldJSONizeCreateAgentRequestBody() throws Exception {
public void shouldJSONizeShouldAssignWorkRequestBody() throws Exception {
HashMap<String, String> configuration = new HashMap<>();
configuration.put("property_name", "property_value");
String actual = new ElasticAgentExtensionConverterV5().shouldAssignWorkRequestBody(elasticAgent(), "prod", configuration, jobIdentifier);
HashMap<String, String> clusterProfileProperties = new HashMap<>();
clusterProfileProperties.put("property_name", "property_value");

String actual = new ElasticAgentExtensionConverterV5().shouldAssignWorkRequestBody(elasticAgent(), "prod", configuration, clusterProfileProperties, jobIdentifier);
String expected = "{" +
" \"environment\":\"prod\"," +
" \"agent\":{" +
Expand All @@ -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" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,16 @@ private Predicate<JobPlan> isElasticAgent() {
}

public boolean shouldAssignWork(ElasticAgentMetadata metadata, String environment, ElasticProfile elasticProfile, JobIdentifier identifier) {
ClusterProfile clusterProfile = clusterProfilesService.findProfile(elasticProfile.getClusterProfileId());
Map<String, String> clusterProfileProperties = clusterProfile != null ? clusterProfile.getConfigurationAsMap(true) : Collections.EMPTY_MAP;
GoPluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptorFor(metadata.elasticPluginId());
Map<String, String> configuration = elasticProfile.getConfigurationAsMap(true);

if (!StringUtils.equals(elasticProfile.getPluginId(), metadata.elasticPluginId())) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}
Expand Down

0 comments on commit 9ecbf11

Please sign in to comment.