Skip to content

Commit

Permalink
support KUBECONFIG from secretFile credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Cole Mickens committed Mar 13, 2018
1 parent 5b6971f commit da07e9a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
import org.jenkinsci.plugins.plaincredentials.FileCredentials;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
Expand Down Expand Up @@ -592,6 +593,7 @@ public ListBoxModel doFillCredentialsIdItems(@QueryParameter String serverUrl) {
.withMatching( //
CredentialsMatchers.anyOf(
CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class),
CredentialsMatchers.instanceOf(FileCredentials.class),
CredentialsMatchers.instanceOf(TokenProducer.class),
CredentialsMatchers.instanceOf(
org.jenkinsci.plugins.kubernetes.credentials.TokenProducer.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static java.nio.charset.StandardCharsets.*;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.Key;
Expand All @@ -14,6 +17,7 @@
import java.util.Collections;
import static java.util.logging.Level.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.annotation.CheckForNull;

Expand All @@ -24,6 +28,7 @@
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCertificateCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import org.jenkinsci.plugins.plaincredentials.FileCredentials;
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;

Expand Down Expand Up @@ -110,15 +115,14 @@ public KubernetesClient createClient() throws NoSuchAlgorithmException, Unrecove
builder = new ConfigBuilder().withMasterUrl(serviceAddress);
}

builder = builder.withRequestTimeout(readTimeout * 1000).withConnectionTimeout(connectTimeout * 1000);

if (!StringUtils.isBlank(namespace)) {
builder.withNamespace(namespace);
} else if (StringUtils.isBlank(builder.getNamespace())) {
builder.withNamespace("default");
}

if (credentials instanceof StringCredentials) {
if (credentials instanceof FileCredentials) {
InputStream configStream = ((FileCredentials) credentials).getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(configStream, StandardCharsets.UTF_8));
String kubeconfigContents = reader.lines().collect(Collectors.joining("\n"));
Config config = Config.fromKubeconfig(kubeconfigContents);
builder = new ConfigBuilder(config);
reader.close();
} else if (credentials instanceof StringCredentials) {
final String token = ((StringCredentials) credentials).getSecret().getPlainText();
builder.withOauthToken(token);
} else if (credentials instanceof TokenProducer) {
Expand Down Expand Up @@ -147,8 +151,16 @@ public KubernetesClient createClient() throws NoSuchAlgorithmException, Unrecove
// JENKINS-38829 CaCertData expects a Base64 encoded certificate
builder.withCaCertData(Base64.encodeBase64String(caCertData.getBytes(UTF_8)));
}

builder = builder.withRequestTimeout(readTimeout * 1000).withConnectionTimeout(connectTimeout * 1000);
builder.withMaxConcurrentRequestsPerHost(maxRequestsPerHost);

if (!StringUtils.isBlank(namespace)) {
builder.withNamespace(namespace);
} else if (StringUtils.isBlank(builder.getNamespace())) {
builder.withNamespace("default");
}

LOGGER.log(FINE, "Creating Kubernetes client: {0}", this.toString());
return new DefaultKubernetesClient(builder.build());
}
Expand Down

0 comments on commit da07e9a

Please sign in to comment.