Search in sources :

Example 21 with Template

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

the class HelmMojo method findTemplate.

private Template findTemplate() throws MojoExecutionException {
    if (kubernetesTemplate != null && kubernetesTemplate.isFile()) {
        Object dto = null;
        try {
            dto = KubernetesHelper.loadYaml(kubernetesTemplate, KubernetesResource.class);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to load kubernetes YAML " + kubernetesTemplate + ". " + e, e);
        }
        if (dto instanceof Template) {
            return (Template) dto;
        }
        if (dto instanceof KubernetesList) {
            KubernetesList list = (KubernetesList) dto;
            List<HasMetadata> items = list.getItems();
            if (items != null) {
                for (HasMetadata item : items) {
                    if (item instanceof Template) {
                        return (Template) item;
                    }
                }
            }
        }
    }
    return null;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) IOException(java.io.IOException) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template)

Example 22 with Template

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

the class HelmMojo method copyTemplateResourcesToTemplatesDir.

private void copyTemplateResourcesToTemplatesDir(File templatesDir, Template template) throws MojoExecutionException {
    List<HasMetadata> objects = template.getObjects();
    if (objects != null) {
        for (HasMetadata object : objects) {
            String name = getNameWithSuffix(KubernetesHelper.getName(object), KubernetesHelper.getKind(object)) + ".yaml";
            File outFile = new File(templatesDir, name);
            try {
                saveYaml(object, outFile);
            } catch (IOException e) {
                throw new MojoExecutionException("Failed to save template " + outFile + ": " + e, e);
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) File(java.io.File)

Example 23 with Template

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

the class HelmMojo method copyResourceFilesToTemplatesDir.

private File copyResourceFilesToTemplatesDir(File outputDir, File sourceDir) throws MojoExecutionException {
    File templatesDir = new File(outputDir, "templates");
    templatesDir.mkdirs();
    File[] files = sourceDir.listFiles();
    if (files != null) {
        for (File file : files) {
            Object dto;
            try {
                dto = KubernetesHelper.loadYaml(file, KubernetesResource.class);
            } catch (IOException e) {
                throw new MojoExecutionException(FAILED_TO_LOAD_KUBERNETES_YAML + file + ". " + e, e);
            }
            if (dto instanceof Template) {
                // lets split the template into separate files!
                Template template = (Template) dto;
                copyTemplateResourcesToTemplatesDir(templatesDir, template);
                continue;
            }
            String name = file.getName();
            if (name.endsWith(".yml")) {
                name = Strings.stripSuffix(name, ".yml") + YAML_EXTENSION;
            }
            File targetFile = new File(templatesDir, name);
            try {
                // lets escape any {{ or }} characters to avoid creating invalid templates
                String text = IOHelpers.readFully(file);
                text = escapeYamlTemplate(text);
                IOHelpers.writeFully(targetFile, text);
            } catch (IOException e) {
                throw new MojoExecutionException("Failed to copy manifest files from " + file + " to " + targetFile + ": " + e, e);
            }
        }
    }
    return templatesDir;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) IOException(java.io.IOException) File(java.io.File) Template(io.fabric8.openshift.api.model.Template)

Example 24 with Template

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

the class PodAnnotationEnricher method adapt.

@Override
public void adapt(KubernetesListBuilder builder) {
    super.adapt(builder);
    List<HasMetadata> items = builder.getItems();
    for (HasMetadata item : items) {
        if (item instanceof Deployment) {
            Deployment deployment = (Deployment) item;
            ObjectMeta metadata = deployment.getMetadata();
            DeploymentSpec spec = deployment.getSpec();
            if (metadata != null && spec != null) {
                PodTemplateSpec template = spec.getTemplate();
                if (template != null) {
                    ObjectMeta templateMetadata = template.getMetadata();
                    if (templateMetadata == null) {
                        templateMetadata = new ObjectMeta();
                        template.setMetadata(templateMetadata);
                    }
                    templateMetadata.setAnnotations(MapUtil.mergeMaps(templateMetadata.getAnnotations(), metadata.getAnnotations()));
                }
            }
        }
    }
    builder.withItems(items);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment)

Example 25 with Template

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

the class DeploymentOpenShiftConverter method convert.

@Override
public HasMetadata convert(HasMetadata item, boolean trimImageInContainerSpec, boolean enableAutomaticTrigger) {
    Deployment resource = (Deployment) item;
    DeploymentConfigBuilder builder = new DeploymentConfigBuilder();
    builder.withMetadata(resource.getMetadata());
    DeploymentSpec spec = resource.getSpec();
    if (spec != null) {
        DeploymentConfigFluent.SpecNested<DeploymentConfigBuilder> specBuilder = builder.withNewSpec();
        Integer replicas = spec.getReplicas();
        if (replicas != null) {
            specBuilder.withReplicas(replicas);
        }
        Integer revisionHistoryLimit = spec.getRevisionHistoryLimit();
        if (revisionHistoryLimit != null) {
            specBuilder.withRevisionHistoryLimit(revisionHistoryLimit);
        }
        LabelSelector selector = spec.getSelector();
        if (selector != null) {
            Map<String, String> matchLabels = selector.getMatchLabels();
            if (matchLabels != null && !matchLabels.isEmpty()) {
                specBuilder.withSelector(matchLabels);
            }
        }
        Map<String, String> containerToImageMap = new HashMap<>();
        PodTemplateSpec template = spec.getTemplate();
        if (template != null) {
            specBuilder.withTemplate(template);
            PodSpec podSpec = template.getSpec();
            notNull(podSpec, "No PodSpec for PodTemplate:" + template);
            List<Container> containers = podSpec.getContainers();
            notNull(podSpec, "No containers for PodTemplate.spec: " + template);
            for (Container container : containers) {
                validateContainer(container);
                containerToImageMap.put(container.getName(), container.getImage());
            }
        }
        DeploymentStrategy strategy = spec.getStrategy();
        String strategyType = null;
        if (strategy != null) {
            strategyType = strategy.getType();
        }
        if (openshiftDeployTimeoutSeconds != null && openshiftDeployTimeoutSeconds > 0) {
            if (Strings.isNullOrBlank(strategyType) || "Rolling".equals(strategyType)) {
                specBuilder.withNewStrategy().withType("Rolling").withNewRollingParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRollingParams().endStrategy();
            } else if ("Recreate".equals(strategyType)) {
                specBuilder.withNewStrategy().withType("Recreate").withNewRecreateParams().withTimeoutSeconds(openshiftDeployTimeoutSeconds).endRecreateParams().endStrategy();
            } else {
                specBuilder.withNewStrategy().withType(strategyType).endStrategy();
            }
        } else if (Strings.isNotBlank(strategyType)) {
            // TODO is there any values we can copy across?
            specBuilder.withNewStrategy().withType(strategyType).endStrategy();
        }
        // lets add a default trigger so that its triggered when we change its config
        if (enableAutomaticTrigger) {
            specBuilder.addNewTrigger().withType("ConfigChange").endTrigger();
        }
        // add a new image change trigger for the build stream
        if (containerToImageMap.size() != 0) {
            if (mode.equals(PlatformMode.openshift)) {
                for (Map.Entry<String, String> entry : containerToImageMap.entrySet()) {
                    String containerName = entry.getKey();
                    ImageName image = new ImageName(entry.getValue());
                    String tag = image.getTag() != null ? image.getTag() : "latest";
                    specBuilder.addNewTrigger().withType("ImageChange").withNewImageChangeParams().withAutomatic(enableAutomaticTrigger).withNewFrom().withKind("ImageStreamTag").withName(image.getSimpleName() + ":" + tag).withNamespace(image.getUser()).endFrom().withContainerNames(containerName).endImageChangeParams().endTrigger();
                }
            }
            if (trimImageInContainerSpec) {
                /*
                         * In Openshift 3.7, update to container image is automatically triggering redeployments
                         * and those subsequent rollouts lead to RC complaining about a missing image reference.
                         *
                         *    See this : https://github.com/openshift/origin/issues/18406#issuecomment-364090247
                         *
                         * this the time it gets fixed. Do this:
                         * Since we're using ImageTrigger here, set container image to " ". If there is any config
                         * change never set to image else than " "; so doing oc apply/rollouts won't be creating
                         * re-deployments again and again.
                         *
                         */
                List<Container> containers = template.getSpec().getContainers();
                for (Integer nIndex = 0; nIndex < containers.size(); nIndex++) {
                    containers.get(nIndex).setImage(" ");
                }
                template.getSpec().setContainers(containers);
                specBuilder.withTemplate(template);
            }
        }
        specBuilder.endSpec();
    }
    return builder.build();
}
Also used : HashMap(java.util.HashMap) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) DeploymentConfigFluent(io.fabric8.openshift.api.model.DeploymentConfigFluent) DeploymentConfigBuilder(io.fabric8.openshift.api.model.DeploymentConfigBuilder) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ImageName(io.fabric8.maven.docker.util.ImageName) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Container(io.fabric8.kubernetes.api.model.Container) HashMap(java.util.HashMap) Map(java.util.Map)

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