Fabric leader clean up 7 (#1648)
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.cloud.kubernetes.commons.leader;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Gytis Trikleris
|
||||
@@ -105,7 +106,7 @@ public class LeaderProperties {
|
||||
}
|
||||
|
||||
public String getNamespace(String defaultValue) {
|
||||
if (namespace == null || namespace.isEmpty()) {
|
||||
if (!StringUtils.hasText(defaultValue)) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2019 the original author or authors.
|
||||
* Copyright 2013-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,25 +16,28 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.fabric8.leader;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import io.fabric8.kubernetes.api.model.ConfigMap;
|
||||
import io.fabric8.kubernetes.client.KubernetesClient;
|
||||
import io.fabric8.kubernetes.client.Watch;
|
||||
import io.fabric8.kubernetes.client.Watcher;
|
||||
import io.fabric8.kubernetes.client.WatcherException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties;
|
||||
import org.springframework.cloud.kubernetes.commons.leader.LeaderRecordWatcher;
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.guarded;
|
||||
|
||||
/**
|
||||
* @author Gytis Trikleris
|
||||
*/
|
||||
public class Fabric8LeaderRecordWatcher implements LeaderRecordWatcher, Watcher<ConfigMap> {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Fabric8LeaderRecordWatcher.class);
|
||||
private static final LogAccessor LOGGER = new LogAccessor(Fabric8LeaderRecordWatcher.class);
|
||||
|
||||
private final Object lock = new Object();
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
private final Fabric8LeadershipController fabric8LeadershipController;
|
||||
|
||||
@@ -42,7 +45,7 @@ public class Fabric8LeaderRecordWatcher implements LeaderRecordWatcher, Watcher<
|
||||
|
||||
private final KubernetesClient kubernetesClient;
|
||||
|
||||
private Watch watch;
|
||||
private volatile Watch configMapWatch;
|
||||
|
||||
public Fabric8LeaderRecordWatcher(LeaderProperties leaderProperties,
|
||||
Fabric8LeadershipController fabric8LeadershipController, KubernetesClient kubernetesClient) {
|
||||
@@ -52,47 +55,47 @@ public class Fabric8LeaderRecordWatcher implements LeaderRecordWatcher, Watcher<
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (this.watch == null) {
|
||||
synchronized (this.lock) {
|
||||
if (this.watch == null) {
|
||||
LOGGER.debug("Starting leader record watcher");
|
||||
this.watch = this.kubernetesClient.configMaps()
|
||||
.inNamespace(this.leaderProperties.getNamespace(this.kubernetesClient.getNamespace()))
|
||||
.withName(this.leaderProperties.getConfigMapName()).watch(this);
|
||||
if (configMapWatch == null) {
|
||||
guarded(lock, () -> {
|
||||
if (configMapWatch == null) {
|
||||
LOGGER.debug(() -> "Starting leader record watcher");
|
||||
configMapWatch = kubernetesClient.configMaps()
|
||||
.inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
|
||||
.withName(leaderProperties.getConfigMapName()).watch(this);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (this.watch != null) {
|
||||
synchronized (this.lock) {
|
||||
if (this.watch != null) {
|
||||
LOGGER.debug("Stopping leader record watcher");
|
||||
this.watch.close();
|
||||
this.watch = null;
|
||||
if (configMapWatch != null) {
|
||||
guarded(lock, () -> {
|
||||
if (this.configMapWatch != null) {
|
||||
LOGGER.debug(() -> "Stopping leader record watcher");
|
||||
configMapWatch.close();
|
||||
configMapWatch = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventReceived(Action action, ConfigMap configMap) {
|
||||
LOGGER.debug("'{}' event received, triggering leadership update", action);
|
||||
LOGGER.debug(() -> action + " event received, triggering leadership update");
|
||||
|
||||
if (!Action.ERROR.equals(action)) {
|
||||
this.fabric8LeadershipController.update();
|
||||
fabric8LeadershipController.update();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(WatcherException cause) {
|
||||
if (cause != null) {
|
||||
synchronized (this.lock) {
|
||||
LOGGER.warn("Watcher stopped unexpectedly, will restart", cause);
|
||||
this.watch = null;
|
||||
guarded(lock, () -> {
|
||||
LOGGER.warn(() -> "Watcher stopped unexpectedly, will restart because of : " + cause);
|
||||
configMapWatch = null;
|
||||
start();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user