diff --git a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractCustomResourceHandler.java b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractCustomResourceHandler.java index 32c21eec738..2fd94ca99fd 100644 --- a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractCustomResourceHandler.java +++ b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/AbstractCustomResourceHandler.java @@ -16,6 +16,7 @@ package io.fabric8.crdv2.generator; import io.fabric8.crd.generator.annotation.AdditionalPrinterColumn; +import io.fabric8.crd.generator.annotation.AdditionalPrinterColumn.Format; import io.fabric8.crd.generator.annotation.PrinterColumn; import io.fabric8.crdv2.generator.AbstractJsonSchema.AnnotationMetadata; import io.fabric8.kubernetes.api.model.HasMetadata; @@ -48,42 +49,32 @@ protected void handlePrinterColumns(AbstractJsonSchema resolver, PrinterCo resolver.getAdditionalPrinterColumns().forEach(apc -> sortedCols.put(apc.path(), new AnnotationMetadata(apc, null))); sortedCols.putAll(resolver.getAllPaths(PrinterColumn.class)); sortedCols.forEach((path, property) -> { - String column; - String type; - String format; - int priority; - String description; if (property.annotation instanceof AdditionalPrinterColumn) { AdditionalPrinterColumn printerColumn = ((AdditionalPrinterColumn) property.annotation); - column = printerColumn.name(); - format = printerColumn.format(); - priority = printerColumn.priority(); - type = printerColumn.getType(); - description = printerColumn.getDescription(); + String column = printerColumn.name(); + String format = printerColumn.format() == Format.NONE ? null : printerColumn.format().getValue(); + String type = printerColumn.type().getValue(); + int priority = printerColumn.priority(); + String description = printerColumn.getDescription(); + handler.addPrinterColumn(path, column, format, priority, type, description); } else { PrinterColumn printerColumn = ((PrinterColumn) property.annotation); - column = printerColumn.name(); - format = printerColumn.format(); - priority = printerColumn.priority(); - type = property.schema.getType(); + String column = printerColumn.name(); + String format = printerColumn.format(); + format = Utils.isNotNullOrEmpty(format) ? format : null; + String type = property.schema.getType(); + if ("object".equals(type) || "array".equals(type)) { + LOGGER.warn("Printer column '{}' has a type '{}' that is not allowed, will use string intead", column, type); + type = "string"; + } else if ("string".equals(type) && "date".equals(property.schema.getFormat())) { + type = "date"; + } + int priority = printerColumn.priority(); // TODO: add description to the annotation? The previous logic considered the comments, which are not available here - description = property.schema.getDescription(); + String description = property.schema.getDescription(); + handler.addPrinterColumn(path, column, format, priority, type, description); } - - if (Utils.isNullOrEmpty(column)) { - column = path.substring(path.lastIndexOf(".") + 1).toUpperCase(); - } - format = Utils.isNotNullOrEmpty(format) ? format : null; - if ("object".equals(type) || "array".equals(type)) { - LOGGER.warn("Printer column '{}' has a type '{}' that is not allowed, will use string intead", column, type); - type = "string"; - } else if ("string".equals(type) && "date".equals(property.schema.getFormat())) { - type = "date"; - } - description = Utils.isNotNullOrEmpty(description) ? description : null; - - handler.addPrinterColumn(path, column, format, priority, type, description); }); } diff --git a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/v1/CustomResourceHandler.java b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/v1/CustomResourceHandler.java index 7088c8f0292..b55ee48564b 100644 --- a/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/v1/CustomResourceHandler.java +++ b/crd-generator/api-v2/src/main/java/io/fabric8/crdv2/generator/v1/CustomResourceHandler.java @@ -69,6 +69,11 @@ public void handle(CustomResourceInfo config, ResolvingContext resolvingContext) handlePrinterColumns(resolver, new PrinterColumnHandler() { @Override public void addPrinterColumn(String path, String column, String format, int priority, String type, String description) { + if (Utils.isNullOrEmpty(column)) { + column = path.substring(path.lastIndexOf(".") + 1).toUpperCase(); + } + description = Utils.isNotNullOrEmpty(description) ? description : null; + builder.addNewAdditionalPrinterColumn() .withType(type) .withName(column) diff --git a/crd-generator/api-v2/src/test/java/io/fabric8/crdv2/example/joke/JokeRequest.java b/crd-generator/api-v2/src/test/java/io/fabric8/crdv2/example/joke/JokeRequest.java index 579fd98523b..1d0b472989a 100644 --- a/crd-generator/api-v2/src/test/java/io/fabric8/crdv2/example/joke/JokeRequest.java +++ b/crd-generator/api-v2/src/test/java/io/fabric8/crdv2/example/joke/JokeRequest.java @@ -16,6 +16,7 @@ package io.fabric8.crdv2.example.joke; import io.fabric8.crd.generator.annotation.AdditionalPrinterColumn; +import io.fabric8.crd.generator.annotation.AdditionalPrinterColumn.Type; import io.fabric8.kubernetes.api.model.Namespaced; import io.fabric8.kubernetes.client.CustomResource; import io.fabric8.kubernetes.model.annotation.Group; @@ -25,7 +26,7 @@ @Group("samples.javaoperatorsdk.io") @Version("v1alpha1") @ShortNames("jr") -@AdditionalPrinterColumn(name = "Age", path = ".metadata.creationTimestamp", getType = "date") +@AdditionalPrinterColumn(name = "Age", path = ".metadata.creationTimestamp", type = Type.DATE) public class JokeRequest extends CustomResource implements Namespaced { } diff --git a/generator-annotations/src/main/java/io/fabric8/crd/generator/annotation/AdditionalPrinterColumn.java b/generator-annotations/src/main/java/io/fabric8/crd/generator/annotation/AdditionalPrinterColumn.java index eec405bd94e..9bc46f7aadf 100644 --- a/generator-annotations/src/main/java/io/fabric8/crd/generator/annotation/AdditionalPrinterColumn.java +++ b/generator-annotations/src/main/java/io/fabric8/crd/generator/annotation/AdditionalPrinterColumn.java @@ -22,27 +22,74 @@ import java.lang.annotation.Target; /** - * Defines an additional printer column. Must be placed at the root of the custom resource. + * Defines an additional printer column. Must be placed at the root of the + * custom resource. */ @Repeatable(AdditionalPrinterColumns.class) @Target({ ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) public @interface AdditionalPrinterColumn { + //https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#type + enum Type { + + STRING("string"), + INTEGER("integer"), + NUMBER("number"), + BOOLEAN("boolean"), + DATE("date"); + + public final String value; + + Type(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + + // https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#format + enum Format { + + NONE(""), + INT32("int32"), + INT64("int64"), + FLOAT("float"), + DOUBLE("double"), + BYTE("byte"), + BINARY("binary"), + DATE("date"), + DATE_TIME("date-time"), + PASSWORD("password"); + + public final String value; + + Format(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + /** - * The name of the column. - * An empty column name implies the use of the last path element + * The name of the column. An empty column name implies the use of the last path + * element * - * @return the column name, or empty string if the last path element should be used. + * @return the column name, or empty string if the last path element should be + * used. */ String name() default ""; /** * The printer column format. * - * @return the format or empty string if no format is specified. + * @return the format or NONE if no format is specified. */ - String format() default ""; + Format format() default Format.NONE; /** * The printer column priority. @@ -63,7 +110,7 @@ * * @return the type */ - String getType(); + Type type() default Type.STRING; /** * The description of the printer column