Skip to content

Commit

Permalink
feature: add a v2 crd generator based on jackson
Browse files Browse the repository at this point in the history
closes: fabric8io#5948

also removes v1beta1 from the new code
  • Loading branch information
shawkins committed Apr 24, 2024
1 parent a8fba97 commit b9891f6
Show file tree
Hide file tree
Showing 134 changed files with 9,334 additions and 20 deletions.
81 changes: 81 additions & 0 deletions crd-generator/api-v2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2015 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>crd-generator-parent</artifactId>
<groupId>io.fabric8</groupId>
<version>6.13-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>crd-generator-api-v2</artifactId>
<name>Fabric8 :: CRD generator :: API V2</name>

<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client-api</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jsonSchema</artifactId>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>generator-annotations</artifactId>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-common</artifactId>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crdv2.generator;

import io.fabric8.crd.generator.annotation.PrinterColumn;
import io.fabric8.crdv2.generator.AbstractJsonSchema.AnnotationMetadata;
import io.fabric8.crdv2.generator.decorator.Decorator;
import io.fabric8.kubernetes.client.utils.Utils;

import java.util.Map;

/**
* This class encapsulates the common behavior between v1beta1 and v1 CRD generation logic. The
* intent is that each CRD spec version is implemented as a sub-class of this one.
*/
public abstract class AbstractCustomResourceHandler {

protected final Resources resources;

protected AbstractCustomResourceHandler(Resources resources) {
this.resources = resources;
}

public abstract void handle(CustomResourceInfo config);

protected void handlePrinterColumns(String name, String version, Map<String, AnnotationMetadata> additionalPrinterColumns) {
additionalPrinterColumns.forEach((path, property) -> {
PrinterColumn printerColumn = ((PrinterColumn)property.annotation);
String column = printerColumn.name();
if (Utils.isNullOrEmpty(column)) {
column = path.substring(path.lastIndexOf("."));
}
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;

resources.decorate(
getPrinterColumnDecorator(name, version, path, property.type, column, description, format, priority));
});
}

/**
* Provides the decorator implementation associated with the CRD generation version.
*
* @param name the resource name
* @param version the associated version
* @param path the path from which the printer column is extracted
* @param type the data type of the printer column
* @param column the name of the column
* @param description the description of the column
* @param format the format of the printer column
* @return the concrete decorator implementing the addition of a printer column to the currently built CRD
*/
protected abstract Decorator<?> getPrinterColumnDecorator(String name, String version, String path,
String type, String column, String description, String format, int priority);

}

0 comments on commit b9891f6

Please sign in to comment.