Synchronize and test leader watchers

This commit is contained in:
Gytis Trikleris
2018-08-22 10:25:27 +02:00
committed by Ioannis Canellos
parent b8405f2005
commit dce324961c
4 changed files with 327 additions and 27 deletions

View File

@@ -32,6 +32,8 @@ public class LeaderRecordWatcher implements Watcher<ConfigMap> {
private static final Logger LOGGER = LoggerFactory.getLogger(LeaderRecordWatcher.class);
private final Object lock = new Object();
private final LeadershipController leadershipController;
private final LeaderProperties leaderProperties;
@@ -49,20 +51,28 @@ public class LeaderRecordWatcher implements Watcher<ConfigMap> {
public void start() {
if (watch == null) {
LOGGER.debug("Starting leader record watcher");
watch = kubernetesClient
.configMaps()
.inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
.withName(leaderProperties.getConfigMapName())
.watch(this);
synchronized (lock) {
if (watch == null) {
LOGGER.debug("Starting leader record watcher");
watch = kubernetesClient
.configMaps()
.inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
.withName(leaderProperties.getConfigMapName())
.watch(this);
}
}
}
}
public void stop() {
if (watch != null) {
LOGGER.debug("Stopping leader record watcher");
watch.close();
watch = null;
synchronized (lock) {
if (watch != null) {
LOGGER.debug("Stopping leader record watcher");
watch.close();
watch = null;
}
}
}
}
@@ -78,9 +88,11 @@ public class LeaderRecordWatcher implements Watcher<ConfigMap> {
@Override
public void onClose(KubernetesClientException cause) {
if (cause != null) {
LOGGER.warn("Watcher stopped unexpectedly, will restart", cause);
watch = null;
start();
synchronized (lock) {
LOGGER.warn("Watcher stopped unexpectedly, will restart", cause);
watch = null;
start();
}
}
}
}

View File

@@ -35,6 +35,8 @@ public class PodReadinessWatcher implements Watcher<Pod> {
private static final Logger LOGGER = LoggerFactory.getLogger(PodReadinessWatcher.class);
private final Object lock = new Object();
private final String podName;
private final KubernetesClient kubernetesClient;
@@ -54,20 +56,28 @@ public class PodReadinessWatcher implements Watcher<Pod> {
public void start() {
if (watch == null) {
LOGGER.debug("Starting pod readiness watcher for '{}'", podName);
PodResource<Pod, DoneablePod> podResource = kubernetesClient
.pods()
.withName(podName);
previousState = podResource.isReady();
watch = podResource.watch(this);
synchronized (lock) {
if (watch == null) {
LOGGER.debug("Starting pod readiness watcher for '{}'", podName);
PodResource<Pod, DoneablePod> podResource = kubernetesClient
.pods()
.withName(podName);
previousState = podResource.isReady();
watch = podResource.watch(this);
}
}
}
}
public void stop() {
if (watch != null) {
LOGGER.debug("Stopping pod readiness watcher for '{}'", podName);
watch.close();
watch = null;
synchronized (lock) {
if (watch != null) {
LOGGER.debug("Stopping pod readiness watcher for '{}'", podName);
watch.close();
watch = null;
}
}
}
}
@@ -75,18 +85,24 @@ public class PodReadinessWatcher implements Watcher<Pod> {
public void eventReceived(Action action, Pod pod) {
boolean currentState = Readiness.isPodReady(pod);
if (previousState != currentState) {
LOGGER.debug("'{}' readiness status changed to '{}', triggering leadership update", podName, currentState);
previousState = currentState;
leadershipController.update();
synchronized (lock) {
if (previousState != currentState) {
LOGGER.debug("'{}' readiness status changed to '{}', triggering leadership update", podName, currentState);
previousState = currentState;
leadershipController.update();
}
}
}
}
@Override
public void onClose(KubernetesClientException cause) {
if (cause != null) {
LOGGER.warn("Watcher stopped unexpectedly, will restart", cause);
watch = null;
start();
synchronized (lock) {
LOGGER.warn("Watcher stopped unexpectedly, will restart", cause);
watch = null;
start();
}
}
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright 2013-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.kubernetes.leader;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapList;
import io.fabric8.kubernetes.api.model.DoneableConfigMap;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author Gytis Trikleris
*/
@RunWith(MockitoJUnitRunner.class)
public class LeaderRecordWatcherTest {
@Mock
private LeaderProperties mockLeaderProperties;
@Mock
private LeadershipController mockLeadershipController;
@Mock
private KubernetesClient mockKubernetesClient;
@Mock
private MixedOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>>
mockConfigMapsOperation;
@Mock
private NonNamespaceOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>>
mockInNamespaceOperation;
@Mock
private Resource<ConfigMap, DoneableConfigMap> mockWithNameResource;
@Mock
private Watch mockWatch;
@Mock
private ConfigMap mockConfigMap;
@Mock
private KubernetesClientException mockKubernetesClientException;
private LeaderRecordWatcher watcher;
@Before
public void before() {
watcher = new LeaderRecordWatcher(mockLeaderProperties, mockLeadershipController, mockKubernetesClient);
given(mockKubernetesClient.configMaps()).willReturn(mockConfigMapsOperation);
given(mockConfigMapsOperation.inNamespace(null)).willReturn(mockInNamespaceOperation);
given(mockInNamespaceOperation.withName(null)).willReturn(mockWithNameResource);
given(mockWithNameResource.watch(watcher)).willReturn(mockWatch);
}
@Test
public void shouldStartOnce() {
watcher.start();
watcher.start();
verify(mockWithNameResource).watch(watcher);
}
@Test
public void shouldStopOnce() {
watcher.start();
watcher.stop();
watcher.stop();
verify(mockWatch).close();
}
@Test
public void shouldHandleEvent() {
watcher.eventReceived(Watcher.Action.ADDED, mockConfigMap);
watcher.eventReceived(Watcher.Action.DELETED, mockConfigMap);
watcher.eventReceived(Watcher.Action.MODIFIED, mockConfigMap);
verify(mockLeadershipController, times(3)).update();
}
@Test
public void shouldIgnoreErrorEvent() {
watcher.eventReceived(Watcher.Action.ERROR, mockConfigMap);
verify(mockLeadershipController, times(0)).update();
}
@Test
public void shouldHandleClose() {
watcher.onClose(mockKubernetesClientException);
verify(mockWithNameResource).watch(watcher);
}
@Test
public void shouldIgnoreCloseWithoutCause() {
watcher.onClose(null);
verify(mockWithNameResource, times(0)).watch(watcher);
}
}

View File

@@ -0,0 +1,138 @@
/*
* Copyright 2013-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.kubernetes.leader;
import java.util.Collections;
import io.fabric8.kubernetes.api.model.DoneablePod;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodCondition;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodStatus;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.PodResource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author Gytis Trikleris
*/
@RunWith(MockitoJUnitRunner.class)
public class PodReadinessWatcherTest {
private static final String POD_NAME = "test-pod";
@Mock
private LeadershipController mockLeadershipController;
@Mock
private KubernetesClient mockKubernetesClient;
@Mock
private MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPodsOperation;
@Mock
private PodResource<Pod, DoneablePod> mockPodResource;
@Mock
private Pod mockPod;
@Mock
private PodStatus mockPodStatus;
@Mock
private Watch mockWatch;
@Mock
private KubernetesClientException mockKubernetesClientException;
private PodReadinessWatcher watcher;
@Before
public void before() {
watcher = new PodReadinessWatcher(POD_NAME, mockKubernetesClient, mockLeadershipController);
given(mockKubernetesClient.pods()).willReturn(mockPodsOperation);
given(mockPodsOperation.withName(POD_NAME)).willReturn(mockPodResource);
given(mockPodResource.watch(watcher)).willReturn(mockWatch);
}
@Test
public void shouldStartOnce() {
watcher.start();
watcher.start();
verify(mockPodResource).watch(watcher);
}
@Test
public void shouldStopOnce() {
watcher.start();
watcher.stop();
watcher.stop();
verify(mockWatch).close();
}
@Test
public void shouldHandleEventWithStateChange() {
given(mockPodResource.isReady()).willReturn(true);
given(mockPod.getStatus()).willReturn(mockPodStatus);
watcher.start();
watcher.eventReceived(Watcher.Action.ADDED, mockPod);
verify(mockLeadershipController).update();
}
@Test
public void shouldIgnoreEventIfStateDoesNotChange() {
given(mockPod.getStatus()).willReturn(mockPodStatus);
watcher.start();
watcher.eventReceived(Watcher.Action.ADDED, mockPod);
verify(mockLeadershipController, times(0)).update();
}
@Test
public void shouldHandleClose() {
watcher.onClose(mockKubernetesClientException);
verify(mockPodResource).watch(watcher);
}
@Test
public void shouldIgnoreCloseWithoutCause() {
watcher.onClose(null);
verify(mockPodResource, times(0)).watch(watcher);
}
}