Skip to content

Commit

Permalink
refactor: isolate finalizer validation until HasMetadata provides it
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Nov 25, 2020
1 parent 8ec63d3 commit 39dd757
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,14 +21,23 @@ 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();
}

static <R extends CustomResource> Class<R> getCustomResourceClass(ResourceController<R> controller) {
return (Class<R>) getAnnotation(controller).customResourceClass();
}

static String getCrdName(ResourceController controller) {
return getAnnotation(controller).crdName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TestCustomResource> {

@Override
public boolean deleteResource(TestCustomResource resource, Context<TestCustomResource> context) {
return false;
}

@Override
public UpdateControl<TestCustomResource> createOrUpdateResource(TestCustomResource resource, Context<TestCustomResource> context) {
return null;
}
}

@Test
public void returnCustomerFinalizerNameIfSet() {
assertEquals(CUSTOM_FINALIZER_NAME, ControllerUtils.getFinalizer(new TestCustomFinalizerController()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ 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);
this.controller = 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

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;
import io.javaoperatorsdk.operator.sample.subresource.SubResourceTestCustomResource;
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;
Expand All @@ -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();
Expand Down

0 comments on commit 39dd757

Please sign in to comment.