Skip to content

Commit

Permalink
Introduce custom specwatch counter
Browse files Browse the repository at this point in the history
To avoid endless restarting loops when k8s-client watcher keeps reconnecting.

(cherry picked from commit 15ebde5)
  • Loading branch information
sgraband committed Mar 22, 2024
1 parent 6e43d34 commit 40cab77
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ final class SpecWatch<S extends CustomResource<?, ?>> implements Watcher<S> {

private static final Logger LOGGER = LogManager.getLogger(SpecWatch.class);

private static int MAX_RECONNECTION_TRIES = 10;
private int reconnectionTries = 0;

private final Map<String, S> cache;
private final TriConsumer<Action, String, String> eventHandler;
private final String resourceName;
Expand All @@ -53,6 +56,11 @@ final class SpecWatch<S extends CustomResource<?, ?>> implements Watcher<S> {
@Override
public void eventReceived(Action action, S resource) {
lastActive = System.currentTimeMillis();
if (reconnectionTries > 0) {
reconnectionTries = 0;
LOGGER.info(formatLogMessage(correlationIdPrefix,
getResourceName() + " did receive event. Resetting retry counter to 0."));
}
String correlationId = generateCorrelationId();
String uid = resource.getMetadata().getUid();
try {
Expand Down Expand Up @@ -104,8 +112,15 @@ public void onClose() {

@Override
public boolean reconnecting() {
reconnectionTries++;
if (reconnectionTries >= MAX_RECONNECTION_TRIES) {
LOGGER.info(formatLogMessage(correlationIdPrefix, getResourceName() + " did not reconnect after "
+ MAX_RECONNECTION_TRIES + " tries. Restarting Operator."));
System.exit(-1);
}
lastActive = System.currentTimeMillis();
LOGGER.info(formatLogMessage(correlationIdPrefix, getResourceName() + " reconnecting"));
LOGGER.info(
formatLogMessage(correlationIdPrefix, getResourceName() + " reconnecting (" + reconnectionTries + ")"));
return Watcher.super.reconnecting();
}

Expand Down

0 comments on commit 40cab77

Please sign in to comment.