Search in sources :

Example 31 with MixedOperation

use of io.fabric8.kubernetes.client.dsl.MixedOperation in project fabric8 by fabric8io.

the class Controller method applyResource.

public <T extends HasMetadata, L, D> void applyResource(T resource, String sourceName, MixedOperation<T, L, D, ? extends Resource<T, D>> resources) throws Exception {
    String namespace = getNamespace();
    String resourceNamespace = getNamespace(resource);
    if (Strings.isNotBlank(namespace) && Strings.isNullOrBlank(resourceNamespace)) {
        getOrCreateMetadata(resource).setNamespace(namespace);
    }
    String id = getName(resource);
    String kind = getKind(resource);
    Objects.notNull(id, "No name for " + resource + " " + sourceName);
    if (isServicesOnlyMode()) {
        LOG.debug("Ignoring " + kind + ": " + namespace + ":" + id);
        return;
    }
    T old = resources.inNamespace(namespace).withName(id).get();
    if (isRunning(old)) {
        if (UserConfigurationCompare.configEqual(resource, old)) {
            LOG.info(kind + " has not changed so not doing anything");
        } else {
            if (isRecreateMode()) {
                LOG.info("Deleting " + kind + ": " + id);
                resources.inNamespace(namespace).withName(id).delete();
                doCreateResource(resource, namespace, sourceName, resources);
            } else {
                LOG.info("Updating " + kind + " from " + sourceName);
                try {
                    Object answer = resources.inNamespace(namespace).withName(id).replace(resource);
                    logGeneratedEntity("Updated " + kind + ": ", namespace, resource, answer);
                } catch (Exception e) {
                    onApplyError("Failed to update " + kind + " from " + sourceName + ". " + e + ". " + resource, e);
                }
            }
        }
    } else {
        if (!isAllowCreate()) {
            LOG.warn("Creation disabled so not creating a " + kind + " from " + sourceName + " namespace " + namespace + " name " + getName(resource));
        } else {
            doCreateResource(resource, namespace, sourceName, resources);
        }
    }
}
Also used : 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 32 with MixedOperation

use of io.fabric8.kubernetes.client.dsl.MixedOperation in project fabric8 by fabric8io.

the class Controller method doCreateResource.

protected <T extends HasMetadata, L, D> void doCreateResource(T resource, String namespace, String sourceName, MixedOperation<T, L, D, ? extends Resource<T, D>> resources) throws Exception {
    String kind = getKind(resource);
    LOG.info("Creating a " + kind + " from " + sourceName + " namespace " + namespace + " name " + getName(resource));
    try {
        Object answer;
        if (Strings.isNotBlank(namespace)) {
            answer = resources.inNamespace(namespace).create(resource);
        } else {
            answer = resources.inNamespace(getNamespace()).create(resource);
        }
        logGeneratedEntity("Created " + kind + ": ", namespace, resource, answer);
    } catch (Exception e) {
        onApplyError("Failed to create " + kind + " from " + sourceName + ". " + e + ". " + resource, e);
    }
}
Also used : 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

Resource (io.fabric8.kubernetes.client.dsl.Resource)21 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)15 Async (io.vertx.ext.unit.Async)15 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)13 Test (org.junit.Test)12 Map (java.util.Map)10 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)4 DeploymentList (io.fabric8.kubernetes.api.model.extensions.DeploymentList)4 DoneableDeployment (io.fabric8.kubernetes.api.model.extensions.DoneableDeployment)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 ScalableResource (io.fabric8.kubernetes.client.dsl.ScalableResource)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 ConfigMapList (io.fabric8.kubernetes.api.model.ConfigMapList)3 DoneableConfigMap (io.fabric8.kubernetes.api.model.DoneableConfigMap)3 DoneablePersistentVolumeClaim (io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim)3 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)3 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)3 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)3 PersistentVolumeClaimList (io.fabric8.kubernetes.api.model.PersistentVolumeClaimList)3 Pod (io.fabric8.kubernetes.api.model.Pod)3