Search in sources :

Example 96 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class Controller method applyNamespace.

/**
 * Returns true if the namespace is created
 */
public boolean applyNamespace(Namespace entity) {
    String namespace = getOrCreateMetadata(entity).getName();
    LOG.info("Using namespace: " + namespace);
    String name = getName(entity);
    Objects.notNull(name, "No name for " + entity);
    Namespace old = kubernetesClient.namespaces().withName(name).get();
    if (!isRunning(old)) {
        try {
            Object answer = kubernetesClient.namespaces().create(entity);
            logGeneratedEntity("Created namespace: ", namespace, entity, answer);
            return true;
        } catch (Exception e) {
            onApplyError("Failed to create namespace: " + name + " due " + e.getMessage(), e);
        }
    }
    return false;
}
Also used : JSONObject(org.json.JSONObject) Namespace(io.fabric8.kubernetes.api.model.Namespace) 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 97 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class Controller method applyPod.

public void applyPod(Pod pod, String sourceName) throws Exception {
    String namespace = getNamespace();
    String id = getName(pod);
    Objects.notNull(id, "No name for " + pod + " " + sourceName);
    if (isServicesOnlyMode()) {
        LOG.debug("Only processing Services right now so ignoring Pod: " + namespace + ":" + id);
        return;
    }
    Pod old = kubernetesClient.pods().inNamespace(namespace).withName(id).get();
    if (isRunning(old)) {
        if (UserConfigurationCompare.configEqual(pod, old)) {
            LOG.info("Pod has not changed so not doing anything");
        } else {
            if (isRecreateMode()) {
                LOG.info("Deleting Pod: " + id);
                kubernetesClient.pods().inNamespace(namespace).withName(id).delete();
                doCreatePod(pod, namespace, sourceName);
            } else {
                LOG.info("Updating a Pod from " + sourceName + " namespace " + namespace + " name " + getName(pod));
                try {
                    Object answer = kubernetesClient.pods().inNamespace(namespace).withName(id).replace(pod);
                    LOG.info("Updated Pod result: " + answer);
                } catch (Exception e) {
                    onApplyError("Failed to update Pod from " + sourceName + ". " + e + ". " + pod, e);
                }
            }
        }
    } else {
        if (!isAllowCreate()) {
            LOG.warn("Creation disabled so not creating a pod from " + sourceName + " namespace " + namespace + " name " + getName(pod));
        } else {
            doCreatePod(pod, namespace, sourceName);
        }
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) 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 98 with Namespace

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

Example 99 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class Controller method applyPersistentVolumeClaim.

public void applyPersistentVolumeClaim(PersistentVolumeClaim entity, String sourceName) throws Exception {
    // we cannot update PVCs
    boolean alwaysRecreate = true;
    String namespace = getNamespace();
    String id = getName(entity);
    Objects.notNull(id, "No name for " + entity + " " + sourceName);
    if (isServicesOnlyMode()) {
        LOG.debug("Only processing Services right now so ignoring PersistentVolumeClaim: " + id);
        return;
    }
    PersistentVolumeClaim old = kubernetesClient.persistentVolumeClaims().inNamespace(namespace).withName(id).get();
    if (isRunning(old)) {
        if (UserConfigurationCompare.configEqual(entity, old)) {
            LOG.info("PersistentVolumeClaim has not changed so not doing anything");
        } else {
            if (alwaysRecreate || isRecreateMode()) {
                if (!isRecreateMode() && isIgnoreBoundPersistentVolumeClaims() && isBound(old)) {
                    LOG.warn("PersistentVolumeClaim " + id + " in namespace " + namespace + " is already bound and will not be replaced with the new one from " + sourceName);
                } else {
                    LOG.info("Deleting PersistentVolumeClaim from namespace " + namespace + " with name " + id);
                    kubernetesClient.persistentVolumeClaims().inNamespace(namespace).withName(id).delete();
                    LOG.info("Deleted PersistentVolumeClaim from namespace " + namespace + " with name " + id);
                    doCreatePersistentVolumeClaim(entity, namespace, sourceName);
                }
            } else {
                LOG.info("Updating a PersistentVolumeClaim from " + sourceName);
                try {
                    Object answer = kubernetesClient.persistentVolumeClaims().inNamespace(namespace).withName(id).replace(entity);
                    logGeneratedEntity("Updated PersistentVolumeClaim: ", namespace, entity, answer);
                } catch (Exception e) {
                    onApplyError("Failed to update PersistentVolumeClaim from " + sourceName + ". " + e + ". " + entity, e);
                }
            }
        }
    } else {
        if (!isAllowCreate()) {
            LOG.warn("Creation disabled so not creating a PersistentVolumeClaim from " + sourceName + " namespace " + namespace + " name " + getName(entity));
        } else {
            doCreatePersistentVolumeClaim(entity, namespace, sourceName);
        }
    }
}
Also used : PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) 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 100 with Namespace

use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.

the class KubernetesHelper method getServiceURLInCurrentNamespace.

/**
 * Returns the URL to access the service; using the environment variables, routes
 * or service clusterIP address
 *
 * @throws IllegalArgumentException if the URL cannot be found for the serviceName and namespace
 */
public static String getServiceURLInCurrentNamespace(KubernetesClient client, String serviceName, String serviceProtocol, String servicePortName, boolean serviceExternal) {
    Service srv = null;
    String serviceHost = KubernetesServices.serviceToHostOrBlank(serviceName);
    String servicePort = KubernetesServices.serviceToPortOrBlank(serviceName, servicePortName);
    String serviceProto = serviceProtocol != null ? serviceProtocol : KubernetesServices.serviceToProtocol(serviceName, servicePort);
    // 1. Inside Kubernetes: Services as ENV vars
    if (!serviceExternal && Strings.isNotBlank(serviceHost) && Strings.isNotBlank(servicePort) && Strings.isNotBlank(serviceProtocol)) {
        return serviceProtocol + "://" + serviceHost + ":" + servicePort;
    // 2. Anywhere: When namespace is passed System / Env var. Mostly needed for integration tests.
    } else {
        srv = client.services().withName(serviceName).get();
    }
    if (srv == null) {
        throw new IllegalArgumentException("No kubernetes service could be found for name: " + serviceName);
    }
    if (Strings.isNullOrBlank(servicePortName) && isOpenShift(client)) {
        OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
        RouteList routeList = openShiftClient.routes().list();
        for (Route route : routeList.getItems()) {
            if (route.getSpec().getTo().getName().equals(serviceName)) {
                return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
            }
        }
    }
    ServicePort port = findServicePortByName(srv, servicePortName);
    if (port == null) {
        throw new RuntimeException("Couldn't find port: " + servicePortName + " for service:" + serviceName);
    }
    String clusterIP = srv.getSpec().getClusterIP();
    if ("None".equals(clusterIP)) {
        throw new IllegalStateException("Service: " + serviceName + " in current namespace is head-less. Search for endpoints instead.");
    }
    return (serviceProto + "://" + clusterIP + ":" + port.getPort()).toLowerCase();
}
Also used : DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) RouteList(io.fabric8.openshift.api.model.RouteList) Route(io.fabric8.openshift.api.model.Route)

Aggregations

IOException (java.io.IOException)81 Test (org.junit.Test)78 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)74 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)65 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)60 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)59 HashMap (java.util.HashMap)59 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)58 FileNotFoundException (java.io.FileNotFoundException)49 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)48 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)48 JSONObject (org.json.JSONObject)44 Service (io.fabric8.kubernetes.api.model.Service)38 Pod (io.fabric8.kubernetes.api.model.Pod)36 ArrayList (java.util.ArrayList)32 File (java.io.File)25 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)24 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)24 Map (java.util.Map)22 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)21