Search in sources :

Example 11 with Generator

use of io.fabric8.maven.generator.api.Generator in project fabric8-maven-plugin by fabric8io.

the class BaseGeneratorTest method defaultAddFrom.

@Test
public void defaultAddFrom() {
    Properties props = new Properties();
    for (boolean isOpenShift : new Boolean[] { false, true }) {
        for (boolean isRedHat : new Boolean[] { false, true }) {
            for (TestFromSelector selector : new TestFromSelector[] { null, new TestFromSelector(ctx, isRedHat) }) {
                for (String from : new String[] { null, "openshift/testfrom" }) {
                    setupContext(props, isOpenShift, from, null);
                    BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder();
                    BaseGenerator generator = createGenerator(selector);
                    generator.addFrom(builder);
                    BuildImageConfiguration config = builder.build();
                    if (isRedHat && isOpenShift && selector != null) {
                        if (from != null) {
                            assertEquals("testfrom:latest", config.getFrom());
                            assertFromExt(config.getFromExt(), "testfrom:latest", "openshift");
                        } else {
                            assertEquals("selectorIstagFromRedhat", config.getFrom());
                            assertFromExt(config.getFromExt(), selector.getIstagFrom(), "openshift");
                        }
                    } else {
                        if (from != null) {
                            assertEquals(config.getFrom(), from);
                            assertNull(config.getFromExt());
                        } else {
                            System.out.println(isRedHat + " " + isOpenShift);
                            assertNull(config.getFromExt());
                            assertEquals(config.getFrom(), selector != null ? (isOpenShift ? selector.getS2iBuildFrom() : selector.getDockerBuildFrom()) : null);
                        }
                    }
                }
            }
        }
    }
}
Also used : Properties(java.util.Properties) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) Test(org.junit.Test)

Example 12 with Generator

use of io.fabric8.maven.generator.api.Generator in project fabric8-maven-plugin by fabric8io.

the class BaseGeneratorTest method addLatestTagIfSnapshot.

@Test
public void addLatestTagIfSnapshot() {
    new Expectations() {

        {
            ctx.getProject();
            result = project;
            project.getVersion();
            result = "1.2-SNAPSHOT";
        }
    };
    BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder();
    BaseGenerator generator = createGenerator(null);
    generator.addLatestTagIfSnapshot(builder);
    ;
    BuildImageConfiguration config = builder.build();
    List<String> tags = config.getTags();
    assertEquals(1, tags.size());
    assertTrue(tags.get(0).endsWith("latest"));
}
Also used : Expectations(mockit.Expectations) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) Test(org.junit.Test)

Example 13 with Generator

use of io.fabric8.maven.generator.api.Generator in project fabric8-maven-plugin by fabric8io.

the class WatchMojo method customizeConfig.

@Override
public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs) {
    try {
        Fabric8ServiceHub serviceHub = getFabric8ServiceHub();
        GeneratorContext ctx = new GeneratorContext.Builder().config(extractGeneratorConfig()).project(project).session(session).goalFinder(goalFinder).goalName("fabric8:watch").logger(log).mode(mode).strategy(buildStrategy).useProjectClasspath(useProjectClasspath).artifactResolver(serviceHub.getArtifactResolverService()).build();
        return GeneratorManager.generate(configs, ctx, false);
    } catch (MojoExecutionException e) {
        throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Fabric8ServiceHub(io.fabric8.maven.core.service.Fabric8ServiceHub) GeneratorContext(io.fabric8.maven.generator.api.GeneratorContext)

Example 14 with Generator

use of io.fabric8.maven.generator.api.Generator 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 15 with Generator

use of io.fabric8.maven.generator.api.Generator in project fabric8-maven-plugin by fabric8io.

the class BaseGeneratorTest method addFromInvalidMode.

@Test
public void addFromInvalidMode() {
    try {
        Properties props = new Properties();
        setupContextKubernetes(props, null, "blub");
        BuildImageConfiguration.Builder builder = new BuildImageConfiguration.Builder();
        BaseGenerator generator = createGenerator(null);
        generator.addFrom(builder);
        fail();
    } catch (IllegalArgumentException exp) {
        assertTrue(exp.getMessage().contains("fromMode"));
        assertTrue(exp.getMessage().contains("test-generator"));
    }
}
Also used : Properties(java.util.Properties) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)7 GeneratorContext (io.fabric8.maven.generator.api.GeneratorContext)7 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)6 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)5 Properties (java.util.Properties)5 KubernetesModelGenerator (com.github.isdream.chameleon.container.kubernetes.KubernetesModelGenerator)4 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)4 ArrayList (java.util.ArrayList)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 MavenProject (org.apache.maven.project.MavenProject)3 KubernetesModelGenerator (com.github.isdream.jointware.kubernetes.KubernetesModelGenerator)2 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)2 StringWriter (java.io.StringWriter)2 Method (java.lang.reflect.Method)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 Visitable (io.fabric8.kubernetes.api.builder.Visitable)1 Visitor (io.fabric8.kubernetes.api.builder.Visitor)1 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)1 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)1