Skip to content

Commit

Permalink
feat(java-generator): support implementsEditable extension in java-ge…
Browse files Browse the repository at this point in the history
…nerator
  • Loading branch information
matteriben authored Apr 19, 2024
1 parent fce2ba2 commit 4dec479
Show file tree
Hide file tree
Showing 21 changed files with 646 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#### Improvements
* Fix #5843: Support existingJavaTypes extension in java-generator
* Fix #5878: (java-generator) Add implements Editable for extraAnnotations

### 6.11.0 (2024-03-25)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
*/
package io.fabric8.java.generator.nodes;

import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.MarkerAnnotationExpr;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;

public interface JObjectExtraAnnotations {

Expand Down Expand Up @@ -46,5 +54,16 @@ default void addExtraAnnotations(ClassOrInterfaceDeclaration clz) {
+ " @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.Volume.class),\n"
+ " @io.sundr.builder.annotations.BuildableReference(io.fabric8.kubernetes.api.model.VolumeMount.class)\n"
+ "}")));

// implements Editable
final String builderName = clz.getNameAsString() + "Builder";
clz.addImplementedType(new ClassOrInterfaceType(null, "io.fabric8.kubernetes.api.builder.Editable")
.setTypeArguments(new ClassOrInterfaceType(null, builderName)));
clz.addMethod("edit", Modifier.Keyword.PUBLIC)
.setAnnotations(NodeList.nodeList(new MarkerAnnotationExpr(Override.class.getName())))
.setType(builderName)
.setBody(new BlockStmt().addStatement(new ReturnStmt(new ObjectCreationExpr()
.setType(builderName)
.setArguments(NodeList.nodeList(new ThisExpr())))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import io.fabric8.java.generator.exceptions.JavaGeneratorException;
import io.sundr.builder.internal.processor.BuildableProcessor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -31,6 +33,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -90,6 +93,7 @@ void yamlCompiles(String yamlFile, int expectedGeneratedSourceFiles) throws Exce
assertEquals(Compilation.Status.SUCCESS, compilation.status());
}

@Disabled("Requires support from sundrio to work with compile-testing, see sundrio PR #469")
@Test
void testCrontabCRDCompilesWithExtraAnnotations() throws Exception {
// Arrange
Expand All @@ -100,10 +104,12 @@ void testCrontabCRDCompilesWithExtraAnnotations() throws Exception {

// Act
new FileJavaGenerator(config, crd).run(tempDir);
Compilation compilation = javac().compile(getSources(tempDir));
Compilation compilation = javac()
.withProcessors(new BuildableProcessor())
.compile(getSources(tempDir));

// Assert
assertTrue(compilation.errors().isEmpty());
assertEquals(Collections.emptyList(), compilation.errors());
assertEquals(3, compilation.sourceFiles().size());
assertEquals(Compilation.Status.SUCCESS, compilation.status());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ CrontabJavaExtraAnnotationsCr[0] = package org.test.v1;
@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 CronTab extends io.fabric8.kubernetes.client.CustomResource<org.test.v1.CronTabSpec, org.test.v1.CronTabStatus> implements io.fabric8.kubernetes.api.model.Namespaced {
public class CronTab extends io.fabric8.kubernetes.client.CustomResource<org.test.v1.CronTabSpec, org.test.v1.CronTabStatus> implements io.fabric8.kubernetes.api.model.Namespaced, io.fabric8.kubernetes.api.builder.Editable<CronTabBuilder> {

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

CrontabJavaExtraAnnotationsCr[1] = package org.test.v1;
Expand All @@ -38,7 +43,12 @@ CrontabJavaExtraAnnotationsCr[1] = package org.test.v1;
@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 CronTabSpec implements io.fabric8.kubernetes.api.model.KubernetesResource {
public class CronTabSpec implements io.fabric8.kubernetes.api.builder.Editable<CronTabSpecBuilder>, io.fabric8.kubernetes.api.model.KubernetesResource {

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

@com.fasterxml.jackson.annotation.JsonProperty("cronSpec")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
Expand Down Expand Up @@ -109,7 +119,12 @@ CrontabJavaExtraAnnotationsCr[2] = package org.test.v1;
@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 CronTabStatus implements io.fabric8.kubernetes.api.model.KubernetesResource {
public class CronTabStatus implements io.fabric8.kubernetes.api.builder.Editable<CronTabStatusBuilder>, io.fabric8.kubernetes.api.model.KubernetesResource {

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

@com.fasterxml.jackson.annotation.JsonProperty("labelSelector")
@com.fasterxml.jackson.annotation.JsonSetter(nulls = com.fasterxml.jackson.annotation.Nulls.SKIP)
Expand Down
2 changes: 2 additions & 0 deletions java-generator/it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
<systemPropertyVariables>
<itDir>${project.basedir}</itDir>
<kubernetesClientVersion>${project.version}</kubernetesClientVersion>
<lombokVersion>${lombok.version}</lombokVersion>
<sundrioVersion>${sundrio.version}</sundrioVersion>
</systemPropertyVariables>
</configuration>
</plugin>
Expand Down
8 changes: 7 additions & 1 deletion java-generator/it/src/it/cert-manager/expected/Auth.expected
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ package io.cert_manager.v1.issuerspec.vault;
@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 Auth implements io.fabric8.kubernetes.api.model.KubernetesResource {
public class Auth implements io.fabric8.kubernetes.api.builder.Editable<AuthBuilder>, io.fabric8.kubernetes.api.model.KubernetesResource {

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

/**
* AppRole authenticates with Vault using the App Role auth mechanism, with the role and secret stored in a Kubernetes Secret resource.
Expand Down Expand Up @@ -65,3 +70,4 @@ public class Auth implements io.fabric8.kubernetes.api.model.KubernetesResource
this.tokenSecretRef = tokenSecretRef;
}
}

17 changes: 17 additions & 0 deletions java-generator/it/src/it/implements-editable/invoker.properties
Original file line number Diff line number Diff line change
@@ -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=test
100 changes: 100 additions & 0 deletions java-generator/it/src/it/implements-editable/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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>

<modelVersion>4.0.0</modelVersion>

<artifactId>implements-editable</artifactId>
<groupId>io.fabric8.it</groupId>
<version>0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>@maven.compiler.source@</maven.compiler.source>
<maven.compiler.target>@maven.compiler.target@</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-integration-tests</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>generator-annotations</artifactId>
<version>@project.version@</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.sundr</groupId>
<artifactId>builder-annotations</artifactId>
<version>@sundrio.version@</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>@lombok.version@</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>java-generator-maven-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<source>src/main/resources/crontab-crd.yml</source>
<extraAnnotations>true</extraAnnotations>
<generatedAnnotations>false</generatedAnnotations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>@maven.surefire.plugin.version@</version>
<configuration>
<useFile>false</useFile>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.test;

import com.example.stable.v1.CronTab;
import com.example.stable.v1.CronTabBuilder;
import io.fabric8.kubernetes.api.builder.Editable;

public class ImplementsEditable {
public void example() {
// Crontab implements Editable<CronTabBuilder>
Editable<CronTabBuilder> orig = new CronTab();
// edit method returns a builder which can build a CronTab
CronTab copy = orig.edit().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#
# 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.
#

# java-package:org.sample
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: crontabs.stable.example.com
spec:
group: stable.example.com
version: v1
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
issuedAt:
format: date-time
type: string
status:
type: object
properties:
replicas:
type: integer
labelSelector:
type: string
# subresources describes the subresources for custom resources.
subresources:
# status enables the status subresource.
status: {}
# scale enables the scale subresource.
scale:
# specReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Spec.Replicas.
specReplicasPath: .spec.replicas
# statusReplicasPath defines the JSONPath inside of a custom resource that corresponds to Scale.Status.Replicas.
statusReplicasPath: .status.replicas
# labelSelectorPath defines the JSONPath inside of a custom resource that corresponds to Scale.Status.Selector.
labelSelectorPath: .status.labelSelector
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
subresources:
status: {}
Loading

0 comments on commit 4dec479

Please sign in to comment.