Search in sources :

Example 51 with TypeDef

use of io.sundr.model.TypeDef in project kubernetes-client by fabric8io.

the class CustomResourceInfo method fromClass.

public static CustomResourceInfo fromClass(Class<? extends CustomResource> customResource) {
    try {
        final CustomResource instance = customResource.getDeclaredConstructor().newInstance();
        final String[] shortNames = CustomResource.getShortNames(customResource);
        final TypeDef definition = Types.typeDefFrom(customResource);
        if (DESCRIBE_TYPE_DEFS) {
            Types.output(definition);
        }
        final Scope scope = Types.isNamespaced(definition) ? Scope.NAMESPACED : Scope.CLUSTER;
        SpecAndStatus specAndStatus = Types.resolveSpecAndStatusTypes(definition);
        if (specAndStatus.isUnreliable()) {
            LOGGER.warn("Cannot reliably determine status types for {} because it isn't parameterized with only spec and status types. Status replicas detection will be deactivated.", customResource.getCanonicalName());
        }
        return new CustomResourceInfo(instance.getGroup(), instance.getVersion(), instance.getKind(), instance.getSingular(), instance.getPlural(), shortNames, instance.isStorage(), instance.isServed(), scope, definition, customResource.getCanonicalName(), specAndStatus.getSpecClassName(), specAndStatus.getStatusClassName());
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw KubernetesClientException.launderThrowable(e);
    }
}
Also used : CustomResource(io.fabric8.kubernetes.client.CustomResource) TypeDef(io.sundr.model.TypeDef) Scope(io.fabric8.kubernetes.model.Scope) SpecAndStatus(io.fabric8.crd.generator.utils.Types.SpecAndStatus) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 52 with TypeDef

use of io.sundr.model.TypeDef in project kubernetes-client by fabric8io.

the class AnnotatedMultiPropertyPathDetector method visit.

@Override
public void visit(TypeDefBuilder builder) {
    TypeDef type = builder.build();
    final List<Property> props = type.getProperties();
    for (Property p : props) {
        if (parents.contains(p)) {
            continue;
        }
        List<Property> newParents = new ArrayList<>(parents);
        boolean match = p.getAnnotations().stream().anyMatch(a -> a.getClassRef().getName().equals(annotationName));
        if (match) {
            newParents.add(p);
            this.properties.put(newParents.stream().map(Property::getName).collect(Collectors.joining(DOT, prefix, "")), p);
        }
    }
    props.stream().filter(p -> p.getTypeRef() instanceof ClassRef).forEach(p -> {
        if (!parents.contains(p)) {
            ClassRef classRef = (ClassRef) p.getTypeRef();
            TypeDef propertyType = Types.typeDefFrom(classRef);
            if (!propertyType.isEnum()) {
                List<Property> newParents = new ArrayList<>(parents);
                newParents.add(p);
                new TypeDefBuilder(propertyType).accept(new AnnotatedMultiPropertyPathDetector(prefix, annotationName, newParents, this.properties)).build();
            }
        }
    });
}
Also used : Types(io.fabric8.crd.generator.utils.Types) List(java.util.List) TypedVisitor(io.sundr.builder.TypedVisitor) Map(java.util.Map) TypeDef(io.sundr.model.TypeDef) Set(java.util.Set) HashMap(java.util.HashMap) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Property(io.sundr.model.Property) Collectors(java.util.stream.Collectors) ClassRef(io.sundr.model.ClassRef) ArrayList(java.util.ArrayList) TypeDef(io.sundr.model.TypeDef) ClassRef(io.sundr.model.ClassRef) ArrayList(java.util.ArrayList) Property(io.sundr.model.Property) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 53 with TypeDef

use of io.sundr.model.TypeDef in project kubernetes-client by fabric8io.

the class JsonSchemaTest method shouldAugmentPropertiesSchemaFromAnnotations.

@Test
void shouldAugmentPropertiesSchemaFromAnnotations() {
    TypeDef annotated = Types.typeDefFrom(Annotated.class);
    JSONSchemaProps schema = JsonSchema.from(annotated);
    assertNotNull(schema);
    Map<String, JSONSchemaProps> properties = schema.getProperties();
    assertEquals(2, properties.size());
    final JSONSchemaProps specSchema = properties.get("spec");
    Map<String, JSONSchemaProps> spec = specSchema.getProperties();
    assertEquals(6, spec.size());
    // check descriptions are present
    assertTrue(spec.containsKey("from-field"));
    JSONSchemaProps prop = spec.get("from-field");
    assertEquals("from-field-description", prop.getDescription());
    assertTrue(spec.containsKey("from-getter"));
    prop = spec.get("from-getter");
    assertEquals("from-getter-description", prop.getDescription());
    // fields without description annotation shouldn't have them
    assertTrue(spec.containsKey("unnamed"));
    assertNull(spec.get("unnamed").getDescription());
    assertTrue(spec.containsKey("emptySetter"));
    assertNull(spec.get("emptySetter").getDescription());
    assertTrue(spec.containsKey("anEnum"));
    // check required list, should register properties with their modified name if needed
    final List<String> required = specSchema.getRequired();
    assertEquals(2, required.size());
    assertTrue(required.contains("emptySetter"));
    assertTrue(required.contains("from-getter"));
    // check the enum values
    final JSONSchemaProps anEnum = spec.get("anEnum");
    final List<JsonNode> enumValues = anEnum.getEnum();
    assertEquals(2, enumValues.size());
    enumValues.stream().map(JsonNode::textValue).forEach(s -> assertTrue("oui".equals(s) || "non".equals(s)));
    // check ignored fields
    assertFalse(spec.containsKey("ignoredFoo"));
    assertFalse(spec.containsKey("ignoredBar"));
}
Also used : TypeDef(io.sundr.model.TypeDef) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 54 with TypeDef

use of io.sundr.model.TypeDef in project kubernetes-client by fabric8io.

the class JsonSchemaTest method shouldExtractPropertiesSchemaFromExtractValueAnnotation.

@Test
void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
    TypeDef extraction = Types.typeDefFrom(Extraction.class);
    JSONSchemaProps schema = JsonSchema.from(extraction);
    assertNotNull(schema);
    Map<String, JSONSchemaProps> properties = schema.getProperties();
    assertEquals(2, properties.size());
    final JSONSchemaProps specSchema = properties.get("spec");
    Map<String, JSONSchemaProps> spec = specSchema.getProperties();
    assertEquals(2, spec.size());
    // check typed SchemaFrom
    JSONSchemaProps foo = spec.get("foo");
    Map<String, JSONSchemaProps> fooProps = foo.getProperties();
    assertNotNull(fooProps);
    // you can change everything
    assertEquals("integer", fooProps.get("BAZ").getType());
    assertTrue(foo.getRequired().contains("BAZ"));
    // you can exclude fields
    assertNull(fooProps.get("baz"));
    // check typed SchemaSwap
    JSONSchemaProps bar = spec.get("bar");
    Map<String, JSONSchemaProps> barProps = bar.getProperties();
    assertNotNull(barProps);
    // you can change everything
    assertEquals("integer", barProps.get("BAZ").getType());
    assertTrue(bar.getRequired().contains("BAZ"));
    // you can exclude fields
    assertNull(barProps.get("baz"));
}
Also used : TypeDef(io.sundr.model.TypeDef) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Test(org.junit.jupiter.api.Test)

Example 55 with TypeDef

use of io.sundr.model.TypeDef in project kubernetes-client by fabric8io.

the class CustomResourceAnnotationProcessor method toCustomResourceInfo.

private CustomResourceInfo toCustomResourceInfo(TypeElement customResource) {
    TypeDef definition = Adapters.adaptType(customResource, AptContext.getContext());
    definition = Types.unshallow(definition);
    if (CustomResourceInfo.DESCRIBE_TYPE_DEFS) {
        Types.output(definition);
    }
    final Name crClassName = customResource.getQualifiedName();
    SpecAndStatus specAndStatus = Types.resolveSpecAndStatusTypes(definition);
    if (specAndStatus.isUnreliable()) {
        System.out.println("Cannot reliably determine status types for  " + crClassName + " because it isn't parameterized with only spec and status types. Status replicas detection will be deactivated.");
    }
    final String group = customResource.getAnnotation(Group.class).value();
    final String version = customResource.getAnnotation(Version.class).value();
    final String kind = Optional.ofNullable(customResource.getAnnotation(Kind.class)).map(Kind::value).orElse(customResource.getSimpleName().toString());
    final String singular = Optional.ofNullable(customResource.getAnnotation(Singular.class)).map(Singular::value).orElse(kind.toLowerCase(Locale.ROOT));
    final String plural = Optional.ofNullable(customResource.getAnnotation(Plural.class)).map(Plural::value).map(s -> s.toLowerCase(Locale.ROOT)).orElse(Pluralize.toPlural(singular));
    final String[] shortNames = Optional.ofNullable(customResource.getAnnotation(ShortNames.class)).map(ShortNames::value).orElse(new String[] {});
    final boolean storage = customResource.getAnnotation(Version.class).storage();
    final boolean served = customResource.getAnnotation(Version.class).served();
    final Scope scope = Types.isNamespaced(definition) ? Scope.NAMESPACED : Scope.CLUSTER;
    return new CustomResourceInfo(group, version, kind, singular, plural, shortNames, storage, served, scope, definition, crClassName.toString(), specAndStatus.getSpecClassName(), specAndStatus.getStatusClassName());
}
Also used : SpecAndStatus(io.fabric8.crd.generator.utils.Types.SpecAndStatus) TypeElement(javax.lang.model.element.TypeElement) CRDGenerator(io.fabric8.crd.generator.CRDGenerator) javax.annotation.processing(javax.annotation.processing) FileObject(javax.tools.FileObject) Pluralize(io.fabric8.kubernetes.api.Pluralize) CustomResourceInfo(io.fabric8.crd.generator.CustomResourceInfo) Diagnostic(javax.tools.Diagnostic) Locale(java.util.Locale) AbstractCRDOutput(io.fabric8.crd.generator.CRDGenerator.AbstractCRDOutput) URI(java.net.URI) Name(javax.lang.model.element.Name) OutputStream(java.io.OutputStream) StandardLocation(javax.tools.StandardLocation) AptContext(io.sundr.adapter.apt.AptContext) CRDGenerationInfo(io.fabric8.crd.generator.CRDGenerationInfo) DefinitionRepository(io.sundr.model.repo.DefinitionRepository) Adapters(io.sundr.adapter.api.Adapters) Set(java.util.Set) Element(javax.lang.model.element.Element) IOException(java.io.IOException) Types(io.fabric8.crd.generator.utils.Types) TypeDef(io.sundr.model.TypeDef) Optional(java.util.Optional) io.fabric8.kubernetes.model.annotation(io.fabric8.kubernetes.model.annotation) Scope(io.fabric8.kubernetes.model.Scope) Name(javax.lang.model.element.Name) TypeDef(io.sundr.model.TypeDef) Scope(io.fabric8.kubernetes.model.Scope) SpecAndStatus(io.fabric8.crd.generator.utils.Types.SpecAndStatus) CustomResourceInfo(io.fabric8.crd.generator.CustomResourceInfo)

Aggregations

TypeDef (io.sundr.model.TypeDef)99 ClassRef (io.sundr.model.ClassRef)43 Test (org.junit.Test)40 RichTypeDef (io.sundr.model.RichTypeDef)38 TypeDefBuilder (io.sundr.model.TypeDefBuilder)35 TypeRef (io.sundr.model.TypeRef)26 Method (io.sundr.model.Method)22 Property (io.sundr.model.Property)22 ArrayList (java.util.ArrayList)20 List (java.util.List)15 TypeElement (javax.lang.model.element.TypeElement)15 Collectors (java.util.stream.Collectors)14 Element (javax.lang.model.element.Element)14 Set (java.util.Set)12 Test (org.junit.jupiter.api.Test)12 AnnotationRef (io.sundr.model.AnnotationRef)11 DefinitionRepository (io.sundr.model.repo.DefinitionRepository)11 HashMap (java.util.HashMap)11 AptContext (io.sundr.adapter.apt.AptContext)10 ClassRefBuilder (io.sundr.model.ClassRefBuilder)10