use of io.fabric8.kubernetes.api.model.Namespace in project fabric8-maven-plugin by fabric8io.
the class ImportMojo method ensureNamespaceExists.
private void ensureNamespaceExists(KubernetesClient kubernetes, String name) {
name = convertToValidDnsLabel(name);
// lets check namespace exists
Namespace namespace = kubernetes.namespaces().withName(name).get();
if (namespace == null) {
Map<String, String> labels = new HashMap<>();
labels.put("provider", "fabric8");
labels.put("kind", "secrets");
namespace = new NamespaceBuilder().withNewMetadata().withName(name).withLabels(labels).endMetadata().build();
if (KubernetesHelper.isOpenShift(kubernetes)) {
ProjectRequest projectRequest = new ProjectRequestBuilder().withMetadata(namespace.getMetadata()).build();
OpenShiftClient openShiftClient = asOpenShiftClient(kubernetes);
log.info("Creating ProjectRequest " + name + " with labels: " + labels);
openShiftClient.projectrequests().create(projectRequest);
} else {
log.info("Creating Namespace " + name + " with labels: " + labels);
kubernetes.namespaces().withName(name).create(namespace);
}
}
}
use of io.fabric8.kubernetes.api.model.Namespace in project fabric8-maven-plugin by fabric8io.
the class ImportMojo method getGogsURL.
private String getGogsURL(KubernetesClient kubernetes, String namespace) throws MojoExecutionException {
try {
Endpoints endpoints = kubernetes.endpoints().inNamespace(namespace).withName(ServiceNames.GOGS).get();
int runningEndpoints = 0;
if (endpoints != null) {
List<EndpointSubset> subsets = endpoints.getSubsets();
for (EndpointSubset subset : subsets) {
List<EndpointAddress> addresses = subset.getAddresses();
if (addresses != null) {
runningEndpoints += addresses.size();
}
}
}
if (runningEndpoints == 0) {
return null;
}
log.info("Running %s endpoints of %s in namespace %s", runningEndpoints, ServiceNames.GOGS, namespace);
return KubernetesHelper.getServiceURL(kubernetes, ServiceNames.GOGS, namespace, "http", true);
} catch (Exception e) {
String errorMessage = e.getMessage();
if (errorMessage.contains(String.format("No service gogs running in namespace %s", namespace))) {
log.info("Gogs service does not exists, defaulting to GitHub");
return null;
} else {
throw new MojoExecutionException(e.getMessage());
}
}
}
use of io.fabric8.kubernetes.api.model.Namespace in project fabric8-maven-plugin by fabric8io.
the class ImportMojo method findBuildConfigForGitRepo.
protected BuildConfig findBuildConfigForGitRepo(OpenShiftClient openShiftClient, String namespace, String gitRepoUrl, String gitRef) throws MojoExecutionException {
BuildConfigList buildConfigList = openShiftClient.buildConfigs().inNamespace(namespace).list();
if (buildConfigList != null) {
List<BuildConfig> items = buildConfigList.getItems();
if (items != null) {
for (BuildConfig item : items) {
BuildConfigSpec spec = item.getSpec();
if (spec != null) {
BuildSource source = spec.getSource();
if (source != null) {
GitBuildSource git = source.getGit();
if (git != null) {
String uri = git.getUri();
String ref = git.getRef();
if (Objects.equal(gitRepoUrl, uri)) {
if (Strings.isNullOrBlank(gitRef) && Strings.isNullOrBlank(ref)) {
return item;
}
if (Objects.equal(gitRef, ref)) {
return item;
}
}
}
}
}
}
}
}
return null;
}
use of io.fabric8.kubernetes.api.model.Namespace in project fabric8-maven-plugin by fabric8io.
the class ImportMojo method getSecretGitConfigMap.
private ConfigMap getSecretGitConfigMap(KubernetesClient kubernetes, String namespace, String secretNamespace) {
ConfigMap configMap = kubernetes.configMaps().inNamespace(secretNamespace).withName(FABRIC8_GIT_APP_SECRETS_CONFIGMAP).get();
if (configMap == null) {
Map<String, String> labels = new HashMap<String, String>();
labels.put("provider", "fabric8");
Map<String, String> data = new HashMap<>();
data.put(GOGS_REPO_HOST, createGitSecretName(namespace, GOGS_REPO_HOST));
configMap = new ConfigMapBuilder().withNewMetadata().withName(FABRIC8_GIT_APP_SECRETS_CONFIGMAP).withLabels(labels).endMetadata().withData(data).build();
log.info("Creating ConfigMap " + FABRIC8_GIT_APP_SECRETS_CONFIGMAP + " in namespace " + secretNamespace);
kubernetes.configMaps().inNamespace(secretNamespace).withName(FABRIC8_GIT_APP_SECRETS_CONFIGMAP).create(configMap);
}
if (configMap.getData() == null) {
configMap.setData(new HashMap<String, String>());
}
return configMap;
}
use of io.fabric8.kubernetes.api.model.Namespace in project fabric8-maven-plugin by fabric8io.
the class ImportMojo method executeInternal.
@Override
public void executeInternal() throws MojoExecutionException, MojoFailureException {
if (!basedir.isDirectory() || !basedir.exists()) {
throw new MojoExecutionException("No directory for base directory: " + basedir);
}
// lets check for a git repo
String gitRemoteURL = null;
Repository repository = null;
try {
repository = GitUtils.findRepository(basedir);
} catch (IOException e) {
throw new MojoExecutionException("Failed to find local git repository in current directory: " + e, e);
}
try {
gitRemoteURL = GitUtils.getRemoteAsHttpsURL(repository);
} catch (Exception e) {
throw new MojoExecutionException("Failed to get the current git branch: " + e, e);
}
try {
clusterAccess = new ClusterAccess(this.namespace);
if (Strings.isNullOrBlank(projectName)) {
projectName = basedir.getName();
}
KubernetesClient kubernetes = clusterAccess.createDefaultClient(log);
KubernetesResourceUtil.validateKubernetesMasterUrl(kubernetes.getMasterUrl());
String namespace = clusterAccess.getNamespace();
OpenShiftClient openShiftClient = getOpenShiftClientOrJenkinsShift(kubernetes, namespace);
if (gitRemoteURL != null) {
// lets check we don't already have this project imported
String branch = repository.getBranch();
BuildConfig buildConfig = findBuildConfigForGitRepo(openShiftClient, namespace, gitRemoteURL, branch);
if (buildConfig != null) {
logBuildConfigLink(kubernetes, namespace, buildConfig, log);
throw new MojoExecutionException("Project already imported into build " + getName(buildConfig) + " for URI: " + gitRemoteURL + " and branch: " + branch);
} else {
Map<String, String> annotations = new HashMap<>();
annotations.put(Annotations.Builds.GIT_CLONE_URL, gitRemoteURL);
buildConfig = createBuildConfig(kubernetes, namespace, projectName, gitRemoteURL, annotations);
openShiftClient.buildConfigs().inNamespace(namespace).create(buildConfig);
ensureExternalGitSecretsAreSetupFor(kubernetes, namespace, gitRemoteURL);
logBuildConfigLink(kubernetes, namespace, buildConfig, log);
}
} else {
// lets create an import a new project
UserDetails userDetails = createGogsUserDetails(kubernetes, namespace);
userDetails.setBranch(branchName);
BuildConfigHelper.CreateGitProjectResults createGitProjectResults;
try {
createGitProjectResults = BuildConfigHelper.importNewGitProject(kubernetes, userDetails, basedir, namespace, projectName, remoteName, "Importing project from mvn fabric8:import", false);
} catch (WebApplicationException e) {
Response response = e.getResponse();
if (response.getStatus() > 400) {
String message = getEntityMessage(response);
log.warn("Could not create the git repository: %s %s", e, message);
log.warn("Are your username and password correct in the Secret %s/%s?", secretNamespace, gogsSecretName);
log.warn("To re-enter your password rerun this command with -Dfabric8.passsword.retry=true");
throw new MojoExecutionException("Could not create the git repository. " + "Are your username and password correct in the Secret " + secretNamespace + "/" + gogsSecretName + "?" + e + message, e);
} else {
throw e;
}
}
BuildConfig buildConfig = createGitProjectResults.getBuildConfig();
openShiftClient.buildConfigs().inNamespace(namespace).create(buildConfig);
logBuildConfigLink(kubernetes, namespace, buildConfig, log);
}
} catch (KubernetesClientException e) {
KubernetesResourceUtil.handleKubernetesClientException(e, this.log);
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
Aggregations