Skip to content

Commit

Permalink
adding back a common method for printer columns
Browse files Browse the repository at this point in the history
also using string instead of object or array types
  • Loading branch information
shawkins committed Apr 30, 2024
1 parent abe9a2a commit 006f6a7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package io.fabric8.crdv2.generator;

import io.fabric8.crd.generator.annotation.PrinterColumn;
import io.fabric8.crdv2.generator.AbstractJsonSchema.AnnotationMetadata;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TreeMap;
import java.util.stream.Stream;

/**
Expand All @@ -25,8 +31,43 @@
*/
public abstract class AbstractCustomResourceHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractJsonSchema.class);

public abstract void handle(CustomResourceInfo config, ResolvingContext resolvingContext);

public interface PrinterColumnHandler {
void addPrinterColumn(String path, String column, String format,
int priority, String type, String description);
}

protected void handlePrinterColumns(AbstractJsonSchema<?, ?> resolver, PrinterColumnHandler handler) {
TreeMap<String, AnnotationMetadata> sortedCols = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
sortedCols.putAll(resolver.getAllPaths(PrinterColumn.class));
sortedCols.forEach((path, property) -> {
PrinterColumn printerColumn = ((PrinterColumn) property.annotation);
String column = printerColumn.name();
if (Utils.isNullOrEmpty(column)) {
column = path.substring(path.lastIndexOf(".") + 1).toUpperCase();
}
String format = printerColumn.format();
format = Utils.isNotNullOrEmpty(format) ? format : null;
int priority = printerColumn.priority();
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";
}

// TODO: add description to the annotation? The previous logic considered the comments, which are not available here
String description = property.schema.getDescription();
description = Utils.isNotNullOrEmpty(description) ? description : null;

handler.addPrinterColumn(path, column, format, priority, type, description);
});
}

public abstract Stream<HasMetadata> finish();

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ public abstract class AbstractJsonSchema<T extends KubernetesJSONSchemaProps, V

public static class AnnotationMetadata {
public final Annotation annotation;
public final String description;
public final String type;
public final KubernetesJSONSchemaProps schema;

public AnnotationMetadata(Annotation annotation, String description, String type) {
public AnnotationMetadata(Annotation annotation, KubernetesJSONSchemaProps schema) {
this.annotation = annotation;
this.description = description;
this.type = type;
this.schema = schema;
}
}

Expand Down Expand Up @@ -339,16 +337,16 @@ private T resolveObject(LinkedHashMap<String, String> visited, InternalSchemaSwa

T schema = resolveProperty(visited, schemaSwaps, name, type, propertySchema);

propertyMetadata.updateSchema(schema);

if (!swapResult.onGoing) {
for (Entry<Class<? extends Annotation>, LinkedHashMap<String, AnnotationMetadata>> entry : pathMetadata.entrySet()) {
ofNullable(beanProperty.getAnnotation(entry.getKey())).ifPresent(
ann -> entry.getValue().put(toFQN(savedVisited, name),
new AnnotationMetadata(ann, propertyMetadata.description, schema.getType())));
new AnnotationMetadata(ann, schema)));
}
}

propertyMetadata.updateSchema(schema);

visited = savedVisited;

addProperty(name, objectSchema, schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public interface KubernetesJSONSchemaProps {

String getType();

String getFormat();

String getDescription();

void setXKubernetesPreserveUnknownFields(Boolean b);

void setMaximum(Double max);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package io.fabric8.crdv2.generator.v1;

import io.fabric8.crd.generator.annotation.PrinterColumn;
import io.fabric8.crdv2.generator.AbstractCustomResourceHandler;
import io.fabric8.crdv2.generator.AbstractJsonSchema.AnnotationMetadata;
import io.fabric8.crdv2.generator.CRDUtils;
import io.fabric8.crdv2.generator.CustomResourceInfo;
import io.fabric8.crdv2.generator.ResolvingContext;
Expand All @@ -36,7 +34,6 @@
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -65,28 +62,18 @@ public void handle(CustomResourceInfo config, ResolvingContext resolvingContext)
.withOpenAPIV3Schema(schema)
.endSchema();

TreeMap<String, AnnotationMetadata> sortedCols = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
sortedCols.putAll(resolver.getAllPaths(PrinterColumn.class));
sortedCols.forEach((path, property) -> {
PrinterColumn printerColumn = ((PrinterColumn) property.annotation);
String column = printerColumn.name();
if (Utils.isNullOrEmpty(column)) {
column = path.substring(path.lastIndexOf(".") + 1).toUpperCase();
handlePrinterColumns(resolver, new PrinterColumnHandler() {
@Override
public void addPrinterColumn(String path, String column, String format, int priority, String type, String description) {
builder.addNewAdditionalPrinterColumn()
.withType(type)
.withName(column)
.withJsonPath(path)
.withFormat(Utils.isNotNullOrEmpty(format) ? format : null)
.withDescription(Utils.isNotNullOrEmpty(description) ? description : null)
.withPriority(priority)
.endAdditionalPrinterColumn();
}
String format = printerColumn.format();
int priority = printerColumn.priority();

// TODO: add description to the annotation? The previous logic considered the comments, which are not available here
String description = property.description;

builder.addNewAdditionalPrinterColumn()
.withType(property.type)
.withName(column)
.withJsonPath(path)
.withFormat(Utils.isNotNullOrEmpty(format) ? format : null)
.withDescription(Utils.isNotNullOrEmpty(description) ? description : null)
.withPriority(priority)
.endAdditionalPrinterColumn();
});

resolver.getSinglePath(SpecReplicas.class).ifPresent(path -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ void jokerequestCRDShouldWork() {
assertEquals("jokeCategory", columnDefinition.getName());
assertEquals(1, columnDefinition.getPriority());
columnDefinition = printerColumns.get(1);
assertEquals("array", columnDefinition.getType());
assertEquals("string", columnDefinition.getType());
assertEquals(".spec.excluded", columnDefinition.getJsonPath());
assertEquals("excludedTopics", columnDefinition.getName());
assertEquals(0, columnDefinition.getPriority());
Expand Down

0 comments on commit 006f6a7

Please sign in to comment.