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

Give access to ConfigBuilder #17

Merged
merged 1 commit into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public interface KubernetesAuth {
* @throws KubernetesAuthException if something fails while dealing with credentials
*/
String buildKubeConfig(KubernetesAuthConfig config) throws JsonProcessingException, KubernetesAuthException;
io.fabric8.kubernetes.api.model.ConfigBuilder buildConfigBuilder(KubernetesAuthConfig config,String context,String clusterName,String username) throws KubernetesAuthException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ abstract class AbstractKubernetesAuth implements KubernetesAuth {
abstract AuthInfoBuilder decorate(AuthInfoBuilder builder, KubernetesAuthConfig config) throws KubernetesAuthException;

public String buildKubeConfig(KubernetesAuthConfig config) throws JsonProcessingException, KubernetesAuthException {
io.fabric8.kubernetes.api.model.ConfigBuilder configBuilder = buildConfigBuilder(config, "k8s", "k8s", "cluster-admin");
return SerializationUtils.getMapper().writeValueAsString(configBuilder.build());
}

public io.fabric8.kubernetes.api.model.ConfigBuilder buildConfigBuilder(KubernetesAuthConfig config, String context, String clusterName, String username) throws KubernetesAuthException {
io.fabric8.kubernetes.api.model.ConfigBuilder configBuilder = new io.fabric8.kubernetes.api.model.ConfigBuilder();
// setup cluster
Cluster cluster = new Cluster();
Expand All @@ -24,26 +29,26 @@ public String buildKubeConfig(KubernetesAuthConfig config) throws JsonProcessing
cluster.setInsecureSkipTlsVerify(config.isSkipTlsVerify());
configBuilder
.addNewCluster()
.withName("k8s")
.withName(clusterName)
.withCluster(cluster)
.endCluster();

// setup user (class-specific)
configBuilder
.addNewUser()
.withName("cluster-admin")
.withName(username)
.withUser(decorate(new AuthInfoBuilder(), config).build())
.endUser();
// setup context
configBuilder
.addNewContext()
.withName("k8s")
.withName(context)
.withNewContext()
.withCluster("k8s")
.withUser("cluster-admin")
.withCluster(clusterName)
.withUser(username)
.endContext()
.endContext();
return SerializationUtils.getMapper().writeValueAsString(configBuilder.build());
return configBuilder;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.kubernetes.auth.impl;

import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.internal.KubeConfigUtils;
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuth;
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthConfig;
import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuthException;
Expand Down Expand Up @@ -31,6 +32,15 @@ public ConfigBuilder decorate(ConfigBuilder builder, KubernetesAuthConfig config
}
}

public io.fabric8.kubernetes.api.model.ConfigBuilder buildConfigBuilder(KubernetesAuthConfig config, String context, String clusterName, String username) throws KubernetesAuthException {
try {
io.fabric8.kubernetes.api.model.Config kubeConfig = KubeConfigUtils.parseConfigFromString(getKubeconfig());
return new io.fabric8.kubernetes.api.model.ConfigBuilder(kubeConfig);
} catch (IOException e) {
throw new KubernetesAuthException(e.getMessage(), e);
}
}

public String getKubeconfig() {
return kubeconfig;
}
Expand Down