Skip to content

Commit

Permalink
feat: make it possible to revert to using the v1 CRD generator
Browse files Browse the repository at this point in the history
Note that this should only be used if a blocking issue is found with the
v2 generator because the v1 version won't be supported moving forward.

Signed-off-by: Chris Laprun <claprun@redhat.com>
  • Loading branch information
metacosm committed Nov 25, 2024
1 parent 91d3b09 commit 60bef18
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 20 deletions.
4 changes: 4 additions & 0 deletions core/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-api-v2</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-api</artifactId>
</dependency>
<dependency>
<groupId>org.semver4j</groupId>
<artifactId>semver4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

import org.jboss.logging.Logger;

import io.fabric8.crdv2.generator.CRDGenerator;
import io.fabric8.crdv2.generator.CustomResourceInfo;
import io.fabric8.kubernetes.client.CustomResource;
import io.quarkiverse.operatorsdk.common.CustomResourceAugmentedClassInfo;
import io.quarkiverse.operatorsdk.common.FileUtils;
Expand Down Expand Up @@ -79,21 +77,7 @@ CRDGenerationInfo generate(OutputTargetBuildItem outputTarget,
FileUtils.ensureDirectoryExists(outputDir);

// generate CRDs with detailed information
final var info = generator.forCRDVersions(crdConfiguration.versions()).inOutputDir(outputDir).detailedGenerate();
final var crdDetailsPerNameAndVersion = info.getCRDDetailsPerNameAndVersion();

crdDetailsPerNameAndVersion.forEach((crdName, initialVersionToCRDInfoMap) -> {
log.infov("Generated {0} CRD:", crdName);
generated.add(crdName);

initialVersionToCRDInfoMap
.forEach((crdSpecVersion, crdInfo) -> {
final var filePath = crdInfo.getFilePath();
log.infov(" - {0} -> {1}", crdSpecVersion, filePath);
converted.addCRDInfo(new CRDInfo(crdName,
crdSpecVersion, filePath, crdInfo.getDependentClassNames()));
});
});
generator.generate(crdConfiguration.versions(), outputDir, generated, converted);
}
return new CRDGenerationInfo(shouldApply(), validateCustomResources, converted, generated);
}
Expand Down Expand Up @@ -145,10 +129,10 @@ void withCustomResource(Class<? extends CustomResource<?, ?>> crClass, String as
// generator MUST be initialized before we start processing classes as initializing it
// will reset the types information held by the generator
if (generator == null) {
generator = new CRDGenerator().withParallelGenerationEnabled(crdConfiguration.generateInParallel());
generator = crdConfiguration.useV1CRDGenerator() ? new CRDGeneratorV1(crdConfiguration.generateInParallel())
: new CRDGeneratorV2(crdConfiguration.generateInParallel());
}
final var info = CustomResourceInfo.fromClass(crClass);
generator.customResources(info);
generator.scheduleForGeneration(crClass);
needGeneration = true;
} catch (Exception e) {
throw new IllegalArgumentException("Cannot process " + crClass.getName() + " custom resource"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkiverse.operatorsdk.deployment;

import java.io.File;
import java.util.List;
import java.util.Set;

import io.fabric8.kubernetes.client.CustomResource;
import io.quarkiverse.operatorsdk.runtime.CRDInfos;

interface CRDGenerator {
void generate(List<String> crdSpecVersions, File outputDir, Set<String> generated, CRDInfos converted);

void scheduleForGeneration(Class<? extends CustomResource<?, ?>> crClass);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkiverse.operatorsdk.deployment;

import java.io.File;
import java.util.List;
import java.util.Set;

import org.jboss.logging.Logger;

import io.fabric8.crd.generator.CustomResourceInfo;
import io.fabric8.kubernetes.client.CustomResource;
import io.quarkiverse.operatorsdk.runtime.CRDInfo;
import io.quarkiverse.operatorsdk.runtime.CRDInfos;

class CRDGeneratorV1 implements CRDGenerator {
private static final Logger log = Logger.getLogger(CRDGeneratorV1.class.getName());
private final io.fabric8.crd.generator.CRDGenerator generator;

public CRDGeneratorV1(boolean parallelGeneration) {
this.generator = new io.fabric8.crd.generator.CRDGenerator().withParallelGenerationEnabled(parallelGeneration);
}

@Override
public void generate(List<String> crdSpecVersions, File outputDir, Set<String> generated, CRDInfos converted) {
final var info = generator.forCRDVersions(crdSpecVersions).inOutputDir(outputDir).detailedGenerate();
final var crdDetailsPerNameAndVersion = info.getCRDDetailsPerNameAndVersion();

crdDetailsPerNameAndVersion.forEach((crdName, initialVersionToCRDInfoMap) -> {
log.infov("Generated {0} CRD:", crdName);
generated.add(crdName);

initialVersionToCRDInfoMap
.forEach((crdSpecVersion, crdInfo) -> {
final var filePath = crdInfo.getFilePath();
log.infov(" - {0} -> {1}", crdSpecVersion, filePath);
converted.addCRDInfo(new CRDInfo(crdName,
crdSpecVersion, filePath, crdInfo.getDependentClassNames()));
});
});
}

@Override
public void scheduleForGeneration(Class<? extends CustomResource<?, ?>> crClass) {
final var info = CustomResourceInfo.fromClass(crClass);
generator.customResources(info);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkiverse.operatorsdk.deployment;

import java.io.File;
import java.util.List;
import java.util.Set;

import org.jboss.logging.Logger;

import io.fabric8.crdv2.generator.CustomResourceInfo;
import io.fabric8.kubernetes.client.CustomResource;
import io.quarkiverse.operatorsdk.runtime.CRDInfo;
import io.quarkiverse.operatorsdk.runtime.CRDInfos;

class CRDGeneratorV2 implements CRDGenerator {
private static final Logger log = Logger.getLogger(CRDGeneratorV2.class.getName());
private final io.fabric8.crdv2.generator.CRDGenerator generator;

public CRDGeneratorV2(boolean parallelGeneration) {
this.generator = new io.fabric8.crdv2.generator.CRDGenerator().withParallelGenerationEnabled(parallelGeneration);
}

@Override
public void generate(List<String> crdSpecVersions, File outputDir, Set<String> generated, CRDInfos converted) {
final var info = generator.forCRDVersions(crdSpecVersions).inOutputDir(outputDir).detailedGenerate();
final var crdDetailsPerNameAndVersion = info.getCRDDetailsPerNameAndVersion();

crdDetailsPerNameAndVersion.forEach((crdName, initialVersionToCRDInfoMap) -> {
log.infov("Generated {0} CRD:", crdName);
generated.add(crdName);

initialVersionToCRDInfoMap
.forEach((crdSpecVersion, crdInfo) -> {
final var filePath = crdInfo.getFilePath();
log.infov(" - {0} -> {1}", crdSpecVersion, filePath);
converted.addCRDInfo(new CRDInfo(crdName,
crdSpecVersion, filePath, crdInfo.getDependentClassNames()));
});
});
}

@Override
public void scheduleForGeneration(Class<? extends CustomResource<?, ?>> crClass) {
final var info = CustomResourceInfo.fromClass(crClass);
generator.customResources(info);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import io.fabric8.kubernetes.client.CustomResource;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;

@ConfigGroup
public interface CRDConfiguration {

String DEFAULT_OUTPUT_DIRECTORY = "kubernetes";
String DEFAULT_VALIDATE = "true";
String DEFAULT_VERSIONS = "v1";
String DEFAULT_USE_V1_CRD_GENERATOR = "false";

/**
* Whether the operator should check that the CRD is properly deployed and that the associated
Expand Down Expand Up @@ -78,4 +80,18 @@ public interface CRDConfiguration {
* @since 6.8.4
*/
Optional<List<String>> externalCRDLocations();

/**
* Whether or not to use the v1 version of the CRD generator. Note that this should only be used if a compatibility issue is
* found with the v2 generator, which is the default one and the one that is actively maintained.
*
* @return {@code true} if the v1 version of the CRD generator should be used
* @since 6.9.1
* @deprecated using this method should be reserved for blocking situations, please open an issue reporting the problem
* you're facing with the v2 generator before reverting to use the v1 version
*/
@Deprecated(forRemoval = true)
@WithDefault(DEFAULT_USE_V1_CRD_GENERATOR)
@WithName("use-deprecated-v1-crd-generator")
Boolean useV1CRDGenerator();
}

0 comments on commit 60bef18

Please sign in to comment.