Skip to content

Commit

Permalink
fix: runtime namespaces are now correctly set from controller propert…
Browse files Browse the repository at this point in the history
…ies (#726) (#784)

Fixes #725

Co-authored-by: Jakub Cechacek <jcechace@gmail.com>
  • Loading branch information
metacosm and jcechace authored Dec 12, 2023
1 parent 503735d commit bebcb15
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.quarkiverse.operatorsdk.runtime.Constants.QOSDK_USE_BUILDTIME_NAMESPACES_SET;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
Expand Down Expand Up @@ -45,8 +46,8 @@ public Supplier<QuarkusConfigurationService> configurationServiceSupplier(Versio
if (extConfig != null) {
extConfig.finalizer.ifPresent(c::setFinalizer);
extConfig.selector.ifPresent(c::setLabelSelector);
extConfig.namespaces.map(HashSet::new).ifPresent(c::setNamespaces);
c.setRetryConfiguration(RetryConfigurationResolver.resolve(extConfig.retry));
setNamespacesFromRuntime(c, extConfig.namespaces);
}

// set retry to default if it hasn't been set already
Expand All @@ -58,22 +59,9 @@ public Supplier<QuarkusConfigurationService> configurationServiceSupplier(Versio
c.setRetryConfiguration(null);
}

// if the namespaces weren't set as an annotation, use the operator-level configuration if it exists
// if the namespaces weren't set on controller level or as an annotation, use the operator-level configuration if it exists
if (!c.isWereNamespacesSet()) {
// The namespaces field has a default value so that we are able to detect if the configuration value is set to "".
// Setting the value to "" will reset the configuration and result in an empty Optional.
// Not setting the value at all will result in the default being applied, which we can test for.
if (runTimeConfiguration.namespaces.isPresent()) {
final var runtimeNamespaces = new HashSet<>(runTimeConfiguration.namespaces.get());
// If it's not the default value, use it because it was set.
// If it is the default value, ignore it and let any build time config be used.
if (!QOSDK_USE_BUILDTIME_NAMESPACES_SET.equals(runtimeNamespaces)) {
c.setNamespaces(runtimeNamespaces);
}
} else {
// Value has been explicitly reset (value was empty string), use all namespaces mode
c.setNamespaces(DEFAULT_NAMESPACES_SET);
}
setNamespacesFromRuntime(c, runTimeConfiguration.namespaces);
}
});

Expand Down Expand Up @@ -110,6 +98,25 @@ public Supplier<QuarkusConfigurationService> configurationServiceSupplier(Versio
};
}

@SuppressWarnings({ "rawtypes", "unchecked", "OptionalUsedAsFieldOrParameterType" })
private static void setNamespacesFromRuntime(QuarkusControllerConfiguration controllerConfig,
Optional<List<String>> runtimeNamespaces) {
// The namespaces field has a default value so that we are able to detect if the configuration value is set to "".
// Setting the value to "" will reset the configuration and result in an empty Optional.
// Not setting the value at all will result in the default being applied, which we can test for.
if (runtimeNamespaces.isPresent()) {
final var namespaces = new HashSet<>(runtimeNamespaces.get());
// If it's not the default value, use it because it was set.
// If it is the default value, ignore it and let any build time config be used.
if (!QOSDK_USE_BUILDTIME_NAMESPACES_SET.equals(namespaces)) {
controllerConfig.setNamespaces(namespaces);
}
} else {
// Value has been explicitly reset (value was empty string), use all namespaces mode
controllerConfig.setNamespaces(DEFAULT_NAMESPACES_SET);
}
}

static boolean shouldStartOperator(Optional<Boolean> fromConfiguration, LaunchMode launchMode) {
if (fromConfiguration == null || fromConfiguration.isEmpty()) {
return LaunchMode.TEST != launchMode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkiverse.operatorsdk.runtime;

import static io.quarkiverse.operatorsdk.runtime.Constants.QOSDK_USE_BUILDTIME_NAMESPACES;

import java.util.List;
import java.util.Optional;

Expand All @@ -15,7 +17,7 @@ public class RunTimeControllerConfiguration {
* The value can be set to "JOSDK_WATCH_CURRENT" to watch the current (default) namespace from kube config.
* Constant(s) can be found in at {@link io.javaoperatorsdk.operator.api.reconciler.Constants}".
*/
@ConfigItem
@ConfigItem(defaultValue = QOSDK_USE_BUILDTIME_NAMESPACES)
public Optional<List<String>> namespaces;

/**
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/includes/quarkus-operator-sdk.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_OPERATOR_SDK_CONTROLLERS__CONTROLLERS__NAMESPACES+++`
endif::add-copy-button-to-env-var[]
--|list of string
|
|`QOSDK_USE_BUILDTIME_NAMESPACES`


a| [[quarkus-operator-sdk_quarkus.operator-sdk.controllers.-controllers-.finalizer]]`link:#quarkus-operator-sdk_quarkus.operator-sdk.controllers.-controllers-.finalizer[quarkus.operator-sdk.controllers."controllers".finalizer]`
Expand Down

0 comments on commit bebcb15

Please sign in to comment.