use of org.jenkinsci.plugins.kubernetes.auth.KubernetesAuth in project kubernetes-plugin by jenkinsci.
the class KubectlBuildWrapper method setUp.
@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");
}
workspace.mkdirs();
FilePath configFile = workspace.createTempFile(".kube", "config");
Set<String> tempFiles = new HashSet<>(Arrays.asList(configFile.getRemote()));
context.env("KUBECONFIG", configFile.getRemote());
context.setDisposer(new CleanupDisposer(tempFiles));
StandardCredentials credentials = CredentialsProvider.findCredentialById(credentialsId, StandardCredentials.class, build, Collections.emptyList());
if (credentials == null) {
throw new AbortException("No credentials found for id \"" + credentialsId + "\"");
}
KubernetesAuth auth = AuthenticationTokens.convert(KubernetesAuth.class, credentials);
if (auth == null) {
throw new AbortException("Unsupported Credentials type " + credentials.getClass().getName());
}
try (Writer w = new OutputStreamWriter(configFile.write(), StandardCharsets.UTF_8)) {
w.write(auth.buildKubeConfig(new KubernetesAuthConfig(getServerUrl(), getCaCertificate(), getCaCertificate() == null)));
} catch (KubernetesAuthException e) {
throw new AbortException(e.getMessage());
}
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());
}
}
Aggregations