Skip to content

Commit 9f213ff

Browse files
authored
Merge branch 'master' into update-kubernetes-client-api-6.4.x
2 parents 2553659 + 760b4a6 commit 9f213ff

File tree

5 files changed

+97
-12
lines changed

5 files changed

+97
-12
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.csanchez.jenkins.plugins.kubernetes;
22

33

4+
import edu.umd.cs.findbugs.annotations.NonNull;
45
import hudson.Util;
56
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
67
import java.util.Base64;
@@ -152,12 +153,40 @@ public KubernetesClient createClient() throws KubernetesAuthException {
152153
builder.withProxyUsername(p.name);
153154
builder.withProxyPassword(password);
154155
}
155-
builder.withNoProxy(p.getNoProxyHost().split("\n"));
156+
builder.withNoProxy(getNoProxyHosts(p));
156157
}
157158
}
158159
}
159160
return new KubernetesClientBuilder().withConfig(builder.build()).build();
160161
}
162+
163+
/**
164+
* Get the no proxy hosts in the format supported by the Kubernetes Client implementation. In particular
165+
* <code>*</code> are not supported.
166+
*
167+
* For Example:
168+
* * <code>example.com</code> to not use the proxy for <code>example.com</code> and its subdomain.
169+
* * <code>.example.com</code> to not use the proxy for subdomains of <code>example.com</code>. But use it
170+
* for <code>.example.com</code>.
171+
*
172+
* Note: Jenkins Proxy supports wildcard such as <code>192.168.*</code> or <code>*my*.example.com</code> that cannot
173+
* be converted to the current kubernetes client implementation.
174+
*
175+
* @see https://github.com/fabric8io/kubernetes-client/blob/master/CHANGELOG.md#610-2022-08-31
176+
* @see https://www.gnu.org/software/wget/manual/html_node/Proxies.html.
177+
* @param proxy a {@link ProxyConfiguration}
178+
* @return the array of no proxy hosts
179+
*/
180+
private String[] getNoProxyHosts(@NonNull ProxyConfiguration proxy) {
181+
String[] noProxyHosts = proxy.getNoProxyHost().split("\n");
182+
int i=0;
183+
while(i<noProxyHosts.length) {
184+
noProxyHosts[i] = noProxyHosts[i].replace("*", "");
185+
i++;
186+
}
187+
return noProxyHosts;
188+
}
189+
161190
private String getProxyPasswordDecrypted(ProxyConfiguration p) {
162191
String passwordEncrypted = p.getPassword();
163192
String password = null;

src/main/resources/org/csanchez/jenkins/plugins/kubernetes/PodTemplate/help-inheritFrom.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<p>
2+
<b>Name</b> of the podTemplate to inherit from.
3+
</p>
14
<p>
25
A podTemplate may or may not inherit from an existing template.<br/>
36
This means that the podTemplate will inherit node selector, service account, image pull secrets, containerTemplates and volumes from the template it inherits from.

src/main/resources/org/csanchez/jenkins/plugins/kubernetes/PodTemplate/help-name.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
-->
16-
17-
The name prefix of the pod.
18-
Defaults to the label.
16+
<p>
17+
Name that identifies the pod template. Also used to prefix the name of the node and pod created.
18+
</p>
19+
<p>
20+
When omitted, the agent and pod name is randomly generated using <i>jenkins-agent-</i> as a prefix.
21+
When omitted <b>in a <i>podTemplate(...)</i> step</b>, the <i>label</i> is used as a prefix for the name of the dynamic pod template.
22+
</p>

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

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,32 @@
2424

2525
package org.csanchez.jenkins.plugins.kubernetes;
2626

27-
import static org.junit.Assert.*;
28-
29-
import java.util.HashMap;
30-
import java.util.Map;
31-
27+
import hudson.ProxyConfiguration;
28+
import io.fabric8.kubernetes.client.KubernetesClient;
29+
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
30+
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException;
3231
import org.junit.After;
3332
import org.junit.Before;
33+
import org.junit.Rule;
3434
import org.junit.Test;
35+
import org.jvnet.hudson.test.Issue;
36+
import org.jvnet.hudson.test.JenkinsRule;
3537

36-
import static io.fabric8.kubernetes.client.Config.*;
37-
import io.fabric8.kubernetes.client.KubernetesClient;
38+
import java.net.MalformedURLException;
39+
import java.util.HashMap;
40+
import java.util.Map;
41+
42+
import static io.fabric8.kubernetes.client.Config.KUBERNETES_HTTPS_PROXY;
43+
import static io.fabric8.kubernetes.client.Config.KUBERNETES_HTTP_PROXY;
44+
import static io.fabric8.kubernetes.client.Config.KUBERNETES_KUBECONFIG_FILE;
45+
import static io.fabric8.kubernetes.client.Config.KUBERNETES_NAMESPACE_FILE;
46+
import static io.fabric8.kubernetes.client.Config.KUBERNETES_NO_PROXY;
47+
import static io.fabric8.kubernetes.client.Config.KUBERNETES_PROXY_PASSWORD;
48+
import static io.fabric8.kubernetes.client.Config.KUBERNETES_PROXY_USERNAME;
49+
import static org.junit.Assert.assertArrayEquals;
50+
import static org.junit.Assert.assertEquals;
51+
import static org.junit.Assert.assertNotNull;
52+
import static org.junit.Assert.assertNull;
3853

3954
/**
4055
* Test the creation of clients
@@ -59,6 +74,9 @@ public class KubernetesFactoryAdapterTest {
5974

6075
private Map<String, String> systemProperties = new HashMap<>();
6176

77+
@Rule
78+
public JenkinsRule j = new JenkinsRule();
79+
6280
@Before
6381
public void saveSystemProperties() {
6482
for (String key : SYSTEM_PROPERTY_NAMES) {
@@ -117,4 +135,35 @@ public void autoConfigWithMasterUrl() throws Exception {
117135
assertEquals(PROXY_USERNAME, client.getConfiguration().getProxyUsername());
118136
assertEquals(PROXY_PASSWORD, client.getConfiguration().getProxyPassword());
119137
}
138+
139+
@Test
140+
@Issue("JENKINS-70563")
141+
public void jenkinsProxyConfiguration() throws KubernetesAuthException, MalformedURLException {
142+
143+
j.jenkins.setProxy(new ProxyConfiguration("proxy.com", 123, PROXY_USERNAME, PROXY_PASSWORD, "*acme.com\n*acme*.com\n*.example.com|192.168.*"));
144+
KubernetesFactoryAdapter factory = new KubernetesFactoryAdapter("http://acme.com", null, null, null, false, 15, 5, 32, true);
145+
try(KubernetesClient client = factory.createClient()) {
146+
assertNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
147+
}
148+
149+
j.jenkins.setProxy(new ProxyConfiguration("proxy.com", 123, PROXY_USERNAME, PROXY_PASSWORD, "*acme.com"));
150+
factory = new KubernetesFactoryAdapter("http://acme.com", null, null, null, false, 15, 5, 32, true);
151+
try(KubernetesClient client = factory.createClient()) {
152+
assertNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
153+
}
154+
factory = new KubernetesFactoryAdapter("http://k8s.acme.com", null, null, null, false, 15, 5, 32, true);
155+
try(KubernetesClient client = factory.createClient()) {
156+
assertNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
157+
}
158+
159+
j.jenkins.setProxy(new ProxyConfiguration("proxy.com", 123, PROXY_USERNAME, PROXY_PASSWORD, "*.acme.com"));
160+
factory = new KubernetesFactoryAdapter("http://acme.com", null, null, null, false, 15, 5, 32, true);
161+
try(KubernetesClient client = factory.createClient()) {
162+
assertNotNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
163+
}
164+
factory = new KubernetesFactoryAdapter("http://k8s.acme.com", null, null, null, false, 15, 5, 32, true);
165+
try(KubernetesClient client = factory.createClient()) {
166+
assertNull(HttpClientUtils.getProxyUrl(client.getConfiguration()));
167+
}
168+
}
120169
}

test-in-k8s.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ spec:
2424
serviceAccountName: jenkins
2525
containers:
2626
- name: jenkins
27-
image: maven:3.8.7-eclipse-temurin-17
27+
image: maven:3.9.0-eclipse-temurin-17
2828
command:
2929
- sleep
3030
args:

0 commit comments

Comments
 (0)