From c9a93d4d2e501a1747047385feba45f0289da2e4 Mon Sep 17 00:00:00 2001 From: GaneshSPatil Date: Fri, 15 Mar 2019 15:25:53 +0530 Subject: [PATCH] Pass along cluster profile properties to create agent request (#5937) * Modify elastic agent extension v5 create-agent request to pass along cluster_profile_properties as part of request body. --- .../access/elastic/ElasticAgentExtension.java | 5 ++-- .../elastic/ElasticAgentPluginRegistry.java | 10 +++---- .../VersionedElasticAgentExtension.java | 2 +- .../elastic/v4/ElasticAgentExtensionV4.java | 4 +-- .../v5/ElasticAgentExtensionConverterV5.java | 6 +++-- .../elastic/v5/ElasticAgentExtensionV5.java | 4 +-- .../elastic/ElasticAgentExtensionV4Test.java | 2 +- .../elastic/ElasticAgentExtensionV5Test.java | 12 ++++++--- .../ElasticAgentPluginRegistryTest.java | 7 ++--- .../ElasticAgentExtensionConverterV5Test.java | 14 ++++++++-- .../elasticagents/CreateAgentListener.java | 4 +-- .../elasticagents/CreateAgentMessage.java | 14 ++++++++-- .../service/ElasticAgentPluginService.java | 20 +++++++++----- .../elasticagents/CreateAgentMessageTest.java | 24 +++++++++++++---- .../ElasticAgentPluginServiceTest.java | 26 ++++++++++++++----- 15 files changed, 108 insertions(+), 46 deletions(-) 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 0b77f6a51e9..fd0da42cc45 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,6 +16,7 @@ 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; @@ -54,8 +55,8 @@ public ElasticAgentExtension(PluginManager pluginManager, ExtensionsRegistry ext } - public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map configuration, JobIdentifier jobIdentifier) { - getVersionedElasticAgentExtension(pluginId).createAgent(pluginId, autoRegisterKey, environment, configuration, 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); } public void serverPing(final 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 faf87ed7a25..25238b65a78 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,12 +34,12 @@ public ElasticAgentPluginRegistry(PluginManager pluginManager, ElasticAgentExten super(pluginManager, elasticAgentExtension); } - public void createAgent(final String pluginId, String autoRegisterKey, String environment, Map configuration, JobIdentifier jobIdentifier) { + public void createAgent(final String pluginId, String autoRegisterKey, String environment, Map configuration, Map clusterProfileConfiguration, JobIdentifier jobIdentifier) { PluginDescriptor plugin = findPlugin(pluginId); if (plugin != null) { - LOGGER.debug("Processing create agent for plugin: {} with environment: {} with configuration: {}", pluginId, environment, configuration); - extension.createAgent(pluginId, autoRegisterKey, environment, configuration, jobIdentifier); - LOGGER.debug("Done processing create agent for plugin: {} with environment: {} with configuration: {}", pluginId, environment, configuration); + LOGGER.debug("Processing create agent for plugin: {} with environment: {} with elastic agent configuration: {} in cluster: {}", pluginId, environment, configuration, clusterProfileConfiguration); + extension.createAgent(pluginId, autoRegisterKey, environment, configuration, clusterProfileConfiguration, jobIdentifier); + LOGGER.debug("Done processing create agent for plugin: {} with environment: {} with elastic agent configuration: {} in cluster: {}", pluginId, environment, configuration, clusterProfileConfiguration); } else { LOGGER.warn("Could not find plugin with id: {}", pluginId); } 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 d8e8ceeaee3..365b3bfdbae 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 @@ -42,7 +42,7 @@ public interface VersionedElasticAgentExtension { ValidationResult validateClusterProfile(String pluginId, Map configuration); - void createAgent(String pluginId, String autoRegisterKey, String environment, Map configuration, JobIdentifier jobIdentifier); + void createAgent(String pluginId, String autoRegisterKey, String environment, Map configuration, Map clusterProfileConfiguration, JobIdentifier jobIdentifier); void serverPing(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 4d65e2c88d5..7a4ca95caf7 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -114,7 +114,7 @@ public ValidationResult validateClusterProfile(String pluginId, Map configuration, JobIdentifier jobIdentifier) { + public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map configuration, Map clusterProfileConfiguration, JobIdentifier jobIdentifier) { pluginRequestHelper.submitRequest(pluginId, REQUEST_CREATE_AGENT, 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 282677b5e29..1c2ce84d2b7 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,6 +19,7 @@ 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; @@ -37,10 +38,11 @@ class ElasticAgentExtensionConverterV5 { private CapabilitiesConverterV5 capabilitiesConverterV5 = new CapabilitiesConverterV5(); private AgentMetadataConverterV5 agentMetadataConverterV5 = new AgentMetadataConverterV5(); - String createAgentRequestBody(String autoRegisterKey, String environment, Map configuration, JobIdentifier jobIdentifier) { + String createAgentRequestBody(String autoRegisterKey, String environment, Map configuration, Map clusterProfileProperties, JobIdentifier jobIdentifier) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("auto_register_key", autoRegisterKey); - jsonObject.add("properties", mapToJsonObject(configuration)); + jsonObject.add("elastic_agent_profile_properties", mapToJsonObject(configuration)); + jsonObject.add("cluster_profile_properties", mapToJsonObject(clusterProfileProperties)); jsonObject.addProperty("environment", environment); jsonObject.add("job_identifier", jobIdentifierJson(jobIdentifier)); 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 8f0d608c3de..fd6de992552 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 @@ -131,11 +131,11 @@ public ValidationResult onSuccess(String responseBody, Map respo } @Override - public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map configuration, JobIdentifier jobIdentifier) { + public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map configuration, Map clusterProfileConfiguration, JobIdentifier jobIdentifier) { pluginRequestHelper.submitRequest(pluginId, REQUEST_CREATE_AGENT, new DefaultPluginInteractionCallback() { @Override public String requestBody(String resolvedExtensionVersion) { - return elasticAgentExtensionConverterV5.createAgentRequestBody(autoRegisterKey, environment, configuration, jobIdentifier); + return elasticAgentExtensionConverterV5.createAgentRequestBody(autoRegisterKey, environment, configuration, clusterProfileConfiguration, jobIdentifier); } }); } 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 4e786085630..11806ab61ad 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 @@ -151,7 +151,7 @@ public void shouldMakeCreateAgentCall() { final JobIdentifier jobIdentifier = new JobIdentifier("up42", 2, "Test", "up42_stage", "10", "up42_job"); when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(null)); - extensionV4.createAgent(PLUGIN_ID, "auto-registration-key", "test-env", profile, jobIdentifier); + extensionV4.createAgent(PLUGIN_ID, "auto-registration-key", "test-env", profile, null, jobIdentifier); String expectedRequestBody = "{\n" + " \"auto_register_key\": \"auto-registration-key\",\n" + 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 077b81349f1..8b471ff285d 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -195,15 +195,19 @@ public void shouldValidateClusterProfile() { @Test public void shouldMakeCreateAgentCall() { - final Map profile = Collections.singletonMap("ServerURL", "https://example.com/go"); + final Map profile = Collections.singletonMap("Image", "alpine:latest"); + final Map clusterProfile = Collections.singletonMap("ServerURL", "https://example.com/go"); final JobIdentifier jobIdentifier = new JobIdentifier("up42", 2, "Test", "up42_stage", "10", "up42_job"); when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(null)); - extensionV5.createAgent(PLUGIN_ID, "auto-registration-key", "test-env", profile, jobIdentifier); + extensionV5.createAgent(PLUGIN_ID, "auto-registration-key", "test-env", profile, clusterProfile, jobIdentifier); String expectedRequestBody = "{\n" + " \"auto_register_key\": \"auto-registration-key\",\n" + - " \"properties\": {\n" + + " \"elastic_agent_profile_properties\": {\n" + + " \"Image\": \"alpine:latest\"\n" + + " },\n" + + " \"cluster_profile_properties\": {\n" + " \"ServerURL\": \"https://example.com/go\"\n" + " },\n" + " \"environment\": \"test-env\",\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 ed825369a05..b2204bf2b72 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,13 +56,14 @@ public void setUp() throws Exception { @Test public void shouldTalkToExtensionToCreateElasticAgent() { final Map configuration = Collections.singletonMap("GoServerURL", "foo"); + final Map clusterConfiguration = Collections.singletonMap("GoServerURL", "foo"); final JobIdentifier jobIdentifier = new JobIdentifier(); final String autoRegisterKey = "auto-register-key"; final String environment = "test-env"; - elasticAgentPluginRegistry.createAgent(PLUGIN_ID, autoRegisterKey, environment, configuration, jobIdentifier); + elasticAgentPluginRegistry.createAgent(PLUGIN_ID, autoRegisterKey, environment, configuration, clusterConfiguration, jobIdentifier); - verify(elasticAgentExtension, times(1)).createAgent(PLUGIN_ID, autoRegisterKey, environment, configuration, jobIdentifier); + verify(elasticAgentExtension, times(1)).createAgent(PLUGIN_ID, autoRegisterKey, environment, configuration, clusterConfiguration, 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 725084c7626..ce4ce391074 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 @@ -58,10 +58,20 @@ public void shouldJSONizeCreateAgentRequestBody() throws Exception { Map configuration = new HashMap<>(); configuration.put("key1", "value1"); configuration.put("key2", "value2"); - String json = new ElasticAgentExtensionConverterV5().createAgentRequestBody("secret-key", "prod", configuration, jobIdentifier); + + Map clusterProfileConfiguration = new HashMap<>(); + clusterProfileConfiguration.put("key1", "value1"); + clusterProfileConfiguration.put("key2", "value2"); + + String json = new ElasticAgentExtensionConverterV5().createAgentRequestBody("secret-key", "prod", configuration, clusterProfileConfiguration, jobIdentifier); + assertThatJson(json).isEqualTo("{" + " \"auto_register_key\":\"secret-key\"," + - " \"properties\":{" + + " \"elastic_agent_profile_properties\":{" + + " \"key1\":\"value1\"," + + " \"key2\":\"value2\"" + + " }," + + " \"cluster_profile_properties\":{" + " \"key1\":\"value1\"," + " \"key2\":\"value2\"" + " }," + diff --git a/server/src/main/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentListener.java b/server/src/main/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentListener.java index 138bd538427..3e1ba141e55 100644 --- a/server/src/main/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentListener.java +++ b/server/src/main/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,6 @@ public CreateAgentListener(ElasticAgentPluginRegistry elasticAgentPluginRegistry @Override public void onMessage(CreateAgentMessage message) { - elasticAgentPluginRegistry.createAgent(message.pluginId(), message.autoregisterKey(), message.environment(), message.configuration(), message.jobIdentifier()); + elasticAgentPluginRegistry.createAgent(message.pluginId(), message.autoregisterKey(), message.environment(), message.configuration(), message.getClusterProfileConfiguration(), message.jobIdentifier()); } } diff --git a/server/src/main/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentMessage.java b/server/src/main/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentMessage.java index 76f65e74962..9f429a92d11 100644 --- a/server/src/main/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentMessage.java +++ b/server/src/main/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentMessage.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,24 +16,28 @@ package com.thoughtworks.go.server.messaging.elasticagents; +import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ElasticProfile; import com.thoughtworks.go.domain.JobIdentifier; import com.thoughtworks.go.server.messaging.PluginAwareMessage; +import java.util.Collections; import java.util.Map; public class CreateAgentMessage implements PluginAwareMessage { private final String autoregisterKey; private final String environment; private final Map configuration; + private Map clusterProfile; private final JobIdentifier jobIdentifier; private final String pluginId; - public CreateAgentMessage(String autoregisterKey, String environment, ElasticProfile elasticProfile, JobIdentifier jobIdentifier) { + public CreateAgentMessage(String autoregisterKey, String environment, ElasticProfile elasticProfile, ClusterProfile clusterProfile, JobIdentifier jobIdentifier) { this.autoregisterKey = autoregisterKey; this.environment = environment; this.pluginId = elasticProfile.getPluginId(); this.configuration = elasticProfile.getConfigurationAsMap(true); + this.clusterProfile = clusterProfile != null ? clusterProfile.getConfigurationAsMap(true) : Collections.emptyMap(); this.jobIdentifier = jobIdentifier; } @@ -51,6 +55,8 @@ public String toString() { "autoregisterKey='" + autoregisterKey + '\'' + ", environment='" + environment + '\'' + ", configuration=" + configuration + + ", clusterProfile=" + clusterProfile + + ", jobIdentifier=" + jobIdentifier + ", pluginId='" + pluginId + '\'' + '}'; } @@ -66,4 +72,8 @@ public Map configuration() { public JobIdentifier jobIdentifier() { return jobIdentifier; } + + public Map getClusterProfileConfiguration() { + return clusterProfile; + } } 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 15822d792e0..df8f1861f1f 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package com.thoughtworks.go.server.service; import com.google.common.collect.Sets; +import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ElasticProfile; import com.thoughtworks.go.domain.AgentInstance; import com.thoughtworks.go.domain.JobIdentifier; @@ -70,6 +71,7 @@ public class ElasticAgentPluginService { @Value("${go.elasticplugin.heartbeat.interval}") private long elasticPluginHeartBeatInterval; private final ElasticAgentMetadataStore elasticAgentMetadataStore; + private ClusterProfilesService clusterProfilesService; // for test only public void setElasticPluginHeartBeatInterval(long elasticPluginHeartBeatInterval) { @@ -81,17 +83,18 @@ public ElasticAgentPluginService( PluginManager pluginManager, ElasticAgentPluginRegistry elasticAgentPluginRegistry, AgentService agentService, EnvironmentConfigService environmentConfigService, CreateAgentQueueHandler createAgentQueue, ServerPingQueueHandler serverPingQueue, - GoConfigService goConfigService, TimeProvider timeProvider, ServerHealthService serverHealthService) { + GoConfigService goConfigService, TimeProvider timeProvider, ClusterProfilesService clusterProfilesService, ServerHealthService serverHealthService) { this(pluginManager, elasticAgentPluginRegistry, agentService, environmentConfigService, createAgentQueue, - serverPingQueue, goConfigService, timeProvider, serverHealthService, ElasticAgentMetadataStore.instance()); + serverPingQueue, goConfigService, timeProvider, serverHealthService, ElasticAgentMetadataStore.instance(), clusterProfilesService); } ElasticAgentPluginService( PluginManager pluginManager, ElasticAgentPluginRegistry elasticAgentPluginRegistry, AgentService agentService, EnvironmentConfigService environmentConfigService, CreateAgentQueueHandler createAgentQueue, ServerPingQueueHandler serverPingQueue, - GoConfigService goConfigService, TimeProvider timeProvider, ServerHealthService serverHealthService, ElasticAgentMetadataStore elasticAgentMetadataStore) { + GoConfigService goConfigService, TimeProvider timeProvider, ServerHealthService serverHealthService, + ElasticAgentMetadataStore elasticAgentMetadataStore, ClusterProfilesService clusterProfilesService) { this.pluginManager = pluginManager; this.elasticAgentPluginRegistry = elasticAgentPluginRegistry; this.agentService = agentService; @@ -102,6 +105,7 @@ public ElasticAgentPluginService( this.timeProvider = timeProvider; this.serverHealthService = serverHealthService; this.elasticAgentMetadataStore = elasticAgentMetadataStore; + this.clusterProfilesService = clusterProfilesService; } public void heartbeat() { @@ -157,15 +161,17 @@ public void createAgentsFor(List old, List newPlan) { for (JobPlan plan : plansThatRequireElasticAgent) { jobCreationTimeMap.put(plan.getJobId(), timeProvider.currentTimeMillis()); - if (elasticAgentPluginRegistry.has(plan.getElasticProfile().getPluginId())) { + ElasticProfile elasticProfile = plan.getElasticProfile(); + if (elasticAgentPluginRegistry.has(elasticProfile.getPluginId())) { String environment = environmentConfigService.envForPipeline(plan.getPipelineName()); - createAgentQueue.post(new CreateAgentMessage(goConfigService.serverConfig().getAgentAutoRegisterKey(), environment, plan.getElasticProfile(), plan.getIdentifier()), messageTimeToLive); + ClusterProfile clusterProfile = clusterProfilesService.findProfile(elasticProfile.getClusterProfileId()); + createAgentQueue.post(new CreateAgentMessage(goConfigService.serverConfig().getAgentAutoRegisterKey(), environment, elasticProfile, clusterProfile, plan.getIdentifier()), messageTimeToLive); serverHealthService.removeByScope(HealthStateScope.forJob(plan.getIdentifier().getPipelineName(), plan.getIdentifier().getStageName(), plan.getIdentifier().getBuildName())); } else { String jobConfigIdentifier = plan.getIdentifier().jobConfigIdentifier().toString(); String description = String.format("Plugin [%s] associated with %s is missing. Either the plugin is not " + "installed or could not be registered. Please check plugins tab " + - "and server logs for more details.", plan.getElasticProfile().getPluginId(), jobConfigIdentifier); + "and server logs for more details.", elasticProfile.getPluginId(), jobConfigIdentifier); serverHealthService.update(ServerHealthState.error(String.format("Unable to find agent for %s", jobConfigIdentifier), description, HealthStateType.general(HealthStateScope.forJob(plan.getIdentifier().getPipelineName(), plan.getIdentifier().getStageName(), plan.getIdentifier().getBuildName())))); LOGGER.error(description); diff --git a/server/src/test-fast/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentMessageTest.java b/server/src/test-fast/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentMessageTest.java index bd2d817b996..793f31a87fc 100644 --- a/server/src/test-fast/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentMessageTest.java +++ b/server/src/test-fast/java/com/thoughtworks/go/server/messaging/elasticagents/CreateAgentMessageTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package com.thoughtworks.go.server.messaging.elasticagents; +import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ElasticProfile; import com.thoughtworks.go.domain.config.ConfigurationKey; import com.thoughtworks.go.domain.config.ConfigurationProperty; @@ -23,6 +24,7 @@ import org.junit.Test; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -33,10 +35,22 @@ public class CreateAgentMessageTest { @Test public void shouldGetPluginId() { List properties = Arrays.asList(new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("value"))); - ElasticProfile jobAgentConfig = new ElasticProfile("foo", "plugin-id", properties); - CreateAgentMessage message = new CreateAgentMessage("key", "env", jobAgentConfig, null); - assertThat(message.pluginId(), is(jobAgentConfig.getPluginId())); - Map configurationAsMap = jobAgentConfig.getConfigurationAsMap(true); + ElasticProfile elasticProfile = new ElasticProfile("foo", "plugin-id", properties); + ClusterProfile clusterProfile = new ClusterProfile("foo", "plugin-id", properties); + CreateAgentMessage message = new CreateAgentMessage("key", "env", elasticProfile, clusterProfile, null); + assertThat(message.pluginId(), is(elasticProfile.getPluginId())); + Map configurationAsMap = elasticProfile.getConfigurationAsMap(true); assertThat(message.configuration(), is(configurationAsMap)); + Map clusterProfileConfigurations = clusterProfile.getConfigurationAsMap(true); + assertThat(message.getClusterProfileConfiguration(), is(clusterProfileConfigurations)); + } + + @Test + public void shouldCreateCreateAgentMessageWhenClusterProfileIsNotSpecified() { + List properties = Arrays.asList(new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("value"))); + ElasticProfile elasticProfile = new ElasticProfile("foo", "plugin-id", properties); + + CreateAgentMessage createAgentMessage = new CreateAgentMessage(null, null, elasticProfile, null, null); + assertThat(createAgentMessage.getClusterProfileConfiguration(), is(Collections.emptyMap())); } } 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 c168b8a0949..e7d31969ca3 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 @@ -1,5 +1,5 @@ /* - * Copyright 2018 ThoughtWorks, Inc. + * Copyright 2019 ThoughtWorks, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package com.thoughtworks.go.server.service; import com.thoughtworks.go.config.AgentConfig; +import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ElasticProfile; import com.thoughtworks.go.domain.*; import com.thoughtworks.go.helper.AgentInstanceMother; @@ -74,6 +75,9 @@ class ElasticAgentPluginServiceTest { private GoConfigService goConfigService; @Mock private CreateAgentQueueHandler createAgentQueue; + @Mock + private ClusterProfilesService clusterProfilesService; + private TimeProvider timeProvider; private String autoRegisterKey = "key"; private ElasticAgentPluginService service; @@ -96,7 +100,7 @@ void setUp() throws Exception { elasticAgentMetadataStore = ElasticAgentMetadataStore.instance(); timeProvider = new TimeProvider(); - service = new ElasticAgentPluginService(pluginManager, registry, agentService, environmentConfigService, createAgentQueue, serverPingQueue, goConfigService, timeProvider, serverHealthService, elasticAgentMetadataStore); + service = new ElasticAgentPluginService(pluginManager, registry, agentService, environmentConfigService, createAgentQueue, serverPingQueue, goConfigService, timeProvider, serverHealthService, elasticAgentMetadataStore, clusterProfilesService); when(goConfigService.serverConfig()).thenReturn(GoConfigMother.configWithAutoRegisterKey(autoRegisterKey).server()); } @@ -136,9 +140,12 @@ void shouldSendServerHeartBeatMessageWithTimeToLive() { @Test void shouldCreateAgentForNewlyAddedJobPlansOnly() { - when(goConfigService.elasticJobStarvationThreshold()).thenReturn(10000L); JobPlan plan1 = plan(1, "docker"); JobPlan plan2 = plan(2, "docker"); + when(goConfigService.elasticJobStarvationThreshold()).thenReturn(10000L); + ClusterProfile clusterProfile = new ClusterProfile(plan1.getElasticProfile().getClusterProfileId(), plan1.getElasticProfile().getPluginId()); + when(clusterProfilesService.findProfile(plan1.getElasticProfile().getClusterProfileId())).thenReturn(clusterProfile); + ArgumentCaptor createAgentMessageArgumentCaptor = ArgumentCaptor.forClass(CreateAgentMessage.class); ArgumentCaptor ttl = ArgumentCaptor.forClass(Long.class); when(environmentConfigService.envForPipeline("pipeline-2")).thenReturn("env-2"); @@ -155,9 +162,11 @@ void shouldCreateAgentForNewlyAddedJobPlansOnly() { @Test void shouldPostCreateAgentMessageWithTimeToLiveLesserThanJobStarvationThreshold() throws Exception { - when(goConfigService.elasticJobStarvationThreshold()).thenReturn(20000L); JobPlan plan1 = plan(1, "docker"); JobPlan plan2 = plan(2, "docker"); + when(goConfigService.elasticJobStarvationThreshold()).thenReturn(20000L); + ClusterProfile clusterProfile = new ClusterProfile(plan1.getElasticProfile().getClusterProfileId(), plan1.getElasticProfile().getPluginId()); + when(clusterProfilesService.findProfile(plan1.getElasticProfile().getClusterProfileId())).thenReturn(clusterProfile); ArgumentCaptor createAgentMessageArgumentCaptor = ArgumentCaptor.forClass(CreateAgentMessage.class); ArgumentCaptor ttl = ArgumentCaptor.forClass(Long.class); @@ -170,8 +179,11 @@ void shouldPostCreateAgentMessageWithTimeToLiveLesserThanJobStarvationThreshold( @Test void shouldRetryCreateAgentForJobThatHasBeenWaitingForAnAgentForALongTime() { - when(goConfigService.elasticJobStarvationThreshold()).thenReturn(0L); JobPlan plan1 = plan(1, "docker"); + + when(goConfigService.elasticJobStarvationThreshold()).thenReturn(0L); + ClusterProfile clusterProfile = new ClusterProfile(plan1.getElasticProfile().getClusterProfileId(), plan1.getElasticProfile().getPluginId()); + when(clusterProfilesService.findProfile(plan1.getElasticProfile().getClusterProfileId())).thenReturn(clusterProfile); ArgumentCaptor captor = ArgumentCaptor.forClass(CreateAgentMessage.class); ArgumentCaptor ttl = ArgumentCaptor.forClass(Long.class); service.createAgentsFor(new ArrayList<>(), Arrays.asList(plan1)); @@ -205,6 +217,8 @@ void shouldReportMissingElasticPlugin() { @Test void shouldRemoveExistingMissingPluginErrorFromAPreviousAttemptIfThePluginIsNowRegistered() { JobPlan plan1 = plan(1, "docker"); + ClusterProfile clusterProfile = new ClusterProfile(plan1.getElasticProfile().getClusterProfileId(), plan1.getElasticProfile().getPluginId()); + when(clusterProfilesService.findProfile(plan1.getElasticProfile().getClusterProfileId())).thenReturn(clusterProfile); ArgumentCaptor captor = ArgumentCaptor.forClass(HealthStateScope.class); ArgumentCaptor ttl = ArgumentCaptor.forClass(Long.class); @@ -353,7 +367,7 @@ void shouldMakeJobCompletionCallToThePluginWhenJobAssignedToNonElastic() { } private JobPlan plan(int jobId, String pluginId) { - ElasticProfile elasticProfile = new ElasticProfile("id", pluginId); + ElasticProfile elasticProfile = new ElasticProfile("id", pluginId, "clusterProfileId"); JobIdentifier identifier = new JobIdentifier("pipeline-" + jobId, 1, "1", "stage", "1", "job"); return new DefaultJobPlan(null, new ArrayList<>(), null, jobId, identifier, null, new EnvironmentVariables(), new EnvironmentVariables(), elasticProfile); }