Skip to content

Commit

Permalink
switching to enums for format and type
Browse files Browse the repository at this point in the history
based upon Bernhard Strähle's review

Signed-off-by: Steve Hawkins <[email protected]>
  • Loading branch information
shawkins committed Oct 1, 2024
1 parent d8d7ec0 commit e9383e0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<JokeRequestSpec, JokeRequestStatus> implements Namespaced {

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,73 @@
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"),
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.
Expand All @@ -63,7 +109,7 @@
*
* @return the type
*/
String getType();
Type type() default Type.STRING;

/**
* The description of the printer column
Expand Down

0 comments on commit e9383e0

Please sign in to comment.