Search in sources :

Example 1 with KubernetesResource

use of io.fabric8.kubernetes.api.model.KubernetesResource in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method loadResources.

public static Set<HasMetadata> loadResources(File manifest) throws IOException {
    Object dto = KubernetesHelper.loadYaml(manifest, KubernetesResource.class);
    if (dto == null) {
        throw new IllegalStateException("Cannot load kubernetes YAML: " + manifest);
    }
    if (dto instanceof Template) {
        Template template = (Template) dto;
        boolean failOnMissingParameterValue = false;
        dto = Templates.processTemplatesLocally(template, failOnMissingParameterValue);
    }
    Set<KubernetesResource<?>> resources = new LinkedHashSet<>();
    Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator());
    for (KubernetesResource<?> resource : resources) {
        entities.addAll(KubernetesHelper.toItemList(resource));
    }
    entities.addAll(KubernetesHelper.toItemList(dto));
    return entities;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) HasMetadataComparator(io.fabric8.kubernetes.internal.HasMetadataComparator) TreeSet(java.util.TreeSet) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) Template(io.fabric8.openshift.api.model.Template)

Example 2 with KubernetesResource

use of io.fabric8.kubernetes.api.model.KubernetesResource in project fabric8-maven-plugin by fabric8io.

the class AppCatalogMojo method loadYamlResourcesOnClassPath.

protected Map<URL, KubernetesResource> loadYamlResourcesOnClassPath(String resourcePath) throws MojoExecutionException {
    List<URL> resourceList = findResourcesOnClassPath(resourcePath);
    Map<URL, KubernetesResource> resourceMap = new HashMap<>();
    for (URL url : resourceList) {
        try (InputStream is = url.openStream()) {
            if (is != null) {
                KubernetesResource<?> resource;
                try {
                    resource = KubernetesHelper.loadYaml(is, KubernetesResource.class);
                    resourceMap.put(url, resource);
                } catch (IOException e) {
                    log.warn("Ignoring resource %s as it could not be parsed: %s", url, e);
                    continue;
                }
            }
        } catch (IOException e) {
            log.warn("Ignoring resource %s as it could not be opened: %s", url, e);
            continue;
        }
    }
    return resourceMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InputStream(java.io.InputStream) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with KubernetesResource

use of io.fabric8.kubernetes.api.model.KubernetesResource in project fabric8 by fabric8io.

the class KubernetesModelProcessorProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    CompilationTaskFactory compilationTaskFactory = new CompilationTaskFactory(processingEnv);
    Set<TypeElement> processors = new HashSet<>();
    // 1st pass collect classes to compile.
    for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
        processors.add(getClassElement(element));
    }
    if (processors.isEmpty()) {
        return true;
    }
    StringWriter writer = new StringWriter();
    try {
        Callable<Boolean> compileTask = compilationTaskFactory.create(processors, writer);
        if (!compileTask.call()) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to compile provider classes. See output below.");
            printCompileErrors(compilationTaskFactory);
            return false;
        }
    } catch (Exception e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error to compile provider classes, due to: " + e.getMessage() + ". See output below.");
        return false;
    } finally {
        String output = writer.toString();
        if (Strings.isNullOrBlank(output)) {
            output = "success";
        }
        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Fabric8 model generator compiler output:" + output);
    }
    // 2nd pass generate json.
    for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
        KubernetesModelProcessor annotation = element.getAnnotation(KubernetesModelProcessor.class);
        String kubernetesJsonFileName = annotation.value();
        KubernetesResource json = readJson(kubernetesJsonFileName);
        Builder<? extends KubernetesResource> builder;
        if (json instanceof KubernetesList) {
            builder = new KubernetesListBuilder((KubernetesList) json);
        } else if (json instanceof Template) {
            builder = new TemplateBuilder((Template) json);
        } else if (json != null) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Unknown Kubernetes json type:" + json.getClass());
            return false;
        } else {
            return false;
        }
        try {
            if (element instanceof TypeElement) {
                for (ExecutableElement methodElement : ElementFilter.methodsIn(element.getEnclosedElements())) {
                    TypeElement classElement = getClassElement(element);
                    Class<?> cls = Class.forName(classElement.getQualifiedName().toString());
                    final Object instance = cls.newInstance();
                    final String methodName = methodElement.getSimpleName().toString();
                    if (builder instanceof Visitable) {
                        ((Visitable) builder).accept(new Visitor() {

                            @Override
                            public void visit(Object o) {
                                for (Method m : findMethods(instance, methodName, o.getClass())) {
                                    Named named = m.getAnnotation(Named.class);
                                    if (named != null && !Strings.isNullOrBlank(named.value())) {
                                        String objectName = getName(o);
                                        // If a name has been explicitly specified check if there is a match
                                        if (!named.value().equals(objectName)) {
                                            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Named method:" + m.getName() + " with name:" + named.value() + " doesn't match: " + objectName + ", ignoring");
                                            return;
                                        }
                                    }
                                    try {
                                        m.invoke(instance, o);
                                    } catch (IllegalAccessException e) {
                                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
                                    } catch (InvocationTargetException e) {
                                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
                                    }
                                }
                            }
                        });
                    } else {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Json type is not visitable.");
                    }
                }
            }
            json = builder.build();
            generateJson(kubernetesJsonFileName, json);
        } catch (Exception ex) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error creating Kubernetes configuration:" + ex.getMessage());
        }
    }
    return true;
}
Also used : Visitor(io.fabric8.kubernetes.api.builder.Visitor) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TemplateBuilder(io.fabric8.openshift.api.model.TemplateBuilder) ExecutableElement(javax.lang.model.element.ExecutableElement) Visitable(io.fabric8.kubernetes.api.builder.Visitable) KubernetesModelProcessor(io.fabric8.kubernetes.generator.annotation.KubernetesModelProcessor) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template) StringWriter(java.io.StringWriter) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Named(javax.inject.Named) TypeElement(javax.lang.model.element.TypeElement) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with KubernetesResource

use of io.fabric8.kubernetes.api.model.KubernetesResource in project fabric8-maven-plugin by fabric8io.

the class AppCatalogMojo method populateLabelsFromResources.

private void populateLabelsFromResources(KubernetesResource resource, Map<String, String> labels) {
    if (resource instanceof KubernetesList) {
        KubernetesList list = (KubernetesList) resource;
        List<HasMetadata> items = list.getItems();
        if (items != null) {
            for (HasMetadata item : items) {
                populateLabelsFromEntity(item, labels);
            }
        }
    } else if (resource instanceof HasMetadata) {
        HasMetadata entity = (HasMetadata) resource;
        populateLabelsFromEntity(entity, labels);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList)

Example 5 with KubernetesResource

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

Aggregations

KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)5 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)4 Template (io.fabric8.openshift.api.model.Template)3 HashMap (java.util.HashMap)3 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)2 TemplateBuilder (io.fabric8.openshift.api.model.TemplateBuilder)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 Method (java.lang.reflect.Method)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 Visitable (io.fabric8.kubernetes.api.builder.Visitable)1 Visitor (io.fabric8.kubernetes.api.builder.Visitor)1