Search in sources :

Example 1 with KubernetesAuth

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());
    }
}
Also used : FilePath(hudson.FilePath) KubernetesAuth(org.jenkinsci.plugins.kubernetes.auth.KubernetesAuth) KubernetesAuthConfig(org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) KubernetesAuthException(org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException) StandardCredentials(com.cloudbees.plugins.credentials.common.StandardCredentials) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) AbortException(hudson.AbortException) HashSet(java.util.HashSet)

Aggregations

StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 AbortException (hudson.AbortException)1 FilePath (hudson.FilePath)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 HashSet (java.util.HashSet)1 KubernetesAuth (org.jenkinsci.plugins.kubernetes.auth.KubernetesAuth)1 KubernetesAuthConfig (org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthConfig)1 KubernetesAuthException (org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException)1