Search in sources :

Example 56 with Template

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

the class ResourceMojo method moveTemplatesToTopLevel.

private void moveTemplatesToTopLevel(KubernetesListBuilder builder, List<HasMetadata> objects) {
    Template template = extractAndRemoveTemplates(objects);
    if (template != null) {
        openshiftDependencyResources.addMissingParameters(template);
        builder.addToItems(template);
    } else {
        for (HasMetadata object : objects) {
            builder.addToItems(object);
        }
    }
}
Also used : Template(io.fabric8.openshift.api.model.Template)

Example 57 with Template

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

the class ResourceMojo method processOpenshiftTemplateIfProvided.

private KubernetesList processOpenshiftTemplateIfProvided(KubernetesList openShiftResources, KubernetesList kubernetesResources) throws MojoExecutionException {
    Template template = getSingletonTemplate(openShiftResources);
    if (template != null) {
        KubernetesList kubernetesTemplateList = createKubernetesTemplate(kubernetesResources, template);
        if (kubernetesTemplateList != null) {
            writeResources(kubernetesTemplateList, ResourceClassifier.KUBERNETES_TEMPLATE, generateRoute);
        }
        kubernetesResources = replaceTemplateExpressions(kubernetesResources, template);
    }
    return kubernetesResources;
}
Also used : Template(io.fabric8.openshift.api.model.Template)

Example 58 with Template

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

the class DebugMojo method enableDebugging.

private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
    if (template != null) {
        PodSpec podSpec = template.getSpec();
        if (podSpec != null) {
            List<Container> containers = podSpec.getContainers();
            boolean enabled = false;
            for (int i = 0; i < containers.size(); i++) {
                Container container = containers.get(i);
                List<EnvVar> env = container.getEnv();
                if (env == null) {
                    env = new ArrayList<>();
                }
                remoteDebugPort = KubernetesResourceUtil.getEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                if (KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG, "true")) {
                    container.setEnv(env);
                    enabled = true;
                }
                if (KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SUSPEND, String.valueOf(debugSuspend))) {
                    container.setEnv(env);
                    enabled = true;
                }
                List<ContainerPort> ports = container.getPorts();
                if (ports == null) {
                    ports = new ArrayList<>();
                }
                if (KubernetesResourceUtil.addPort(ports, remoteDebugPort, "debug", log)) {
                    container.setPorts(ports);
                    enabled = true;
                }
                if (debugSuspend) {
                    // Setting a random session value to force pod restart
                    this.debugSuspendValue = String.valueOf(new Random().nextLong());
                    KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION, this.debugSuspendValue);
                    container.setEnv(env);
                    if (container.getReadinessProbe() != null) {
                        log.info("Readiness probe will be disabled on " + getKind(entity) + " " + getName(entity) + " to allow attaching a remote debugger during suspension");
                        container.setReadinessProbe(null);
                    }
                    enabled = true;
                } else {
                    if (KubernetesResourceUtil.removeEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION)) {
                        container.setEnv(env);
                        enabled = true;
                    }
                }
            }
            if (enabled) {
                log.info("Enabling debug on " + getKind(entity) + " " + getName(entity));
                return true;
            }
        }
    }
    return false;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) Random(java.util.Random) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 59 with Template

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

the class AppCatalogMojo method executeInternal.

public void executeInternal() throws MojoExecutionException, MojoFailureException {
    List<HasMetadata> openshiftResources = new ArrayList<>();
    List<HasMetadata> kubernetesResources = new ArrayList<>();
    Map<URL, KubernetesResource> openshiftMap = loadYamlResourcesOnClassPath("META-INF/fabric8/openshift.yml");
    log.info("Found " + openshiftMap.size() + " openshift resources");
    for (Map.Entry<URL, KubernetesResource> entry : openshiftMap.entrySet()) {
        URL url = entry.getKey();
        KubernetesResource<?> resource = entry.getValue();
        Template template = null;
        if (resource instanceof Template) {
            template = (Template) resource;
            getOrCreateAnnotations(template).put(RESOURCE_APP_CATALOG_ANNOTATION, "true");
            log.debug("Found Template " + getName(template) + " with " + notNullList(template.getParameters()).size() + " parameters");
        } else {
            TemplateBuilder builder = new TemplateBuilder();
            boolean foundMetadata = false;
            if (resource instanceof HasMetadata) {
                HasMetadata hasMetadata = (HasMetadata) resource;
                ObjectMeta metadata = hasMetadata.getMetadata();
                if (metadata != null) {
                    if (Strings.isNotBlank(metadata.getName())) {
                        foundMetadata = true;
                        getOrCreateAnnotations(hasMetadata).put(RESOURCE_APP_CATALOG_ANNOTATION, "true");
                        builder.withMetadata(metadata);
                    }
                }
            }
            if (!foundMetadata) {
                Map<String, String> labels = new HashMap<>();
                Map<String, String> annotations = new HashMap<>();
                annotations.put(RESOURCE_APP_CATALOG_ANNOTATION, "true");
                String name = extractNameFromURL(url, labels);
                if (name.equals("META-INF")) {
                    log.debug("Ignoring local build dependency %s", url);
                    continue;
                }
                if (Strings.isNullOrBlank(name)) {
                    log.warn("Cannot generate a template name from URL: %s", url);
                    continue;
                }
                populateLabelsFromResources(resource, labels);
                populateAnnotationsFromResources(resource, annotations);
                builder.withNewMetadata().withName(name).withLabels(labels).withAnnotations(annotations).endMetadata();
            }
            if (resource instanceof KubernetesList) {
                KubernetesList list = (KubernetesList) resource;
                List<HasMetadata> items = list.getItems();
                if (items == null || items.isEmpty()) {
                    log.warn("Ignoring resource %s as it contains a List which contains no items!", url);
                    continue;
                }
                builder.withObjects(items);
            }
            template = builder.build();
        }
        if (template != null) {
            openshiftResources.add(template);
        }
    }
    Map<String, Template> kubernetesTemplates = new HashMap<>();
    Map<URL, KubernetesResource> kubernetesTemplateMap = loadYamlResourcesOnClassPath("META-INF/fabric8/" + KUBERNETES_TEMPLATE.getValue() + ".yml");
    for (Map.Entry<URL, KubernetesResource> entry : kubernetesTemplateMap.entrySet()) {
        URL url = entry.getKey();
        KubernetesResource<?> resource = entry.getValue();
        if (resource instanceof Template) {
            Template template = (Template) resource;
            String name = getName(template);
            if (Strings.isNullOrBlank(name)) {
                log.warn("Ignoring Template from %s as it has no name!", url);
                continue;
            }
            if (kubernetesTemplates.containsKey(name)) {
                log.warn("Found duplicate template named: %s for url: %s", name, url);
            }
            kubernetesTemplates.put(name, template);
        }
    }
    Set<String> kubernetesTemplateFileNames = new HashSet<>();
    Set<String> openshiftTemplateFileNames = new HashSet<>();
    Map<URL, KubernetesResource> kubernetesMap = loadYamlResourcesOnClassPath("META-INF/fabric8/kubernetes.yml");
    for (Map.Entry<URL, KubernetesResource> entry : kubernetesMap.entrySet()) {
        URL url = entry.getKey();
        KubernetesResource<?> resource = entry.getValue();
        Map<String, String> labels = new HashMap<>();
        Map<String, String> annotations = new HashMap<>();
        String name = extractNameFromURL(url, labels);
        if (name.equals("META-INF")) {
            log.debug("Ignoring local build dependency %s", url);
            continue;
        }
        if (Strings.isNullOrBlank(name)) {
            log.warn("Cannot generate a template name from URL: %s", url);
            continue;
        }
        if (kubernetesTemplates.containsKey(name)) {
            log.info("Ignoring duplicate template %s from url: %s", name, url);
            continue;
        }
        populateLabelsFromResources(resource, labels);
        populateAnnotationsFromResources(resource, annotations);
        TemplateBuilder builder = new TemplateBuilder();
        builder.withNewMetadata().withName(name).withLabels(labels).withAnnotations(annotations).endMetadata();
        if (resource instanceof KubernetesList) {
            KubernetesList list = (KubernetesList) resource;
            List<HasMetadata> items = list.getItems();
            if (items == null || items.isEmpty()) {
                log.warn("Ignoring resource %s as it contains a List which contains no items!", url);
                continue;
            }
            builder.withObjects(items);
        } else if (resource instanceof HasMetadata) {
            HasMetadata hasMetadata = (HasMetadata) resource;
            builder.withObjects(hasMetadata);
        }
        Template template = builder.build();
        if (template != null) {
            kubernetesTemplates.put(name, template);
            openshiftTemplateFileNames.add(name + "-template.yml");
        }
    }
    for (Map.Entry<String, Template> entry : kubernetesTemplates.entrySet()) {
        String name = entry.getKey();
        Template template = entry.getValue();
        String templateYaml = null;
        try {
            templateYaml = KubernetesHelper.toYaml(template);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to convert template " + name + " into YAML: " + e, e);
        }
        String catalogName = "catalog-" + name;
        Map<String, String> labels = new LinkedHashMap<>(KubernetesHelper.getLabels(template));
        Map<String, String> annotations = getOrCreateAnnotations(template);
        annotations.put(RESOURCE_APP_CATALOG_ANNOTATION, "true");
        populateLabelsFromResources(template, labels);
        populateAnnotationsFromResources(template, annotations);
        labels.put("kind", "catalog");
        Map<String, String> data = new HashMap<>();
        data.put(catalogName + ".yml", templateYaml);
        kubernetesTemplateFileNames.add(catalogName + "-configmap.yml");
        ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName(catalogName).withLabels(labels).withAnnotations(annotations).endMetadata().withData(data).build();
        kubernetesResources.add(configMap);
    }
    if (openshiftResources.isEmpty()) {
        log.warn("No OpenShift resources generated");
    } else {
        writeResources(new KubernetesListBuilder().withItems(openshiftResources).build(), ResourceClassifier.OPENSHIFT, true);
    }
    if (kubernetesResources.isEmpty()) {
        log.warn("No Kubernetes resources generated");
    } else {
        writeResources(new KubernetesListBuilder().withItems(kubernetesResources).build(), ResourceClassifier.KUBERNETES, true);
    }
    // lets remove the dependencies which are not app templates
    removeGeneratedFilesNotMatchingSuffix("kubernetes", kubernetesTemplateFileNames);
    removeGeneratedFilesNotMatchingSuffix("openshift", openshiftTemplateFileNames);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TemplateBuilder(io.fabric8.openshift.api.model.TemplateBuilder) ArrayList(java.util.ArrayList) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) URL(java.net.URL) Template(io.fabric8.openshift.api.model.Template) LinkedHashMap(java.util.LinkedHashMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) HashSet(java.util.HashSet) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) IOException(java.io.IOException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Example 60 with Template

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

the class HelmMojo method generateHelmChartDirectory.

protected void generateHelmChartDirectory(String chartName, HelmConfig.HelmType type) throws MojoExecutionException {
    File outputDir = prepareOutputDir(type);
    File sourceDir = checkSourceDir(chartName, type);
    if (sourceDir == null) {
        return;
    }
    log.info("Creating Helm Chart \"%s\" for %s", chartName, type.getDescription());
    log.verbose("SourceDir: %s", sourceDir);
    log.verbose("OutputDir: %s", outputDir);
    // Copy over all resource descriptors into the helm templates dir
    File templatesDir = copyResourceFilesToTemplatesDir(outputDir, sourceDir);
    // Save Helm chart
    createChartYaml(chartName, outputDir);
    // Copy over support files
    copyTextFile(outputDir, "README");
    copyTextFile(outputDir, "LICENSE");
    Template template = findTemplate();
    if (template != null) {
        createTemplateParameters(outputDir, template, templatesDir);
    }
    // now lets create the tarball
    File destinationFile = new File(project.getBuild().getDirectory(), chartName + "-" + project.getVersion() + "-" + type.getClassifier() + "." + getChartFileExtension());
    MavenUtil.createArchive(outputDir.getParentFile(), destinationFile, this.archiver);
    projectHelper.attachArtifact(project, getChartFileExtension(), type.getClassifier(), destinationFile);
}
Also used : File(java.io.File) 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