Skip to content

Commit b76fc9d

Browse files
authored
[JENKINS-76157] Restore autoconfigure when auth is not null (#1752)
1 parent f9ac1df commit b76fc9d

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesFactoryAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public KubernetesClient createClient() throws KubernetesAuthException {
176176
builder = new ConfigBuilder(Config.autoConfigure(null));
177177
} else {
178178
// Using Config.empty() disables autoconfiguration when both serviceAddress and auth are set
179-
builder = auth == null ? new ConfigBuilder() : new ConfigBuilder(Config.empty());
179+
builder = auth == null ? new ConfigBuilder(Config.autoConfigure(null)) : new ConfigBuilder(Config.empty());
180180
builder = builder.withMasterUrl(serviceAddress);
181181
}
182182

src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesFactoryAdapterTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,36 @@ public void autoConfigWithMasterUrl() throws Exception {
151151
assertEquals("http://example.com/", client.getConfiguration().getMasterUrl());
152152
}
153153

154+
@Test
155+
public void autoConfigWithMasterUrlWithoutAuth() throws Exception {
156+
System.setProperty(KUBERNETES_NAMESPACE_FILE, "src/test/resources/kubenamespace");
157+
KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter("http://example.com", null, null, false);
158+
KubernetesClient client = factory.createClient();
159+
assertEquals("test-namespace", client.getNamespace());
160+
assertEquals(HTTP_PROXY, client.getConfiguration().getHttpProxy());
161+
assertEquals(HTTPS_PROXY, client.getConfiguration().getHttpsProxy());
162+
assertArrayEquals(new String[] {NO_PROXY}, client.getConfiguration().getNoProxy());
163+
assertEquals(PROXY_USERNAME, client.getConfiguration().getProxyUsername());
164+
assertEquals(PROXY_PASSWORD, client.getConfiguration().getProxyPassword());
165+
assertTrue(client.getConfiguration().getAutoConfigure());
166+
assertEquals("http://example.com/", client.getConfiguration().getMasterUrl());
167+
}
168+
169+
@Test
170+
public void autoConfigWithKubeconfig() throws Exception {
171+
System.setProperty(KUBERNETES_KUBECONFIG_FILE, "src/test/resources/kubeconfig");
172+
KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter("http://example.com", null, null, false);
173+
KubernetesClient client = factory.createClient();
174+
assertEquals("test-namespace", client.getNamespace());
175+
assertEquals(HTTP_PROXY, client.getConfiguration().getHttpProxy());
176+
assertEquals(HTTPS_PROXY, client.getConfiguration().getHttpsProxy());
177+
assertArrayEquals(new String[] {NO_PROXY}, client.getConfiguration().getNoProxy());
178+
assertEquals(PROXY_USERNAME, client.getConfiguration().getProxyUsername());
179+
assertEquals(PROXY_PASSWORD, client.getConfiguration().getProxyPassword());
180+
assertTrue(client.getConfiguration().getAutoConfigure());
181+
assertEquals("http://example.com/", client.getConfiguration().getMasterUrl());
182+
}
183+
154184
@Test
155185
@Issue("JENKINS-70416")
156186
public void autoConfigWithAuth() throws Exception {

src/test/resources/kubeconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: v1
2+
kind: Config
3+
clusters:
4+
- name: test-cluster
5+
cluster:
6+
server: http://example.com
7+
insecure-skip-tls-verify: true
8+
contexts:
9+
- name: test-context
10+
context:
11+
cluster: test-cluster
12+
user: test-user
13+
namespace: test-namespace
14+
current-context: test-context
15+
users:
16+
- name: test-user
17+
user:
18+
token: dummy-token

0 commit comments

Comments
 (0)