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-60798] Fix kubectl buildwrapper (2nd round) #701

Merged
merged 5 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>kubernetes-credentials</artifactId>
<version>0.5.0</version>
<version>0.6.1-rc83.76c66431124a</version> <!-- https://github.com/jenkinsci/kubernetes-credentials-plugin/pull/19 -->
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter;
Expand All @@ -42,15 +43,15 @@
*/
public class KubectlBuildWrapper extends SimpleBuildWrapper {

private final String serverUrl;
private final String credentialsId;
private final String caCertificate;
private String serverUrl;
private String credentialsId;
private String caCertificate;

@DataBoundConstructor
public KubectlBuildWrapper(@Nonnull String serverUrl, @Nonnull String credentialsId,
@Nonnull String caCertificate) {
this.serverUrl = serverUrl;
this.credentialsId = credentialsId;
this.credentialsId = Util.fixEmpty(credentialsId);
this.caCertificate = Util.fixEmptyAndTrim(caCertificate);
}

Expand All @@ -66,8 +67,17 @@ public String getCaCertificate() {
return caCertificate;
}

protected Object readResolve() {
this.credentialsId = Util.fixEmpty(credentialsId);
this.caCertificate = Util.fixEmptyAndTrim(caCertificate);
return this;
}

@Override
public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException {
if (credentialsId == null) {
throw new AbortException("No credentials defined to setup Kubernetes CLI");
}
FilePath configFile = workspace.createTempFile(".kube", "config");
Set<String> tempFiles = newHashSet(configFile.getRemote());

Expand All @@ -90,9 +100,16 @@ public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher
throw new AbortException(e.getMessage());
}
}

int status = launcher.launch().cmdAsSingleString("kubectl version").join();
if (status != 0) throw new AbortException("Failed to run kubectl version. Returned status code " + status + ".");
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
String cmd = "kubectl version";
int status = launcher.launch().cmdAsSingleString(cmd).stdout(out).stderr(err).quiet(true).envs("KUBECONFIG="+configFile.getRemote()).join();
if (status != 0) {
StringBuilder msgBuilder = new StringBuilder("Failed to run \"").append(cmd).append("\". Returned status code ").append(status).append(".\n");
msgBuilder.append("stdout:\n").append(out).append("\n");
msgBuilder.append("stderr:\n").append(err);
throw new AbortException(msgBuilder.toString());
}
}

@Extension
Expand Down