Skip to content

Commit

Permalink
fix: trim namespaces when setting them, add more tests (#680)
Browse files Browse the repository at this point in the history
Fixes #678
  • Loading branch information
metacosm authored Aug 16, 2023
1 parent c72a69b commit 5044f08
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.lang.annotation.Annotation;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.model.HasMetadata;
Expand Down Expand Up @@ -115,7 +112,7 @@ public QuarkusControllerConfiguration(
this.dependentsMetadata = dependentsMetadata;
this.workflow = workflow;
this.retryConfiguration = ControllerConfiguration.super.getRetryConfiguration();
this.namespaces = Set.copyOf(namespaces);
setNamespaces(namespaces);
this.wereNamespacesSet = wereNamespacesSet;
setFinalizer(finalizerName);
this.labelSelector = labelSelector;
Expand Down Expand Up @@ -193,9 +190,9 @@ public Set<String> getNamespaces() {
}

@SuppressWarnings("unchecked")
void setNamespaces(Set<String> namespaces) {
void setNamespaces(Collection<String> namespaces) {
if (!namespaces.equals(this.namespaces)) {
this.namespaces = namespaces;
this.namespaces = namespaces.stream().map(String::trim).collect(Collectors.toSet());
wereNamespacesSet = true;
// propagate namespace changes to the dependents' config if needed
this.dependentsMetadata.forEach((name, spec) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
quarkus.operator-sdk.namespaces=operator-level
#quarkus.operator-sdk.namespaces=operator-level
quarkus.operator-sdk.generation-aware=false
# this shouldn't impact anything for the tests since we're not checking manifest generation here
quarkus.operator-sdk.generate-with-watched-namespaces=operator-level-for-manifests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.quarkiverse.operatorsdk.it;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;

@QuarkusTest
@TestProfile(EmptyOperatorLevelNamespacesTestProfile.class)
public class EmptyOperatorLevelNamespacesTest {

@Test
public void reconcilersWithoutSpecificNamespacesShouldWatchAllNamespaces() {
given()
.when().get("/operator/" + TestReconciler.NAME +
"/config")
.then()
.statusCode(200)
.body("watchAllNamespaces", is(true));
}

@Test
public void reconcilerWithSpecificNamespacesShouldUseThem() {
given()
.when().get("/operator/" + ApplicationScopedReconciler.NAME +
"/config")
.then()
.statusCode(200)
.body("watchAllNamespaces", is(false))
.body("namespaces.size()", is(1))
.body("namespaces[0]", equalTo("default"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkiverse.operatorsdk.it;

import java.util.Collections;
import java.util.Map;

import io.quarkus.test.junit.QuarkusTestProfile;

public class EmptyOperatorLevelNamespacesTestProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Collections.singletonMap("quarkus.operator-sdk.namespaces", "");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void allControllersShouldHaveAssociatedConfiguration() {
}

@Test
void configurationForControllerShouldExistAndUseOperatorLevelConfigurationWhenSet() {
void configurationForControllerShouldExistAndUseBuildTimeOperatorLevelConfigurationWhenSet() {
// check that the config for the test controller can be retrieved and is conform to our
// expectations
final var resourceName = io.quarkiverse.operatorsdk.it.Test.class.getCanonicalName();
Expand All @@ -127,8 +127,10 @@ void configurationForControllerShouldExistAndUseOperatorLevelConfigurationWhenSe
"customResourceClass", equalTo(resourceName),
"name", equalTo(TestReconciler.NAME),
"watchCurrentNamespace", equalTo(false),
"namespaces", hasSize(1),
"namespaces", hasItem("operator-level"),
// build time values are propagated at runtime if no runtime value is specified
"namespaces", hasSize(2),
"namespaces", hasItem("builtime-namespace1"),
"namespaces", hasItem("buildtime-ns2"),
"retry.maxAttempts", equalTo(1),
"generationAware", equalTo(false),
"maxReconciliationIntervalSeconds", equalTo(TestReconciler.INTERVAL));
Expand Down Expand Up @@ -173,7 +175,7 @@ void dependentAnnotationsShouldAppearInConfiguration() {
.statusCode(200).body(
"watchCurrentNamespace", equalTo(false),
"namespaces", hasSize(1),
"namespaces", hasItem("operator-level"),
"namespaces", hasItem("operator-level-for-manifests"),
"dependents", hasSize(2),
"dependents.dependentClass",
hasItems(ReadOnlyDependentResource.class.getCanonicalName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.quarkiverse.operatorsdk.it;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;

@QuarkusTest
@TestProfile(SetOperatorLevelNamespacesTestProfile.class)
public class SetOperatorLevelNamespacesTest {

@Test
void configurationForControllerShouldExistAndUseOperatorLevelConfigurationWhenSet() {
// check that the config for the test controller can be retrieved and is conform to our
// expectations
final var resourceName = io.quarkiverse.operatorsdk.it.Test.class.getCanonicalName();
given()
.when()
.get("/operator/" + TestReconciler.NAME + "/config")
.then()
.statusCode(200)
.body(
"customResourceClass", equalTo(resourceName),
"name", equalTo(TestReconciler.NAME),
"watchCurrentNamespace", is(false),
"namespaces", hasSize(1),
"namespaces", hasItem("operator-level"),
"retry.maxAttempts", equalTo(1),
"generationAware", equalTo(false),
"maxReconciliationIntervalSeconds", equalTo(TestReconciler.INTERVAL));
}

@Test
void dependentAnnotationsShouldAppearInConfiguration() {
given()
.when()
.get("/operator/" + DependentDefiningReconciler.NAME + "/config")
.then()
.statusCode(200).body(
"watchCurrentNamespace", Matchers.equalTo(false),
"namespaces", hasSize(1),
"namespaces", hasItem("operator-level"),
"dependents", hasSize(2),
"dependents.dependentClass",
hasItems(ReadOnlyDependentResource.class.getCanonicalName(),
CRUDDependentResource.class.getCanonicalName()),
"dependents.dependentConfig.labelSelector",
hasItems(ReadOnlyDependentResource.LABEL_SELECTOR, CRUDDependentResource.LABEL_SELECTOR),
"dependents.dependentConfig.onAddFilter",
hasItem(CRUDDependentResource.TestOnAddFilter.class.getCanonicalName()),
"dependents.dependentConfig.resourceDiscriminator",
hasItems(ReadOnlyDependentResource.ReadOnlyResourceDiscriminator.class.getCanonicalName(),
CRUDDependentResource.TestResourceDiscriminator.class.getCanonicalName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkiverse.operatorsdk.it;

import java.util.Collections;
import java.util.Map;

import io.quarkus.test.junit.QuarkusTestProfile;

public class SetOperatorLevelNamespacesTestProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Collections.singletonMap("quarkus.operator-sdk.namespaces", "operator-level");
}

}

0 comments on commit 5044f08

Please sign in to comment.