Skip to content

Commit

Permalink
fix: Treat empty runtime namespaces as All Namespaces mode (#658)
Browse files Browse the repository at this point in the history
If the runtime configuration specifies an empty string as the namespaces to watch, we should treat that as all namespaces mode, mainly because that's how OLM signifies that an operator should watch all namespaces.

An empty string is treated the same as unset by the configuration parser, so we need to set a default on the field so we can explicitly check for it being unset, to determine if the empty string was used.

Fixes #656

Signed-off-by: James Hewitt <james.hewitt@uk.ibm.com>
  • Loading branch information
Jamstah authored Aug 11, 2023
1 parent 94d4c18 commit ad898c3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.quarkiverse.operatorsdk.runtime;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.DEFAULT_NAMESPACES_SET;
import static io.quarkiverse.operatorsdk.runtime.Constants.QOSDK_USE_BUILDTIME_NAMESPACES_SET;

import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -55,9 +58,22 @@ public Supplier<QuarkusConfigurationService> configurationServiceSupplier(Versio
c.setRetryConfiguration(null);
}

// if despite all of this, we still haven't set the namespaces, use the operator-level default if it exists
// if the namespaces weren't set as an annotation, use the operator-level configuration if it exists
if (!c.isWereNamespacesSet()) {
runTimeConfiguration.namespaces.ifPresent(ns -> c.setNamespaces(new HashSet<>(ns)));
// 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);
}
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkiverse.operatorsdk.runtime;

import java.util.Collections;
import java.util.Set;

public final class Constants {
private Constants() {
}

public static final Set<String> QOSDK_USE_BUILDTIME_NAMESPACES_SET = Collections
.singleton(Constants.QOSDK_USE_BUILDTIME_NAMESPACES);

public static final String QOSDK_USE_BUILDTIME_NAMESPACES = "QOSDK_USE_BUILDTIME_NAMESPACES";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package io.quarkiverse.operatorsdk.runtime;

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

import java.time.Duration;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -59,7 +61,10 @@ public class RunTimeOperatorConfiguration {
* specified by the kube config file the operator uses.
* </p>
*/
@ConfigItem
// We use a default here 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, but not setting the value at all will result in the default being
// applied.
@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 @@ -319,7 +319,7 @@ ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_OPERATOR_SDK_NAMESPACES+++`
endif::add-copy-button-to-env-var[]
--|list of string
|
|`QOSDK_USE_BUILDTIME_NAMESPACES`


a| [[quarkus-operator-sdk_quarkus.operator-sdk.concurrent-workflow-threads]]`link:#quarkus-operator-sdk_quarkus.operator-sdk.concurrent-workflow-threads[quarkus.operator-sdk.concurrent-workflow-threads]`
Expand Down

0 comments on commit ad898c3

Please sign in to comment.