Skip to content

Commit bf2af63

Browse files
committed
chore: remove synchronization altogether
1 parent 047ac35 commit bf2af63

File tree

1 file changed

+46
-59
lines changed
  • operator-framework-core/src/main/java/io/javaoperatorsdk/operator

1 file changed

+46
-59
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class Operator implements AutoCloseable {
2525
private static final Logger log = LoggerFactory.getLogger(Operator.class);
2626
private final KubernetesClient k8sClient;
2727
private final ConfigurationService configurationService;
28-
private final ReentrantLock lock = new ReentrantLock();
2928
private final List<ConfiguredController> controllers = new LinkedList<>();
3029
private volatile boolean started = false;
3130

@@ -65,44 +64,39 @@ public ConfigurationService getConfigurationService() {
6564
*/
6665
@SuppressWarnings("unchecked")
6766
public void start() {
68-
try {
69-
lock.lock();
70-
if (started) {
71-
return;
72-
}
73-
if (controllers.isEmpty()) {
74-
throw new OperatorException("No ResourceController exists. Exiting!");
75-
}
67+
if (started) {
68+
return;
69+
}
70+
if (controllers.isEmpty()) {
71+
throw new OperatorException("No ResourceController exists. Exiting!");
72+
}
7673

77-
final var version = configurationService.getVersion();
78-
log.info(
79-
"Operator SDK {} (commit: {}) built on {} starting...",
80-
version.getSdkVersion(),
81-
version.getCommit(),
82-
version.getBuiltTime());
74+
final var version = configurationService.getVersion();
75+
log.info(
76+
"Operator SDK {} (commit: {}) built on {} starting...",
77+
version.getSdkVersion(),
78+
version.getCommit(),
79+
version.getBuiltTime());
8380

84-
log.info("Client version: {}", Version.clientVersion());
85-
try {
86-
final var k8sVersion = k8sClient.getVersion();
87-
if (k8sVersion != null) {
88-
log.info("Server version: {}.{}", k8sVersion.getMajor(), k8sVersion.getMinor());
89-
}
90-
} catch (Exception e) {
91-
final String error;
92-
if (e.getCause() instanceof ConnectException) {
93-
error = "Cannot connect to cluster";
94-
} else {
95-
error = "Error retrieving the server version";
96-
}
97-
log.error(error, e);
98-
throw new OperatorException(error, e);
81+
log.info("Client version: {}", Version.clientVersion());
82+
try {
83+
final var k8sVersion = k8sClient.getVersion();
84+
if (k8sVersion != null) {
85+
log.info("Server version: {}.{}", k8sVersion.getMajor(), k8sVersion.getMinor());
9986
}
100-
101-
controllers.parallelStream().forEach(ConfiguredController::start);
102-
started = true;
103-
} finally {
104-
lock.unlock();
87+
} catch (Exception e) {
88+
final String error;
89+
if (e.getCause() instanceof ConnectException) {
90+
error = "Cannot connect to cluster";
91+
} else {
92+
error = "Error retrieving the server version";
93+
}
94+
log.error(error, e);
95+
throw new OperatorException(error, e);
10596
}
97+
98+
controllers.parallelStream().forEach(ConfiguredController::start);
99+
started = true;
106100
}
107101

108102
/** Stop the operator. */
@@ -111,25 +105,20 @@ public void close() {
111105
log.info(
112106
"Operator SDK {} is shutting down...", configurationService.getVersion().getSdkVersion());
113107

114-
try {
115-
lock.lock();
116-
if (!started) {
117-
return;
108+
if (!started) {
109+
return;
110+
}
111+
112+
this.controllers.parallelStream().forEach(closeable -> {
113+
try {
114+
log.debug("closing {}", closeable);
115+
closeable.close();
116+
} catch (IOException e) {
117+
log.warn("Error closing {}", closeable, e);
118118
}
119+
});
119120

120-
this.controllers.parallelStream().forEach(closeable -> {
121-
try {
122-
log.debug("closing {}", closeable);
123-
closeable.close();
124-
} catch (IOException e) {
125-
log.warn("Error closing {}", closeable, e);
126-
}
127-
});
128-
129-
started = false;
130-
} finally {
131-
lock.unlock();
132-
}
121+
started = false;
133122
}
134123

135124
/**
@@ -173,13 +162,11 @@ public <R extends CustomResource> void register(
173162
if (configuration == null) {
174163
configuration = existing;
175164
}
176-
synchronized (lock) {
177-
final var configuredController =
178-
new ConfiguredController(controller, configuration, k8sClient);
179-
this.controllers.add(configuredController);
180-
if (started) {
181-
configuredController.start();
182-
}
165+
final var configuredController =
166+
new ConfiguredController(controller, configuration, k8sClient);
167+
this.controllers.add(configuredController);
168+
if (started) {
169+
configuredController.start();
183170
}
184171

185172
final var watchedNS =

0 commit comments

Comments
 (0)