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);
}
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;
}
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);
}
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);
}
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);
}
}
Aggregations