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

fix: runtime namespaces are now correctly set from controller properties (#726) #784

Merged
merged 1 commit into from
Dec 12, 2023
Merged
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
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
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