Search in sources :

Example 71 with OpenShiftClient

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

the class Controller method applyRoleBinding.

public void applyRoleBinding(RoleBinding entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrJenkinshift();
    if (openShiftClient != null) {
        String id = getName(entity);
        Objects.notNull(id, "No name for " + entity + " " + sourceName);
        String namespace = KubernetesHelper.getNamespace(entity);
        if (Strings.isNullOrBlank(namespace)) {
            namespace = getNamespace();
        }
        applyNamespace(namespace);
        RoleBinding old = openShiftClient.roleBindings().inNamespace(namespace).withName(id).get();
        if (isRunning(old)) {
            if (UserConfigurationCompare.configEqual(entity, old)) {
                LOG.info("RoleBinding has not changed so not doing anything");
            } else {
                if (isRecreateMode()) {
                    LOG.info("Deleting RoleBinding: " + id);
                    openShiftClient.roleBindings().inNamespace(namespace).withName(id).delete();
                    doCreateRoleBinding(entity, namespace, sourceName);
                } else {
                    LOG.info("Updating RoleBinding from " + sourceName);
                    try {
                        String resourceVersion = KubernetesHelper.getResourceVersion(old);
                        ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(entity);
                        metadata.setNamespace(namespace);
                        metadata.setResourceVersion(resourceVersion);
                        Object answer = openShiftClient.roleBindings().inNamespace(namespace).withName(id).replace(entity);
                        logGeneratedEntity("Updated RoleBinding: ", namespace, entity, answer);
                    } catch (Exception e) {
                        onApplyError("Failed to update RoleBinding from " + sourceName + ". " + e + ". " + entity, e);
                    }
                }
            }
        } else {
            if (!isAllowCreate()) {
                LOG.warn("Creation disabled so not creating RoleBinding from " + sourceName + " namespace " + namespace + " name " + getName(entity));
            } else {
                doCreateRoleBinding(entity, namespace, sourceName);
            }
        }
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) JSONObject(org.json.JSONObject) RoleBinding(io.fabric8.openshift.api.model.RoleBinding) 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 72 with OpenShiftClient

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

the class Controller method applyBuildConfig.

public void applyBuildConfig(BuildConfig entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrJenkinshift();
    if (openShiftClient != null) {
        String id = getName(entity);
        Objects.notNull(id, "No name for " + entity + " " + sourceName);
        String namespace = KubernetesHelper.getNamespace(entity);
        if (Strings.isNullOrBlank(namespace)) {
            namespace = getNamespace();
        }
        applyNamespace(namespace);
        BuildConfig old = openShiftClient.buildConfigs().inNamespace(namespace).withName(id).get();
        if (isRunning(old)) {
            if (UserConfigurationCompare.configEqual(entity, old)) {
                LOG.info("BuildConfig has not changed so not doing anything");
            } else {
                if (isRecreateMode()) {
                    LOG.info("Deleting BuildConfig: " + id);
                    openShiftClient.buildConfigs().inNamespace(namespace).withName(id).delete();
                    doCreateBuildConfig(entity, namespace, sourceName);
                } else {
                    LOG.info("Updating BuildConfig from " + sourceName);
                    try {
                        String resourceVersion = KubernetesHelper.getResourceVersion(old);
                        ObjectMeta metadata = KubernetesHelper.getOrCreateMetadata(entity);
                        metadata.setNamespace(namespace);
                        metadata.setResourceVersion(resourceVersion);
                        Object answer = openShiftClient.buildConfigs().inNamespace(namespace).withName(id).replace(entity);
                        logGeneratedEntity("Updated BuildConfig: ", namespace, entity, answer);
                    } catch (Exception e) {
                        onApplyError("Failed to update BuildConfig from " + sourceName + ". " + e + ". " + entity, e);
                    }
                }
            }
        } else {
            if (!isAllowCreate()) {
                LOG.warn("Creation disabled so not creating BuildConfig from " + sourceName + " namespace " + namespace + " name " + getName(entity));
            } else {
                doCreateBuildConfig(entity, namespace, sourceName);
            }
        }
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) 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 73 with OpenShiftClient

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

the class Controller method doCreateTemplate.

protected void doCreateTemplate(Template entity, String namespace, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.TEMPLATE)) {
        LOG.info("Creating a Template from " + sourceName + " namespace " + namespace + " name " + getName(entity));
        try {
            Object answer = openShiftClient.templates().inNamespace(namespace).create(entity);
            logGeneratedEntity("Created Template: ", namespace, entity, answer);
        } catch (Exception e) {
            onApplyError("Failed to Template entity from " + sourceName + ". " + e + ". " + entity, e);
        }
    }
}
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 74 with OpenShiftClient

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

the class Controller method installTemplate.

/**
 * Installs the template into the namespace without processing it
 */
public void installTemplate(Template entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient == null || !openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.TEMPLATE)) {
        // lets not install the template on Kubernetes!
        return;
    }
    if (!isProcessTemplatesLocally()) {
        String namespace = getNamespace();
        String id = getName(entity);
        Objects.notNull(id, "No name for " + entity + " " + sourceName);
        Template old = openShiftClient.templates().inNamespace(namespace).withName(id).get();
        if (isRunning(old)) {
            if (UserConfigurationCompare.configEqual(entity, old)) {
                LOG.info("Template has not changed so not doing anything");
            } else {
                boolean recreateMode = isRecreateMode();
                // TODO seems you can't update templates right now
                recreateMode = true;
                if (recreateMode) {
                    openShiftClient.templates().inNamespace(namespace).withName(id).delete();
                    doCreateTemplate(entity, namespace, sourceName);
                } else {
                    LOG.info("Updating a Template from " + sourceName);
                    try {
                        Object answer = openShiftClient.templates().inNamespace(namespace).withName(id).replace(entity);
                        LOG.info("Updated Template: " + answer);
                    } catch (Exception e) {
                        onApplyError("Failed to update Template from " + sourceName + ". " + e + ". " + entity, e);
                    }
                }
            }
        } else {
            if (!isAllowCreate()) {
                LOG.warn("Creation disabled so not creating a Template from " + sourceName + " namespace " + namespace + " name " + getName(entity));
            } else {
                doCreateTemplate(entity, namespace, sourceName);
            }
        }
    }
}
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) Template(io.fabric8.openshift.api.model.Template)

Example 75 with OpenShiftClient

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

the class DeleteBuildConfig method main.

public static void main(String... args) {
    if (args.length < 1) {
        System.out.println("Usage nameOfBuildConfig");
        return;
    }
    try {
        String name = args[0];
        System.out.println("Deleting BuildConfig: " + name);
        KubernetesClient kube = new DefaultKubernetesClient();
        String namespace = kube.getNamespace();
        System.out.println("Using namespace: " + namespace);
        Controller controller = new Controller(kube);
        OpenShiftClient openshift = controller.getOpenShiftClientOrJenkinshift();
        if (openshift == null) {
            System.err.println("Cannot connect to OpenShift or Jenkinshift!");
            return;
        }
        BuildConfig buildConfig = openshift.buildConfigs().withName(name).get();
        if (buildConfig != null) {
            System.out.println("Managed to load BuildConfig with resourceVersion " + KubernetesHelper.getResourceVersion(buildConfig));
        } else {
            System.err.println("Could not find BuildConfig called: " + name);
            return;
        }
        Boolean result = openshift.buildConfigs().withName(name).delete();
        System.out.println("Deleted BuildConfig with name " + name + " result: " + result);
    } catch (Exception e) {
        System.out.println("FAILED: " + e);
        e.printStackTrace();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Aggregations

OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)65 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 IOException (java.io.IOException)18 Test (org.junit.Test)17 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)15 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)14 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)12 API (org.wso2.carbon.apimgt.core.models.API)12 FileNotFoundException (java.io.FileNotFoundException)11 Service (io.fabric8.kubernetes.api.model.Service)10 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)10 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)10 BeforeTest (org.testng.annotations.BeforeTest)9 Test (org.testng.annotations.Test)9 Controller (io.fabric8.kubernetes.api.Controller)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)8 Route (io.fabric8.openshift.api.model.Route)8 JSONObject (org.json.JSONObject)8