Skip to content

Commit

Permalink
feat(crd-generator): Add CRD-Generator CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo42 committed Sep 7, 2024
1 parent 7f12dbe commit 97841e9
Show file tree
Hide file tree
Showing 19 changed files with 1,293 additions and 3 deletions.
98 changes: 98 additions & 0 deletions crd-generator/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# CRD-Generator CLI

Generate Custom Resource Definitions (CRD) for Kubernetes from Java classes.

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

Download the latest version with the following commands:

```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 -O crd-gen https://oss.sonatype.org/content/repositories/releases/io/fabric8/crd-generator-cli/$VERSION/crd-generator-cli-$VERSION.sh
chmod a+x crd-gen
./crd-gen --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

```
crd-gen [-hVv] [--disable-parallel] [--force-index] [--force-scan] [--implicit-preserve-unknown-fields]
[-o=<outputDirectory>] [-cp=<classpathElement>]... [--exclude-package=<package>]...
[--include-package=<package>]... <source>...
Description:
Fabric8 CRD-Generator
Generate Custom Resource Definitions (CRD) for Kubernetes from Java classes.
Parameters:
<source>... A directory or JAR file to scan for Custom Resource classes, or a full qualified Custom
Resource class name.
Options:
-o, --output-dir=<outputDirectory>
The output directory where the CRDs are emitted.
Default: .
-cp, --classpath=<classpathElement>
Additional classpath element, e.g. a dependency packaged as JAR file or a directory of class
files.
--force-index Create Jandex index even if the directory or JAR file contains an existing index.
--force-scan Scan directories and JAR files even if Custom Resource classes are given.
--disable-parallel Disable parallel generation of CRDs.
--implicit-preserve-unknown-fields
`x-kubernetes-preserve-unknown-fields: true` will be added on objects which contain an
any-setter or any-getter.
--include-package=<package>
Filter Custom Resource classes after scanning by package inclusions.
--exclude-package=<package>
Filter Custom Resource classes after scanning by package exclusions.
-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.
Exit Codes:
0 Successful execution
1 Unexpected error
2 Invalid input
70 Custom Resource class loading failed
80 No Custom Resource classes retained after filtering
```

### Examples

**Generate CRDs for custom resource classes in the directory:**

```bash
crd-gen target/classes/
```

**Generate CRDs for custom resource classes in the JAR file:**

```bash
crd-gen my-jar-with-custom-resources.jar
```

**Generate CRD by using a single class only:**

```bash
crd-gen -cp target/classes/ com.example.MyCustomResource
```

**Generate CRD by using multiple classes only:**

```bash
crd-gen -cp target/classes/ com.example.v1.MyCustomResource com.example.v2.MyCustomResource
```
132 changes: 132 additions & 0 deletions crd-generator/cli/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?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>
</dependency>

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

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</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>
<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>
<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

0 comments on commit 97841e9

Please sign in to comment.