diff --git a/operator-framework/src/main/java/io/javaoperatorsdk/operator/ControllerUtils.java b/operator-framework/src/main/java/io/javaoperatorsdk/operator/ControllerUtils.java index 448f01acea..2c9b79c2b4 100644 --- a/operator-framework/src/main/java/io/javaoperatorsdk/operator/ControllerUtils.java +++ b/operator-framework/src/main/java/io/javaoperatorsdk/operator/ControllerUtils.java @@ -1,5 +1,6 @@ package io.javaoperatorsdk.operator; +import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.client.CustomResource; import io.javaoperatorsdk.operator.api.Controller; import io.javaoperatorsdk.operator.api.ResourceController; @@ -20,6 +21,15 @@ static String getDefaultFinalizerIdentifier(ResourceController controller) { return getAnnotation(controller).crdName() + FINALIZER_NAME_SUFFIX; } + /** + * @param finalizer + * @return + * @deprecated this should be removed once k8s client provides that method on HasMetadata + */ + static boolean isFinalizerValid(String finalizer) { + return HasMetadata.FINALIZER_NAME_MATCHER.reset(finalizer).matches(); + } + static boolean getGenerationEventProcessing(ResourceController controller) { return getAnnotation(controller).generationAwareEventProcessing(); } @@ -27,7 +37,7 @@ static boolean getGenerationEventProcessing(ResourceController controller) { static Class getCustomResourceClass(ResourceController controller) { return (Class) getAnnotation(controller).customResourceClass(); } - + static String getCrdName(ResourceController controller) { return getAnnotation(controller).crdName(); } diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/ConcurrencyIT.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/ConcurrencyIT.java index e12b642122..f13a4d4846 100644 --- a/operator-framework/src/test/java/io/javaoperatorsdk/operator/ConcurrencyIT.java +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/ConcurrencyIT.java @@ -5,7 +5,6 @@ import java.util.stream.Collectors; import io.fabric8.kubernetes.api.model.ConfigMap; -import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.client.DefaultKubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient; import io.javaoperatorsdk.operator.sample.TestCustomResource; @@ -34,10 +33,9 @@ public class ConcurrencyIT { public void setup() { KubernetesClient k8sClient = new DefaultKubernetesClient(); final TestCustomResourceController controller = new TestCustomResourceController(k8sClient, true); - assertThat(HasMetadata.FINALIZER_NAME_MATCHER.reset(ControllerUtils.getDefaultFinalizerIdentifier(controller)).matches()).isTrue(); integrationTest.initialize(k8sClient, controller, "test-crd.yaml"); } - + @BeforeEach public void cleanup() { integrationTest.cleanup(); diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/ControllerUtilsTest.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/ControllerUtilsTest.java index 7280806d89..c78f4f8454 100644 --- a/operator-framework/src/test/java/io/javaoperatorsdk/operator/ControllerUtilsTest.java +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/ControllerUtilsTest.java @@ -1,6 +1,5 @@ package io.javaoperatorsdk.operator; -import io.fabric8.kubernetes.api.model.HasMetadata; import io.javaoperatorsdk.operator.api.Context; import io.javaoperatorsdk.operator.api.Controller; import io.javaoperatorsdk.operator.api.ResourceController; @@ -14,34 +13,34 @@ import static org.junit.jupiter.api.Assertions.assertFalse; class ControllerUtilsTest { - + public static final String CUSTOM_FINALIZER_NAME = "a.custom/finalizer"; - + @Test public void returnsValuesFromControllerAnnotationFinalizer() { final TestCustomResourceController controller = new TestCustomResourceController(null); final String finalizer = ControllerUtils.getFinalizer(controller); Assertions.assertEquals(TestCustomResourceController.FINALIZER_NAME, finalizer); - Assertions.assertTrue(HasMetadata.FINALIZER_NAME_MATCHER.reset(finalizer).matches()); + Assertions.assertTrue(ControllerUtils.isFinalizerValid(finalizer)); assertEquals(TestCustomResource.class, ControllerUtils.getCustomResourceClass(controller)); Assertions.assertEquals(TestCustomResourceController.CRD_NAME, ControllerUtils.getCrdName(controller)); assertFalse(ControllerUtils.getGenerationEventProcessing(controller)); } - + @Controller(crdName = "test.crd", customResourceClass = TestCustomResource.class, finalizerName = CUSTOM_FINALIZER_NAME) static class TestCustomFinalizerController implements ResourceController { - + @Override public boolean deleteResource(TestCustomResource resource, Context context) { return false; } - + @Override public UpdateControl createOrUpdateResource(TestCustomResource resource, Context context) { return null; } } - + @Test public void returnCustomerFinalizerNameIfSet() { assertEquals(CUSTOM_FINALIZER_NAME, ControllerUtils.getFinalizer(new TestCustomFinalizerController())); diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/IntegrationTestSupport.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/IntegrationTestSupport.java index 7fc3158248..2844cdb4f9 100644 --- a/operator-framework/src/test/java/io/javaoperatorsdk/operator/IntegrationTestSupport.java +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/IntegrationTestSupport.java @@ -34,6 +34,8 @@ public class IntegrationTestSupport { private ResourceController controller; public void initialize(KubernetesClient k8sClient, ResourceController controller, String crdPath) { + assertThat(ControllerUtils.isFinalizerValid(ControllerUtils.getDefaultFinalizerIdentifier(controller))).isTrue(); + log.info("Initializing integration test in namespace {}", TEST_NAMESPACE); this.k8sClient = k8sClient; loadCRDAndApplyToCluster(crdPath); @@ -41,7 +43,7 @@ public void initialize(KubernetesClient k8sClient, ResourceController controller Class customResourceClass = ControllerUtils.getCustomResourceClass(controller); this.crOperations = k8sClient.customResources(customResourceClass, CustomResourceList.class); - + if (k8sClient.namespaces().withName(TEST_NAMESPACE).get() == null) { k8sClient.namespaces().create(new NamespaceBuilder() .withMetadata(new ObjectMetaBuilder().withName(TEST_NAMESPACE).build()).build()); diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/SubResourceUpdateIT.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/SubResourceUpdateIT.java index 7e7ae2ed87..9a68ee27ac 100644 --- a/operator-framework/src/test/java/io/javaoperatorsdk/operator/SubResourceUpdateIT.java +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/SubResourceUpdateIT.java @@ -2,7 +2,6 @@ import java.util.concurrent.TimeUnit; -import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; import io.fabric8.kubernetes.client.DefaultKubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient; @@ -10,7 +9,6 @@ import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResourceController; import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResourceSpec; import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResourceStatus; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; @@ -24,11 +22,6 @@ public class SubResourceUpdateIT { private IntegrationTestSupport integrationTestSupport = new IntegrationTestSupport(); - @BeforeAll - void checkFinalizer() { - assertThat(HasMetadata.FINALIZER_NAME_MATCHER.reset(ControllerUtils.getDefaultFinalizerIdentifier(new SubResourceTestCustomResourceController())).matches()).isTrue(); - } - @BeforeEach public void initAndCleanup() { KubernetesClient k8sClient = new DefaultKubernetesClient();