Search in sources :

Example 1 with KubernetesAuthConfig

use of org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthConfig 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)

Example 2 with KubernetesAuthConfig

use of org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthConfig in project kubernetes-plugin by jenkinsci.

the class KubernetesFactoryAdapter method createClient.

public KubernetesClient createClient() throws KubernetesAuthException {
    ConfigBuilder builder;
    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 (auth != null) {
        builder = auth.decorate(builder, new KubernetesAuthConfig(builder.getMasterUrl(), caCertData, skipTlsVerify));
    }
    if (skipTlsVerify) {
        builder.withTrustCerts(true);
    }
    if (caCertData != null) {
        // JENKINS-38829 CaCertData expects a Base64 encoded certificate
        builder.withCaCertData(Base64.getEncoder().encodeToString(caCertData.getBytes(UTF_8)));
    }
    builder = builder.withRequestTimeout(readTimeout * 1000).withConnectionTimeout(connectTimeout * 1000);
    builder.withMaxConcurrentRequestsPerHost(maxRequestsPerHost);
    builder.withMaxConcurrentRequests(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());
    // JENKINS-63584 If Jenkins has an configured Proxy and cloud has enabled proxy usage pass the arguments to K8S
    LOGGER.log(FINE, "Proxy Settings for Cloud: " + useJenkinsProxy);
    if (useJenkinsProxy) {
        Jenkins jenkins = Jenkins.getInstanceOrNull();
        LOGGER.log(FINE, "Jenkins Instance: " + jenkins);
        if (jenkins != null) {
            ProxyConfiguration p = jenkins.proxy;
            LOGGER.log(FINE, "Proxy Instance: " + p);
            if (p != null) {
                builder.withHttpsProxy("http://" + p.name + ":" + p.port);
                builder.withHttpProxy("http://" + p.name + ":" + p.port);
                if (p.name != null) {
                    String password = getProxyPasswordDecrypted(p);
                    builder.withProxyUsername(p.name);
                    builder.withProxyPassword(password);
                }
                builder.withNoProxy(p.getNoProxyHost().split("\n"));
            }
        }
    }
    return new DefaultKubernetesClient(builder.build());
}
Also used : Jenkins(jenkins.model.Jenkins) ProxyConfiguration(hudson.ProxyConfiguration) KubernetesAuthConfig(org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthConfig) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Aggregations

KubernetesAuthConfig (org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthConfig)2 StandardCredentials (com.cloudbees.plugins.credentials.common.StandardCredentials)1 AbortException (hudson.AbortException)1 FilePath (hudson.FilePath)1 ProxyConfiguration (hudson.ProxyConfiguration)1 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)1 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 HashSet (java.util.HashSet)1 Jenkins (jenkins.model.Jenkins)1 KubernetesAuth (org.jenkinsci.plugins.kubernetes.auth.KubernetesAuth)1 KubernetesAuthException (org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException)1