Skip to content

Fixes #638

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

Merged
merged 2 commits into from
Oct 29, 2021
Merged

Fixes #638

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 @@ -159,11 +159,10 @@ public <R extends CustomResource> void register(
}
}

private static class ControllerManager implements Closeable {
static class ControllerManager implements Closeable {
private final Map<String, ConfiguredController> controllers = new HashMap<>();
private boolean started = false;


public synchronized void shouldStart() {
if (started) {
return;
Expand Down Expand Up @@ -201,9 +200,9 @@ public synchronized void add(ConfiguredController configuredController) {
final var crdName = configuration.getCRDName();
final var existing = controllers.get(crdName);
if (existing != null) {
throw new OperatorException("Cannot register controller " + configuration.getName()
+ ": another controller (" + existing.getConfiguration().getName()
+ ") is already registered for CRD " + crdName);
throw new OperatorException("Cannot register controller '" + configuration.getName()
+ "': another controller named '" + existing.getConfiguration().getName()
+ "' is already registered for CRD '" + crdName + "'");
}
this.controllers.put(crdName, configuredController);
if (started) {
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.javaoperatorsdk.operator;

import org.junit.Test;

import io.fabric8.kubernetes.client.CustomResource;
import io.javaoperatorsdk.operator.Operator.ControllerManager;
import io.javaoperatorsdk.operator.api.ResourceController;
import io.javaoperatorsdk.operator.api.config.DefaultControllerConfiguration;
import io.javaoperatorsdk.operator.processing.ConfiguredController;
import io.javaoperatorsdk.operator.sample.simple.DuplicateCRController;
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
import io.javaoperatorsdk.operator.sample.simple.TestCustomResourceController;
import io.javaoperatorsdk.operator.sample.simple.TestCustomResourceControllerV2;
import io.javaoperatorsdk.operator.sample.simple.TestCustomResourceV2;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ControllerManagerTest {

@Test
public void shouldNotAddMultipleControllersForSameCustomResource() {
final var registered = new TestControllerConfiguration<>(new TestCustomResourceController(null),
TestCustomResource.class);
final var duplicated =
new TestControllerConfiguration<>(new DuplicateCRController(), TestCustomResource.class);

checkException(registered, duplicated);
}

@Test
public void addingMultipleControllersForCustomResourcesWithDifferentVersionsShouldNotWork() {
final var registered = new TestControllerConfiguration<>(new TestCustomResourceController(null),
TestCustomResource.class);
final var duplicated = new TestControllerConfiguration<>(new TestCustomResourceControllerV2(),
TestCustomResourceV2.class);

checkException(registered, duplicated);

}

private <T extends CustomResource<?, ?>, U extends CustomResource<?, ?>> void checkException(
TestControllerConfiguration<T> registered,
TestControllerConfiguration<U> duplicated) {
final var exception = assertThrows(OperatorException.class, () -> {
final var controllerManager = new ControllerManager();
controllerManager.add(new ConfiguredController<>(registered.controller, registered, null));
controllerManager.add(new ConfiguredController<>(duplicated.controller, duplicated, null));
});
final var msg = exception.getMessage();
assertTrue(
msg.contains("Cannot register controller '" + duplicated.getControllerName() + "'")
&& msg.contains(registered.getControllerName())
&& msg.contains(registered.getCRDName()));
}

private static class TestControllerConfiguration<R extends CustomResource<?, ?>>
extends DefaultControllerConfiguration<R> {
private final ResourceController<R> controller;

public TestControllerConfiguration(ResourceController<R> controller, Class<R> crClass) {
super(null, getControllerName(controller),
CustomResource.getCRDName(crClass), null, false, null, null, null, null, crClass, null);
this.controller = controller;
}

static <R extends CustomResource<?, ?>> String getControllerName(
ResourceController<R> controller) {
return controller.getClass().getSimpleName() + "Controller";
}

private String getControllerName() {
return getControllerName(controller);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.javaoperatorsdk.operator.sample.simple;

import io.javaoperatorsdk.operator.api.Context;
import io.javaoperatorsdk.operator.api.Controller;
import io.javaoperatorsdk.operator.api.ResourceController;
import io.javaoperatorsdk.operator.api.UpdateControl;

@Controller
public class DuplicateCRController implements ResourceController<TestCustomResource> {

@Override
public UpdateControl<TestCustomResource> createOrUpdateResource(TestCustomResource resource,
Context<TestCustomResource> context) {
return UpdateControl.noUpdate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.javaoperatorsdk.operator.sample.simple;

import io.javaoperatorsdk.operator.api.Context;
import io.javaoperatorsdk.operator.api.Controller;
import io.javaoperatorsdk.operator.api.ResourceController;
import io.javaoperatorsdk.operator.api.UpdateControl;

@Controller
public class TestCustomResourceControllerV2 implements ResourceController<TestCustomResourceV2> {

@Override
public UpdateControl<TestCustomResourceV2> createOrUpdateResource(TestCustomResourceV2 resource,
Context<TestCustomResourceV2> context) {
return UpdateControl.noUpdate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.javaoperatorsdk.operator.sample.simple;

import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

@Group("sample.javaoperatorsdk.io")
@Version("v2")
public class TestCustomResourceV2
extends CustomResource<TestCustomResourceSpec, TestCustomResourceStatus> {

}
18 changes: 0 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
<git-commit-id-maven-plugin.version>5.0.0</git-commit-id-maven-plugin.version>
<jandex-maven-plugin.version>1.2.1</jandex-maven-plugin.version>
<formatter-maven-plugin.version>2.16.0</formatter-maven-plugin.version>
<directory-maven-plugin.version>1.0</directory-maven-plugin.version>
<impsort-maven-plugin.version>1.6.2</impsort-maven-plugin.version>
Expand Down Expand Up @@ -224,11 +223,6 @@
<artifactId>maven-install-plugin</artifactId>
<version>${maven-install-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>${jandex-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
Expand Down Expand Up @@ -269,18 +263,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<executions>
<execution>
<id>make-index</id>
<goals>
<goal>jandex</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down