Skip to content

Commit 60bef18

Browse files
committed
feat: make it possible to revert to using the v1 CRD generator
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>
1 parent 91d3b09 commit 60bef18

File tree

6 files changed

+130
-20
lines changed

6 files changed

+130
-20
lines changed

core/deployment/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<groupId>io.fabric8</groupId>
5151
<artifactId>crd-generator-api-v2</artifactId>
5252
</dependency>
53+
<dependency>
54+
<groupId>io.fabric8</groupId>
55+
<artifactId>crd-generator-api</artifactId>
56+
</dependency>
5357
<dependency>
5458
<groupId>org.semver4j</groupId>
5559
<artifactId>semver4j</artifactId>

core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CRDGeneration.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
import org.jboss.logging.Logger;
1313

14-
import io.fabric8.crdv2.generator.CRDGenerator;
15-
import io.fabric8.crdv2.generator.CustomResourceInfo;
1614
import io.fabric8.kubernetes.client.CustomResource;
1715
import io.quarkiverse.operatorsdk.common.CustomResourceAugmentedClassInfo;
1816
import io.quarkiverse.operatorsdk.common.FileUtils;
@@ -79,21 +77,7 @@ CRDGenerationInfo generate(OutputTargetBuildItem outputTarget,
7977
FileUtils.ensureDirectoryExists(outputDir);
8078

8179
// generate CRDs with detailed information
82-
final var info = generator.forCRDVersions(crdConfiguration.versions()).inOutputDir(outputDir).detailedGenerate();
83-
final var crdDetailsPerNameAndVersion = info.getCRDDetailsPerNameAndVersion();
84-
85-
crdDetailsPerNameAndVersion.forEach((crdName, initialVersionToCRDInfoMap) -> {
86-
log.infov("Generated {0} CRD:", crdName);
87-
generated.add(crdName);
88-
89-
initialVersionToCRDInfoMap
90-
.forEach((crdSpecVersion, crdInfo) -> {
91-
final var filePath = crdInfo.getFilePath();
92-
log.infov(" - {0} -> {1}", crdSpecVersion, filePath);
93-
converted.addCRDInfo(new CRDInfo(crdName,
94-
crdSpecVersion, filePath, crdInfo.getDependentClassNames()));
95-
});
96-
});
80+
generator.generate(crdConfiguration.versions(), outputDir, generated, converted);
9781
}
9882
return new CRDGenerationInfo(shouldApply(), validateCustomResources, converted, generated);
9983
}
@@ -145,10 +129,10 @@ void withCustomResource(Class<? extends CustomResource<?, ?>> crClass, String as
145129
// generator MUST be initialized before we start processing classes as initializing it
146130
// will reset the types information held by the generator
147131
if (generator == null) {
148-
generator = new CRDGenerator().withParallelGenerationEnabled(crdConfiguration.generateInParallel());
132+
generator = crdConfiguration.useV1CRDGenerator() ? new CRDGeneratorV1(crdConfiguration.generateInParallel())
133+
: new CRDGeneratorV2(crdConfiguration.generateInParallel());
149134
}
150-
final var info = CustomResourceInfo.fromClass(crClass);
151-
generator.customResources(info);
135+
generator.scheduleForGeneration(crClass);
152136
needGeneration = true;
153137
} catch (Exception e) {
154138
throw new IllegalArgumentException("Cannot process " + crClass.getName() + " custom resource"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.quarkiverse.operatorsdk.deployment;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
import io.fabric8.kubernetes.client.CustomResource;
8+
import io.quarkiverse.operatorsdk.runtime.CRDInfos;
9+
10+
interface CRDGenerator {
11+
void generate(List<String> crdSpecVersions, File outputDir, Set<String> generated, CRDInfos converted);
12+
13+
void scheduleForGeneration(Class<? extends CustomResource<?, ?>> crClass);
14+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.quarkiverse.operatorsdk.deployment;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
import org.jboss.logging.Logger;
8+
9+
import io.fabric8.crd.generator.CustomResourceInfo;
10+
import io.fabric8.kubernetes.client.CustomResource;
11+
import io.quarkiverse.operatorsdk.runtime.CRDInfo;
12+
import io.quarkiverse.operatorsdk.runtime.CRDInfos;
13+
14+
class CRDGeneratorV1 implements CRDGenerator {
15+
private static final Logger log = Logger.getLogger(CRDGeneratorV1.class.getName());
16+
private final io.fabric8.crd.generator.CRDGenerator generator;
17+
18+
public CRDGeneratorV1(boolean parallelGeneration) {
19+
this.generator = new io.fabric8.crd.generator.CRDGenerator().withParallelGenerationEnabled(parallelGeneration);
20+
}
21+
22+
@Override
23+
public void generate(List<String> crdSpecVersions, File outputDir, Set<String> generated, CRDInfos converted) {
24+
final var info = generator.forCRDVersions(crdSpecVersions).inOutputDir(outputDir).detailedGenerate();
25+
final var crdDetailsPerNameAndVersion = info.getCRDDetailsPerNameAndVersion();
26+
27+
crdDetailsPerNameAndVersion.forEach((crdName, initialVersionToCRDInfoMap) -> {
28+
log.infov("Generated {0} CRD:", crdName);
29+
generated.add(crdName);
30+
31+
initialVersionToCRDInfoMap
32+
.forEach((crdSpecVersion, crdInfo) -> {
33+
final var filePath = crdInfo.getFilePath();
34+
log.infov(" - {0} -> {1}", crdSpecVersion, filePath);
35+
converted.addCRDInfo(new CRDInfo(crdName,
36+
crdSpecVersion, filePath, crdInfo.getDependentClassNames()));
37+
});
38+
});
39+
}
40+
41+
@Override
42+
public void scheduleForGeneration(Class<? extends CustomResource<?, ?>> crClass) {
43+
final var info = CustomResourceInfo.fromClass(crClass);
44+
generator.customResources(info);
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.quarkiverse.operatorsdk.deployment;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
import org.jboss.logging.Logger;
8+
9+
import io.fabric8.crdv2.generator.CustomResourceInfo;
10+
import io.fabric8.kubernetes.client.CustomResource;
11+
import io.quarkiverse.operatorsdk.runtime.CRDInfo;
12+
import io.quarkiverse.operatorsdk.runtime.CRDInfos;
13+
14+
class CRDGeneratorV2 implements CRDGenerator {
15+
private static final Logger log = Logger.getLogger(CRDGeneratorV2.class.getName());
16+
private final io.fabric8.crdv2.generator.CRDGenerator generator;
17+
18+
public CRDGeneratorV2(boolean parallelGeneration) {
19+
this.generator = new io.fabric8.crdv2.generator.CRDGenerator().withParallelGenerationEnabled(parallelGeneration);
20+
}
21+
22+
@Override
23+
public void generate(List<String> crdSpecVersions, File outputDir, Set<String> generated, CRDInfos converted) {
24+
final var info = generator.forCRDVersions(crdSpecVersions).inOutputDir(outputDir).detailedGenerate();
25+
final var crdDetailsPerNameAndVersion = info.getCRDDetailsPerNameAndVersion();
26+
27+
crdDetailsPerNameAndVersion.forEach((crdName, initialVersionToCRDInfoMap) -> {
28+
log.infov("Generated {0} CRD:", crdName);
29+
generated.add(crdName);
30+
31+
initialVersionToCRDInfoMap
32+
.forEach((crdSpecVersion, crdInfo) -> {
33+
final var filePath = crdInfo.getFilePath();
34+
log.infov(" - {0} -> {1}", crdSpecVersion, filePath);
35+
converted.addCRDInfo(new CRDInfo(crdName,
36+
crdSpecVersion, filePath, crdInfo.getDependentClassNames()));
37+
});
38+
});
39+
}
40+
41+
@Override
42+
public void scheduleForGeneration(Class<? extends CustomResource<?, ?>> crClass) {
43+
final var info = CustomResourceInfo.fromClass(crClass);
44+
generator.customResources(info);
45+
}
46+
}

core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/CRDConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
import io.fabric8.kubernetes.client.CustomResource;
77
import io.quarkus.runtime.annotations.ConfigGroup;
88
import io.smallrye.config.WithDefault;
9+
import io.smallrye.config.WithName;
910

1011
@ConfigGroup
1112
public interface CRDConfiguration {
1213

1314
String DEFAULT_OUTPUT_DIRECTORY = "kubernetes";
1415
String DEFAULT_VALIDATE = "true";
1516
String DEFAULT_VERSIONS = "v1";
17+
String DEFAULT_USE_V1_CRD_GENERATOR = "false";
1618

1719
/**
1820
* Whether the operator should check that the CRD is properly deployed and that the associated
@@ -78,4 +80,18 @@ public interface CRDConfiguration {
7880
* @since 6.8.4
7981
*/
8082
Optional<List<String>> externalCRDLocations();
83+
84+
/**
85+
* Whether or not to use the v1 version of the CRD generator. Note that this should only be used if a compatibility issue is
86+
* found with the v2 generator, which is the default one and the one that is actively maintained.
87+
*
88+
* @return {@code true} if the v1 version of the CRD generator should be used
89+
* @since 6.9.1
90+
* @deprecated using this method should be reserved for blocking situations, please open an issue reporting the problem
91+
* you're facing with the v2 generator before reverting to use the v1 version
92+
*/
93+
@Deprecated(forRemoval = true)
94+
@WithDefault(DEFAULT_USE_V1_CRD_GENERATOR)
95+
@WithName("use-deprecated-v1-crd-generator")
96+
Boolean useV1CRDGenerator();
8197
}

0 commit comments

Comments
 (0)