Skip to content

Commit

Permalink
feat: java-generator support for required spec and status (6807)
Browse files Browse the repository at this point in the history
feat: java-generator support for required spec and status

Signed-off-by: Matt Riben <[email protected]>
---
Merge branch 'main' into iss_6802

Co-authored-by: Andrea Peruffo <[email protected]>
  • Loading branch information
matteriben and andreaTP authored Jan 20, 2025
1 parent 56ba980 commit 1d94068
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#### Dependency Upgrade

#### New Features
* Fix #6802: Java generator support for required spec and status

* Fix #5993: Support for Kubernetes v1.31 (elli)
* Fix #6767: Support for Kubernetes v1.32 (penelope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
package io.fabric8.java.generator.nodes;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.NormalAnnotationExpr;
import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.expr.SuperExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import io.fabric8.java.generator.Config;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
Expand Down Expand Up @@ -115,11 +122,25 @@ public GeneratorResult generateJava() {
? new ClassOrInterfaceType().setName(type + "Spec")
: jlVoid;
fields.remove("spec");
if (required.contains("spec")) {
clz.addMethod("getSpec", Modifier.Keyword.PUBLIC)
.setType(spec)
.setBody(new BlockStmt().addStatement(new ReturnStmt(new MethodCallExpr(new SuperExpr(), "getSpec"))))
.addAnnotation(new NormalAnnotationExpr(new Name("java.lang.Override"), new NodeList<>()))
.addAnnotation(new NormalAnnotationExpr(new Name("io.fabric8.generator.annotation.Required"), new NodeList<>()));
}

ClassOrInterfaceType status = (fields.containsKey("status"))
? new ClassOrInterfaceType().setName(type + "Status")
: jlVoid;
fields.remove("status");
if (required.contains("status")) {
clz.addMethod("getStatus", Modifier.Keyword.PUBLIC)
.setType(status)
.setBody(new BlockStmt().addStatement(new ReturnStmt(new MethodCallExpr(new SuperExpr(), "getStatus"))))
.addAnnotation(new NormalAnnotationExpr(new Name("java.lang.Override"), new NodeList<>()))
.addAnnotation(new NormalAnnotationExpr(new Name("io.fabric8.generator.annotation.Required"), new NodeList<>()));
}

ClassOrInterfaceType crType = new ClassOrInterfaceType()
.setName("io.fabric8.kubernetes.client.CustomResource")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class JObject extends AbstractJSONSchema2Pojo implements JObjectExtraAnno
protected final String className;
protected final String pkg;
protected final Map<String, AbstractJSONSchema2Pojo> fields;
private final Set<String> required;
protected final Set<String> required;
private final Set<String> deprecated = new HashSet<>();

private final boolean preserveUnknownFields;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ private static Stream<Arguments> getCRDGenerationInputData() {
Arguments.of("testCalicoIPPoolCrd", "calico-ippool-crd.yml", "IPPool", "CalicoIPPoolCr", new Config()),
Arguments.of("testExistingJavaType", "existing-java-type-crd.yml", "ExistingJavaType", "ExistingJavaTypeCr",
Config.builder().existingJavaTypes(Collections.singletonMap(
"org.test.v1.existingjavatypespec.Affinity", "io.fabric8.kubernetes.api.model.Affinity")).build()));
"org.test.v1.existingjavatypespec.Affinity", "io.fabric8.kubernetes.api.model.Affinity")).build()),
Arguments.of("testRequireSpecAndStatusCrd", "require-spec-and-status-crd.yml", "RequireSpecAndStatus",
"RequireSpecAndStatusJavaCr", new Config()));
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
RequireSpecAndStatusJavaCr[0] = package org.test.v1;

@io.fabric8.kubernetes.model.annotation.Version(value = "v1" , storage = true , served = true)
@io.fabric8.kubernetes.model.annotation.Group("stable.example.com")
@io.fabric8.kubernetes.model.annotation.Singular("requirespecandstatus")
@io.fabric8.kubernetes.model.annotation.Plural("requirespecandstatuses")
@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner")
public class RequireSpecAndStatus extends io.fabric8.kubernetes.client.CustomResource<org.test.v1.RequireSpecAndStatusSpec, org.test.v1.RequireSpecAndStatusStatus> implements io.fabric8.kubernetes.api.model.Namespaced {

@java.lang.Override()
@io.fabric8.generator.annotation.Required()
public org.test.v1.RequireSpecAndStatusSpec getSpec() {
return super.getSpec();
}

@java.lang.Override()
@io.fabric8.generator.annotation.Required()
public org.test.v1.RequireSpecAndStatusStatus getStatus() {
return super.getStatus();
}
}

RequireSpecAndStatusJavaCr[1] = package org.test.v1;

@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
@com.fasterxml.jackson.annotation.JsonPropertyOrder({})
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class)
@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner")
public class RequireSpecAndStatusSpec implements io.fabric8.kubernetes.api.model.KubernetesResource {
}

RequireSpecAndStatusJavaCr[2] = package org.test.v1;

@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
@com.fasterxml.jackson.annotation.JsonPropertyOrder({})
@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class)
@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner")
public class RequireSpecAndStatusStatus implements io.fabric8.kubernetes.api.model.KubernetesResource {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# 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: requirespecandstatus.stable.example.com
spec:
group: stable.example.com
scope: Namespaced
names:
plural: requirespecandstatuses
singular: requirespecandstatus
kind: RequireSpecAndStatus
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
required:
- spec
- status
properties:
spec:
type: object
status:
type: object

0 comments on commit 1d94068

Please sign in to comment.