Skip to content

Commit

Permalink
Merge b627f87 into 46256e9
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Oct 4, 2020
2 parents 46256e9 + b627f87 commit 17973c8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
9 changes: 4 additions & 5 deletions .travis.yml
Expand Up @@ -10,12 +10,11 @@ cache:
- $HOME/.m2

env:
- KUBECTL_VERSION=v1.18.3
- KUBECTL_VERSION=v1.17.6
- KUBECTL_VERSION=v1.16.10
- KUBECTL_VERSION=v1.19.1
- KUBECTL_VERSION=v1.18.8
- KUBECTL_VERSION=v1.17.9
- KUBECTL_VERSION=v1.16.14
- KUBECTL_VERSION=v1.15.12
- KUBECTL_VERSION=v1.14.10
- KUBECTL_VERSION=v1.13.12

install:
- curl -LO https://storage.googleapis.com/kubernetes-release/release/$KUBECTL_VERSION/bin/linux/amd64/kubectl
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -23,7 +23,7 @@ node {

## Prerequisites
* A jenkins installation running version 2.204.6 or higher.
* An executor with `kubectl` installed (tested against [v1.13 to v1.18][travis-config] included).
* An executor with `kubectl` installed (tested against [v1.15 to v1.19][travis-config] included).
* A Kubernetes cluster.

## How it works
Expand Down
Expand Up @@ -6,6 +6,7 @@
import hudson.FilePath;
import org.jenkinsci.plugins.kubernetes.cli.helpers.DummyCredentials;
import org.jenkinsci.plugins.kubernetes.cli.helpers.TestResourceLoader;
import org.jenkinsci.plugins.kubernetes.cli.helpers.Version;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand Down Expand Up @@ -46,7 +47,7 @@ protected boolean kubectlPresent() {

@Before
public void checkKubectlPresence() {
assumeTrue("The 'kubectl' binary could not be found in the PATH",kubectlPresent());
assumeTrue("The 'kubectl' binary could not be found in the PATH", kubectlPresent());
}

@Test
Expand Down Expand Up @@ -89,7 +90,7 @@ public void testSingleKubeConfig() throws Exception {
public void testMultiKubeConfig() throws Exception {
CredentialsStore store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
store.addCredentials(Domain.global(), DummyCredentials.fileCredential(CREDENTIAL_ID));
store.addCredentials(Domain.global(), DummyCredentials.fileCredential(SECONDARY_CREDENTIAL_ID,"test-cluster2","test-user2"));
store.addCredentials(Domain.global(), DummyCredentials.fileCredential(SECONDARY_CREDENTIAL_ID, "test-cluster2", "test-user2"));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "multiKubeConfig");
p.setDefinition(new CpsFlowDefinition(TestResourceLoader.loadAsString("withKubeCredentialsPipelineConfigDump.groovy"), true));
Expand All @@ -104,11 +105,11 @@ public void testMultiKubeConfig() throws Exception {
assertThat(configDumpContent, containsString("apiVersion: v1\n" +
"clusters:\n" +
"- cluster:\n" +
" insecure-skip-tls-verify: true\n"+
" insecure-skip-tls-verify: true\n" +
" server: https://test-cluster\n" +
" name: test-cluster\n" +
"- cluster:\n" +
" insecure-skip-tls-verify: true\n"+
" insecure-skip-tls-verify: true\n" +
" server: https://test-cluster2\n" +
" name: test-cluster2\n" +
"contexts:\n" +
Expand All @@ -120,7 +121,7 @@ public void testMultiKubeConfig() throws Exception {
" cluster: test-cluster2\n" +
" user: test-user2\n" +
" name: test-cluster2\n" +
"current-context: test-cluster\n"+
"current-context: test-cluster\n" +
"kind: Config\n" +
"preferences: {}\n" +
"users:\n" +
Expand All @@ -134,7 +135,7 @@ public void testMultiKubeConfig() throws Exception {
public void testMultiKubeConfigUsernames() throws Exception {
CredentialsStore store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
store.addCredentials(Domain.global(), DummyCredentials.secretCredential(CREDENTIAL_ID));
store.addCredentials(Domain.global(), DummyCredentials.fileCredential(SECONDARY_CREDENTIAL_ID,"test-cluster2","test-user2"));
store.addCredentials(Domain.global(), DummyCredentials.fileCredential(SECONDARY_CREDENTIAL_ID, "test-cluster2", "test-user2"));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "multiKubeConfigUsernames");
p.setDefinition(new CpsFlowDefinition(TestResourceLoader.loadAsString("withKubeCredentialsPipelineAndUsernames.groovy"), true));
Expand All @@ -146,6 +147,10 @@ public void testMultiKubeConfigUsernames() throws Exception {
assertTrue(configDump.exists());
String configDumpContent = configDump.readToString().trim();

String expectedToken = "REDACTED";
if ((new Version("1.19.0")).compareTo(KubectlVersion()) >= 0) {
expectedToken = "s3cr3t";
}
assertEquals("apiVersion: v1\n" +
"clusters:\n" +
"- cluster:\n" +
Expand All @@ -159,7 +164,7 @@ public void testMultiKubeConfigUsernames() throws Exception {
"- cluster:\n" +
" insecure-skip-tls-verify: true\n" +
" server: https://test-cluster2\n" +
" name: test-cluster2\n"+
" name: test-cluster2\n" +
"contexts:\n" +
"- context:\n" +
" cluster: clus1234\n" +
Expand All @@ -179,7 +184,7 @@ public void testMultiKubeConfigUsernames() throws Exception {
"users:\n" +
"- name: test-credentials\n" +
" user:\n" +
" token: s3cr3t\n" +
" token: " + expectedToken + "\n" +
"- name: test-user2\n" +
" user: {}", configDumpContent);
}
Expand All @@ -188,7 +193,7 @@ public void testMultiKubeConfigUsernames() throws Exception {
public void testMultiKubeConfigWithServer() throws Exception {
CredentialsStore store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
store.addCredentials(Domain.global(), DummyCredentials.fileCredential(CREDENTIAL_ID));
store.addCredentials(Domain.global(), DummyCredentials.fileCredential(SECONDARY_CREDENTIAL_ID,"test-cluster2","test-user2"));
store.addCredentials(Domain.global(), DummyCredentials.fileCredential(SECONDARY_CREDENTIAL_ID, "test-cluster2", "test-user2"));

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "multiKubeConfigWithServer");
p.setDefinition(new CpsFlowDefinition(TestResourceLoader.loadAsString("withKubeCredentialsPipelineAndServer.groovy"), true));
Expand All @@ -211,7 +216,7 @@ public void testMultiKubeConfigWithServer() throws Exception {
" server: https://test-cluster\n" +
" name: test-cluster\n" +
"- cluster:\n" +
" insecure-skip-tls-verify: true\n"+
" insecure-skip-tls-verify: true\n" +
" server: https://test-cluster2\n" +
" name: test-cluster2\n" +
"contexts:\n" +
Expand All @@ -223,7 +228,7 @@ public void testMultiKubeConfigWithServer() throws Exception {
" cluster: cred9999\n" +
" user: test-user2\n" +
" name: test-cluster2\n" +
"current-context: test-cluster\n"+
"current-context: test-cluster\n" +
"kind: Config\n" +
"preferences: {}\n" +
"users:\n" +
Expand All @@ -232,4 +237,15 @@ public void testMultiKubeConfigWithServer() throws Exception {
"- name: test-user2\n" +
" user: {}"));
}

private Version KubectlVersion() {
String version = System.getenv("KUBECTL_VERSION");
if (version.startsWith("v")){
version = version.replaceFirst("^v", "");
}
if (version == null) {
return new Version("99.99.99");
}
return new Version(version);
}
}
@@ -0,0 +1,32 @@
package org.jenkinsci.plugins.kubernetes.cli.helpers;

import org.checkerframework.checker.nullness.qual.NonNull;

/*
From https://gist.github.com/brianguertin/ada4b65c6d1c4f6d3eee3c12b6ce021b
*/
public class Version implements Comparable<Version> {
@NonNull
public final int[] numbers;

public Version(@NonNull String version) {
final String split[] = version.split("\\-")[0].split("\\.");
numbers = new int[split.length];
for (int i = 0; i < split.length; i++) {
numbers[i] = Integer.valueOf(split[i]);
}
}

@Override
public int compareTo(@NonNull Version another) {
final int maxLength = Math.max(numbers.length, another.numbers.length);
for (int i = 0; i < maxLength; i++) {
final int left = i < numbers.length ? numbers[i] : 0;
final int right = i < another.numbers.length ? another.numbers[i] : 0;
if (left != right) {
return left < right ? -1 : 1;
}
}
return 0;
}
}

0 comments on commit 17973c8

Please sign in to comment.