Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-49817] Support kubeconfig from secretFile credentials #294

Merged
merged 3 commits into from
Mar 20, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use try with resources
and reorder Config creation flow
  • Loading branch information
carlossg committed Mar 20, 2018
commit 4ab127ca9510756b55637bf599f0504d5a474866
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildWrapper;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.kubernetes.credentials.TokenProducer;
import org.jenkinsci.plugins.plaincredentials.FileCredentials;
@@ -38,6 +39,7 @@
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.Key;
@@ -125,13 +127,8 @@ public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher
}

if (c instanceof FileCredentials) {
InputStream configStream = ((FileCredentials) c).getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(configStream, StandardCharsets.UTF_8));
try {
String kubeconfigContents = reader.lines().collect(Collectors.joining("\n"));
configFile.write(kubeconfigContents, null);
} finally {
reader.close();
try (InputStream in = ((FileCredentials) c).getContent(); OutputStream out = configFile.write()) {
IOUtils.copy(in, out);
}
return;
}
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
import javax.annotation.CheckForNull;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
@@ -103,28 +104,29 @@ public KubernetesClient createClient() throws NoSuchAlgorithmException, Unrecove
KeyStoreException, IOException, CertificateEncodingException {

ConfigBuilder builder;
// autoconfigure if url is not set
if (StringUtils.isBlank(serviceAddress)) {
LOGGER.log(FINE, "Autoconfiguring Kubernetes client");
builder = new ConfigBuilder(Config.autoConfigure(null));
// configure from kubeconfig
if (credentials instanceof FileCredentials) {
LOGGER.log(FINE, "Configuring Kubernetes client from kubeconfig file");
try (InputStream is = ((FileCredentials) credentials).getContent()) {
Config config = Config.fromKubeconfig(IOUtils.toString(is, StandardCharsets.UTF_8));
builder = new ConfigBuilder(config);
}
} else {
// although this will still autoconfigure based on Config constructor notes
// In future releases (2.4.x) the public constructor will be empty.
// The current functionality will be provided by autoConfigure().
// This is a necessary change to allow us distinguish between auto configured values and builder values.
builder = new ConfigBuilder().withMasterUrl(serviceAddress);
// autoconfigure if url is not set
if (StringUtils.isBlank(serviceAddress)) {
LOGGER.log(FINE, "Autoconfiguring Kubernetes client");
builder = new ConfigBuilder(Config.autoConfigure(null));
} else {
// although this will still autoconfigure based on Config constructor notes
// In future releases (2.4.x) the public constructor will be empty.
// The current functionality will be provided by autoConfigure().
// This is a necessary change to allow us distinguish between auto configured values and builder values.
builder = new ConfigBuilder().withMasterUrl(serviceAddress);
}
}

if (credentials instanceof FileCredentials) {
InputStream configStream = ((FileCredentials) credentials).getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(configStream, StandardCharsets.UTF_8));
try {
String kubeconfigContents = reader.lines().collect(Collectors.joining("\n"));
Config config = Config.fromKubeconfig(kubeconfigContents);
builder = new ConfigBuilder(config);
} finally {
reader.close();
}
// already handled above
} else if (credentials instanceof StringCredentials) {
final String token = ((StringCredentials) credentials).getSecret().getPlainText();
builder.withOauthToken(token);