Search in sources :

Example 1 with KubernetesHelper.getNamespace

use of io.fabric8.kubernetes.api.KubernetesHelper.getNamespace in project fabric8 by fabric8io.

the class Controller method applyRoute.

public void applyRoute(Route entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.ROUTE)) {
        String id = getName(entity);
        Objects.notNull(id, "No name for " + entity + " " + sourceName);
        String namespace = KubernetesHelper.getNamespace(entity);
        if (Strings.isNullOrBlank(namespace)) {
            namespace = getNamespace();
        }
        Route route = openShiftClient.routes().inNamespace(namespace).withName(id).get();
        if (route == null) {
            try {
                LOG.info("Creating Route " + namespace + ":" + id + " " + KubernetesHelper.summaryText(entity));
                openShiftClient.routes().inNamespace(namespace).create(entity);
            } catch (Exception e) {
                onApplyError("Failed to create Route from " + sourceName + ". " + e + ". " + entity, e);
            }
        }
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Route(io.fabric8.openshift.api.model.Route) 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 2 with KubernetesHelper.getNamespace

use of io.fabric8.kubernetes.api.KubernetesHelper.getNamespace in project fabric8 by fabric8io.

the class Controller method applyPolicyBinding.

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

use of io.fabric8.kubernetes.api.KubernetesHelper.getNamespace 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 4 with KubernetesHelper.getNamespace

use of io.fabric8.kubernetes.api.KubernetesHelper.getNamespace 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)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)4 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)4 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 JSONObject (org.json.JSONObject)3 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)1 PolicyBinding (io.fabric8.openshift.api.model.PolicyBinding)1 RoleBinding (io.fabric8.openshift.api.model.RoleBinding)1 Route (io.fabric8.openshift.api.model.Route)1