Search in sources :

Example 1 with Types

use of io.fabric8.crd.generator.utils.Types in project che-server by eclipse-che.

the class KubernetesEnvironmentProvisionerTest method setUp.

@BeforeMethod
public void setUp() {
    workspaceConfig = new WorkspaceConfigImpl();
    // "openshift" is what we use in the test devfile files and what we need to test the upgrade
    // and multiple k8s-based types
    Map<String, Set<String>> allowedUpgrades = new HashMap<>();
    allowedUpgrades.compute("openshift", (__, ___) -> new HashSet<>()).add(KubernetesEnvironment.TYPE);
    Set<String> k8sEnvTypes = new HashSet<>();
    k8sEnvTypes.add(KubernetesEnvironment.TYPE);
    k8sEnvTypes.add("openshift");
    k8sEnvProvisioner = new KubernetesEnvironmentProvisioner(k8sRecipeParser, allowedUpgrades, k8sEnvTypes);
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) Mock(org.mockito.Mock) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Assert.assertEquals(org.testng.Assert.assertEquals) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) Files(org.testng.reporters.Files) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Serialization(io.fabric8.kubernetes.client.utils.Serialization) Map(java.util.Map) Service(io.fabric8.kubernetes.api.model.Service) Mockito.doReturn(org.mockito.Mockito.doReturn) Serialization.unmarshal(io.fabric8.kubernetes.client.utils.Serialization.unmarshal) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Collections.emptyMap(java.util.Collections.emptyMap) KubernetesObjectUtil.newPVC(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newPVC) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) IOException(java.io.IOException) Assert.assertNotNull(org.testng.Assert.assertNotNull) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) YAML_CONTENT_TYPE(org.eclipse.che.workspace.infrastructure.kubernetes.devfile.KubernetesEnvironmentProvisioner.YAML_CONTENT_TYPE) List(java.util.List) PROJECTS_VOLUME_NAME(org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) KubernetesRecipeParser(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesRecipeParser) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashSet(java.util.HashSet) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with Types

use of io.fabric8.crd.generator.utils.Types in project devspaces-images by redhat-developer.

the class KubernetesEnvironmentProvisionerTest method setUp.

@BeforeMethod
public void setUp() {
    workspaceConfig = new WorkspaceConfigImpl();
    // "openshift" is what we use in the test devfile files and what we need to test the upgrade
    // and multiple k8s-based types
    Map<String, Set<String>> allowedUpgrades = new HashMap<>();
    allowedUpgrades.compute("openshift", (__, ___) -> new HashSet<>()).add(KubernetesEnvironment.TYPE);
    Set<String> k8sEnvTypes = new HashSet<>();
    k8sEnvTypes.add(KubernetesEnvironment.TYPE);
    k8sEnvTypes.add("openshift");
    k8sEnvProvisioner = new KubernetesEnvironmentProvisioner(k8sRecipeParser, allowedUpgrades, k8sEnvTypes);
}
Also used : MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) KUBERNETES_COMPONENT_TYPE(org.eclipse.che.api.workspace.server.devfile.Constants.KUBERNETES_COMPONENT_TYPE) Arrays(java.util.Arrays) Listeners(org.testng.annotations.Listeners) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) Mock(org.mockito.Mock) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) Assert.assertEquals(org.testng.Assert.assertEquals) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) Files(org.testng.reporters.Files) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Serialization(io.fabric8.kubernetes.client.utils.Serialization) Map(java.util.Map) Service(io.fabric8.kubernetes.api.model.Service) Mockito.doReturn(org.mockito.Mockito.doReturn) Serialization.unmarshal(io.fabric8.kubernetes.client.utils.Serialization.unmarshal) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) Collections.emptyMap(java.util.Collections.emptyMap) KubernetesObjectUtil.newPVC(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.newPVC) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) IOException(java.io.IOException) Assert.assertNotNull(org.testng.Assert.assertNotNull) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) YAML_CONTENT_TYPE(org.eclipse.che.workspace.infrastructure.kubernetes.devfile.KubernetesEnvironmentProvisioner.YAML_CONTENT_TYPE) List(java.util.List) PROJECTS_VOLUME_NAME(org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) KubernetesRecipeParser(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesRecipeParser) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashSet(java.util.HashSet) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with Types

use of io.fabric8.crd.generator.utils.Types in project kubernetes-client by fabric8io.

the class AbstractJsonSchema method getSchemaTypeFor.

public static String getSchemaTypeFor(TypeRef typeRef) {
    String type = COMMON_MAPPINGS.get(typeRef);
    if (type == null && typeRef instanceof ClassRef) {
        // Handle complex types
        ClassRef classRef = (ClassRef) typeRef;
        TypeDef def = Types.typeDefFrom(classRef);
        type = def.isEnum() ? STRING_MARKER : "object";
    }
    return type;
}
Also used : IntOrString(io.fabric8.kubernetes.api.model.IntOrString)

Example 4 with Types

use of io.fabric8.crd.generator.utils.Types in project kubernetes-client by fabric8io.

the class CustomResourceAnnotationProcessor method process.

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        final Messager messager = processingEnv.getMessager();
        final CRDGenerationInfo allCRDs = generator.withOutput(new FileObjectCRDOutput(processingEnv)).detailedGenerate();
        allCRDs.getCRDDetailsPerNameAndVersion().forEach((crdName, versionToInfo) -> {
            messager.printMessage(Diagnostic.Kind.NOTE, "Generating CRD " + crdName + ":\n");
            versionToInfo.forEach((version, info) -> messager.printMessage(Diagnostic.Kind.NOTE, "  - " + version + " -> " + info.getFilePath()));
        });
        return true;
    }
    // make sure we create the context before using it
    AptContext.create(processingEnv.getElementUtils(), processingEnv.getTypeUtils(), DefinitionRepository.getRepository());
    // Collect all annotated types.
    for (TypeElement annotation : annotations) {
        for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
            if (element instanceof TypeElement) {
                generator.customResources(toCustomResourceInfo((TypeElement) element));
            }
        }
    }
    return false;
}
Also used : CRDGenerationInfo(io.fabric8.crd.generator.CRDGenerationInfo) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element)

Example 5 with Types

use of io.fabric8.crd.generator.utils.Types in project kubernetes-client by fabric8io.

the class JObject method generateJava.

@Override
public GeneratorResult generateJava() {
    CompilationUnit cu = new CompilationUnit();
    if (!this.pkg.isEmpty()) {
        cu.setPackageDeclaration(this.pkg);
    }
    ClassOrInterfaceDeclaration clz = cu.addClass(this.className);
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonInclude"), new NameExpr("com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL")));
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonPropertyOrder"), new NameExpr(getSortedFieldsAsParam(this.fields.keySet()))));
    clz.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.databind.annotation.JsonDeserialize"), new NameExpr("using = com.fasterxml.jackson.databind.JsonDeserializer.None.class")));
    if (config.isObjectExtraAnnotations()) {
        addExtraAnnotations(clz);
    }
    clz.addImplementedType("io.fabric8.kubernetes.api.model.KubernetesResource");
    List<GeneratorResult.ClassResult> buffer = new ArrayList<>(this.fields.size() + 1);
    List<String> sortedKeys = this.fields.keySet().stream().sorted().collect(Collectors.toList());
    for (String k : sortedKeys) {
        AbstractJSONSchema2Pojo prop = this.fields.get(k);
        boolean isRequired = this.required.contains(k);
        GeneratorResult gr = prop.generateJava();
        // For now the inner types are only for enums
        if (!gr.getInnerClasses().isEmpty()) {
            for (GeneratorResult.ClassResult enumCR : gr.getInnerClasses()) {
                Optional<EnumDeclaration> ed = enumCR.getCompilationUnit().getEnumByName(enumCR.getName());
                if (ed.isPresent()) {
                    clz.addMember(ed.get());
                }
            }
        }
        buffer.addAll(gr.getTopLevelClasses());
        String originalFieldName = k;
        String fieldName = AbstractJSONSchema2Pojo.sanitizeString(k);
        String fieldType = prop.getType();
        try {
            FieldDeclaration objField = clz.addField(fieldType, fieldName, Modifier.Keyword.PRIVATE);
            objField.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonProperty"), new StringLiteralExpr(originalFieldName)));
            if (isRequired) {
                objField.addAnnotation("javax.validation.constraints.NotNull");
            }
            objField.createGetter();
            objField.createSetter();
            if (Utils.isNotNullOrEmpty(prop.getDescription())) {
                objField.setJavadocComment(prop.getDescription());
                objField.addAnnotation(new SingleMemberAnnotationExpr(new Name("com.fasterxml.jackson.annotation.JsonPropertyDescription"), new StringLiteralExpr(prop.getDescription().replace("\"", "\\\""))));
            }
        } catch (Exception cause) {
            throw new JavaGeneratorException("Error generating field " + fieldName + " with type " + prop.getType(), cause);
        }
    }
    if (this.preserveUnknownFields || config.isAlwaysPreserveUnknownFields()) {
        ClassOrInterfaceType mapType = new ClassOrInterfaceType().setName(Keywords.JAVA_UTIL_MAP).setTypeArguments(new ClassOrInterfaceType().setName("String"), new ClassOrInterfaceType().setName("Object"));
        FieldDeclaration objField = clz.addField(mapType, Keywords.ADDITIONAL_PROPERTIES, Modifier.Keyword.PRIVATE);
        objField.setVariables(new NodeList<>(new VariableDeclarator().setName(Keywords.ADDITIONAL_PROPERTIES).setType(mapType).setInitializer("new java.util.HashMap<>()")));
        objField.addAnnotation("com.fasterxml.jackson.annotation.JsonIgnore");
        objField.createGetter().addAnnotation("com.fasterxml.jackson.annotation.JsonAnyGetter");
        objField.createSetter().addAnnotation("com.fasterxml.jackson.annotation.JsonAnySetter");
    }
    buffer.add(new GeneratorResult.ClassResult(this.className, cu));
    return new GeneratorResult(buffer);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException)

Aggregations

IOException (java.io.IOException)6 List (java.util.List)6 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)5 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)4 DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)4 Template (io.fabric8.openshift.api.model.Template)4 InlineLogging (io.strimzi.api.kafka.model.InlineLogging)4 KafkaClusterSpec (io.strimzi.api.kafka.model.KafkaClusterSpec)4 Logging (io.strimzi.api.kafka.model.Logging)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)3 Pod (io.fabric8.kubernetes.api.model.Pod)3 Service (io.fabric8.kubernetes.api.model.Service)3 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)3 TemplateBuilder (io.fabric8.openshift.api.model.TemplateBuilder)3 Storage (io.strimzi.api.kafka.model.storage.Storage)3 MetricsAndLogging (io.strimzi.operator.common.MetricsAndLogging)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3