Search in sources :

Example 61 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8-maven-plugin by fabric8io.

the class DependencyEnricher method processArtifactSetResources.

private void processArtifactSetResources(Set<URL> artifactSet, Function<List<HasMetadata>, Void> function) {
    for (URL url : artifactSet) {
        try {
            InputStream is = url.openStream();
            if (is != null) {
                log.debug("Processing Kubernetes YAML in at: %s", url);
                KubernetesList resources = new ObjectMapper(new YAMLFactory()).readValue(is, KubernetesList.class);
                List<HasMetadata> items = notNullList(resources.getItems());
                if (items.size() == 0 && Objects.equals("Template", resources.getKind())) {
                    is = url.openStream();
                    Template template = new ObjectMapper(new YAMLFactory()).readValue(is, Template.class);
                    if (template != null) {
                        items.add(template);
                    }
                }
                for (HasMetadata item : items) {
                    KubernetesResourceUtil.setSourceUrlAnnotationIfNotSet(item, url.toString());
                    log.debug("  found %s  %s", getKind(item), KubernetesHelper.getName(item));
                }
                function.apply(items);
            }
        } catch (IOException e) {
            getLog().debug("Skipping %s: %s", url, e);
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) InputStream(java.io.InputStream) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) IOException(java.io.IOException) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(io.fabric8.openshift.api.model.Template)

Example 62 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8-maven-plugin by fabric8io.

the class AbstractArtifactSearchMojo method findManifestValue.

protected static <T> T findManifestValue(Object manifest, Function<HasMetadata, T> function) {
    if (manifest instanceof HasMetadata) {
        HasMetadata metadata = (HasMetadata) manifest;
        T answer = function.apply(metadata);
        if (answer != null)
            return answer;
    }
    if (manifest instanceof KubernetesList) {
        KubernetesList list = (KubernetesList) manifest;
        return findManifestValueFromList(list.getItems(), function);
    }
    if (manifest instanceof Template) {
        Template template = (Template) manifest;
        return findManifestValueFromList(template.getObjects(), function);
    }
    return null;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template)

Example 63 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8-maven-plugin by fabric8io.

the class DockerImageWatcher method updateImageName.

private boolean updateImageName(HasMetadata entity, PodTemplateSpec template, String imagePrefix, String imageName) {
    boolean answer = false;
    PodSpec spec = template.getSpec();
    if (spec != null) {
        List<Container> containers = spec.getContainers();
        if (containers != null) {
            for (Container container : containers) {
                String image = container.getImage();
                if (image != null && image.startsWith(imagePrefix)) {
                    container.setImage(imageName);
                    log.info("Updating " + getKind(entity) + " " + KubernetesHelper.getName(entity) + " to use image: " + imageName);
                    answer = true;
                }
            }
        }
    }
    return answer;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) PodSpec(io.fabric8.kubernetes.api.model.PodSpec)

Example 64 with Template

use of io.fabric8.openshift.api.model.Template 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 65 with Template

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

Aggregations

Template (io.fabric8.openshift.api.model.Template)23 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)19 File (java.io.File)14 IOException (java.io.IOException)13 HashMap (java.util.HashMap)12 Test (org.junit.Test)12 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)11 Service (io.fabric8.kubernetes.api.model.Service)10 Container (io.fabric8.kubernetes.api.model.Container)8 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 OpenShiftHelper (io.vertx.it.openshift.utils.OpenShiftHelper)7 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 BeforeClass (org.junit.BeforeClass)7 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)6 TreeMap (java.util.TreeMap)6 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)5 Parameter (io.fabric8.openshift.api.model.Parameter)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4