Skip to content

Commit 54388f8

Browse files
authored
Bump up elastic agent extension version to v5 (#106)
* implement migrate config call * fix elastic agent profile related api calls * Implement cluster profile metadata and view call * Implement validate cluster profile call * Implement create agent call using cluster profiles passed from request body * Implement should assign work call using cluster profiles passed from request body * Implement job completion call using cluster profiles passed from request body * Implement capabilities * refresh instances at kubernetes plugin * implement cluster status report call * implement agent status report call * implement server ping request * Modify refresh all to accept plugin settings instead of plugin requests * Remove get plugin settings api call * remove unnecessary plugin settings related calls * Fix issue with gson serailization * Handle cluster profile changed API call * Add test to verify same uuid is generated for cluster profile properties
1 parent d812b4c commit 54388f8

File tree

61 files changed

+2302
-891
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2302
-891
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public interface AgentInstances<T> {
7272
* This call should be should ideally remember if the agent instances are refreshed, and do nothing if instances
7373
* were previously refreshed.
7474
*
75-
* @param pluginRequest the plugin request object
75+
* @param pluginSettings the plugin settings object
7676
*/
77-
void refreshAll(PluginRequest pluginRequest) throws Exception;
77+
void refreshAll(PluginSettings pluginSettings) throws Exception;
7878

7979
/**
8080
* This
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2019 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.contrib.elasticagent;
18+
19+
import com.google.gson.annotations.Expose;
20+
import com.google.gson.annotations.SerializedName;
21+
22+
import java.util.HashMap;
23+
import java.util.Objects;
24+
25+
import static cd.go.contrib.elasticagent.utils.Util.GSON;
26+
27+
public class ClusterProfile {
28+
@Expose
29+
@SerializedName("id")
30+
private String id;
31+
32+
@Expose
33+
@SerializedName("plugin_id")
34+
private String pluginId;
35+
36+
@Expose
37+
@SerializedName("properties")
38+
private ClusterProfileProperties clusterProfileProperties;
39+
40+
41+
public ClusterProfile() {
42+
}
43+
44+
public ClusterProfile(String id, String pluginId, PluginSettings pluginSettings) {
45+
this.id = id;
46+
this.pluginId = pluginId;
47+
setClusterProfileProperties(pluginSettings);
48+
}
49+
50+
public static ClusterProfile fromJSON(String json) {
51+
return GSON.fromJson(json, ClusterProfile.class);
52+
}
53+
54+
@Override
55+
public boolean equals(Object o) {
56+
if (this == o) return true;
57+
if (o == null || getClass() != o.getClass()) return false;
58+
ClusterProfile that = (ClusterProfile) o;
59+
return Objects.equals(id, that.id) &&
60+
Objects.equals(pluginId, that.pluginId) &&
61+
Objects.equals(clusterProfileProperties, that.clusterProfileProperties);
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return Objects.hash(id, pluginId, clusterProfileProperties);
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return "ClusterProfile{" +
72+
"id='" + id + '\'' +
73+
", pluginId='" + pluginId + '\'' +
74+
", clusterProfileProperties=" + clusterProfileProperties +
75+
'}';
76+
}
77+
78+
public String getId() {
79+
return id;
80+
}
81+
82+
public String getPluginId() {
83+
return pluginId;
84+
}
85+
86+
public ClusterProfileProperties getClusterProfileProperties() {
87+
return clusterProfileProperties;
88+
}
89+
90+
public void setId(String id) {
91+
this.id = id;
92+
}
93+
94+
public void setPluginId(String pluginId) {
95+
this.pluginId = pluginId;
96+
}
97+
98+
public void setClusterProfileProperties(ClusterProfileProperties clusterProfileProperties) {
99+
this.clusterProfileProperties = clusterProfileProperties;
100+
}
101+
102+
public void setClusterProfileProperties(PluginSettings pluginSettings) {
103+
this.clusterProfileProperties = ClusterProfileProperties.fromConfiguration(GSON.fromJson(GSON.toJson(pluginSettings), HashMap.class));
104+
}
105+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2019 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.contrib.elasticagent;
18+
19+
import java.util.Map;
20+
import java.util.Objects;
21+
22+
import static cd.go.contrib.elasticagent.utils.Util.GSON;
23+
24+
public class ClusterProfileProperties extends PluginSettings {
25+
public ClusterProfileProperties() {
26+
}
27+
28+
public ClusterProfileProperties(String goServerUrl, String clusterUrl, String clusterCACertData) {
29+
super(goServerUrl, clusterUrl, clusterCACertData);
30+
}
31+
32+
public static ClusterProfileProperties fromJSON(String json) {
33+
return GSON.fromJson(json, ClusterProfileProperties.class);
34+
}
35+
36+
public static ClusterProfileProperties fromConfiguration(Map<String, String> clusterProfileProperties) {
37+
return GSON.fromJson(GSON.toJson(clusterProfileProperties), ClusterProfileProperties.class);
38+
}
39+
40+
public String uuid() {
41+
return Integer.toHexString(Objects.hash(this));
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return super.toString();
47+
}
48+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public interface Constants {
2929

3030
// The extension point API version that this plugin understands
3131
String PROCESSOR_API_VERSION = "1.0";
32-
String EXTENSION_API_VERSION = "4.0";
32+
String EXTENSION_API_VERSION = "5.0";
3333
String SERVER_INFO_API_VERSION = "1.0";
3434

3535
// the identifier of this plugin
@@ -39,7 +39,6 @@ public interface Constants {
3939
String REQUEST_SERVER_PREFIX = "go.processor";
4040
String REQUEST_SERVER_DISABLE_AGENT = REQUEST_SERVER_PREFIX + ".elastic-agents.disable-agents";
4141
String REQUEST_SERVER_DELETE_AGENT = REQUEST_SERVER_PREFIX + ".elastic-agents.delete-agents";
42-
String REQUEST_SERVER_GET_PLUGIN_SETTINGS = REQUEST_SERVER_PREFIX + ".plugin-settings.get";
4342
String REQUEST_SERVER_LIST_AGENTS = REQUEST_SERVER_PREFIX + ".elastic-agents.list-agents";
4443
String REQUEST_SERVER_INFO = REQUEST_SERVER_PREFIX + ".server-info.get";
4544

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2019 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.contrib.elasticagent;
18+
19+
import com.google.gson.annotations.Expose;
20+
import com.google.gson.annotations.SerializedName;
21+
22+
import java.util.HashMap;
23+
import java.util.Objects;
24+
25+
import static cd.go.contrib.elasticagent.utils.Util.GSON;
26+
27+
public class ElasticAgentProfile {
28+
@Expose
29+
@SerializedName("id")
30+
private String id;
31+
32+
@Expose
33+
@SerializedName("plugin_id")
34+
private String pluginId;
35+
36+
@Expose
37+
@SerializedName("cluster_profile_id")
38+
private String clusterProfileId;
39+
40+
@Expose
41+
@SerializedName("properties")
42+
private HashMap<String, String> properties;
43+
44+
public static ElasticAgentProfile fromJSON(String json) {
45+
return GSON.fromJson(json, ElasticAgentProfile.class);
46+
}
47+
48+
public String getId() {
49+
return id;
50+
}
51+
52+
public String getPluginId() {
53+
return pluginId;
54+
}
55+
56+
public String getClusterProfileId() {
57+
return clusterProfileId;
58+
}
59+
60+
public HashMap<String, String> getProperties() {
61+
return properties;
62+
}
63+
64+
public void setId(String id) {
65+
this.id = id;
66+
}
67+
68+
public void setPluginId(String pluginId) {
69+
this.pluginId = pluginId;
70+
}
71+
72+
public void setClusterProfileId(String clusterProfileId) {
73+
this.clusterProfileId = clusterProfileId;
74+
}
75+
76+
public void setProperties(HashMap<String, String> properties) {
77+
this.properties = properties;
78+
}
79+
80+
@Override
81+
public boolean equals(Object o) {
82+
if (this == o) return true;
83+
if (o == null || getClass() != o.getClass()) return false;
84+
ElasticAgentProfile that = (ElasticAgentProfile) o;
85+
return Objects.equals(id, that.id) &&
86+
Objects.equals(pluginId, that.pluginId) &&
87+
Objects.equals(clusterProfileId, that.clusterProfileId) &&
88+
Objects.equals(properties, that.properties);
89+
}
90+
91+
@Override
92+
public int hashCode() {
93+
return Objects.hash(id, pluginId, clusterProfileId, properties);
94+
}
95+
96+
@Override
97+
public String toString() {
98+
return "ElasticAgentProfile{" +
99+
"id='" + id + '\'' +
100+
", pluginId='" + pluginId + '\'' +
101+
", clusterProfileId='" + clusterProfileId + '\'' +
102+
", properties=" + properties +
103+
'}';
104+
}
105+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2019 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cd.go.contrib.elasticagent;
18+
19+
import cd.go.contrib.elasticagent.model.Metadata;
20+
import org.apache.commons.lang3.StringUtils;
21+
22+
import java.net.URI;
23+
24+
25+
public class GoServerURLMetadata extends Metadata {
26+
private static String GO_SERVER_URL = "go_server_url";
27+
28+
public GoServerURLMetadata() {
29+
super(GO_SERVER_URL, true, false);
30+
}
31+
32+
@Override
33+
public String doValidate(String input) {
34+
String validationResult = super.doValidate(input);
35+
if (StringUtils.isNotBlank(validationResult) || StringUtils.isBlank(input)) {
36+
return validationResult;
37+
}
38+
39+
try {
40+
URI uri = new URI(input);
41+
if (uri.getHost().equalsIgnoreCase("localhost") || uri.getHost().equalsIgnoreCase("127.0.0.1")) {
42+
return String.format("%s must not be localhost, since this gets resolved on the agents.", GO_SERVER_URL);
43+
}
44+
if (!StringUtils.endsWith(input, "/go")) {
45+
return String.format("%s must be in format https://<GO_SERVER_URL>:<GO_SERVER_PORT>/go.", GO_SERVER_URL);
46+
}
47+
} catch (Exception e) {
48+
return String.format("%s must be a valid URL (https://example.com:8154/go).", GO_SERVER_URL);
49+
}
50+
51+
return null;
52+
}
53+
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public KubernetesAgentInstances(KubernetesClientFactory factory, KubernetesInsta
6060
public KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) {
6161
final Integer maxAllowedContainers = settings.getMaxPendingPods();
6262
synchronized (instances) {
63-
refreshAll(pluginRequest);
63+
refreshAll(settings);
6464
doWithLockOnSemaphore(new SetupSemaphore(maxAllowedContainers, instances, semaphore));
6565

6666
if (semaphore.tryAcquire()) {
@@ -144,9 +144,9 @@ public Agents instancesCreatedAfterTimeout(PluginSettings settings, Agents agent
144144
}
145145

146146
@Override
147-
public void refreshAll(PluginRequest pluginRequest) {
148-
LOG.debug("[Refresh Instances] Syncing k8s elastic agent pod information.");
149-
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
147+
public void refreshAll(PluginSettings properties) {
148+
LOG.debug("[Refresh Instances] Syncing k8s elastic agent pod information for cluster {}.", properties);
149+
KubernetesClient client = factory.client(properties);
150150
PodList list = client.pods().list();
151151

152152
instances.clear();
@@ -167,7 +167,7 @@ public KubernetesInstance find(String agentId) {
167167
return instances.get(agentId);
168168
}
169169

170-
private void register(KubernetesInstance instance) {
170+
public void register(KubernetesInstance instance) {
171171
instances.put(instance.name(), instance);
172172
}
173173

@@ -194,6 +194,7 @@ private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings setting
194194
unregisteredInstances.register(kubernetesInstanceFactory.fromKubernetesPod(pod));
195195
}
196196
}
197+
197198
return unregisteredInstances;
198199
}
199200

@@ -209,4 +210,8 @@ private Pod getPod(KubernetesClient client, String instanceName) {
209210
public boolean instanceExists(KubernetesInstance instance) {
210211
return instances.contains(instance);
211212
}
213+
214+
public boolean hasInstance(String elasticAgentId) {
215+
return find(elasticAgentId) != null;
216+
}
212217
}

0 commit comments

Comments
 (0)