Skip to content

fix: prevent double registration of same CR with different controllers #627

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 1 commit into from
Oct 27, 2021
Merged
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 @@ -3,9 +3,10 @@
import java.io.Closeable;
import java.io.IOException;
import java.net.ConnectException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -50,7 +51,7 @@ public ConfigurationService getConfigurationService() {
}

public List<ConfiguredController> getControllers() {
return Collections.unmodifiableList(controllers.controllers);
return new ArrayList<>(controllers.controllers.values());
}

/**
Expand Down Expand Up @@ -159,7 +160,7 @@ public <R extends CustomResource> void register(
}

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


Expand All @@ -173,7 +174,7 @@ public synchronized void shouldStart() {
}

public synchronized void start() {
controllers.parallelStream().forEach(ConfiguredController::start);
controllers.values().parallelStream().forEach(ConfiguredController::start);
started = true;
}

Expand All @@ -183,7 +184,7 @@ public synchronized void close() {
return;
}

this.controllers.parallelStream().forEach(closeable -> {
this.controllers.values().parallelStream().forEach(closeable -> {
try {
log.debug("closing {}", closeable);
closeable.close();
Expand All @@ -196,7 +197,15 @@ public synchronized void close() {
}

public synchronized void add(ConfiguredController configuredController) {
this.controllers.add(configuredController);
final var configuration = configuredController.getConfiguration();
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);
}
this.controllers.put(crdName, configuredController);
if (started) {
configuredController.start();
}
Expand Down