Search in sources :

Example 1 with ProjectRequest

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);
        }
    }
}
Also used : ProjectRequestBuilder(io.fabric8.openshift.api.model.ProjectRequestBuilder) HashMap(java.util.HashMap) ProjectRequest(io.fabric8.openshift.api.model.ProjectRequest) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Namespace(io.fabric8.kubernetes.api.model.Namespace) KubernetesHelper.getNamespace(io.fabric8.kubernetes.api.KubernetesHelper.getNamespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder)

Example 2 with ProjectRequest

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;
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) JSONObject(org.json.JSONObject) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 3 with ProjectRequest

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);
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ProjectRequest(io.fabric8.openshift.api.model.ProjectRequest) Namespace(io.fabric8.kubernetes.api.model.Namespace)

Aggregations

OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)3 Namespace (io.fabric8.kubernetes.api.model.Namespace)2 ProjectRequest (io.fabric8.openshift.api.model.ProjectRequest)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 KubernetesHelper.getNamespace (io.fabric8.kubernetes.api.KubernetesHelper.getNamespace)1 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 ProjectRequestBuilder (io.fabric8.openshift.api.model.ProjectRequestBuilder)1 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1