Search in sources :

Example 76 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method createGogsUserDetails.

protected UserDetails createGogsUserDetails(KubernetesClient kubernetes, String namespace) throws MojoExecutionException {
    String url = getGogsURL(kubernetes, namespace);
    Secret secret = null;
    UserDetails userDetails;
    if (Strings.isNullOrBlank(url)) {
        return createGithubProjectAndUserDetails(kubernetes, namespace);
    }
    log.debug("Got gogs URL: " + url);
    String gogsSecretName = getGogsSecretName(namespace);
    if (Strings.isNullOrBlank(gitUserName) || Strings.isNullOrBlank(gitPassword)) {
        secret = findOrCreateGitSecret(kubernetes, gogsSecretName, GOGS_REPO_HOST);
    }
    if (Strings.isNullOrBlank(gitUserName)) {
        gitUserName = getGogsSecretField(kubernetes, secret, GOGS_REPO_HOST, "username");
    }
    if (Strings.isNullOrBlank(gitPassword)) {
        gitPassword = getGogsSecretField(kubernetes, secret, GOGS_REPO_HOST, "password");
    }
    if (Strings.isNullOrBlank(gitEmail)) {
        gitEmail = findEmailFromDotGitConfig();
    }
    createOrUpdateSecret(kubernetes, secret);
    ConfigMap configMap = getSecretGitConfigMap(kubernetes, namespace, secretNamespace);
    updateSecretGitConfigMap(kubernetes, secretNamespace, configMap, GOGS_REPO_HOST, gogsSecretName);
    log.info("git username: " + gitUserName + " password: " + hidePassword(gitPassword) + " email: " + gitEmail);
    return new UserDetails(url, url, gitUserName, gitPassword, gitEmail);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) UserDetails(io.fabric8.project.support.UserDetails) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Example 77 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method findOrCreateGitSecret.

private Secret findOrCreateGitSecret(KubernetesClient kubernetes, String secretName, String repositoryHost) {
    String secretNamespace = getSecretNamespace();
    ensureNamespaceExists(kubernetes, secretNamespace);
    Secret gogsSecret = kubernetes.secrets().inNamespace(secretNamespace).withName(secretName).get();
    if (gogsSecret == null) {
        // lets create a new secret!
        Map<String, String> labels = new HashMap<>();
        labels.put("provider", "fabric8");
        labels.put("repository", repositoryHost);
        labels.put("scm", "git");
        gogsSecret = new SecretBuilder().withNewMetadata().withName(secretName).withLabels(labels).endMetadata().withData(new HashMap<String, String>()).build();
    }
    return gogsSecret;
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) HashMap(java.util.HashMap)

Example 78 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method createGithubProjectAndUserDetails.

protected UserDetails createGithubProjectAndUserDetails(KubernetesClient kubernetes, String namespace) throws MojoExecutionException {
    log.info("Creating Project on GitHub");
    String url = "https://api.github.com";
    UserDetails userDetails = null;
    String secretNamespace = getSecretNamespace();
    ensureNamespaceExists(kubernetes, secretNamespace);
    ConfigMap configMap = getSecretGitConfigMap(kubernetes, namespace, secretNamespace);
    String host = convertToValidDnsLabel(currentUserName() + "-github.com");
    String currentSecretName = configMap.getData().get(host);
    if (currentSecretName == null) {
        currentSecretName = createGitSecretName(namespace, host);
    }
    Secret secret = findOrCreateGitSecret(kubernetes, currentSecretName, host);
    // FIXME: Rename the methods to be generic
    // if empty or retrying lets re-enter the user/pwd
    gitUserName = getGogsSecretField(kubernetes, secret, host, "username");
    gitPassword = getGogsSecretField(kubernetes, secret, host, "password");
    if (Strings.isNullOrBlank(gitEmail)) {
        gitEmail = findEmailFromDotGitConfig();
    }
    createOrUpdateSecret(kubernetes, secret);
    updateSecretGitConfigMap(kubernetes, secretNamespace, configMap, host, currentSecretName);
    return new UserDetails("https://api.github.com", "https://api.github.com", gitUserName, gitPassword, gitEmail);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) UserDetails(io.fabric8.project.support.UserDetails) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Example 79 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method ensureExternalGitSecretsAreSetupFor.

protected void ensureExternalGitSecretsAreSetupFor(KubernetesClient kubernetes, String namespace, String gitRemoteURL) throws MojoExecutionException {
    String secretNamespace = getSecretNamespace();
    ensureNamespaceExists(kubernetes, secretNamespace);
    ConfigMap configMap = getSecretGitConfigMap(kubernetes, namespace, secretNamespace);
    String host = GitUtils.getGitHostName(gitRemoteURL);
    if (host == null) {
        host = "default";
    }
    String protocol = GitUtils.getGitProtocol(gitRemoteURL);
    boolean isSsh = Objects.equal("ssh", protocol);
    String currentSecretName = configMap.getData().get(host);
    if (currentSecretName == null) {
        currentSecretName = createGitSecretName(namespace, host);
    }
    Secret secret = findOrCreateGitSecret(kubernetes, currentSecretName, host);
    if (isSsh) {
        // lets see if we need to import ssh keys
        Map<String, String> secretData = secret.getData();
        if (secretData == null) {
            secretData = new HashMap<>();
        }
        if (!secretData.containsKey(PROPERTY_PRIVATE_KEY) || !secretData.containsKey(PROPERTY_PUBLIC_KEY)) {
            String answer = null;
            try {
                answer = prompter.prompt("Would you like to import your local SSH public/private key pair from your ~/.ssh folder? (Y/n)");
            } catch (PrompterException e) {
                log.warn("Failed to get prompt: %s", e);
            }
            if (answer != null && answer.trim().isEmpty() || answer.trim().toUpperCase().startsWith("Y")) {
                chooseSshKeyPairs(secretData, host);
                secret.setData(secretData);
            }
        }
    } else {
        // if empty or retrying lets re-enter the user/pwd
        getGogsSecretField(kubernetes, secret, host, "username");
        getGogsSecretField(kubernetes, secret, host, "password");
    }
    createOrUpdateSecret(kubernetes, secret);
    updateSecretGitConfigMap(kubernetes, secretNamespace, configMap, host, currentSecretName);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Example 80 with KubernetesClient

use of io.fabric8.kubernetes.client.KubernetesClient in project fabric8-maven-plugin by fabric8io.

the class DockerImageWatcher method restartContainer.

protected void restartContainer(WatchService.ImageWatcher watcher, Set<HasMetadata> resources) throws MojoExecutionException {
    ImageConfiguration imageConfig = watcher.getImageConfiguration();
    String imageName = imageConfig.getName();
    try {
        ClusterAccess clusterAccess = new ClusterAccess(getContext().getNamespace());
        KubernetesClient client = clusterAccess.createDefaultClient(log);
        String namespace = clusterAccess.getNamespace();
        String imagePrefix = getImagePrefix(imageName);
        for (HasMetadata entity : resources) {
            updateImageName(client, namespace, entity, imagePrefix, imageName);
        }
    } catch (KubernetesClientException e) {
        KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
    } catch (MojoExecutionException e) {
        throw e;
    } catch (Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ClusterAccess(io.fabric8.maven.core.access.ClusterAccess) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) DockerAccessException(io.fabric8.maven.docker.access.DockerAccessException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)62 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)40 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)21 HashMap (java.util.HashMap)19 Pod (io.fabric8.kubernetes.api.model.Pod)17 Service (io.fabric8.kubernetes.api.model.Service)16 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)14 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)13 IOException (java.io.IOException)12 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)10 File (java.io.File)10 ArrayList (java.util.ArrayList)9 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Test (org.junit.Test)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)7 Map (java.util.Map)7 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)7 Controller (io.fabric8.kubernetes.api.Controller)6