diff --git a/CHANGELOG.md b/CHANGELOG.md index 88173f67eb2..526f2e8bfa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ #### Improvements * Fix #5166: Remove opinionated messages from Config's `errorMessages` and deprecate it * Fix #5233: Generalized SchemaSwap to allow for cycle expansion +* Fix #5287: Add an option to filter the files processed by the java-generator, based on a suffix allowlist #### Dependency Upgrade diff --git a/doc/java-generation-from-CRD.md b/doc/java-generation-from-CRD.md index 04757d04b8b..258ddd96af8 100644 --- a/doc/java-generation-from-CRD.md +++ b/doc/java-generation-from-CRD.md @@ -91,6 +91,8 @@ Usage: java-gen [-hV] [-add-extra-annotations] [-enum-uppercase] crds -enum-uppercase, --enum-uppercase Uppercase the enum values + -files-suffixes, --files-suffixes= + Filter the source files with the specific suffixes -h, --help Show this help message and exit. -package-overrides, --package-overrides= Apply the overrides to the package names @@ -118,6 +120,10 @@ And the corresponding configurations of the Maven plugin are (output of `mvn hel User property: fabric8.java-generator.extra-annotations Generate Extra annotation for lombok and sundrio integration + filesSuffixes + User property: fabric8.java-generator.files-suffixes + Files suffixes to be processed + generatedAnnotations User property: fabric8.java-generator.generated-annotations *advanced* Emit the @javax.annotation.processing.Generated annotation on diff --git a/java-generator/cli/src/main/java/io/fabric8/java/generator/cli/GenerateJavaSources.java b/java-generator/cli/src/main/java/io/fabric8/java/generator/cli/GenerateJavaSources.java index 14bc9028e57..2c8e9042c77 100644 --- a/java-generator/cli/src/main/java/io/fabric8/java/generator/cli/GenerateJavaSources.java +++ b/java-generator/cli/src/main/java/io/fabric8/java/generator/cli/GenerateJavaSources.java @@ -67,6 +67,10 @@ public class GenerateJavaSources implements Runnable { "--package-overrides" }, description = "Apply the overrides to the package names", required = false) Map packageOverrides = null; + @Option(names = { "-files-suffixes", + "--files-suffixes" }, description = "Filter the source files with the specific suffixes", required = false) + List filesSuffixes = null; + @Override public void run() { final Boolean noGeneratedAnnotations = (skipGeneratedAnnotations != null) ? skipGeneratedAnnotations : false; @@ -74,7 +78,8 @@ public void run() { uppercaseEnum, addExtraAnnotations, !noGeneratedAnnotations, - packageOverrides); + packageOverrides, + filesSuffixes); List runners = new ArrayList<>(); diff --git a/java-generator/core/src/main/java/io/fabric8/java/generator/Config.java b/java-generator/core/src/main/java/io/fabric8/java/generator/Config.java index 306022b4a70..924546d0cdc 100644 --- a/java-generator/core/src/main/java/io/fabric8/java/generator/Config.java +++ b/java-generator/core/src/main/java/io/fabric8/java/generator/Config.java @@ -18,7 +18,9 @@ import lombok.Builder; import lombok.NoArgsConstructor; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; @Builder(toBuilder = true) @@ -28,11 +30,13 @@ public class Config { private static final boolean DEFAULT_ADD_EXTRA_ANNOTATIONS = false; private static final boolean DEFAULT_ADD_GENERATED_ANNOTATIONS = true; private static final Map DEFAULT_PACKAGE_OVERRIDES = new HashMap<>(); + private static final List DEFAULT_FILES_SUFFIXES = Arrays.asList(".yaml", ".yml", ".json"); private Boolean uppercaseEnums = DEFAULT_UPPERCASE_ENUM; private Boolean objectExtraAnnotations = DEFAULT_ADD_EXTRA_ANNOTATIONS; private Boolean generatedAnnotations = DEFAULT_ADD_GENERATED_ANNOTATIONS; private Map packageOverrides = DEFAULT_PACKAGE_OVERRIDES; + private List filesSuffixes = DEFAULT_FILES_SUFFIXES; public Config( Boolean uppercaseEnums, @@ -53,6 +57,29 @@ public Config( } } + public Config( + Boolean uppercaseEnums, + Boolean objectExtraAnnotations, + Boolean generatedAnnotations, + Map packageOverrides, + List filesSuffixes) { + if (uppercaseEnums != null) { + this.uppercaseEnums = uppercaseEnums; + } + if (objectExtraAnnotations != null) { + this.objectExtraAnnotations = objectExtraAnnotations; + } + if (generatedAnnotations != null) { + this.generatedAnnotations = generatedAnnotations; + } + if (packageOverrides != null) { + this.packageOverrides = packageOverrides; + } + if (filesSuffixes != null) { + this.filesSuffixes = filesSuffixes; + } + } + public boolean isUppercaseEnums() { return (uppercaseEnums == null) ? DEFAULT_UPPERCASE_ENUM : uppercaseEnums; } @@ -70,8 +97,14 @@ public boolean isGeneratedAnnotations() { } public Map getPackageOverrides() { - return (packageOverrides == null) + return (packageOverrides == null || packageOverrides.isEmpty()) ? DEFAULT_PACKAGE_OVERRIDES : packageOverrides; } + + public List getFilesSuffixes() { + return (filesSuffixes == null || filesSuffixes.isEmpty()) + ? DEFAULT_FILES_SUFFIXES + : filesSuffixes; + } } diff --git a/java-generator/core/src/main/java/io/fabric8/java/generator/FileJavaGenerator.java b/java-generator/core/src/main/java/io/fabric8/java/generator/FileJavaGenerator.java index bbac937c957..a53be171856 100644 --- a/java-generator/core/src/main/java/io/fabric8/java/generator/FileJavaGenerator.java +++ b/java-generator/core/src/main/java/io/fabric8/java/generator/FileJavaGenerator.java @@ -46,11 +46,13 @@ public class FileJavaGenerator implements JavaGenerator { private static final Logger LOGGER = LoggerFactory.getLogger(FileJavaGenerator.class); + private final Config config; private final File source; private final CRGeneratorRunner crGeneratorRunner; public FileJavaGenerator(Config config, File source) { crGeneratorRunner = new CRGeneratorRunner(config); + this.config = config; this.source = source; } @@ -64,6 +66,7 @@ public void run(File outputDirectory) { walk .map(Path::toFile) .filter(f -> !f.getAbsolutePath().equals(source.getAbsolutePath()) && f.isFile()) + .filter(f -> config.getFilesSuffixes().stream().anyMatch(suffix -> f.getName().endsWith(suffix))) .forEach(f -> runOnSingleSource(f, outputDirectory)); } catch (IOException e) { throw new JavaGeneratorException( diff --git a/java-generator/gradle-plugin/src/main/java/io/fabric8/java/generator/gradle/plugin/JavaGeneratorPluginExtension.java b/java-generator/gradle-plugin/src/main/java/io/fabric8/java/generator/gradle/plugin/JavaGeneratorPluginExtension.java index 7b3f82bc152..14673b6d4ec 100644 --- a/java-generator/gradle-plugin/src/main/java/io/fabric8/java/generator/gradle/plugin/JavaGeneratorPluginExtension.java +++ b/java-generator/gradle-plugin/src/main/java/io/fabric8/java/generator/gradle/plugin/JavaGeneratorPluginExtension.java @@ -100,7 +100,8 @@ public void setEnumUppercase(final Boolean isEnumUppercase) { javaGeneratorConfig = new Config(isEnumUppercase, javaGeneratorConfig.isObjectExtraAnnotations(), javaGeneratorConfig.isGeneratedAnnotations(), - javaGeneratorConfig.getPackageOverrides()); + javaGeneratorConfig.getPackageOverrides(), + javaGeneratorConfig.getFilesSuffixes()); } /** @@ -115,7 +116,8 @@ public void setExtraAnnotations(final Boolean isExtraAnnotations) { javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(), isExtraAnnotations, javaGeneratorConfig.isGeneratedAnnotations(), - javaGeneratorConfig.getPackageOverrides()); + javaGeneratorConfig.getPackageOverrides(), + javaGeneratorConfig.getFilesSuffixes()); } /** @@ -130,7 +132,8 @@ public void setGeneratedAnnotations(final Boolean isGeneratedAnnotations) { javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(), javaGeneratorConfig.isObjectExtraAnnotations(), isGeneratedAnnotations, - javaGeneratorConfig.getPackageOverrides()); + javaGeneratorConfig.getPackageOverrides(), + javaGeneratorConfig.getFilesSuffixes()); } /** @@ -145,6 +148,23 @@ public void setPackageOverrides(final Map packageOverrides) { javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(), javaGeneratorConfig.isObjectExtraAnnotations(), javaGeneratorConfig.isGeneratedAnnotations(), - packageOverrides); + packageOverrides, + javaGeneratorConfig.getFilesSuffixes()); + } + + /** + * Files suffixes to be processed + * + */ + public List getFilesSuffixes() { + return javaGeneratorConfig.getFilesSuffixes(); + } + + public void setPackageOverrides(final List filesSuffixes) { + javaGeneratorConfig = new Config(javaGeneratorConfig.isUppercaseEnums(), + javaGeneratorConfig.isObjectExtraAnnotations(), + javaGeneratorConfig.isGeneratedAnnotations(), + javaGeneratorConfig.getPackageOverrides(), + filesSuffixes); } } diff --git a/java-generator/it/src/it/skip-files-suffixes/invoker.properties b/java-generator/it/src/it/skip-files-suffixes/invoker.properties new file mode 100644 index 00000000000..3a52c692968 --- /dev/null +++ b/java-generator/it/src/it/skip-files-suffixes/invoker.properties @@ -0,0 +1,17 @@ +# +# 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. +# + +invoker.goals.1=package diff --git a/java-generator/it/src/it/skip-files-suffixes/pom.xml b/java-generator/it/src/it/skip-files-suffixes/pom.xml new file mode 100644 index 00000000000..f781957b46b --- /dev/null +++ b/java-generator/it/src/it/skip-files-suffixes/pom.xml @@ -0,0 +1,87 @@ + + + + + 4.0.0 + + cert-manager-extension + io.fabric8.it + 0.0-SNAPSHOT + jar + + + @maven.compiler.source@ + @maven.compiler.target@ + + + + + io.sundr + sundr-adapter-reflect + @sundrio.version@ + compile + + + io.sundr + builder-annotations + @sundrio.version@ + compile + + + org.projectlombok + lombok + @lombok.version@ + provided + + + io.fabric8 + kubernetes-client + @project.version@ + + + io.fabric8 + generator-annotations + @project.version@ + + + + + + + io.fabric8 + java-generator-maven-plugin + @project.version@ + + + + generate + + + + + src/main/resources/ + true + false + .json + + + + + + diff --git a/java-generator/it/src/it/skip-files-suffixes/src/main/resources/cert-manager.crds.1.7.1.yaml b/java-generator/it/src/it/skip-files-suffixes/src/main/resources/cert-manager.crds.1.7.1.yaml new file mode 100644 index 00000000000..4f0e5478cf7 --- /dev/null +++ b/java-generator/it/src/it/skip-files-suffixes/src/main/resources/cert-manager.crds.1.7.1.yaml @@ -0,0 +1,107 @@ +# +# 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. +# + + +--- +# Partial Source: cert-manager/templates/templates.out +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: certificaterequests.cert-manager.io + annotations: + cert-manager.io/inject-ca-from-secret: 'cert-manager/cert-manager-webhook-ca' + labels: + app: 'cert-manager' + app.kubernetes.io/name: 'cert-manager' + app.kubernetes.io/instance: 'cert-manager' + # Generated labels + app.kubernetes.io/version: "v1.7.1" +spec: + group: cert-manager.io + names: + kind: CertificateRequest + listKind: CertificateRequestList + plural: certificaterequests + shortNames: + - cr + - crs + singular: certificaterequest + categories: + - cert-manager + scope: Namespaced + versions: + - name: v1 + subresources: + status: {} + schema: + openAPIV3Schema: + description: "A CertificateRequest is used to request a signed certificate from one of the configured issuers. \n All fields within the CertificateRequest's `spec` are immutable after creation. A CertificateRequest will either succeed or fail, as denoted by its `status.state` field. \n A CertificateRequest is a one-shot resource, meaning it represents a single point in time request for a certificate and cannot be re-used." + type: object + required: + - spec + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: Desired state of the CertificateRequest resource. + type: object + required: + - issuerRef + - request + properties: + duration: + description: The requested 'duration' (i.e. lifetime) of the Certificate. This option may be ignored/overridden by some issuer types. + type: string + status: + type: object + properties: + authorizations: + description: Authorizations contains data returned from the ACME server on what authorizations must be completed in order to validate the DNS names specified on the Order. + type: array + items: + description: ACMEAuthorization contains data returned from the ACME server on an authorization that must be completed in order validate a DNS name on an ACME Order resource. + type: object + required: + - url + properties: + challenges: + description: Challenges specifies the challenge types offered by the ACME server. One of these challenge types will be selected when validating the DNS name and an appropriate Challenge resource will be created to perform the ACME challenge process. + type: array + items: + description: Challenge specifies a challenge offered by the ACME server for an Order. An appropriate Challenge resource can be created to perform the ACME challenge process. + type: object + required: + - token + - type + - url + properties: + token: + description: Token is the token that must be presented for this challenge. This is used to compute the 'key' that must also be presented. + type: string + type: + description: Type is the type of challenge being offered, e.g. 'http-01', 'dns-01', 'tls-sni-01', etc. This is the raw value retrieved from the ACME server. Only 'http-01' and 'dns-01' are supported by cert-manager, other values will be ignored. + type: string + url: + description: URL is the URL of this challenge. It can be used to retrieve additional metadata about the Challenge from the ACME server. + type: string + served: true + storage: true diff --git a/java-generator/it/src/it/skip-files-suffixes/verify.groovy b/java-generator/it/src/it/skip-files-suffixes/verify.groovy new file mode 100644 index 00000000000..fbfee0ccbf2 --- /dev/null +++ b/java-generator/it/src/it/skip-files-suffixes/verify.groovy @@ -0,0 +1,18 @@ +/** + * 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. + */ +import static org.junit.jupiter.api.Assertions.assertFalse + +assertFalse(new File(basedir, "/target/generated-sources/").exists()) diff --git a/java-generator/maven-plugin/src/main/java/io/fabric8/java/generator/maven/plugin/JavaGeneratorMojo.java b/java-generator/maven-plugin/src/main/java/io/fabric8/java/generator/maven/plugin/JavaGeneratorMojo.java index 4e87e7075f7..97767fc9ec9 100644 --- a/java-generator/maven-plugin/src/main/java/io/fabric8/java/generator/maven/plugin/JavaGeneratorMojo.java +++ b/java-generator/maven-plugin/src/main/java/io/fabric8/java/generator/maven/plugin/JavaGeneratorMojo.java @@ -102,6 +102,13 @@ public class JavaGeneratorMojo extends AbstractMojo { @Parameter(property = "fabric8.java-generator.package-overrides", required = false) Map packageOverrides = null; + /** + * Files suffixes to be processed + * + */ + @Parameter(property = "fabric8.java-generator.files-suffixes", required = false) + List filesSuffixes = null; + @Override public void execute() throws MojoExecutionException { final Config config = Config.builder() @@ -109,6 +116,7 @@ public void execute() throws MojoExecutionException { .objectExtraAnnotations(extraAnnotations) .generatedAnnotations(generatedAnnotations) .packageOverrides(packageOverrides) + .filesSuffixes(filesSuffixes) .build(); List runners = new ArrayList<>();