fabric8io / kubernetes-client

Java client for Kubernetes & OpenShift
http://fabric8.io
Apache License 2.0
3.42k stars 1.46k forks source link

How to create Builders with java generator? #6659

Open rafhuys-klarrio opened 15 hours ago

rafhuys-klarrio commented 15 hours ago

I have a CRD from which I generate the Java Classes. I can add annotations to indicate the Builders of these classes, but is there a way to actually generate the Builder classes as well?

Concretely, I generate the following class based on Traefik Middleware CRD with ./java-gen/java-generator-cli-6.13.4.sh -add-extra-annotations -s traefik.io_middlewares.yaml -t .

package io.traefik.v1alpha1;

@io.fabric8.kubernetes.model.annotation.Version(value = "v1alpha1" , storage = true , served = true)
@io.fabric8.kubernetes.model.annotation.Group("traefik.io")
@io.fabric8.kubernetes.model.annotation.Singular("middleware")
@io.fabric8.kubernetes.model.annotation.Plural("middlewares")
@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner")
@lombok.ToString()
@lombok.EqualsAndHashCode(callSuper = true)
@io.sundr.builder.annotations.Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, builderPackage = "io.fabric8.kubernetes.api.builder", refs = {
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.ObjectMeta.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.ObjectReference.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.LabelSelector.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.Container.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.EnvVar.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.ContainerPort.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.Volume.class),
    @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.VolumeMount.class)
})
public class Middleware extends io.fabric8.kubernetes.client.CustomResource<io.traefik.v1alpha1.MiddlewareSpec, java.lang.Void> implements io.fabric8.kubernetes.api.model.Namespaced, io.fabric8.kubernetes.api.builder.Editable<MiddlewareBuilder> {

    @java.lang.Override
    public MiddlewareBuilder edit() {
        return new MiddlewareBuilder(this);
    }
}

Very nice, however there is no constructor for MiddlewareBuilder created. How can I generate one?

manusa commented 15 hours ago

You need to add the sundrio and lombok dependencies to your project: https://github.com/fabric8io/kubernetes-client/blob/main/doc/java-generation-from-CRD.md#quick-start-maven

Here you can find a working example: https://github.com/marcnuri-demo/kubernetes-client/blob/a3ab4ac552b0932324f0ce97387af2a791850611/java-generator/pom.xml

rafhuys-klarrio commented 15 hours ago

is this possible with the cli as well? or only maven?

manusa commented 14 hours ago

the CLI won't generate the builders, it generates source-code that can be processed by the builder generators. Where is the code generated by the CLI consumed? Wherever that code is stored, then the project needs to annotate-process the generated code.

rafhuys-klarrio commented 14 hours ago

Thank you for the link you sent. But when I run https://github.com/marcnuri-demo/kubernetes-client/blob/master/java-generator/src/test/java/com/hashicorp/secrets/v1beta1/VaultConnectionTest.java I get

/~/kubernetes-client-java-generator/java-generator/src/test/java/com/hashicorp/secrets/v1beta1/VaultConnectionTest.java:50:21
java: cannot find symbol
  symbol:   class VaultConnectionBuilder
  location: class com.hashicorp.secrets.v1beta1.VaultConnectionTest
rafhuys-klarrio commented 14 hours ago

ok! didn't know about the annotate-process part! Tx

manusa commented 14 hours ago

Thank you for the link you sent. But when I run https://github.com/marcnuri-demo/kubernetes-client/blob/master/java-generator/src/test/java/com/hashicorp/secrets/v1beta1/VaultConnectionTest.java I get

/~/kubernetes-client-java-generator/java-generator/src/test/java/com/hashicorp/secrets/v1beta1/VaultConnectionTest.java:50:21
java: cannot find symbol
  symbol:   class VaultConnectionBuilder
  location: class com.hashicorp.secrets.v1beta1.VaultConnectionTest

Not sure how you're running it, but should simply work by using Maven (mvn verify)

ok! didn't know about the annotate-process part! Tx

Yes, this is basically the important part. Your builders will be generated as generated-sources after the annotation processor processes them.

rafhuys-klarrio commented 13 hours ago

yeah tx for the pointer! Actually I'm writing an SBT plugin for the java-generator. The basic POJO generation is there, but since I don't use Maven ever it's a bit getting used to. So, if I'm following this correctly it's the sundr.io plugin that (should) kick in after the pojo is generated and then creates the Builder class based on the annotation of the base class. I'll have a look if I can manage that to work but this might become a rabbithole rather quickly. Thanks for your help!

manusa commented 13 hours ago

So, if I'm following this correctly it's the sundr.io plugin that (should) kick in after the pojo is generated and then creates the Builder class based on the annotation of the base class.

That's correct.

I guess you can run the compilation of the classes to kick in the APT (javac -proc:only) and then use some other process to move the generated sources elsewhere.