Skip to content

Commit cc88ea3

Browse files
authored
feat: use ConfigMapping (#918)
Signed-off-by: Chris Laprun <[email protected]>
1 parent e4928a5 commit cc88ea3

18 files changed

+136
-147
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private List<HasMetadata> bindingsFor(QuarkusControllerConfiguration<?> controll
6464

6565
// if we validate the CRDs, also create a binding for the CRD validating role
6666
List<HasMetadata> itemsToAdd;
67-
if (operatorConfiguration.crd.validate) {
67+
if (operatorConfiguration.crd().validate()) {
6868
final var crBindingName = getCRDValidatingBindingName(controllerName);
6969
final var crdValidatorRoleBinding = createClusterRoleBinding(serviceAccountName, controllerName,
7070
crBindingName, "validate CRDs", CRD_VALIDATING_ROLE_REF);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,25 @@ public BuildTimeHybridControllerConfiguration(
2929
boolean generationAware() {
3030
return ConfigurationUtils.extract(
3131
externalConfiguration,
32-
controllerAnnotation, c -> c.generationAware,
32+
controllerAnnotation, BuildTimeControllerConfiguration::generationAware,
3333
"generationAwareEventProcessing",
3434
AnnotationValue::asBoolean,
35-
() -> operatorConfiguration.generationAware.orElse(true));
35+
() -> operatorConfiguration.generationAware().orElse(true));
3636
}
3737

3838
Set<String> generateWithWatchedNamespaces(boolean wereNamespacesSet) {
3939
Set<String> namespaces = null;
4040
if (externalConfiguration != null) {
41-
Optional<List<String>> overrideNamespaces = externalConfiguration.generateWithWatchedNamespaces;
41+
Optional<List<String>> overrideNamespaces = externalConfiguration.generateWithWatchedNamespaces();
4242
if (overrideNamespaces.isPresent()) {
4343
namespaces = new HashSet<>(overrideNamespaces.get());
4444
}
4545
}
4646

4747
// check if we have an operator-level configuration only if namespaces haven't been explicitly set already
48-
if (!wereNamespacesSet && namespaces == null && operatorConfiguration.generateWithWatchedNamespaces.isPresent()) {
49-
namespaces = new HashSet<>(operatorConfiguration.generateWithWatchedNamespaces.get());
48+
final var watchedNamespaces = operatorConfiguration.generateWithWatchedNamespaces();
49+
if (!wereNamespacesSet && namespaces == null && watchedNamespaces.isPresent()) {
50+
namespaces = new HashSet<>(watchedNamespaces.get());
5051
}
5152

5253
return namespaces;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static boolean shouldApply(Optional<Boolean> configuredApply, LaunchMode launchM
4949
}
5050

5151
boolean shouldApply() {
52-
return shouldApply(crdConfiguration.apply, mode);
52+
return shouldApply(crdConfiguration.apply(), mode);
5353
}
5454

5555
/**
@@ -73,14 +73,14 @@ CRDGenerationInfo generate(OutputTargetBuildItem outputTarget,
7373
final var generated = new HashSet<String>();
7474

7575
if (needGeneration) {
76-
Path targetDirectory = crdConfiguration.outputDirectory
76+
Path targetDirectory = crdConfiguration.outputDirectory()
7777
.map(d -> Paths.get("").toAbsolutePath().resolve(d))
7878
.orElse(outputTarget.getOutputDirectory().resolve(KUBERNETES));
7979
final var outputDir = targetDirectory.toFile();
8080
FileUtils.ensureDirectoryExists(outputDir);
8181

8282
// generate CRDs with detailed information
83-
final var info = generator.forCRDVersions(crdConfiguration.versions).inOutputDir(outputDir).detailedGenerate();
83+
final var info = generator.forCRDVersions(crdConfiguration.versions()).inOutputDir(outputDir).detailedGenerate();
8484
final var crdDetailsPerNameAndVersion = info.getCRDDetailsPerNameAndVersion();
8585

8686
crdDetailsPerNameAndVersion.forEach((crdName, initialVersionToCRDInfoMap) -> {
@@ -103,7 +103,7 @@ CRDGenerationInfo generate(OutputTargetBuildItem outputTarget,
103103

104104
private boolean needsGeneration(Map<String, CRDInfo> existingCRDInfos, Set<String> changedClassNames, String targetCRName) {
105105
final boolean[] generateCurrent = { true }; // request CRD generation by default
106-
crdConfiguration.versions.forEach(v -> {
106+
crdConfiguration.versions().forEach(v -> {
107107
final var crd = existingCRDInfos.get(v);
108108
// if we don't have any information about this CRD version, we need to generate the CRD
109109
if (crd == null) {
@@ -147,7 +147,7 @@ boolean scheduleForGenerationIfNeeded(CustomResourceAugmentedClassInfo crInfo,
147147
public void withCustomResource(Class<? extends CustomResource<?, ?>> crClass, String crdName,
148148
String associatedControllerName) {
149149
// first check if the CR is not filtered out
150-
if (crdConfiguration.excludeResources.map(excluded -> excluded.contains(crClass.getName())).orElse(false)) {
150+
if (crdConfiguration.excludeResources().map(excluded -> excluded.contains(crClass.getName())).orElse(false)) {
151151
log.infov("CRD generation was skipped for ''{0}'' because it was excluded from generation", crClass.getName());
152152
return;
153153
}
@@ -156,7 +156,7 @@ public void withCustomResource(Class<? extends CustomResource<?, ?>> crClass, St
156156
// generator MUST be initialized before we start processing classes as initializing it
157157
// will reset the types information held by the generator
158158
if (generator == null) {
159-
generator = new CRDGenerator().withParallelGenerationEnabled(crdConfiguration.generateInParallel);
159+
generator = new CRDGenerator().withParallelGenerationEnabled(crdConfiguration.generateInParallel());
160160
}
161161
final var info = CustomResourceInfo.fromClass(crClass);
162162
crMappings.add(info, crdName, associatedControllerName);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ GeneratedCRDInfoBuildItem generateCRDs(
2828
LiveReloadBuildItem liveReload,
2929
OutputTargetBuildItem outputTarget,
3030
CombinedIndexBuildItem combinedIndexBuildItem) {
31-
final var crdConfig = operatorConfiguration.crd;
32-
final boolean validateCustomResources = ConfigurationUtils.shouldValidateCustomResources(crdConfig.validate);
31+
final var crdConfig = operatorConfiguration.crd();
32+
final boolean validateCustomResources = ConfigurationUtils.shouldValidateCustomResources(crdConfig.validate());
3333

3434
//apply should imply generate: we cannot apply if we're not generating!
3535
final var launchMode = launchModeBuildItem.getLaunchMode();
@@ -41,7 +41,7 @@ GeneratedCRDInfoBuildItem generateCRDs(
4141
stored = new ContextStoredCRDInfos();
4242
}
4343

44-
final var generate = CRDGeneration.shouldGenerate(crdConfig.generate, crdConfig.apply, launchMode);
44+
final var generate = CRDGeneration.shouldGenerate(crdConfig.generate(), crdConfig.apply(), launchMode);
4545
final var storedCRDInfos = stored;
4646
final var changedClasses = ConfigurationUtils.getChangedClasses(liveReload);
4747
final var scheduledForGeneration = new HashSet<String>(7);
@@ -74,7 +74,7 @@ GeneratedCRDInfoBuildItem generateCRDs(
7474
});
7575

7676
// generate non-reconciler associated CRDs if requested
77-
if (crdConfig.generateAll) {
77+
if (crdConfig.generateAll()) {
7878
ClassUtils.getProcessableSubClassesOf(Constants.CUSTOM_RESOURCE, combinedIndexBuildItem.getIndex(), log,
7979
// pass already generated CRD names so that we can only keep the unhandled ones
8080
Map.of(CustomResourceAugmentedClassInfo.EXISTING_CRDS_KEY, scheduledForGeneration))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static QuarkusControllerConfiguration createConfiguration(
149149
// extract the configuration from annotation and/or external configuration
150150
final var controllerAnnotation = info.declaredAnnotation(CONTROLLER_CONFIGURATION);
151151

152-
final var externalConfiguration = buildTimeConfiguration.controllers.get(name);
152+
final var externalConfiguration = buildTimeConfiguration.controllers().get(name);
153153
final var configExtractor = new BuildTimeHybridControllerConfiguration(buildTimeConfiguration,
154154
externalConfiguration,
155155
controllerAnnotation);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private static class IsRBACEnabled implements BooleanSupplier {
1515

1616
@Override
1717
public boolean getAsBoolean() {
18-
return !config.disableRbacGeneration;
18+
return !config.disableRbacGeneration();
1919
}
2020
}
2121

@@ -26,7 +26,7 @@ void augmentRBACForResources(BuildTimeOperatorConfiguration buildTimeConfigurati
2626

2727
final var configs = configurations.getControllerConfigs().values();
2828
decorators.produce(new DecoratorBuildItem(
29-
new AddClusterRolesDecorator(configs, buildTimeConfiguration.crd.validate)));
29+
new AddClusterRolesDecorator(configs, buildTimeConfiguration.crd().validate())));
3030
decorators.produce(new DecoratorBuildItem(
3131
new AddRoleBindingsDecorator(configs, buildTimeConfiguration)));
3232
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private void checkVersionCompatibility(BuildTimeOperatorConfiguration buildTimeC
5050
if (!expectedVersion.equals(foundVersion)) {
5151
String message = "Mismatched " + name + " version found: \"" + found + "\", expected: \"" + expected
5252
+ "\"";
53-
if (buildTimeConfiguration.failOnVersionCheck) {
53+
if (buildTimeConfiguration.failOnVersionCheck()) {
5454
throw new RuntimeException(message);
5555
} else {
5656
final var diff = expectedVersion.diff(foundVersion);

core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/helm/HelmGenerationEnabled.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class HelmGenerationEnabled implements BooleanSupplier {
99

1010
@Override
1111
public boolean getAsBoolean() {
12-
return config.helm.enabled;
12+
return config.helm().enabled();
1313
}
1414
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
import java.util.Optional;
55

66
import io.quarkus.runtime.annotations.ConfigGroup;
7-
import io.quarkus.runtime.annotations.ConfigItem;
7+
import io.smallrye.config.WithDefault;
88

99
@ConfigGroup
10-
public class BuildTimeControllerConfiguration {
10+
public interface BuildTimeControllerConfiguration {
1111

1212
/**
1313
* Whether the controller should only process events if the associated resource generation has
1414
* increased since last reconciliation, otherwise will process all events.
1515
*/
16-
@ConfigItem
17-
public Optional<Boolean> generationAware;
16+
Optional<Boolean> generationAware();
1817

1918
/**
2019
* An optional list of comma-separated watched namespace names that will be used to generate manifests at build time.
@@ -39,8 +38,7 @@ public class BuildTimeControllerConfiguration {
3938
* were concerned.
4039
* </p>
4140
*/
42-
@ConfigItem
43-
public Optional<List<String>> generateWithWatchedNamespaces;
41+
Optional<List<String>> generateWithWatchedNamespaces();
4442

4543
/**
4644
* Indicates whether the primary resource for the associated controller is unowned, meaning that another controller is the
@@ -49,6 +47,6 @@ public class BuildTimeControllerConfiguration {
4947
* required even though another controller already handles reconciliations of resources of that type. Set this property to
5048
* {@code true} if you want to indicate that the controller doesn't own its primary resource
5149
*/
52-
@ConfigItem(defaultValue = "false")
53-
public boolean unownedPrimary;
50+
@WithDefault("false")
51+
boolean unownedPrimary();
5452
}

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,60 @@
44
import java.util.Map;
55
import java.util.Optional;
66

7-
import io.quarkus.runtime.annotations.ConfigItem;
87
import io.quarkus.runtime.annotations.ConfigPhase;
98
import io.quarkus.runtime.annotations.ConfigRoot;
9+
import io.smallrye.config.ConfigMapping;
10+
import io.smallrye.config.WithDefault;
11+
import io.smallrye.config.WithName;
1012

11-
@ConfigRoot(name = "operator-sdk", phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
12-
public class BuildTimeOperatorConfiguration {
13+
@ConfigMapping(prefix = "quarkus.operator-sdk")
14+
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
15+
public interface BuildTimeOperatorConfiguration {
1316

1417
/**
1518
* Maps a controller name to its configuration.
1619
*/
17-
@ConfigItem
18-
public Map<String, BuildTimeControllerConfiguration> controllers;
20+
Map<String, BuildTimeControllerConfiguration> controllers();
1921

2022
/**
2123
* The optional CRD-related configuration options
2224
*/
23-
@ConfigItem
24-
public CRDConfiguration crd;
25+
CRDConfiguration crd();
2526

2627
/**
2728
* Whether controllers should only process events if the associated resource generation has
2829
* increased since last reconciliation, otherwise will process all events. Sets the default value
2930
* for all controllers.
3031
*/
31-
@ConfigItem(defaultValue = "true")
32-
public Optional<Boolean> generationAware;
32+
@WithDefault("true")
33+
Optional<Boolean> generationAware();
3334

3435
/**
3536
* Whether Role-Based Access Control (RBAC) resources generated by the Kubernetes extension should be augmented by this
3637
* extension.
3738
*/
38-
@ConfigItem(defaultValue = "false")
39-
public Boolean disableRbacGeneration;
39+
@WithDefault("false")
40+
Boolean disableRbacGeneration();
4041

4142
/**
4243
* Whether the operator should be automatically started or not. Mostly useful for testing
4344
* scenarios.
4445
*/
45-
@ConfigItem(defaultValue = "true")
46-
public Boolean startOperator;
46+
@WithDefault("true")
47+
Boolean startOperator();
4748

4849
/**
4950
* Whether the injected Kubernetes client should be stopped when the operator is stopped.
5051
*/
51-
@ConfigItem(defaultValue = "true")
52-
public Boolean closeClientOnStop;
52+
@WithDefault("true")
53+
Boolean closeClientOnStop();
5354

5455
/**
5556
* Whether the operator should stop if an informer error (such as one caused by missing / improper
5657
* RBACs) occurs during startup.
5758
*/
58-
@ConfigItem(defaultValue = "true")
59-
public Boolean stopOnInformerErrorDuringStartup;
59+
@WithDefault("true")
60+
Boolean stopOnInformerErrorDuringStartup();
6061

6162
/**
6263
* Whether to fail or emit a debug-level (warning-level when misalignment is at the minor or above version level) log when
@@ -69,38 +70,37 @@ public class BuildTimeOperatorConfiguration {
6970
* <li>Fabric8 client version used by Quarkus vs. actually used Fabric8 client version</li>
7071
* </ul>
7172
*/
72-
@ConfigItem(defaultValue = "false")
73-
public Boolean failOnVersionCheck;
73+
@WithDefault("false")
74+
Boolean failOnVersionCheck();
7475

7576
/**
7677
* The list of profile names for which leader election should be activated. This is mostly useful for testing scenarios
7778
* where leader election behavior might lead to issues.
7879
*/
79-
@ConfigItem(defaultValue = "prod")
80-
public List<String> activateLeaderElectionForProfiles;
80+
@WithDefault("prod")
81+
List<String> activateLeaderElectionForProfiles();
8182

8283
/**
8384
* The optional Server-Side Apply (SSA) related configuration.
8485
*/
85-
@ConfigItem(name = "enable-ssa", defaultValue = "true")
86-
public boolean enableSSA;
86+
@WithName("enable-ssa")
87+
@WithDefault("true")
88+
boolean enableSSA();
8789

8890
/**
8991
* An optional list of comma-separated watched namespace names that will be used to generate manifests at build time if
9092
* controllers do <strong>NOT</strong> specify a value individually. See
9193
* {@link BuildTimeControllerConfiguration#generateWithWatchedNamespaces} for more information.
9294
*/
93-
@ConfigItem
94-
public Optional<List<String>> generateWithWatchedNamespaces;
95+
Optional<List<String>> generateWithWatchedNamespaces();
9596

9697
/**
9798
* Helm Chart related configurations.
9899
*/
99-
@ConfigItem
100-
public HelmConfiguration helm;
100+
HelmConfiguration helm();
101101

102-
public boolean isControllerOwningPrimary(String controllerName) {
103-
final var controllerConfiguration = controllers.get(controllerName);
104-
return controllerConfiguration == null || !controllerConfiguration.unownedPrimary;
102+
default boolean isControllerOwningPrimary(String controllerName) {
103+
final var controllerConfiguration = controllers().get(controllerName);
104+
return controllerConfiguration == null || !controllerConfiguration.unownedPrimary();
105105
}
106106
}

0 commit comments

Comments
 (0)