Search in sources :

Example 86 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8 by jboss-fuse.

the class Controller method printSummary.

protected void printSummary(Object kubeResource) throws IOException {
    if (kubeResource != null) {
        LOG.debug("  " + kubeResource.getClass().getSimpleName() + " " + kubeResource);
    }
    if (kubeResource instanceof Template) {
        Template template = (Template) kubeResource;
        String id = getName(template);
        LOG.info("  Template " + id + " " + summaryText(template));
        printSummary(template.getObjects());
        return;
    }
    List<HasMetadata> list = toItemList(kubeResource);
    for (HasMetadata object : list) {
        if (object != null) {
            if (object == list) {
                LOG.debug("Ignoring recursive list " + list);
                continue;
            } else if (object instanceof List) {
                printSummary(object);
            } else {
                String kind = object.getClass().getSimpleName();
                String id = getObjectId(object);
                LOG.info("    " + kind + " " + id + " " + summaryText(object));
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) KubernetesHelper.toItemList(io.fabric8.kubernetes.api.KubernetesHelper.toItemList) List(java.util.List) ArrayList(java.util.ArrayList) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template)

Example 87 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8 by jboss-fuse.

the class Controller method doCreateReplicationController.

protected void doCreateReplicationController(ReplicationController replicationController, String namespace, String sourceName) {
    LOG.info("Creating a ReplicationController from " + sourceName + " namespace " + namespace + " name " + getName(replicationController));
    try {
        // lets check that if secrets are required they exist
        ReplicationControllerSpec spec = replicationController.getSpec();
        if (spec != null) {
            PodTemplateSpec template = spec.getTemplate();
            if (template != null) {
                PodSpec podSpec = template.getSpec();
                validatePodSpec(podSpec, namespace);
            }
        }
        Object answer;
        if (Strings.isNotBlank(namespace)) {
            answer = kubernetesClient.replicationControllers().inNamespace(namespace).create(replicationController);
        } else {
            answer = kubernetesClient.replicationControllers().inNamespace(getNamespace()).create(replicationController);
        }
        logGeneratedEntity("Created ReplicationController: ", namespace, replicationController, answer);
    } catch (Exception e) {
        onApplyError("Failed to create ReplicationController from " + sourceName + ". " + e + ". " + replicationController, e);
    }
}
Also used : PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) 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) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 88 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8 by jboss-fuse.

the class Templates method combineTemplates.

public static Template combineTemplates(Template firstTemplate, Template template) {
    List<HasMetadata> objects = template.getObjects();
    if (objects != null) {
        for (HasMetadata object : objects) {
            addTemplateObject(firstTemplate, object);
        }
    }
    List<Parameter> parameters = firstTemplate.getParameters();
    if (parameters == null) {
        parameters = new ArrayList<>();
        firstTemplate.setParameters(parameters);
    }
    combineParameters(parameters, template.getParameters());
    String name = KubernetesHelper.getName(template);
    if (Strings.isNotBlank(name)) {
        // lets merge all the fabric8 annotations using the template id qualifier as a postfix
        Map<String, String> annotations = KubernetesHelper.getOrCreateAnnotations(firstTemplate);
        Map<String, String> otherAnnotations = KubernetesHelper.getOrCreateAnnotations(template);
        Set<Map.Entry<String, String>> entries = otherAnnotations.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (!annotations.containsKey(key)) {
                annotations.put(key, value);
            }
        }
    }
    return firstTemplate;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Parameter(io.fabric8.openshift.api.model.Parameter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 89 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8 by jboss-fuse.

the class Templates method processTemplatesLocally.

/**
 * Lets locally process the templates so that we can process templates on any kubernetes environment
 */
public static KubernetesList processTemplatesLocally(Template entity, boolean failOnMissingParameterValue) throws IOException {
    List<HasMetadata> objects = null;
    if (entity != null) {
        objects = entity.getObjects();
        if (objects == null || objects.isEmpty()) {
            return null;
        }
    }
    List<Parameter> parameters = entity != null ? entity.getParameters() : null;
    if (parameters != null && !parameters.isEmpty()) {
        String json = "{\"kind\": \"List\", \"apiVersion\": \"" + KubernetesHelper.defaultApiVersion + "\",\n" + "  \"items\": " + KubernetesHelper.toJson(objects) + " }";
        // lets make a few passes in case there's expressions in values
        for (int i = 0; i < 5; i++) {
            for (Parameter parameter : parameters) {
                String name = parameter.getName();
                String regex = "${" + name + "}";
                String value = parameter.getValue();
                // TODO generate random strings for passwords etc!
                if (Strings.isNullOrBlank(value)) {
                    if (failOnMissingParameterValue) {
                        throw new IllegalArgumentException("No value available for parameter name: " + name);
                    } else {
                        value = "";
                    }
                }
                json = Strings.replaceAllWithoutRegex(json, regex, value);
            }
        }
        return OBJECT_MAPPER.readerFor(KubernetesList.class).readValue(json);
    } else {
        KubernetesList answer = new KubernetesList();
        answer.setItems(objects);
        return answer;
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Parameter(io.fabric8.openshift.api.model.Parameter) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList)

Aggregations

Template (io.fabric8.openshift.api.model.Template)32 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)24 IOException (java.io.IOException)20 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)16 HashMap (java.util.HashMap)15 Test (org.junit.Test)15 File (java.io.File)14 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)12 Service (io.fabric8.kubernetes.api.model.Service)11 Parameter (io.fabric8.openshift.api.model.Parameter)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 Container (io.fabric8.kubernetes.api.model.Container)8 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)8 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)8 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)8 FileNotFoundException (java.io.FileNotFoundException)8 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)7 OpenShiftHelper (io.vertx.it.openshift.utils.OpenShiftHelper)7