Skip to content

Commit 2fc8b0e

Browse files
committed
Tell client to bring a pod in specified namespace.
1 parent c062aa7 commit 2fc8b0e

File tree

8 files changed

+51
-20
lines changed

8 files changed

+51
-20
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private KubernetesClient createClientFor(PluginSettings pluginSettings) {
5151
.withMasterUrl(pluginSettings.getClusterUrl())
5252
.withCaCertData(pluginSettings.getCaCertData());
5353

54-
return new DefaultKubernetesClient(configBuilder.build());
54+
return new DefaultKubernetesClient(configBuilder.build())
55+
.inNamespace(pluginSettings.getNamespace());
5556
}
5657
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class PluginSettings {
4747
@SerializedName("kubernetes_cluster_ca_cert")
4848
private String clusterCACertData;
4949

50+
@Expose
51+
@SerializedName("namespace")
52+
private String namespace = "default";
53+
5054
private Period autoRegisterPeriod;
5155

5256
public PluginSettings() {
@@ -93,6 +97,10 @@ public String getCaCertData() {
9397
return clusterCACertData;
9498
}
9599

100+
public String getNamespace() {
101+
return namespace;
102+
}
103+
96104
@Override
97105
public boolean equals(Object o) {
98106
if (this == o) return true;
@@ -107,7 +115,9 @@ public boolean equals(Object o) {
107115
return false;
108116
if (clusterUrl != null ? !clusterUrl.equals(that.clusterUrl) : that.clusterUrl != null) return false;
109117
if (oauthToken != null ? !oauthToken.equals(that.oauthToken) : that.oauthToken != null) return false;
110-
return clusterCACertData != null ? clusterCACertData.equals(that.clusterCACertData) : that.clusterCACertData == null;
118+
if (clusterCACertData != null ? !clusterCACertData.equals(that.clusterCACertData) : that.clusterCACertData != null)
119+
return false;
120+
return namespace != null ? namespace.equals(that.namespace) : that.namespace == null;
111121
}
112122

113123
@Override
@@ -118,6 +128,7 @@ public int hashCode() {
118128
result = 31 * result + (clusterUrl != null ? clusterUrl.hashCode() : 0);
119129
result = 31 * result + (oauthToken != null ? oauthToken.hashCode() : 0);
120130
result = 31 * result + (clusterCACertData != null ? clusterCACertData.hashCode() : 0);
131+
result = 31 * result + (namespace != null ? namespace.hashCode() : 0);
121132
return result;
122133
}
123134
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ public class GetPluginConfigurationExecutor implements RequestExecutor {
3131
public static final Field GO_SERVER_URL = new GoServerUrlField("go_server_url", "GoCD server URL", false, "0");
3232
public static final Field AUTO_REGISTER_TIMEOUT = new PositiveNumberField("auto_register_timeout", "Agent auto-register timeout (in minutes)", "10", false, false, "1");
3333
public static final Field MAX_PENDING_PODS = new PositiveNumberField("pending_pods_count", "Maximum pending pods", "10", false, false, "2");
34-
public static final Field OAUTH_TOKEN = new NonBlankField("oauth_token", "Oauth token", true, "3");
35-
public static final Field CLUSTER_URL = new SecureURLField("kubernetes_cluster_url", "Cluster URL", true, "4");
36-
public static final Field CLUSTER_CA_CERT = new Field("kubernetes_cluster_ca_cert", "Cluster ca certificate", null, false, true, "5");
34+
public static final Field CLUSTER_URL = new HttpsURLField("kubernetes_cluster_url", "Cluster URL", true, "3");
35+
public static final Field NAMESPACE = new Field("namespace", "Namespace", null, false, false, "4");
36+
public static final Field OAUTH_TOKEN = new NonBlankField("oauth_token", "Oauth token", true, "5");
37+
public static final Field CLUSTER_CA_CERT = new Field("kubernetes_cluster_ca_cert", "Cluster ca certificate", null, false, true, "6");
3738

3839
static {
3940
FIELDS.put(GO_SERVER_URL.key(), GO_SERVER_URL);
@@ -42,6 +43,7 @@ public class GetPluginConfigurationExecutor implements RequestExecutor {
4243
FIELDS.put(CLUSTER_URL.key(), CLUSTER_URL);
4344
FIELDS.put(CLUSTER_CA_CERT.key(), CLUSTER_CA_CERT);
4445
FIELDS.put(OAUTH_TOKEN.key(), OAUTH_TOKEN);
46+
FIELDS.put(NAMESPACE.key(), NAMESPACE);
4547
}
4648

4749
public GoPluginApiResponse execute() {

src/main/java/cd/go/contrib/elasticagent/model/GoServerUrlField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.net.URI;
2222
import java.net.URISyntaxException;
2323

24-
public class GoServerUrlField extends SecureURLField {
24+
public class GoServerUrlField extends HttpsURLField {
2525
public GoServerUrlField(String key, String displayName, Boolean required, String displayOrder) {
2626
super(key, displayName, required, displayOrder);
2727
}

src/main/java/cd/go/contrib/elasticagent/model/SecureURLField.java renamed to src/main/java/cd/go/contrib/elasticagent/model/HttpsURLField.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
import java.net.URI;
2222

23-
public class SecureURLField extends Field {
23+
public class HttpsURLField extends Field {
2424

25-
public SecureURLField(String key, String displayName, Boolean required, String displayOrder) {
25+
public HttpsURLField(String key, String displayName, Boolean required, String displayOrder) {
2626
super(key, displayName, null, required, false, displayOrder);
2727
}
2828

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,22 @@
9494
<fieldset>
9595
<legend>Cluster credentials</legend>
9696
<div class="row">
97-
<label>Cluster URL<span class='asterix'>*</span></label>
98-
<input type="text" ng-model="kubernetes_cluster_url" ng-required="true"/>
99-
<span class="form_error" ng-show="GOINPUTNAME[kubernetes_cluster_url].$error.server">{{GOINPUTNAME[kubernetes_cluster_url].$error.server}}</span>
100-
<label class="form-help-content">
101-
Kubernetes Cluster URL. Can be obtained by running <code>kubectl cluster-info</code>
102-
</label>
97+
<div class="columns large-6">
98+
<label>Cluster URL<span class='asterix'>*</span></label>
99+
<input type="text" ng-model="kubernetes_cluster_url" ng-required="true"/>
100+
<span class="form_error" ng-show="GOINPUTNAME[kubernetes_cluster_url].$error.server">{{GOINPUTNAME[kubernetes_cluster_url].$error.server}}</span>
101+
<label class="form-help-content">
102+
Kubernetes Cluster URL. Can be obtained by running <code>kubectl cluster-info</code>
103+
</label>
104+
</div>
105+
<div class="columns large-6">
106+
<label>Namespace</label>
107+
<input type="text" ng-model="namespace" ng-required="true"/>
108+
<span class="form_error" ng-show="GOINPUTNAME[namespace].$error.server">{{GOINPUTNAME[namespace].$error.server}}</span>
109+
<label class="form-help-content">
110+
Namespace in which plugin will create the agent pods. defaults to <code>default</code> namespace.
111+
</label>
112+
</div>
103113
</div>
104114

105115
<div class="row">

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void shouldHaveDefaultValueAfterDeSerialization() {
5656
assertNull(pluginSettings.getGoServerUrl());
5757
assertThat(pluginSettings.getAutoRegisterTimeout(), is(10));
5858
assertThat(pluginSettings.getMaxPendingPods(), is(10));
59+
assertThat(pluginSettings.getNamespace(), is("default"));
5960
assertNull(pluginSettings.getClusterUrl());
6061
assertNull(pluginSettings.getCaCertData());
6162
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,25 @@ public void assertJsonStructure() throws Exception {
7171
" \"display-name\": \"Cluster URL\",\n" +
7272
" \"required\": true,\n" +
7373
" \"secure\": false,\n" +
74-
" \"display-order\": \"4\"\n" +
74+
" \"display-order\": \"3\"\n" +
7575
" },\n" +
76-
" \"kubernetes_cluster_ca_cert\": {\n" +
77-
" \"display-name\": \"Cluster ca certificate\",\n" +
76+
" \"namespace\": {\n" +
77+
" \"display-name\": \"Namespace\",\n" +
7878
" \"required\": false,\n" +
79-
" \"secure\": true,\n" +
80-
" \"display-order\": \"5\"\n" +
79+
" \"secure\": false,\n" +
80+
" \"display-order\": \"4\"\n" +
8181
" },\n" +
8282
" \"oauth_token\": {\n" +
8383
" \"display-name\": \"Oauth token\",\n" +
8484
" \"required\": true,\n" +
8585
" \"secure\": true,\n" +
86-
" \"display-order\": \"3\"\n" +
86+
" \"display-order\": \"5\"\n" +
87+
" },\n" +
88+
" \"kubernetes_cluster_ca_cert\": {\n" +
89+
" \"display-name\": \"Cluster ca certificate\",\n" +
90+
" \"required\": false,\n" +
91+
" \"secure\": true,\n" +
92+
" \"display-order\": \"6\"\n" +
8793
" }\n" +
8894
"}";
8995

0 commit comments

Comments
 (0)