Skip to content

Commit

Permalink
Bump up elastic agent extension version to v5 (#106)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
GaneshSPatil authored Apr 22, 2019
1 parent d812b4c commit 54388f8
Show file tree
Hide file tree
Showing 61 changed files with 2,302 additions and 891 deletions.
4 changes: 2 additions & 2 deletions src/main/java/cd/go/contrib/elasticagent/AgentInstances.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public interface AgentInstances<T> {
* This call should be should ideally remember if the agent instances are refreshed, and do nothing if instances
* were previously refreshed.
*
* @param pluginRequest the plugin request object
* @param pluginSettings the plugin settings object
*/
void refreshAll(PluginRequest pluginRequest) throws Exception;
void refreshAll(PluginSettings pluginSettings) throws Exception;

/**
* This
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/cd/go/contrib/elasticagent/ClusterProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cd.go.contrib.elasticagent;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.HashMap;
import java.util.Objects;

import static cd.go.contrib.elasticagent.utils.Util.GSON;

public class ClusterProfile {
@Expose
@SerializedName("id")
private String id;

@Expose
@SerializedName("plugin_id")
private String pluginId;

@Expose
@SerializedName("properties")
private ClusterProfileProperties clusterProfileProperties;


public ClusterProfile() {
}

public ClusterProfile(String id, String pluginId, PluginSettings pluginSettings) {
this.id = id;
this.pluginId = pluginId;
setClusterProfileProperties(pluginSettings);
}

public static ClusterProfile fromJSON(String json) {
return GSON.fromJson(json, ClusterProfile.class);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClusterProfile that = (ClusterProfile) o;
return Objects.equals(id, that.id) &&
Objects.equals(pluginId, that.pluginId) &&
Objects.equals(clusterProfileProperties, that.clusterProfileProperties);
}

@Override
public int hashCode() {
return Objects.hash(id, pluginId, clusterProfileProperties);
}

@Override
public String toString() {
return "ClusterProfile{" +
"id='" + id + '\'' +
", pluginId='" + pluginId + '\'' +
", clusterProfileProperties=" + clusterProfileProperties +
'}';
}

public String getId() {
return id;
}

public String getPluginId() {
return pluginId;
}

public ClusterProfileProperties getClusterProfileProperties() {
return clusterProfileProperties;
}

public void setId(String id) {
this.id = id;
}

public void setPluginId(String pluginId) {
this.pluginId = pluginId;
}

public void setClusterProfileProperties(ClusterProfileProperties clusterProfileProperties) {
this.clusterProfileProperties = clusterProfileProperties;
}

public void setClusterProfileProperties(PluginSettings pluginSettings) {
this.clusterProfileProperties = ClusterProfileProperties.fromConfiguration(GSON.fromJson(GSON.toJson(pluginSettings), HashMap.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cd.go.contrib.elasticagent;

import java.util.Map;
import java.util.Objects;

import static cd.go.contrib.elasticagent.utils.Util.GSON;

public class ClusterProfileProperties extends PluginSettings {
public ClusterProfileProperties() {
}

public ClusterProfileProperties(String goServerUrl, String clusterUrl, String clusterCACertData) {
super(goServerUrl, clusterUrl, clusterCACertData);
}

public static ClusterProfileProperties fromJSON(String json) {
return GSON.fromJson(json, ClusterProfileProperties.class);
}

public static ClusterProfileProperties fromConfiguration(Map<String, String> clusterProfileProperties) {
return GSON.fromJson(GSON.toJson(clusterProfileProperties), ClusterProfileProperties.class);
}

public String uuid() {
return Integer.toHexString(Objects.hash(this));
}

@Override
public String toString() {
return super.toString();
}
}
3 changes: 1 addition & 2 deletions src/main/java/cd/go/contrib/elasticagent/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface Constants {

// The extension point API version that this plugin understands
String PROCESSOR_API_VERSION = "1.0";
String EXTENSION_API_VERSION = "4.0";
String EXTENSION_API_VERSION = "5.0";
String SERVER_INFO_API_VERSION = "1.0";

// the identifier of this plugin
Expand All @@ -39,7 +39,6 @@ public interface Constants {
String REQUEST_SERVER_PREFIX = "go.processor";
String REQUEST_SERVER_DISABLE_AGENT = REQUEST_SERVER_PREFIX + ".elastic-agents.disable-agents";
String REQUEST_SERVER_DELETE_AGENT = REQUEST_SERVER_PREFIX + ".elastic-agents.delete-agents";
String REQUEST_SERVER_GET_PLUGIN_SETTINGS = REQUEST_SERVER_PREFIX + ".plugin-settings.get";
String REQUEST_SERVER_LIST_AGENTS = REQUEST_SERVER_PREFIX + ".elastic-agents.list-agents";
String REQUEST_SERVER_INFO = REQUEST_SERVER_PREFIX + ".server-info.get";

Expand Down
105 changes: 105 additions & 0 deletions src/main/java/cd/go/contrib/elasticagent/ElasticAgentProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cd.go.contrib.elasticagent;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.HashMap;
import java.util.Objects;

import static cd.go.contrib.elasticagent.utils.Util.GSON;

public class ElasticAgentProfile {
@Expose
@SerializedName("id")
private String id;

@Expose
@SerializedName("plugin_id")
private String pluginId;

@Expose
@SerializedName("cluster_profile_id")
private String clusterProfileId;

@Expose
@SerializedName("properties")
private HashMap<String, String> properties;

public static ElasticAgentProfile fromJSON(String json) {
return GSON.fromJson(json, ElasticAgentProfile.class);
}

public String getId() {
return id;
}

public String getPluginId() {
return pluginId;
}

public String getClusterProfileId() {
return clusterProfileId;
}

public HashMap<String, String> getProperties() {
return properties;
}

public void setId(String id) {
this.id = id;
}

public void setPluginId(String pluginId) {
this.pluginId = pluginId;
}

public void setClusterProfileId(String clusterProfileId) {
this.clusterProfileId = clusterProfileId;
}

public void setProperties(HashMap<String, String> properties) {
this.properties = properties;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ElasticAgentProfile that = (ElasticAgentProfile) o;
return Objects.equals(id, that.id) &&
Objects.equals(pluginId, that.pluginId) &&
Objects.equals(clusterProfileId, that.clusterProfileId) &&
Objects.equals(properties, that.properties);
}

@Override
public int hashCode() {
return Objects.hash(id, pluginId, clusterProfileId, properties);
}

@Override
public String toString() {
return "ElasticAgentProfile{" +
"id='" + id + '\'' +
", pluginId='" + pluginId + '\'' +
", clusterProfileId='" + clusterProfileId + '\'' +
", properties=" + properties +
'}';
}
}
53 changes: 53 additions & 0 deletions src/main/java/cd/go/contrib/elasticagent/GoServerURLMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cd.go.contrib.elasticagent;

import cd.go.contrib.elasticagent.model.Metadata;
import org.apache.commons.lang3.StringUtils;

import java.net.URI;


public class GoServerURLMetadata extends Metadata {
private static String GO_SERVER_URL = "go_server_url";

public GoServerURLMetadata() {
super(GO_SERVER_URL, true, false);
}

@Override
public String doValidate(String input) {
String validationResult = super.doValidate(input);
if (StringUtils.isNotBlank(validationResult) || StringUtils.isBlank(input)) {
return validationResult;
}

try {
URI uri = new URI(input);
if (uri.getHost().equalsIgnoreCase("localhost") || uri.getHost().equalsIgnoreCase("127.0.0.1")) {
return String.format("%s must not be localhost, since this gets resolved on the agents.", GO_SERVER_URL);
}
if (!StringUtils.endsWith(input, "/go")) {
return String.format("%s must be in format https://<GO_SERVER_URL>:<GO_SERVER_PORT>/go.", GO_SERVER_URL);
}
} catch (Exception e) {
return String.format("%s must be a valid URL (https://example.com:8154/go).", GO_SERVER_URL);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public KubernetesAgentInstances(KubernetesClientFactory factory, KubernetesInsta
public KubernetesInstance create(CreateAgentRequest request, PluginSettings settings, PluginRequest pluginRequest) {
final Integer maxAllowedContainers = settings.getMaxPendingPods();
synchronized (instances) {
refreshAll(pluginRequest);
refreshAll(settings);
doWithLockOnSemaphore(new SetupSemaphore(maxAllowedContainers, instances, semaphore));

if (semaphore.tryAcquire()) {
Expand Down Expand Up @@ -144,9 +144,9 @@ public Agents instancesCreatedAfterTimeout(PluginSettings settings, Agents agent
}

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

instances.clear();
Expand All @@ -167,7 +167,7 @@ public KubernetesInstance find(String agentId) {
return instances.get(agentId);
}

private void register(KubernetesInstance instance) {
public void register(KubernetesInstance instance) {
instances.put(instance.name(), instance);
}

Expand All @@ -194,6 +194,7 @@ private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings setting
unregisteredInstances.register(kubernetesInstanceFactory.fromKubernetesPod(pod));
}
}

return unregisteredInstances;
}

Expand All @@ -209,4 +210,8 @@ private Pod getPod(KubernetesClient client, String instanceName) {
public boolean instanceExists(KubernetesInstance instance) {
return instances.contains(instance);
}

public boolean hasInstance(String elasticAgentId) {
return find(elasticAgentId) != null;
}
}
Loading

0 comments on commit 54388f8

Please sign in to comment.