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

feat(crd-generator): Allow to configure CRDGenerator if running as annotation processor, add approval and compiler tests #5896

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#### Improvements

* Fix #5896: (crd-generator) Allow to configure CustomResourceAnnotationProcessor

#### Dependency Upgrade

#### New Features
Expand Down
7 changes: 5 additions & 2 deletions crd-generator/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
<scope>compile</scope>
</dependency>


<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -74,7 +73,11 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.approvaltests</groupId>
<artifactId>approvaltests</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,12 @@ private static Stream<KubernetesValidationRule> extractKubernetesValidationRules
case ANNOTATION_VALIDATION_RULE:
return Stream.of(KubernetesValidationRule.from(annotationRef));
case ANNOTATION_VALIDATION_RULES:
return Arrays.stream(((ValidationRule[]) annotationRef.getParameters().get(VALUE)))
.map(KubernetesValidationRule::from);
Object obj = annotationRef.getParameters().get(VALUE);
if (obj instanceof ValidationRule[]) {
return Arrays.stream(((ValidationRule[]) obj))
.map(KubernetesValidationRule::from);
}
return Stream.empty();
default:
return Stream.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import io.fabric8.crd.generator.visitor.ClassDependenciesVisitor;

import java.io.File;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -35,9 +33,9 @@ public Map<String, Map<String, CRDInfo>> getCRDDetailsPerNameAndVersion() {
return crdNameToVersionToCRDInfoMap;
}

void add(String crdName, String version, URI fileURI) {
void add(String crdName, String version, String filePath) {
crdNameToVersionToCRDInfoMap.computeIfAbsent(crdName, k -> new HashMap<>())
.put(version, new CRDInfo(crdName, version, new File(fileURI).getAbsolutePath(),
.put(version, new CRDInfo(crdName, version, filePath,
ClassDependenciesVisitor.getDependentClassesFromCRDName(crdName)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import io.fabric8.kubernetes.client.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -56,6 +57,7 @@ public class CRDGenerator {
private final Map<String, AbstractCustomResourceHandler> handlers = new HashMap<>(2);
private CRDOutput<? extends OutputStream> output;
private boolean parallel;
private String header = "Generated by Fabric8 CRDGenerator, manual edits might get overwritten!";
private Map<String, CustomResourceInfo> infos;

public static final ObjectMapper YAML_MAPPER = JsonMapper.builder(new YAMLFactory()
Expand Down Expand Up @@ -116,6 +118,11 @@ public CRDGenerator forCRDVersions(String... versions) {
return this;
}

public CRDGenerator withHeader(String header) {
this.header = header;
return this;
}

Map<String, AbstractCustomResourceHandler> getHandlers() {
return handlers;
}
Expand Down Expand Up @@ -185,12 +192,12 @@ public CRDGenerationInfo detailedGenerate() {
try {
final String outputName = getOutputName(crdName, version);
try (final OutputStream outputStream = output.outputFor(outputName)) {
outputStream.write(
"# Generated by Fabric8 CRDGenerator, manual edits might get overwritten!\n"
.getBytes());
if (Utils.isNotNullOrEmpty(header)) {
outputStream.write(("# " + header + "\n").getBytes());
}
YAML_MAPPER.writeValue(outputStream, crd);
final URI fileURI = output.crdURI(outputName);
crdGenerationInfo.add(crdName, version, fileURI);
crdGenerationInfo.add(crdName, version, toStringWithFallback(fileURI));
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -260,4 +267,26 @@ public URI crdURI(String crdName) {
return getCRDFile(crdName).toURI();
}
}

/**
* Returns the absolute file path of a given URI.
* If this is not possible, it returns the URI itself as string.
* <p>
* The fallback is required during compiler testing because in this case
* the URI can be an in memory file like the following example:
* </p>
* <p>
* {@code mem:///CLASS_OUTPUT/META-INF/fabric8/multiples.sample.fabric8.io-v1.yml}
* </p>
*
* @param fileURI the URI
* @return the string representation
*/
private static String toStringWithFallback(URI fileURI) {
try {
return new File(fileURI).getAbsolutePath();
} catch (IllegalArgumentException e) {
return fileURI.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
public class CustomResourceInfo {

private static final Logger LOGGER = LoggerFactory.getLogger(CustomResourceInfo.class);
/**
* Is this used somewhere upstream?
*/
@Deprecated
public static final boolean DESCRIBE_TYPE_DEFS = false;
private final String group;
private final String version;
Expand Down Expand Up @@ -157,13 +161,17 @@ public String[] labels() {
}

public static CustomResourceInfo fromClass(Class<? extends CustomResource<?, ?>> customResource) {
return fromClass(customResource, false);
}

public static CustomResourceInfo fromClass(Class<? extends CustomResource<?, ?>> customResource, boolean describeTypeDefs) {
try {
final CustomResource<?, ?> instance = customResource.getDeclaredConstructor().newInstance();

final String[] shortNames = CustomResource.getShortNames(customResource);

final TypeDef definition = Types.typeDefFrom(customResource);
if (DESCRIBE_TYPE_DEFS) {
if (describeTypeDefs) {
Types.output(definition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
import io.fabric8.kubernetes.model.annotation.Version;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static io.fabric8.crd.generator.CRDGeneratorAssertions.assertCRDOutputEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Use Approval tests, see package example
*/
@Deprecated
class CRDGeneratorExamplesTest {

protected boolean parallelCRDGeneration;

@Test
void multiple() throws IOException {
void multiple() {
assertCRDOutputEquals(newCRDGenerator(),
io.fabric8.crd.example.multiple.v1.Multiple.class, io.fabric8.crd.example.multiple.v2.Multiple.class);
}
Expand All @@ -50,7 +52,7 @@ void multipleStorage_thenFail() {
}

@Test
void k8sValidation() throws IOException {
void k8sValidation() {
assertCRDOutputEquals(newCRDGenerator(), K8sValidation.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,20 @@ void notDefiningOutputShouldNotGenerateAnything() {
assertEquals(0, generator.generate());
}

@Test
void testHeaderOverride() {
CRDGenerator generator = newCRDGenerator()
.customResourceClasses(Joke.class)
.forCRDVersions("v1")
.withHeader("my-header")
.withOutput(output);

generator.detailedGenerate();

String crdFileContent = output.getStreamFor("jokes.samples.javaoperatorsdk.io-v1").toString();
assertTrue(crdFileContent.startsWith("# my-header\n"));
}

@Test
void generatingACycleShouldFail() {
final CRDGenerator generator = newCRDGenerator()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.crd.generator;

import io.fabric8.crd.example.multiple.v2.MultipleSpec;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;

import static org.junit.jupiter.api.Assertions.assertThrows;

class MultipleStoredVersionsTest {

@Group("sample.fabric8.io")
@Version(value = "v3")
public static class Multiple extends CustomResource<MultipleSpec, Void> {
}

@Test
void generateV1_expectException(@TempDir File tmpDir) {
test("v1", false, tmpDir);
}

@Test
void generateV1beta1_expectException(@TempDir File tmpDir) {
test("v1beta1", false, tmpDir);
}

@Test
void generateV1Parallel_expectException(@TempDir File tmpDir) {
test("v1", true, tmpDir);
}

@Test
void generateV1beta1Parallel_expectException(@TempDir File tmpDir) {
test("v1beta1", true, tmpDir);
}

private void test(String crdVersion, boolean parallel, File tmpDir) {
final CRDGenerator crdGenerator = new CRDGenerator()
.inOutputDir(tmpDir)
.withParallelGenerationEnabled(parallel)
.forCRDVersions(crdVersion)
.customResourceClasses(io.fabric8.crd.example.multiple.v2.Multiple.class, Multiple.class);

assertThrows(IllegalStateException.class, crdGenerator::generate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package io.fabric8.crd.generator;

/**
* Use Approval tests, see package example
*/
@Deprecated
public class ParallelCRDGeneratorExamplesTest extends CRDGeneratorExamplesTest {
public ParallelCRDGeneratorExamplesTest() {
parallelCRDGeneration = true;
Expand Down
Loading
Loading