use of io.fabric8.openshift.api.model.ProjectRequest 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.openshift.api.model.ProjectRequest in project fabric8 by fabric8io.
the class Controller method applyProjectRequest.
/**
* Returns true if the ProjectRequest is created
*/
public boolean applyProjectRequest(ProjectRequest entity) {
String namespace = getOrCreateMetadata(entity).getName();
LOG.info("Using project: " + namespace);
String name = getName(entity);
Objects.notNull(name, "No name for " + entity);
OpenShiftClient openshiftClient = getOpenShiftClientOrNull();
if (openshiftClient == null || !openshiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.PROJECT)) {
LOG.warn("Cannot check for Project " + namespace + " as not running against OpenShift!");
return false;
}
boolean exists = checkNamespace(name);
// We may want to be more fine-grained on the phase of the project
if (!exists) {
try {
Object answer = openshiftClient.projectrequests().create(entity);
logGeneratedEntity("Created ProjectRequest: ", namespace, entity, answer);
return true;
} catch (Exception e) {
onApplyError("Failed to create ProjectRequest: " + name + " due " + e.getMessage(), e);
}
}
return false;
}
use of io.fabric8.openshift.api.model.ProjectRequest in project fabric8 by fabric8io.
the class Controller method applyNamespace.
public void applyNamespace(String namespaceName, Map<String, String> labels) {
if (Strings.isNullOrBlank(namespaceName)) {
return;
}
OpenShiftClient openshiftClient = getOpenShiftClientOrNull();
if (openshiftClient != null && openshiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.PROJECT)) {
ProjectRequest entity = new ProjectRequest();
ObjectMeta metadata = getOrCreateMetadata(entity);
metadata.setName(namespaceName);
String namespace = kubernetesClient.getNamespace();
if (Strings.isNotBlank(namespace)) {
Map<String, String> entityLabels = getOrCreateLabels(entity);
if (labels != null) {
entityLabels.putAll(labels);
} else {
// lets associate this new namespace with the project that it was created from
entityLabels.put("project", namespace);
}
}
applyProjectRequest(entity);
} else {
Namespace entity = new Namespace();
ObjectMeta metadata = getOrCreateMetadata(entity);
metadata.setName(namespaceName);
String namespace = kubernetesClient.getNamespace();
if (Strings.isNotBlank(namespace)) {
Map<String, String> entityLabels = getOrCreateLabels(entity);
if (labels != null) {
entityLabels.putAll(labels);
} else {
// lets associate this new namespace with the project that it was created from
entityLabels.put("project", namespace);
}
}
applyNamespace(entity);
}
}
Aggregations