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);
}
}
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();
}
}
});
}
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"));
}
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"));
}
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());
}
Aggregations