Search in sources :

Example 36 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.

the class Controller method applyOAuthClient.

public void applyOAuthClient(OAuthClient entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.OAUTH)) {
        if (supportOAuthClients) {
            String id = getName(entity);
            Objects.notNull(id, "No name for " + entity + " " + sourceName);
            if (isServicesOnlyMode()) {
                LOG.debug("Only processing Services right now so ignoring OAuthClient: " + id);
                return;
            }
            OAuthClient old = openShiftClient.oAuthClients().withName(id).get();
            if (isRunning(old)) {
                if (isIgnoreRunningOAuthClients()) {
                    LOG.info("Not updating the OAuthClient which are shared across namespaces as its already running");
                    return;
                }
                if (UserConfigurationCompare.configEqual(entity, old)) {
                    LOG.info("OAuthClient has not changed so not doing anything");
                } else {
                    if (isRecreateMode()) {
                        openShiftClient.oAuthClients().withName(id).delete();
                        doCreateOAuthClient(entity, sourceName);
                    } else {
                        try {
                            Object answer = openShiftClient.oAuthClients().withName(id).replace(entity);
                            LOG.info("Updated OAuthClient result: " + answer);
                        } catch (Exception e) {
                            onApplyError("Failed to update OAuthClient from " + sourceName + ". " + e + ". " + entity, e);
                        }
                    }
                }
            } else {
                if (!isAllowCreate()) {
                    LOG.warn("Creation disabled so not creating an OAuthClient from " + sourceName + " name " + getName(entity));
                } else {
                    doCreateOAuthClient(entity, sourceName);
                }
            }
        }
    }
}
Also used : OAuthClient(io.fabric8.openshift.api.model.OAuthClient) 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 37 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient 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 38 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.

the class Controller method getOpenShiftClientOrJenkinshift.

public OpenShiftClient getOpenShiftClientOrJenkinshift() {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient == null) {
        // lets try talk to the jenkinshift service which provides a BuildConfig REST API based on Jenkins
        // for when using vanilla Kubernetes
        String jenkinshiftUrl = Systems.getEnvVar("JENKINSHIFT_URL", "http://jenkinshift/");
        LOG.debug("Using jenknshift URL: " + jenkinshiftUrl);
        openShiftClient = KubernetesHelper.createJenkinshiftOpenShiftClient(jenkinshiftUrl);
    }
    return openShiftClient;
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient)

Example 39 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient in project fabric8 by fabric8io.

the class Controller method applyImageStream.

public void applyImageStream(ImageStream entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.IMAGE)) {
        String kind = getKind(entity);
        String name = getName(entity);
        String namespace = getNamespace();
        try {
            Resource<ImageStream, DoneableImageStream> resource = openShiftClient.imageStreams().inNamespace(namespace).withName(name);
            ImageStream old = resource.get();
            if (old == null) {
                LOG.info("Creating " + kind + " " + name + " from " + sourceName);
                resource.create(entity);
            } else {
                LOG.info("Updating " + kind + " " + name + " from " + sourceName);
                copyAllImageStreamTags(entity, old);
                resource.replace(old);
            }
            openShiftClient.resource(entity).inNamespace(namespace).apply();
        } catch (Exception e) {
            onApplyError("Failed to create " + kind + " from " + sourceName + ". " + e, e);
        }
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DoneableImageStream(io.fabric8.openshift.api.model.DoneableImageStream) DoneableImageStream(io.fabric8.openshift.api.model.DoneableImageStream) ImageStream(io.fabric8.openshift.api.model.ImageStream) 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 40 with OpenShiftClient

use of io.fabric8.openshift.client.OpenShiftClient 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)92 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)31 IOException (java.io.IOException)31 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)23 FileNotFoundException (java.io.FileNotFoundException)21 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)20 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)20 Test (org.junit.Test)17 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)16 JSONObject (org.json.JSONObject)16 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)15 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)15 Route (io.fabric8.openshift.api.model.Route)14 Service (io.fabric8.kubernetes.api.model.Service)12 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)12 API (org.wso2.carbon.apimgt.core.models.API)12 Controller (io.fabric8.kubernetes.api.Controller)11 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)10 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)9 BeforeTest (org.testng.annotations.BeforeTest)9