Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(crd-generator): Add CRD-Generator CLI #9

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
cc94fd8
feat(crd-generator): Add CRD-Generator Maven plugin
baloo42 Apr 25, 2024
d75c228
Add crd-generator-collector module
baloo42 May 3, 2024
dd1d5a2
Refactor crd-generator-maven-plugin to use collector module
baloo42 May 3, 2024
72a8a88
Adjusted log levels and allow to force create indices
baloo42 May 3, 2024
193caa5
Enable parallel by default in crd-generator maven plugin
baloo42 May 3, 2024
2070892
Remove not necessary classes from base index
baloo42 May 3, 2024
35edc65
Cleanup
baloo42 May 3, 2024
8f765a6
More cleanup
baloo42 May 3, 2024
a406aff
Fix base index (must also include CustomResource)
baloo42 May 3, 2024
ff7129d
Fix includes/excludes and some code smells
baloo42 May 4, 2024
2452fd1
Add CustomResourceCollector tests
baloo42 May 4, 2024
10c969a
Cleanup, add tests and fix code smells
baloo42 May 7, 2024
50c6b8a
Add tests for crd-generator-maven-plugin and fix code smells
baloo42 May 8, 2024
bf09906
Fix code smells
baloo42 May 8, 2024
bac6f11
Remove high level filtering and return an array of cr classes instead…
baloo42 May 8, 2024
b0ca124
Refactor Jandex utility methods to be more reusable
baloo42 May 9, 2024
c24015a
Polish var names, add javadoc, improve error handling, refactor tests
baloo42 May 10, 2024
3b9a839
Fix CrdGeneratorMojoTest
baloo42 May 12, 2024
018b14b
Fix code smells
baloo42 May 12, 2024
e0251c0
Try to fix tests
baloo42 May 12, 2024
52baf88
Use same jandex version for core module and maven plugin
baloo42 May 13, 2024
ef4cbc6
Add README for crd-generator maven plugin
baloo42 May 14, 2024
c9febb6
Fix README
baloo42 May 15, 2024
f256923
Add integration tests using maven-invoker plugin
baloo42 May 15, 2024
761cbcc
Add more integration tests and verify content
baloo42 May 15, 2024
d18dc79
Add integration tests for intermediate custom resource classes
baloo42 May 15, 2024
072ab0a
Remove CrdGeneratorMojoTest - already covered by integration test
baloo42 May 15, 2024
c561a71
Fix ClasspathTypeTest
baloo42 May 15, 2024
88f1aed
Fix groovy files formatting
baloo42 May 16, 2024
3f27bbb
Fix version after rebase
baloo42 May 30, 2024
a60d362
Reimplement integration tests to be OS independent
baloo42 May 30, 2024
cbd8148
Add collection input methods to CRDGenerator
baloo42 May 31, 2024
d7ad811
Refactor CustomResourceCollector to use only a list as output
baloo42 May 31, 2024
111b8d7
Fix integration tests for Java 8
baloo42 Jul 2, 2024
15d3c63
Make file comparison in integration tests OS independent
baloo42 Jul 3, 2024
c4ab0d1
Add groovy formatter settings to parent pom and remove crd-generator …
baloo42 Jul 3, 2024
c4b93b1
Make reset methods package-private
baloo42 Jul 3, 2024
0809f9d
Add @SuppressWarnings("UnusedReturnValue") to get rid of all warnings
baloo42 Jul 3, 2024
18839b6
Improve log message
baloo42 Jul 3, 2024
c1e0f06
Fix jandex version after rebase
baloo42 Aug 27, 2024
e9e5839
feat(crd-generator): Add CRD-Generator CLI
baloo42 May 3, 2024
3797a87
Implement missing CLI options and polish interface
baloo42 May 6, 2024
978b410
Remove high-level filtering
baloo42 May 9, 2024
32a6de9
Fix after rebase
baloo42 May 12, 2024
199952d
Fix parameter descriptions and add README
baloo42 May 14, 2024
a1dcd00
Fix version after rebase
baloo42 May 30, 2024
ef5172d
Fix CRDGeneratorCLI after rebase
baloo42 May 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -42,7 +43,6 @@
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CRDGenerator {

Expand Down Expand Up @@ -112,7 +112,27 @@ Map<String, AbstractCustomResourceHandler> getHandlers() {
// (we also cannot use @SafeVarargs, because that requires the method to be final, which is another signature change)
@SuppressWarnings("unchecked")
public final CRDGenerator customResourceClasses(Class<? extends HasMetadata>... crClasses) {
return customResources(Stream.of(crClasses).map(CustomResourceInfo::fromClass).toArray(CustomResourceInfo[]::new));
if (crClasses == null) {
return this;
}
return customResourceClasses(Arrays.asList(crClasses));
}

public CRDGenerator customResourceClasses(Collection<Class<? extends HasMetadata>> crClasses) {
if (crClasses == null) {
return this;
}
return customResources(crClasses.stream()
.filter(Objects::nonNull)
.map(CustomResourceInfo::fromClass)
.toArray(CustomResourceInfo[]::new));
}

public CRDGenerator customResources(Collection<CustomResourceInfo> infos) {
if (infos == null) {
return this;
}
return customResources(infos.toArray(new CustomResourceInfo[0]));
}

public CRDGenerator customResources(CustomResourceInfo... infos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.fabric8.crdv2.example.nocyclic.NoCyclic;
import io.fabric8.crdv2.example.simplest.Simplest;
import io.fabric8.crdv2.generator.CRDGenerator.AbstractCRDOutput;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceColumnDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionNames;
Expand All @@ -51,6 +52,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -109,12 +111,27 @@ void addingCustomResourceInfosShouldWork() {
generator.customResources();
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResources(null);
generator.customResources((CustomResourceInfo[]) null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResources((CustomResourceInfo) null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResources((Collection<CustomResourceInfo>) null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResources(null, null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResourceClasses((Class<? extends HasMetadata>[]) null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResourceClasses((Class<? extends HasMetadata>) null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResourceClasses((Collection<Class<? extends HasMetadata>>) null);
assertTrue(generator.getCustomResourceInfos().isEmpty());

generator.customResourceClasses(Simplest.class);
assertEquals(1, generator.getCustomResourceInfos().size());
assertTrue(generator.getCustomResourceInfos().stream().allMatch(cri -> cri.crClassName().equals(Simplest.class.getName())));
Expand Down
65 changes: 65 additions & 0 deletions crd-generator/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# CRD-Generator CLI

Generate CRDs from Java model.

## Install

The CRD-Generator CLI is available for download on Sonatype at the link:

```
https://oss.sonatype.org/content/repositories/releases/io/fabric8/crd-generator-cli/<version>/crd-generator-cli-<version>.sh
```

you can get it working locally with few lines:

```bash
export VERSION=$(wget -q -O - https://github.com/fabric8io/kubernetes-client/releases/latest --header "Accept: application/json" | jq -r '.tag_name' | cut -c 2-)
wget https://oss.sonatype.org/content/repositories/releases/io/fabric8/crd-generator-cli/$VERSION/crd-generator-cli-$VERSION.sh
chmod a+x crd-generator-cli-$VERSION.sh
./crd-generator-cli-$VERSION.sh --version
```

Alternatively, if you already have [jbang](https://www.jbang.dev/) installed, you can run the CLI by using the following command:

```bash
jbang io.fabric8:crd-generator-cli:<version>
```

## Usage

```
Usage: crd-gen [-hVv] [--force-index] [--force-scan]
[--implicit-preserve-unknown-fields] [--no-parallel]
[-o=<outputDirectory>] [--classpath=<classpathElements>]...
[--exclude-package=<excludedPackages>]...
[--include-package=<includedPackages>]... <source>...

Fabric8 CRD-Generator:
Generate Custom Resource Definitions (CRD) for Kubernetes from Java model.

<source>... A directory or JAR file to scan for Custom Resource
classes, or a full qualified Custom Resource class name.
--classpath=<classpathElements>
Additional classpath elements, e.g. a dependency packaged
as JAR file or a directory of class files.
-o, --output-dir=<outputDirectory>
The output directory for the generated CRDs.
Default: .
--force-index Create Jandex index even if the directory or JAR file
contains an existing index.
--force-scan Create Jandex index even if the directory or JAR file
contains an existing index.
--no-parallel Disable parallel generation.
--implicit-preserve-unknown-fields
`x-kubernetes-preserve-unknown-fields: true` will be
added to objects which contain an any-setter or
any-getter
--include-package=<includedPackages>
Use only Custom Resource classes of one or more packages.
--exclude-package=<excludedPackages>
Exclude Custom Resource classes by package.
-v Verbose mode. Helpful for troubleshooting. Multiple -v
options increase the verbosity.
-h, --help Show this help message and exit.
-V, --version Print version information and exit.
```
135 changes: 135 additions & 0 deletions crd-generator/cli/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?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>7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>crd-generator-cli</artifactId>
<name>Fabric8 :: CRD generator :: CLI</name>

<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-collector</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>

<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>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>info.picocli</groupId>
<artifactId>picocli-codegen</artifactId>
<version>${picocli.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Aproject=${project.groupId}/${project.artifactId}</arg>
</compilerArgs>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.fabric8.crd.generator.cli.CRDGeneratorCLI</mainClass>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

<!-- now make the jar chmod +x style executable -->
<plugin>
<groupId>org.skife.maven</groupId>
<artifactId>really-executable-jar-maven-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<flags>-Xmx1G</flags>
<programFile>crd-gen</programFile>
<attachProgramFile>true</attachProgramFile>
</configuration>

<executions>
<execution>
<phase>package</phase>
<goals>
<goal>really-executable-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading