forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crd-generator): Add CRD-Generator CLI
- Loading branch information
Showing
8 changed files
with
482 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>6.13-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>java-gen</programFile> | ||
<attachProgramFile>true</attachProgramFile> | ||
</configuration> | ||
|
||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>really-executable-jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
118 changes: 118 additions & 0 deletions
118
crd-generator/cli/src/main/java/io/fabric8/crd/generator/cli/CRDGeneratorCLI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* 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.crd.generator.cli; | ||
|
||
import io.fabric8.crd.generator.collector.CustomResourceCollector; | ||
import io.fabric8.crdv2.generator.CRDGenerationInfo; | ||
import io.fabric8.crdv2.generator.CRDGenerator; | ||
import io.fabric8.crdv2.generator.CustomResourceInfo; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import picocli.CommandLine; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@CommandLine.Command(name = "crd-gen", mixinStandardHelpOptions = true, helpCommand = true, versionProvider = KubernetesClientVersionProvider.class) | ||
public class CRDGeneratorCLI implements Runnable { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(CustomResourceCollector.class); | ||
private static final CRDGenerationInfo EMPTY_INFO = new CRDGenerationInfo(); | ||
|
||
@CommandLine.Spec | ||
CommandLine.Model.CommandSpec spec; | ||
|
||
@CommandLine.Option(names = { "-c", | ||
"--classes" }, description = "Directories or JAR files to be used to scan for Custom Resource classes") | ||
List<File> classesToIndex; | ||
|
||
@CommandLine.Option(names = { "-cr", | ||
"--custom-resource" }, description = "Custom Resource classes, which should be considered to generate the CRDs. If set, scanning is disabled.") | ||
List<String> customResourceClasses; | ||
|
||
@CommandLine.Option(names = { "-cp", "--classpath" }, description = "The classpath which be used during the CRD generation") | ||
List<String> classpaths = new ArrayList<>(); | ||
|
||
@CommandLine.Option(names = { "-o", "--output-dir" }, description = "The output directory for the generated CRDs") | ||
File outputDirectory = new File("."); | ||
|
||
@CommandLine.Option(names = { "-f", | ||
"--force-index" }, description = "If true, a Jandex index will be created even if the directory or JAR file contains an existing index.", defaultValue = "false") | ||
Boolean forceIndex; | ||
|
||
@CommandLine.Option(names = { "-p", "--parallel" }, description = "Enable parallel generation", defaultValue = "true") | ||
Boolean parallel; | ||
|
||
@CommandLine.Option(names = { | ||
"--implicit-preserve-unknown-fields" }, description = "If enabled, all objects are implicitly marked with x-kubernetes-preserve-unknown-fields: true.", defaultValue = "false") | ||
Boolean implicitPreserveUnknownFields; | ||
|
||
private CRDGenerationInfo crdGenerationInfo = EMPTY_INFO; | ||
|
||
@Override | ||
public void run() { | ||
CustomResourceCollector customResourceCollector = new CustomResourceCollector() | ||
.withParentClassLoader(Thread.currentThread().getContextClassLoader()) | ||
.withClasspaths(classpaths) | ||
.withFilesToIndex(classesToIndex) | ||
.withForceIndex(forceIndex) | ||
// TODO: allow to exclude / include | ||
/* | ||
* .withIncludePackages(inclusions.getPackages()) | ||
* .withIncludeGroups(inclusions.getGroups()) | ||
* .withIncludeVersions(inclusions.getVersions()) | ||
* .withExcludePackages(exclusions.getPackages()) | ||
* .withExcludeGroups(exclusions.getGroups()) | ||
* .withExcludeVersions(exclusions.getVersions()) | ||
*/ | ||
.withCustomResourceClasses(customResourceClasses); | ||
|
||
CustomResourceInfo[] customResourceInfos = customResourceCollector.findCustomResources(); | ||
|
||
log.info("Found {} CustomResources", customResourceInfos.length); | ||
|
||
try { | ||
Files.createDirectories(outputDirectory.toPath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Could not create output directory: " + e.getMessage()); | ||
} | ||
|
||
CRDGenerator crdGenerator = new CRDGenerator() | ||
.customResources(customResourceInfos) | ||
.withParallelGenerationEnabled(parallel) | ||
.withImplicitPreserveUnknownFields(implicitPreserveUnknownFields) | ||
.inOutputDir(outputDirectory); | ||
|
||
crdGenerationInfo = crdGenerator.detailedGenerate(); | ||
crdGenerationInfo.getCRDDetailsPerNameAndVersion().forEach((crdName, versionToInfo) -> { | ||
log.info("Generated CRD {}:", crdName); | ||
versionToInfo.forEach( | ||
(version, info) -> log.info(" {} -> {}", version, info.getFilePath())); | ||
}); | ||
} | ||
|
||
public CRDGenerationInfo getCrdGenerationInfo() { | ||
return crdGenerationInfo; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int exitCode = new CommandLine(new CRDGeneratorCLI()).execute(args); | ||
System.exit(exitCode); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...rator/cli/src/main/java/io/fabric8/crd/generator/cli/KubernetesClientVersionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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.crd.generator.cli; | ||
|
||
import io.fabric8.kubernetes.client.Version; | ||
import picocli.CommandLine; | ||
|
||
public class KubernetesClientVersionProvider implements CommandLine.IVersionProvider { | ||
@Override | ||
public String[] getVersion() throws Exception { | ||
return new String[] { Version.clientVersion() }; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
crd-generator/cli/src/test/java/io/fabric8/crd/generator/cli/CRDGeneratorCLITest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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.crd.generator.cli; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import picocli.CommandLine; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class CRDGeneratorCLITest { | ||
|
||
@Test | ||
public void givenNoInput_thenGenerateNoCRDs() { | ||
CRDGeneratorCLI cliApp = new CRDGeneratorCLI(); | ||
CommandLine cmd = new CommandLine(cliApp); | ||
int exitCode = cmd.execute(); | ||
assertEquals(0, exitCode); | ||
assertEquals(0, cliApp.getCrdGenerationInfo().numberOfGeneratedCRDs()); | ||
} | ||
|
||
@Test | ||
public void givenSingleCRClassNameFromSameClasspath_thenGenerate(@TempDir Path tempDir) { | ||
CRDGeneratorCLI cliApp = new CRDGeneratorCLI(); | ||
CommandLine cmd = new CommandLine(cliApp); | ||
String[] args = new String[] { | ||
"-cr", "io.fabric8.crd.generator.cli.examples.basic.Basic", | ||
"-o", tempDir.toString() | ||
}; | ||
int exitCode = cmd.execute(args); | ||
assertEquals(0, exitCode); | ||
assertEquals(1, cliApp.getCrdGenerationInfo().numberOfGeneratedCRDs()); | ||
assertTrue(Paths.get(tempDir.toString(), "basics.sample.fabric8.io-v1.yml").toFile().exists()); | ||
} | ||
|
||
@Test | ||
public void givenSingleCRClassNameFromExternalClasspath_thenGenerate(@TempDir Path tempDir) { | ||
CRDGeneratorCLI cliApp = new CRDGeneratorCLI(); | ||
CommandLine cmd = new CommandLine(cliApp); | ||
String[] args = new String[] { | ||
"-cp", "../api-v2/target/test-classes/", | ||
"-cr", "io.fabric8.crdv2.example.basic.Basic", | ||
"-o", tempDir.toString() | ||
}; | ||
|
||
int exitCode = cmd.execute(args); | ||
assertEquals(0, exitCode); | ||
assertEquals(1, cliApp.getCrdGenerationInfo().numberOfGeneratedCRDs()); | ||
assertTrue(Paths.get(tempDir.toString(), "basics.sample.fabric8.io-v1.yml").toFile().exists()); | ||
} | ||
|
||
@Test | ||
public void givenClassesDirectory_thenScanAndGenerate(@TempDir Path tempDir) { | ||
CRDGeneratorCLI cliApp = new CRDGeneratorCLI(); | ||
CommandLine cmd = new CommandLine(cliApp); | ||
String[] args = new String[] { | ||
"-c", "target/test-classes/", | ||
//"-k", "basics.sample.fabric8.io", | ||
"-o", tempDir.toString() | ||
}; | ||
|
||
int exitCode = cmd.execute(args); | ||
assertEquals(0, exitCode); | ||
assertEquals(1, cliApp.getCrdGenerationInfo().numberOfGeneratedCRDs()); | ||
assertTrue(Paths.get(tempDir.toString(), "basics.sample.fabric8.io-v1.yml").toFile().exists()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
crd-generator/cli/src/test/java/io/fabric8/crd/generator/cli/examples/basic/Basic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.crd.generator.cli.examples.basic; | ||
|
||
import io.fabric8.kubernetes.api.model.Namespaced; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.model.annotation.Group; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
|
||
@Group("sample.fabric8.io") | ||
@Version("v1alpha1") | ||
public class Basic extends CustomResource<BasicSpec, BasicStatus> implements Namespaced { | ||
|
||
} |
Oops, something went wrong.