Skip to content

Commit 89c89c0

Browse files
authored
Merge pull request #377 from LivelyVideo/add-cluster-timeout-configuration
adding cluster timeout config
2 parents 2809464 + bd77d46 commit 89c89c0

11 files changed

+60
-16
lines changed

docs/configure_cluster_profile.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,27 @@
1313

1414
1. Optionally specify `Agent auto-register timeout (in minutes)`. This defaults to 10 (minutes) if not provided.
1515

16-
1. Optionally Specify `Maximum pending pods`. This defaults to 10 (pods) if not provided.
16+
2. Optionally specify `The cluster request timeout`. This defaults to 10000 (milliseconds) if not provided.
1717

18-
1. Specify `Cluster URL`.
18+
3. Optionally Specify `Maximum pending pods`. This defaults to 10 (pods) if not provided.
1919

20-
1. Optionally specify `Namespace`. If not provided, the plugin will launch GoCD
20+
4. Specify `Cluster URL`.
21+
22+
5. Optionally specify `Namespace`. If not provided, the plugin will launch GoCD
2123
agent pods in the default Kubernetes namespace. Note: If you have multiple
2224
GoCD servers with cluster profiles pointing to the same Kubernetes cluster,
2325
make sure that the namespace used by each GoCD server is different.
2426
Otherwise, the plugin of one GoCD server will end up terminating pods
2527
started by the plugin in the other GoCD servers.
2628

27-
1. Specify `Security token`. This should be a Kubernetes API token with the
29+
6. Specify `Security token`. This should be a Kubernetes API token with the
2830
following permissions:
2931

30-
| Resource | Actions |
31-
| -------------- | ----------- |
32-
| nodes | list |
33-
| events | list |
34-
| pods, pods/log | * |
32+
| Resource | Actions |
33+
| -------------- | ------- |
34+
| nodes | list |
35+
| events | list |
36+
| pods, pods/log | * |
3537

3638
If the plugin is using a non-default namespace, then the pods and pods/log permissions
3739
can be limited to that namespace (using a role + role binding), and the plugin
@@ -42,7 +44,7 @@
4244
If you are comfortable with cluster-wide permissions you can refer to the [example within the GoCD official helm
4345
chart](https://github.com/gocd/helm-chart/blob/master/gocd/templates/gocd-cluster-role.yaml).
4446

45-
1. Specify `Cluster CA certificate data`. This should be the base-64-encoded certificate
47+
7. Specify `Cluster CA certificate data`. This should be the base-64-encoded certificate
4648
of the Kubernetes API server. It can be omitted in the rare case that the Kubernetes API
4749
is configured to serve plain HTTP.
4850

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ private void clearOutClientOnTimer() {
7676
private KubernetesClient createClientFor(PluginSettings pluginSettings) {
7777
final ConfigBuilder configBuilder = new ConfigBuilder()
7878
.withAutoConfigure(false)
79+
.withRequestTimeout(pluginSettings.getClusterRequestTimeout())
7980
.withOauthToken(pluginSettings.getSecurityToken())
8081
.withMasterUrl(pluginSettings.getClusterUrl())
8182
.withCaCertData(pluginSettings.getCaCertData())

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) {
113113
}
114114
} catch (Exception e) {
115115
LOG.error("Failed to handle request " + request.requestName(), e);
116+
116117
return DefaultGoPluginApiResponse.error("Failed to handle request " + request.requestName());
117118
}
118119
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public class PluginSettings {
4040
@SerializedName("pending_pods_count")
4141
private String maxPendingPods;
4242

43+
@Expose
44+
@SerializedName("cluster_request_timeout")
45+
private String clusterRequestTimeout;
46+
4347
@Expose
4448
@SerializedName("kubernetes_cluster_url")
4549
private String clusterUrl;
@@ -91,6 +95,13 @@ public Integer getMaxPendingPods() {
9195
return getOrDefault(maximumPendingPodsInt, 10);
9296
}
9397

98+
public Integer getClusterRequestTimeout() {
99+
Integer maximumPendingPodsInt = !isBlank(this.clusterRequestTimeout)
100+
? Integer.valueOf(this.clusterRequestTimeout)
101+
: null;
102+
return getOrDefault(maximumPendingPodsInt, 10000);
103+
}
104+
94105
public String getGoServerUrl() {
95106
return goServerUrl;
96107
}
@@ -130,12 +141,16 @@ public boolean equals(Object o) {
130141

131142
PluginSettings that = (PluginSettings) o;
132143

133-
if (goServerUrl != null ? !goServerUrl.equals(that.goServerUrl) : that.goServerUrl != null) return false;
144+
if (goServerUrl != null ? !goServerUrl.equals(that.goServerUrl) : that.goServerUrl != null)
145+
return false;
134146
if (autoRegisterTimeout != null ? !autoRegisterTimeout.equals(that.autoRegisterTimeout) : that.autoRegisterTimeout != null)
135147
return false;
136148
if (maxPendingPods != null ? !maxPendingPods.equals(that.maxPendingPods) : that.maxPendingPods != null)
137149
return false;
138-
if (clusterUrl != null ? !clusterUrl.equals(that.clusterUrl) : that.clusterUrl != null) return false;
150+
if (clusterRequestTimeout != null ? !clusterRequestTimeout.equals(that.clusterRequestTimeout) : that.clusterRequestTimeout != null)
151+
return false;
152+
if (clusterUrl != null ? !clusterUrl.equals(that.clusterUrl) : that.clusterUrl != null)
153+
return false;
139154
if (securityToken != null ? !securityToken.equals(that.securityToken) : that.securityToken != null)
140155
return false;
141156
if (clusterCACertData != null ? !clusterCACertData.equals(that.clusterCACertData) : that.clusterCACertData != null)
@@ -148,6 +163,7 @@ public int hashCode() {
148163
int result = goServerUrl != null ? goServerUrl.hashCode() : 0;
149164
result = 31 * result + (autoRegisterTimeout != null ? autoRegisterTimeout.hashCode() : 0);
150165
result = 31 * result + (maxPendingPods != null ? maxPendingPods.hashCode() : 0);
166+
result = 31 * result + (clusterRequestTimeout != null ? clusterRequestTimeout.hashCode() : 0);
151167
result = 31 * result + (clusterUrl != null ? clusterUrl.hashCode() : 0);
152168
result = 31 * result + (securityToken != null ? securityToken.hashCode() : 0);
153169
result = 31 * result + (clusterCACertData != null ? clusterCACertData.hashCode() : 0);
@@ -161,6 +177,7 @@ public String toString() {
161177
"goServerUrl='" + goServerUrl + '\'' +
162178
", autoRegisterTimeout=" + autoRegisterTimeout +
163179
", maxPendingPods=" + maxPendingPods +
180+
", clusterRequestTimeout=" + clusterRequestTimeout +
164181
", clusterUrl='" + clusterUrl + '\'' +
165182
", securityToken='" + securityToken + '\'' +
166183
", clusterCACertData='" + clusterCACertData + '\'' +

src/main/java/cd/go/contrib/elasticagent/executors/GetClusterProfileMetadataExecutor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class GetClusterProfileMetadataExecutor implements RequestExecutor {
3333
public static final Metadata GO_SERVER_URL = new GoServerURLMetadata();
3434
public static final Metadata AUTO_REGISTER_TIMEOUT = new Metadata("auto_register_timeout", false, false);
3535
public static final Metadata MAX_PENDING_PODS = new Metadata("pending_pods_count", false, false);
36+
public static final Metadata CLUSTER_REQUEST_TIMEOUT = new Metadata("cluster_request_timeout", false, false);
3637
public static final Metadata CLUSTER_URL = new Metadata("kubernetes_cluster_url", true, false);
3738
public static final Metadata NAMESPACE = new Metadata("namespace", false, false);
3839
public static final Metadata SECURITY_TOKEN = new Metadata("security_token", true, true);
@@ -44,6 +45,7 @@ public class GetClusterProfileMetadataExecutor implements RequestExecutor {
4445
FIELDS.add(GO_SERVER_URL);
4546
FIELDS.add(AUTO_REGISTER_TIMEOUT);
4647
FIELDS.add(MAX_PENDING_PODS);
48+
FIELDS.add(CLUSTER_REQUEST_TIMEOUT);
4749
FIELDS.add(CLUSTER_URL);
4850
FIELDS.add(NAMESPACE);
4951
FIELDS.add(SECURITY_TOKEN);

src/main/resources/plugin-settings.template.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
<label class="form-help-content">Defaults to <code>10 minutes</code>.</label>
8585
</div>
8686

87+
<div class="row">
88+
<label>The cluster request timeout(in milliseconds)</label>
89+
<input type="text" ng-model="cluster_request_timeout" ng-required="true"/>
90+
<span class="form_error" ng-show="GOINPUTNAME[cluster_request_timeout].$error.server">{{GOINPUTNAME[cluster_request_timeout].$error.server}}</span>
91+
<label class="form-help-content">Defaults to <code>10000 milliseconds</code>.</label>
92+
</div>
93+
8794
<div class="row">
8895
<label>Maximum pending pods</label>
8996
<input type="text" ng-model="pending_pods_count" ng-required="true"/>

src/test/java/cd/go/contrib/elasticagent/KubernetesAgentInstancesTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import static cd.go.contrib.elasticagent.Constants.JOB_ID_LABEL_KEY;
4040
import static org.junit.jupiter.api.Assertions.assertTrue;
41+
import static org.mockito.ArgumentMatchers.any;
4142
import static org.mockito.Mockito.*;
4243
import static org.mockito.MockitoAnnotations.openMocks;
4344

@@ -77,6 +78,7 @@ public void setUp() {
7778
testProperties = new HashMap<>();
7879
when(mockCreateAgentRequest.properties()).thenReturn(testProperties);
7980
when(mockPluginSettings.getMaxPendingPods()).thenReturn(10);
81+
when(mockPluginSettings.getClusterRequestTimeout()).thenReturn(10000);
8082
when(factory.client(mockPluginSettings)).thenReturn(mockKubernetesClient);
8183
JobIdentifier jobId = new JobIdentifier("test", 1L, "Test pipeline", "test name", "1", "test job", 100L);
8284
when(mockCreateAgentRequest.jobIdentifier()).thenReturn(jobId);
@@ -117,7 +119,8 @@ public void shouldCreateKubernetesPodFromFileAndCacheCreatedInstance() throws IO
117119
thenReturn(kubernetesInstance);
118120
testProperties.put("PodSpecType", "remote");
119121
KubernetesAgentInstances agentInstances = new KubernetesAgentInstances(factory, mockKubernetesInstanceFactory);
120-
KubernetesInstance instance = agentInstances.create(mockCreateAgentRequest, mockPluginSettings, mockPluginRequest, consoleLogAppender);
122+
KubernetesInstance instance = agentInstances.create(mockCreateAgentRequest, mockPluginSettings,
123+
mockPluginRequest, consoleLogAppender);
121124
assertTrue(agentInstances.instanceExists(instance));
122125
}
123126

src/test/java/cd/go/contrib/elasticagent/KubernetesClientFactoryTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void setUp() throws Exception {
4242
pluginSettingsMap.put("go_server_url", "https://foo.go.cd/go");
4343
pluginSettingsMap.put("auto_register_timeout", "13");
4444
pluginSettingsMap.put("pending_pods_count", "14");
45+
pluginSettingsMap.put("cluster_request_timeout", "10000");
4546
pluginSettingsMap.put("kubernetes_cluster_url", "https://cloud.example.com");
4647
pluginSettingsMap.put("security_token", "foo-token");
4748
pluginSettingsMap.put("namespace", "gocd");

src/test/java/cd/go/contrib/elasticagent/PluginSettingsTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void shouldDeserializeFromJSON() {
3333
pluginSettingsMap.put("go_server_url", "https://foo.go.cd/go");
3434
pluginSettingsMap.put("auto_register_timeout", "13");
3535
pluginSettingsMap.put("pending_pods_count", "14");
36+
pluginSettingsMap.put("cluster_request_timeout", "60000");
3637
pluginSettingsMap.put("kubernetes_cluster_url", "https://cloud.example.com");
3738
pluginSettingsMap.put("security_token", "foo-token");
3839
pluginSettingsMap.put("kubernetes_cluster_ca_cert", "foo-ca-certs");
@@ -43,6 +44,7 @@ public void shouldDeserializeFromJSON() {
4344
assertThat(pluginSettings.getGoServerUrl()).isEqualTo("https://foo.go.cd/go");
4445
assertThat(pluginSettings.getAutoRegisterTimeout()).isEqualTo(13);
4546
assertThat(pluginSettings.getMaxPendingPods()).isEqualTo(14);
47+
assertThat(pluginSettings.getClusterRequestTimeout()).isEqualTo(60000);
4648
assertThat(pluginSettings.getClusterUrl()).isEqualTo("https://cloud.example.com");
4749
assertThat(pluginSettings.getCaCertData()).isEqualTo("foo-ca-certs");
4850
assertThat(pluginSettings.getSecurityToken()).isEqualTo("foo-token");
@@ -56,6 +58,7 @@ public void shouldHandleEmptyValuesForPendingPodsAndAutoRegisterTimeout() {
5658
pluginSettingsMap.put("go_server_url", "https://foo.go.cd/go");
5759
pluginSettingsMap.put("auto_register_timeout", "");
5860
pluginSettingsMap.put("pending_pods_count", null);
61+
pluginSettingsMap.put("cluster_request_timeout", null);
5962
pluginSettingsMap.put("kubernetes_cluster_url", "https://cloud.example.com");
6063
pluginSettingsMap.put("security_token", "foo-token");
6164
pluginSettingsMap.put("kubernetes_cluster_ca_cert", "foo-ca-certs");
@@ -66,6 +69,7 @@ public void shouldHandleEmptyValuesForPendingPodsAndAutoRegisterTimeout() {
6669
assertThat(pluginSettings.getGoServerUrl()).isEqualTo("https://foo.go.cd/go");
6770
assertThat(pluginSettings.getAutoRegisterTimeout()).isEqualTo(10);
6871
assertThat(pluginSettings.getMaxPendingPods()).isEqualTo(10);
72+
assertThat(pluginSettings.getClusterRequestTimeout()).isEqualTo(10000);
6973
assertThat(pluginSettings.getClusterUrl()).isEqualTo("https://cloud.example.com");
7074
assertThat(pluginSettings.getCaCertData()).isEqualTo("foo-ca-certs");
7175
assertThat(pluginSettings.getSecurityToken()).isEqualTo("foo-token");
@@ -79,6 +83,7 @@ public void shouldHaveDefaultValueAfterDeSerialization() {
7983
assertNull(pluginSettings.getGoServerUrl());
8084
assertThat(pluginSettings.getAutoRegisterTimeout()).isEqualTo(10);
8185
assertThat(pluginSettings.getMaxPendingPods()).isEqualTo(10);
86+
assertThat(pluginSettings.getClusterRequestTimeout()).isEqualTo(10000);
8287
assertThat(pluginSettings.getNamespace()).isEqualTo("default");
8388
assertNull(pluginSettings.getClusterUrl());
8489
assertNull(pluginSettings.getCaCertData());

src/test/java/cd/go/contrib/elasticagent/executors/GetClusterProfileMetadataExecutorTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import static org.assertj.core.api.Assertions.assertThat;
2929
import static org.junit.jupiter.api.Assertions.assertEquals;
3030

31-
3231
public class GetClusterProfileMetadataExecutorTest {
3332
@Test
3433
public void shouldSerializeAllFields() throws Exception {
@@ -66,6 +65,13 @@ public void assertJsonStructure() throws Exception {
6665
" }\n" +
6766
" },\n" +
6867
" {\n" +
68+
" \"key\": \"cluster_request_timeout\",\n" +
69+
" \"metadata\": {\n" +
70+
" \"required\": false,\n" +
71+
" \"secure\": false\n" +
72+
" }\n" +
73+
" },\n" +
74+
" {\n" +
6975
" \"key\": \"kubernetes_cluster_url\",\n" +
7076
" \"metadata\": {\n" +
7177
" \"required\": true,\n" +

0 commit comments

Comments
 (0)