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

Double generation from CRD to Java and back to CRD #4351

Closed
honnix opened this issue Aug 24, 2022 · 6 comments · Fixed by #4443
Closed

Double generation from CRD to Java and back to CRD #4351

honnix opened this issue Aug 24, 2022 · 6 comments · Fixed by #4443
Assignees
Labels
Milestone

Comments

@honnix
Copy link
Contributor

honnix commented Aug 24, 2022

Describe the bug

I have the use case that I need to both generate CDR from Java, as well as generating Java from CDR. So I have both the annotation processor and maven plugin configured.

From what I can see, the maven plugin runs first and it generates Java code, and then the annotation processor kicks in, which totally makes sense because before compilation code needs to be generated.

The result is a roundtrip happens for the CDR and I get the generated one under /META-INF/fabric8 along with the original one in a different place. This is not a real problem, but could be confusing.

Fabric8 Kubernetes Client version

6.0.0

Steps to reproduce

Configure annotation processor

    <dependency>
      <groupId>io.fabric8</groupId>
      <artifactId>crd-generator-apt</artifactId>
      <scope>provided</scope>
    </dependency>

Configure maven plugin

      <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>java-generator-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <source>src/main/resources/kubernetes/crds</source>
        </configuration>
      </plugin>

Compile

mvn compile

Expected behavior

Maybe there is a way to configure the annotation processor to skip generated code that I'm not aware of. The expected behaviour is not to generate CDR form generated code.

Runtime

Kubernetes (vanilla)

Kubernetes API Server version

1.24.2@latest

Environment

macOS

Fabric8 Kubernetes Client Logs

NA

Additional context

For a reason I have to use 5.12.3 annotation processor, but 6.0.0 maven plugin. Not sure whether that matters.

@manusa
Copy link
Member

manusa commented Aug 26, 2022

Maybe you want to separate the Java Generation into a separate module. AFAIK you cannot disable an annotation processor for specific parts of the code.

So considering yourproject you may be able to create a:

  • yourproject-api: Has a crd.yaml file that is processed by the plugin
  • yourproject-core: Has whatever else that needs the crd generator APT, and a dependency to yourproject-api
  • yourproject: Is now the parent for both submodules

@honnix
Copy link
Contributor Author

honnix commented Aug 26, 2022

@manusa Thanks for replying! Yeah that's indeed a good workaround, although it may feel slightly overcomplicated for a small code base. I think I will be fine to live with the fact that there are duplicates, which doesn't do any harm.

Please feel free to close this issue or keep it open if you plan to document this type of things.

@manusa
Copy link
Member

manusa commented Aug 26, 2022

Please feel free to close this issue or keep it open if you plan to document this type of things.

We should either document this, or work on something code-wise to prevent this from happening. Maybe the CRD generator can be tuned to detect if a Java Class was generated by the client Java-Generator.

/cc @andreaTP @metacosm

@honnix
Copy link
Contributor Author

honnix commented Aug 26, 2022

Right that is a possible solution. So for generated code, it is usually a good practice to use @Generated annotation, so something like:

import javax.annotation.processing.Generated;

@Generated("...")
@io.fabric8.kubernetes.model.annotation.Version(value = "v1", storage = true, served = true)
@io.fabric8.kubernetes.model.annotation.Group("com.example")
public class Foo
    extends io.fabric8.kubernetes.client.CustomResource<
        FooSpec,
        FooStatus>
    implements io.fabric8.kubernetes.api.model.Namespaced {}

So the CRD generator could look at the annotation to skip a file.

@honnix
Copy link
Contributor Author

honnix commented Aug 26, 2022

I guess, better to have some identity so it won't skip other generated files, @Generated(value="io.facric8....").

@andreaTP
Copy link
Member

I checked and there is, at the moment, no easy way to disable the CRD generator on a particular CRD and the separation @manusa suggested is the only viable option.

That said I'm all in for adding javax.annotation.processing.Generated which is indeed good practice, thanks for the suggestion @honnix 👍 .

The only suggestion(if it's not me going to implement it) is to make it configurable (even a hidden configuration is fine) to not prevent testing the two generators as we are doing in the it tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants