Skip to content

Commit 007dfd9

Browse files
committed
Build kubernetes client config with namespace configured in plugin settings
1 parent 7ba091d commit 007dfd9

16 files changed

+23
-41
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public interface Constants {
4848
String JOB_ID_LABEL_KEY = "Elastic-Agent-Job-Id";
4949
String JOB_IDENTIFIER_LABEL_KEY = "Elastic-Agent-Job-Identifier";
5050

51-
String KUBERNETES_NAMESPACE = "default";
5251
String KUBERNETES_POD_KIND_LABEL_KEY = "kind";
5352
String KUBERNETES_POD_KIND_LABEL_VALUE = "kubernetes-elastic-agent";
5453
String KUBERNETES_POD_NAME_PREFIX = "k8s-ea";

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public Agents instancesCreatedAfterTimeout(PluginSettings settings, Agents agent
151151
public void refreshAll(PluginRequest pluginRequest) {
152152
LOG.debug("[Refresh Instances]. Syncing k8s elastic agent pod information.");
153153
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
154-
PodList list = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).list();
154+
PodList list = client.pods().list();
155155

156156
for (Pod pod : list.getItems()) {
157157
Map<String, String> podLabels = pod.getMetadata().getLabels();
@@ -181,7 +181,7 @@ private KubernetesAgentInstances unregisteredAfterTimeout(PluginSettings setting
181181
if (knownAgents.containsAgentWithId(instanceName)) {
182182
continue;
183183
}
184-
Pod pod = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).withName(instanceName).get();
184+
Pod pod = client.pods().withName(instanceName).get();
185185
Date createdAt = getSimpleDateFormat().parse(pod.getMetadata().getCreationTimestamp());
186186
DateTime dateTimeCreated = new DateTime(createdAt);
187187

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ private KubernetesClient createClientFor(PluginSettings pluginSettings) {
4949
final ConfigBuilder configBuilder = new ConfigBuilder()
5050
.withOauthToken(pluginSettings.getOauthToken())
5151
.withMasterUrl(pluginSettings.getClusterUrl())
52-
.withCaCertData(pluginSettings.getCaCertData());
52+
.withCaCertData(pluginSettings.getCaCertData())
53+
.withNamespace(pluginSettings.getNamespace());
5354

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public KubernetesInstance(DateTime createdAt, String environment, String name, M
4040
}
4141

4242
public void terminate(KubernetesClient client) {
43-
client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).withName(name).delete();
43+
client.pods().withName(name).delete();
4444
}
4545

4646
public String name() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private static void setAnnotations(Pod pod, CreateAgentRequest request) {
131131

132132
private KubernetesInstance createKubernetesPod(KubernetesClient client, Pod elasticAgentPod) {
133133
LOG.info(format("[Create Agent] Creating K8s pod with spec: {0}.", elasticAgentPod.toString()));
134-
Pod pod = client.pods().inNamespace(KUBERNETES_NAMESPACE).create(elasticAgentPod);
134+
Pod pod = client.pods().create(elasticAgentPod);
135135
return fromKubernetesPod(pod);
136136
}
137137

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public GoPluginApiResponse execute() {
6767

6868
private Pod findPodUsingJobIdentifier(JobIdentifier jobIdentifier, KubernetesClient client) {
6969
try {
70-
return client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE)
70+
return client.pods()
7171
.withLabel(Constants.JOB_ID_LABEL_KEY, String.valueOf(jobIdentifier.getJobId()))
7272
.list().getItems().get(0);
7373
} catch (Exception e) {
@@ -76,7 +76,7 @@ private Pod findPodUsingJobIdentifier(JobIdentifier jobIdentifier, KubernetesCli
7676
}
7777

7878
private Pod findPodUsingElasticAgentId(String elasticAgentId, KubernetesClient client) {
79-
List<Pod> pods = client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE).list().getItems();
79+
List<Pod> pods = client.pods().list().getItems();
8080
for (Pod pod : pods) {
8181
if (pod.getMetadata().getName().equals(elasticAgentId)) {
8282
return pod;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public KubernetesCluster(KubernetesClient client) throws ParseException {
4242
private void fetchPods(KubernetesClient dockerClient) throws ParseException {
4343
final Map<String, KubernetesNode> dockerNodeMap = nodes.stream().distinct().collect(toMap(KubernetesNode::getName, node -> node));
4444

45-
final List<Pod> pods = dockerClient.pods().inNamespace(Constants.KUBERNETES_NAMESPACE)
45+
final List<Pod> pods = dockerClient.pods()
4646
.withLabel(Constants.CREATED_BY_LABEL_KEY, Constants.PLUGIN_ID)
4747
.list().getItems();
4848

src/main/java/cd/go/contrib/elasticagent/model/reports/agent/KubernetesElasticAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static ArrayList<KubernetesPodEvent> getAllEventsForPod(Pod pod, Kuberne
9292
}
9393

9494
private static String getPodLogs(Pod pod, KubernetesClient client) {
95-
return client.pods().inNamespace(Constants.KUBERNETES_NAMESPACE)
95+
return client.pods()
9696
.withName(pod.getMetadata().getName()).getLog(true);
9797
}
9898

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@
9494
<fieldset>
9595
<legend>Cluster credentials</legend>
9696
<div class="row">
97-
<div class="columns large-6">
97+
<div class="columns large-5">
9898
<label>Cluster URL<span class='asterix'>*</span></label>
9999
<input type="text" ng-model="kubernetes_cluster_url" ng-required="true"/>
100100
<span class="form_error" ng-show="GOINPUTNAME[kubernetes_cluster_url].$error.server">{{GOINPUTNAME[kubernetes_cluster_url].$error.server}}</span>
101101
<label class="form-help-content">
102102
Kubernetes Cluster URL. Can be obtained by running <code>kubectl cluster-info</code>
103103
</label>
104104
</div>
105-
<div class="columns large-6">
105+
<div class="columns large-5 end">
106106
<label>Namespace</label>
107107
<input type="text" ng-model="namespace" ng-required="true"/>
108108
<span class="form_error" ng-show="GOINPUTNAME[namespace].$error.server">{{GOINPUTNAME[namespace].$error.server}}</span>
@@ -129,7 +129,8 @@
129129
<textarea ng-model="kubernetes_cluster_ca_cert" rows="7"></textarea>
130130
<span class="form_error" ng-show="GOINPUTNAME[kubernetes_cluster_ca_cert].$error.server">{{GOINPUTNAME[kubernetes_cluster_ca_cert].$error.server}}</span>
131131
<label class="form-help-content">
132-
Kubernetes cluster ca certificate data.
132+
Kubernetes cluster ca certificate data. Do not provide <code> -----BEGIN * </code> and <code> -----END
133+
* </code> in your certificate data.
133134
</label>
134135
</div>
135136
</fieldset>

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ public void setUp() throws Exception {
6363
initMocks(this);
6464
kubernetesAgentInstances = new KubernetesAgentInstances(mockedKubernetesClientFactory);
6565
when(mockedKubernetesClientFactory.client(any())).thenReturn(mockKubernetesClient);
66-
67-
when(pods.inNamespace(Constants.KUBERNETES_NAMESPACE)).thenReturn(pods);
68-
6966
when(pods.create(any())).thenAnswer((Answer<Pod>) invocation -> {
7067
Object[] args = invocation.getArguments();
7168
return (Pod) args[0];

0 commit comments

Comments
 (0)