Search in sources :

Example 6 with KubernetesResource

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

the class AppCatalogMojo method populateAnnotationsFromResources.

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

Example 7 with KubernetesResource

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

the class KubernetesProviderProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    CompilationTaskFactory compilationTaskFactory = new CompilationTaskFactory(processingEnv);
    Set<TypeElement> providers = new HashSet<>();
    // 1st pass collect classes to compile.
    for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesProvider.class)) {
        providers.add(getClassElement(element));
    }
    if (providers.isEmpty()) {
        return true;
    }
    StringWriter writer = new StringWriter();
    try {
        Callable<Boolean> compileTask = compilationTaskFactory.create(providers, writer);
        if (!compileTask.call()) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to compile provider classes. See output below.");
            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.
    Map<String, Set> providedMap = new HashMap<>();
    for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesProvider.class)) {
        try {
            if (element instanceof ExecutableElement) {
                Set provided = getProvidedSet(providedMap, element);
                ExecutableElement methodElement = (ExecutableElement) element;
                String methodName = methodElement.getSimpleName().toString();
                TypeElement classElement = getClassElement(element);
                Class<?> cls = Class.forName(classElement.getQualifiedName().toString());
                Object instance = cls.newInstance();
                Method providerMethod = instance.getClass().getDeclaredMethod(methodName);
                if (providerMethod != null) {
                    providerMethod.setAccessible(true);
                    provided.add(providerMethod.invoke(instance));
                }
            }
        } catch (Exception ex) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error creating Kubernetes configuration.");
        }
    }
    for (Map.Entry<String, Set> entry : providedMap.entrySet()) {
        KubernetesResource answer;
        try {
            answer = (KubernetesResource) KubernetesHelper.combineJson(entry.getValue().toArray());
            generateKubernetesManifest(entry.getKey(), answer);
        } catch (Exception e) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to combine provider items");
            return false;
        }
    }
    return true;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Method(java.lang.reflect.Method) StringWriter(java.io.StringWriter) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource)

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 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 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)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 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1