data) {
- if (data == null) {
- return null;
- }
-
- String leaderKey = getLeaderKey();
- String leaderId = data.get(leaderKey);
- LOGGER.debug(() -> "retrieved leaderId: " + leaderId + " from leaderKey : " + leaderId);
- if (!StringUtils.hasText(leaderId)) {
- return null;
- }
-
- return new Leader(candidate.getRole(), leaderId);
- }
-
- protected void handleLeaderChange(Leader newLeader) {
- if (Objects.equals(localLeader, newLeader)) {
- LOGGER.debug(() -> "Leader is still : " + localLeader);
- return;
- }
-
- Leader oldLeader = localLeader;
- localLeader = newLeader;
-
- if (oldLeader != null && oldLeader.isCandidate(candidate)) {
- notifyOnRevoked();
- }
- else if (newLeader != null && newLeader.isCandidate(candidate)) {
- notifyOnGranted();
- }
-
- restartLeaderReadinessWatcher();
-
- LOGGER.debug(() -> "New leader is " + localLeader);
- }
-
- protected void notifyOnGranted() {
- LOGGER.debug(() -> "Leadership has been granted to : " + candidate);
-
- Context context = new LeaderContext(candidate, this);
- leaderEventPublisher.publishOnGranted(this, context, candidate.getRole());
- try {
- candidate.onGranted(context);
- }
- catch (InterruptedException e) {
- LOGGER.warn(e::getMessage);
- Thread.currentThread().interrupt();
- }
- }
-
- protected void notifyOnRevoked() {
- LOGGER.debug(() -> "Leadership has been revoked from :" + candidate);
-
- Context context = new LeaderContext(candidate, this);
- leaderEventPublisher.publishOnRevoked(this, context, candidate.getRole());
- candidate.onRevoked(context);
- }
-
- protected void notifyOnFailedToAcquire() {
- if (leaderProperties.isPublishFailedEvents()) {
- Context context = new LeaderContext(candidate, this);
- leaderEventPublisher.publishOnFailedToAcquire(this, context, candidate.getRole());
- }
- }
-
- protected void restartLeaderReadinessWatcher() {
- if (leaderReadinessWatcher != null) {
- leaderReadinessWatcher.stop();
- leaderReadinessWatcher = null;
- }
-
- if (localLeader != null && !localLeader.isCandidate(candidate)) {
- leaderReadinessWatcher = createPodReadinessWatcher(localLeader.getId());
- leaderReadinessWatcher.start();
- }
- }
-
- protected abstract PodReadinessWatcher createPodReadinessWatcher(String localLeaderId);
-
-}
diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/PodReadinessWatcher.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/PodReadinessWatcher.java
deleted file mode 100644
index 56cc24bd3..000000000
--- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/PodReadinessWatcher.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.commons.leader;
-
-/**
- * @author Ryan Baxter
- */
-public interface PodReadinessWatcher {
-
- void start();
-
- void stop();
-
-}
diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/ConditionalOnLeaderElectionDisabled.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/ConditionalOnLeaderElectionDisabled.java
deleted file mode 100644
index 6fce5c9a6..000000000
--- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/ConditionalOnLeaderElectionDisabled.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.commons.leader.election;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Inherited;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-import org.springframework.boot.autoconfigure.condition.NoneNestedConditions;
-import org.springframework.context.annotation.Conditional;
-
-/**
- * @author wind57
- */
-@Target({ ElementType.TYPE, ElementType.METHOD })
-@Retention(RetentionPolicy.RUNTIME)
-@Documented
-@Inherited
-@Conditional(ConditionalOnLeaderElectionDisabled.OnLeaderElectionDisabled.class)
-public @interface ConditionalOnLeaderElectionDisabled {
-
- class OnLeaderElectionDisabled extends NoneNestedConditions {
-
- OnLeaderElectionDisabled() {
- super(ConfigurationPhase.REGISTER_BEAN);
- }
-
- @ConditionalOnLeaderElectionEnabled
- static class OnLeaderElectionDisabledClass {
-
- }
-
- }
-
-}
diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/ConditionalOnLeaderElectionEnabled.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/ConditionalOnLeaderElectionEnabled.java
index a58a68e55..70a327af0 100644
--- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/ConditionalOnLeaderElectionEnabled.java
+++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/ConditionalOnLeaderElectionEnabled.java
@@ -25,7 +25,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.LEADER_ELECTION_ENABLED_PROPERTY;
+import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils.LEADER_ELECTION_ENABLED_PROPERTY;
/**
* Provides a more succinct conditional for:
@@ -37,7 +37,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
-@ConditionalOnProperty(value = LEADER_ELECTION_ENABLED_PROPERTY, havingValue = "true", matchIfMissing = false)
+@ConditionalOnProperty(value = LEADER_ELECTION_ENABLED_PROPERTY, havingValue = "true", matchIfMissing = true)
public @interface ConditionalOnLeaderElectionEnabled {
}
diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionCallbacks.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionCallbacks.java
index a1c463c0b..828b29a1d 100644
--- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionCallbacks.java
+++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionCallbacks.java
@@ -19,7 +19,6 @@
import java.net.UnknownHostException;
import java.util.function.Consumer;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderUtils;
import org.springframework.cloud.kubernetes.commons.leader.election.events.NewLeaderEvent;
import org.springframework.cloud.kubernetes.commons.leader.election.events.StartLeadingEvent;
import org.springframework.cloud.kubernetes.commons.leader.election.events.StopLeadingEvent;
diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionProperties.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionProperties.java
index 25e5dc35f..ed8ebdf9e 100644
--- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionProperties.java
+++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderElectionProperties.java
@@ -21,7 +21,7 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
-import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.LEADER_ELECTION_PROPERTY_PREFIX;
+import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils.LEADER_ELECTION_PROPERTY_PREFIX;
/**
*
diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderUtils.java
similarity index 92%
rename from spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtils.java
rename to spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderUtils.java
index 27d797e5d..21a148bc3 100644
--- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtils.java
+++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderUtils.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.cloud.kubernetes.commons.leader;
+package org.springframework.cloud.kubernetes.commons.leader.election;
import java.io.File;
import java.io.IOException;
@@ -23,7 +23,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
-import java.util.concurrent.locks.ReentrantLock;
import org.springframework.cloud.kubernetes.commons.EnvReader;
import org.springframework.core.log.LogAccessor;
@@ -104,14 +103,4 @@ public static String hostName() throws UnknownHostException {
}
}
- public static void guarded(ReentrantLock lock, Runnable runnable) {
- try {
- lock.lock();
- runnable.run();
- }
- finally {
- lock.unlock();
- }
- }
-
}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInfoContributorTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInfoContributorTests.java
deleted file mode 100644
index aeba15354..000000000
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderInfoContributorTests.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.commons.leader;
-
-import java.util.Map;
-
-import org.assertj.core.api.Assertions;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-import org.springframework.boot.actuate.info.Info;
-import org.springframework.integration.leader.Candidate;
-import org.springframework.integration.leader.DefaultCandidate;
-import org.springframework.integration.leader.event.LeaderEventPublisher;
-
-/**
- * @author wind57
- */
-class LeaderInfoContributorTests {
-
- @Test
- void testLeaderMissing() {
-
- Candidate candidate = new DefaultCandidate("id", "role");
- LeaderProperties leaderProperties = new LeaderProperties();
- LeaderEventPublisher leaderEventPublisher = Mockito.mock(LeaderEventPublisher.class);
- LeadershipController leadershipController = new LeadershipControllerStub(candidate, leaderProperties,
- leaderEventPublisher);
-
- LeaderInfoContributor leaderInfoContributor = new LeaderInfoContributor(leadershipController, candidate);
- Info.Builder builder = new Info.Builder();
- leaderInfoContributor.contribute(builder);
-
- Assertions.assertThat(builder.build().getDetails().get("leaderElection"))
- .isEqualTo(Map.of("leaderId", "Unknown"));
- }
-
- @Test
- void testLeaderPresentIsLeader() {
-
- Candidate candidate = new DefaultCandidate("leaderId", "leaderRole");
- LeaderProperties leaderProperties = new LeaderProperties();
- LeaderEventPublisher leaderEventPublisher = Mockito.mock(LeaderEventPublisher.class);
- LeadershipController leadershipController = new LeadershipControllerStub(candidate, leaderProperties,
- leaderEventPublisher);
-
- Leader leader = new Leader("leaderRole", "leaderId");
-
- leadershipController.handleLeaderChange(leader);
-
- LeaderInfoContributor leaderInfoContributor = new LeaderInfoContributor(leadershipController, candidate);
- Info.Builder builder = new Info.Builder();
- leaderInfoContributor.contribute(builder);
-
- Assertions.assertThat(builder.build().getDetails().get("leaderElection"))
- .isEqualTo(Map.of("role", "leaderRole", "isLeader", true, "leaderId", "leaderId"));
- }
-
- @Test
- void testLeaderPresentIsNotLeader() {
-
- Candidate candidate = new DefaultCandidate("leaderId", "notLeaderRole");
- LeaderProperties leaderProperties = new LeaderProperties();
- LeaderEventPublisher leaderEventPublisher = Mockito.mock(LeaderEventPublisher.class);
- LeadershipController leadershipController = new LeadershipControllerStub(candidate, leaderProperties,
- leaderEventPublisher);
-
- Leader leader = new Leader("leaderRole", "leaderId");
-
- leadershipController.handleLeaderChange(leader);
-
- LeaderInfoContributor leaderInfoContributor = new LeaderInfoContributor(leadershipController, candidate);
- Info.Builder builder = new Info.Builder();
- leaderInfoContributor.contribute(builder);
-
- Assertions.assertThat(builder.build().getDetails().get("leaderElection"))
- .isEqualTo(Map.of("role", "leaderRole", "isLeader", false, "leaderId", "leaderId"));
- }
-
-}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderPropertiesTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderPropertiesTests.java
deleted file mode 100644
index 9781b1135..000000000
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderPropertiesTests.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.commons.leader;
-
-import org.assertj.core.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-/**
- * @author wind57
- */
-class LeaderPropertiesTests {
-
- @Test
- void getNamespaceNull() {
- LeaderProperties leaderProperties = new LeaderProperties();
- String namespace = leaderProperties.getNamespace("a");
- Assertions.assertThat(namespace).isEqualTo("a");
- }
-
- @Test
- void getNamespaceEmpty() {
- LeaderProperties leaderProperties = new LeaderProperties();
- leaderProperties.setNamespace("");
- String namespace = leaderProperties.getNamespace("a");
- Assertions.assertThat(namespace).isEqualTo("a");
- }
-
- @Test
- void getNamespacePresent() {
- LeaderProperties leaderProperties = new LeaderProperties();
- leaderProperties.setNamespace("c");
- String namespace = leaderProperties.getNamespace("a");
- Assertions.assertThat(namespace).isEqualTo("c");
- }
-
-}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeadershipControllerStub.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeadershipControllerStub.java
deleted file mode 100644
index 53b6648ef..000000000
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeadershipControllerStub.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.commons.leader;
-
-import java.util.Optional;
-
-import org.springframework.integration.leader.Candidate;
-import org.springframework.integration.leader.event.LeaderEventPublisher;
-
-/**
- * @author wind57
- */
-final class LeadershipControllerStub extends LeadershipController {
-
- private Leader leader;
-
- LeadershipControllerStub(Candidate candidate, LeaderProperties leaderProperties,
- LeaderEventPublisher leaderEventPublisher) {
- super(candidate, leaderProperties, leaderEventPublisher);
- }
-
- @Override
- public void update() {
-
- }
-
- @Override
- public void revoke() {
-
- }
-
- @Override
- protected PodReadinessWatcher createPodReadinessWatcher(String localLeaderId) {
- return null;
- }
-
- @Override
- protected void handleLeaderChange(Leader leader) {
- this.leader = leader;
- }
-
- @Override
- public Optional getLocalLeader() {
- return Optional.ofNullable(leader);
- }
-
-}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtilsTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderUtilsTests.java
similarity index 97%
rename from spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtilsTests.java
rename to spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderUtilsTests.java
index b3bad5be3..5a95b8403 100644
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/LeaderUtilsTests.java
+++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/leader/election/LeaderUtilsTests.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.springframework.cloud.kubernetes.commons.leader;
+package org.springframework.cloud.kubernetes.commons.leader.election;
import java.net.InetAddress;
import java.net.UnknownHostException;
diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/README.md b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/README.md
deleted file mode 100644
index dabb0d2e1..000000000
--- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/README.md
+++ /dev/null
@@ -1,162 +0,0 @@
-# Spring Cloud Kubernetes leader election example
-
-## Setting up the Environment
-
-This example uses a Fabric8 Maven Plugin to deploy an application to a Kubernetes cluster.
-To try it locally, download and install [Minikube](https://minikube.sigs.k8s.io/docs/start/).
-
-Once Minikube is downloaded, start it with the following command:
-```
-minikube start
-```
-
-And configure environment variables to point to the Minikube's Docker daemon:
-```
-eval $(minikube docker-env)
-```
-
-## Overview
-
-Spring Cloud Kubernetes leader election mechanism implements leader election API of Spring Integration using Kubernetes ConfigMap.
-Multiple instances of the same application compete for a leadership of a specified role.
-But only one of them can become a leader and receive an `OnGrantedEvent` application event with a leadership `Context`.
-All the instances periodically try to get a leadership and whichever comes first - becomes a leader.
-The new leader will remain until either its instance disappears from the cluster or it yields its leadership.
-Once the leader is gone, any of the existing instances can become a new leader (even the previous leader if it yielded leadership but stayed in the cluster).
-And finally, if the leadership is yielded or revoked for some reason, the old leader receives `OnRevokedEvent` application event.
-
-## Example application usage
-
-Leader election mechanism uses Kubernetes ConfigMap feature to coordinate leadership information.
-To access ConfigMap user needs correct role and role binding.
-Create them using the following commands:
-```
-kubectl apply -f leader-role.yml
-kubectl apply -f leader-rolebinding.yml
-```
-
-Build an image using the Spring Boot Build Image Plugin
-```
-./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=org/kubernetes-leader-election-example
-```
-
-You can then deploy the application using the following yaml.
-
-```yaml
----
-apiVersion: v1
-kind: List
-items:
- - apiVersion: v1
- kind: Service
- metadata:
- labels:
- app: kubernetes-leader-election-example
- name: kubernetes-leader-election-example
- spec:
- ports:
- - name: http
- port: 80
- targetPort: 8080
- selector:
- app: kubernetes-leader-election-example
- type: ClusterIP
- - apiVersion: v1
- kind: ServiceAccount
- metadata:
- labels:
- app: kubernetes-leader-election-example
- name: kubernetes-leader-election-example
- - apiVersion: rbac.authorization.k8s.io/v1
- kind: RoleBinding
- metadata:
- labels:
- app: kubernetes-leader-election-example
- name: kubernetes-leader-election-example:view
- roleRef:
- kind: Role
- apiGroup: rbac.authorization.k8s.io
- name: namespace-reader
- subjects:
- - kind: ServiceAccount
- name: kubernetes-leader-election-example
- - apiVersion: rbac.authorization.k8s.io/v1
- kind: Role
- metadata:
- namespace: default
- name: namespace-reader
- rules:
- - apiGroups: ["", "extensions", "apps"]
- resources: ["configmaps", "pods", "services", "endpoints", "secrets"]
- verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
- - apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: kubernetes-leader-election-example
- spec:
- replicas: 4
- selector:
- matchLabels:
- app: kubernetes-leader-election-example
- template:
- metadata:
- labels:
- app: kubernetes-leader-election-example
- spec:
- serviceAccountName: kubernetes-leader-election-example
- containers:
- - name: kubernetes-leader-election-example
- image: org/kubernetes-leader-election-example:latest
- imagePullPolicy: IfNotPresent
- readinessProbe:
- httpGet:
- port: 8080
- path: /actuator/health/readiness
- livenessProbe:
- httpGet:
- port: 8080
- path: /actuator/health/liveness
- ports:
- - containerPort: 8080
-
-
-```
-
-This will deploy a single application instance to the cluster and that instance will automatically become a leader.
-
-Create an environment variable for an easier application access:
-```
-SERVICE_URL=$(minikube service kubernetes-leader-election-example --url)
-```
-
-Get leadership information:
-```
-curl $SERVICE_URL
-```
-
-You should receive a message like this:
-```
-I am 'kubernetes-leader-election-example-1234567890-abcde' and I am the leader of the 'world'
-```
-
-Yield the leadership:
-```
-curl -X PUT $SERVICE_URL
-```
-
-And check the leadership information again:
-```
-curl $SERVICE_URL
-```
-
-If another instance was able to acquire the leadership you should see a different instance is now the leadership. You may
-have to try this a few times depending on how the service is load balanced.
-
-`DEBUG` logging is enabled so you can view the logs of the instances in order to see which instance is acquiring leadership.
-
-> Note: with multiple replicas in the cluster, `curl` command will access one of them depending on the Kubernetes load balancing configuration.
-Thus, when trying to yield the leadership, request might go to a non-leader node first. Just execute command again until it reaches the correct node.
-
-> Note: instances periodically try to acquire leadership and Spring Cloud Kubernetes doesn't decide which one of them is more worth to become one.
-Thus, it is possible that the instance which just yielded the leadership, made another leadership take over request faster than another instances and became a leader again.
-
diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/leader-role.yml b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/leader-role.yml
deleted file mode 100644
index c3d0ebeaf..000000000
--- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/leader-role.yml
+++ /dev/null
@@ -1,25 +0,0 @@
-apiVersion: rbac.authorization.k8s.io/v1
-kind: Role
-metadata:
- name: leader
- labels:
- app: kubernetes-leader-election-example
- group: org.springframework.cloud
-rules:
-- apiGroups:
- - ""
- resources:
- - pods
- verbs:
- - watch
- - get
-- apiGroups:
- - ""
- resources:
- - configmaps
- verbs:
- - watch
- - get
- - update
- # resourceNames:
- # -
diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/leader-rolebinding.yml b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/leader-rolebinding.yml
deleted file mode 100644
index 72e412167..000000000
--- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/leader-rolebinding.yml
+++ /dev/null
@@ -1,15 +0,0 @@
-apiVersion: rbac.authorization.k8s.io/v1
-kind: RoleBinding
-metadata:
- labels:
- app: kubernetes-leader-election-example
- group: org.springframework.cloud
- name: leader
-roleRef:
- apiGroup: ""
- kind: Role
- name: leader
-subjects:
-- kind: ServiceAccount
- name: default
- apiGroup: ""
diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml
deleted file mode 100644
index b2005a4eb..000000000
--- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml
+++ /dev/null
@@ -1,78 +0,0 @@
-
-
- 4.0.0
-
- org.springframework.cloud
- spring-cloud-kubernetes-examples
- 5.0.2-SNAPSHOT
-
-
- kubernetes-leader-election-example
- Spring Cloud Kubernetes :: Examples :: Leader Election
- Leader election demonstration with Spring Integration and ConfigMap
-
-
-
-
- org.springframework.boot
- spring-boot-dependencies
- pom
- import
- ${spring-boot.version}
-
-
- org.springframework.cloud
- spring-cloud-kubernetes-fabric8-leader
- ${project.version}
-
-
-
-
-
-
- org.springframework.boot
- spring-boot-starter-web
-
-
- org.springframework.boot
- spring-boot-starter-actuator
-
-
- org.springframework.cloud
- spring-cloud-kubernetes-fabric8-leader
-
-
- org.springframework.boot
- spring-boot-starter-test
- test
-
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-deploy-plugin
- ${maven-deploy-plugin.version}
-
-
-
-
-
- org.springframework.boot
- spring-boot-maven-plugin
-
-
- org.apache.maven.plugins
- maven-deploy-plugin
-
- true
-
-
-
-
-
-
diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java
deleted file mode 100644
index 4fab7c43a..000000000
--- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/java/org/springframework/cloud/kubernetes/examples/App.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright 2018-present 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
- *
- * https://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.examples;
-
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-@SpringBootApplication
-public class App {
-
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- }
-
-}
diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/java/org/springframework/cloud/kubernetes/examples/LeaderController.java b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/java/org/springframework/cloud/kubernetes/examples/LeaderController.java
deleted file mode 100644
index 27f634f4e..000000000
--- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/java/org/springframework/cloud/kubernetes/examples/LeaderController.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.examples;
-
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.event.EventListener;
-import org.springframework.http.ResponseEntity;
-import org.springframework.integration.leader.Context;
-import org.springframework.integration.leader.event.OnGrantedEvent;
-import org.springframework.integration.leader.event.OnRevokedEvent;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PutMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-@RestController
-public class LeaderController {
-
- private final String host;
-
- @Value("${spring.cloud.kubernetes.leader.role}")
- private String role;
-
- private Context context;
-
- public LeaderController() throws UnknownHostException {
- this.host = InetAddress.getLocalHost().getHostName();
- }
-
- /**
- * Return a message whether this instance is a leader or not.
- * @return info
- */
- @GetMapping("/")
- public String getInfo() {
- if (this.context == null) {
- return String.format("I am '%s' but I am not a leader of the '%s'", this.host, this.role);
- }
-
- return String.format("I am '%s' and I am the leader of the '%s'", this.host, this.role);
- }
-
- /**
- * PUT request to try and revoke a leadership of this instance. If the instance is not
- * a leader, leadership cannot be revoked. Thus "HTTP Bad Request" response. If the
- * instance is a leader, it must have a leadership context instance which can be used
- * to give up the leadership.
- * @return info about leadership
- */
- @PutMapping("/")
- public ResponseEntity revokeLeadership() {
- if (this.context == null) {
- String message = String.format("Cannot revoke leadership because '%s' is not a leader", this.host);
- return ResponseEntity.badRequest().body(message);
- }
-
- this.context.yield();
-
- String message = String.format("Leadership revoked for '%s'", this.host);
- return ResponseEntity.ok(message);
- }
-
- /**
- * Handle a notification that this instance has become a leader.
- * @param event on granted event
- */
- @EventListener
- public void handleEvent(OnGrantedEvent event) {
- System.out.println(String.format("'%s' leadership granted", event.getRole()));
- this.context = event.getContext();
- }
-
- /**
- * Handle a notification that this instance's leadership has been revoked.
- * @param event on revoked event
- */
- @EventListener
- public void handleEvent(OnRevokedEvent event) {
- System.out.println(String.format("'%s' leadership revoked", event.getRole()));
- this.context = null;
- }
-
-}
diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/resources/application.properties b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/resources/application.properties
deleted file mode 100644
index faf8c085c..000000000
--- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/main/resources/application.properties
+++ /dev/null
@@ -1,6 +0,0 @@
-# Role to which leader election this instance will participate
-spring.cloud.kubernetes.leader.role=world
-# Configmap to which leader election metadata will be saved
-spring.cloud.kubernetes.leader.config-map-name=leader
-logging.level.org.springframework.cloud.kubernetes.fabric8.leader=DEBUG
-logging.level.org.springframework.cloud.kubernetes.commons.leader=DEBUG
diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/test/java/org/springframework/cloud/kubernetes/examples/LeaderControllerTest.java b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/test/java/org/springframework/cloud/kubernetes/examples/LeaderControllerTest.java
deleted file mode 100644
index 05d12fb14..000000000
--- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/src/test/java/org/springframework/cloud/kubernetes/examples/LeaderControllerTest.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.examples;
-
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.integration.leader.Context;
-import org.springframework.integration.leader.event.OnGrantedEvent;
-import org.springframework.integration.leader.event.OnRevokedEvent;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.BDDMockito.given;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-
-@ExtendWith(MockitoExtension.class)
-public class LeaderControllerTest {
-
- @Mock
- private OnGrantedEvent mockOnGrantedEvent;
-
- @Mock
- private OnRevokedEvent mockOnRevokedEvent;
-
- @Mock
- private Context mockContext;
-
- private String host;
-
- private LeaderController leaderController;
-
- @BeforeEach
- public void before() throws UnknownHostException {
- this.host = InetAddress.getLocalHost().getHostName();
- this.leaderController = new LeaderController();
- }
-
- @Test
- public void shouldGetNonLeaderInfo() {
- String message = String.format("I am '%s' but I am not a leader of the 'null'", this.host);
- assertThat(this.leaderController.getInfo()).isEqualTo(message);
- }
-
- @Test
- public void shouldHandleGrantedEvent() {
- given(this.mockOnGrantedEvent.getContext()).willReturn(this.mockContext);
-
- this.leaderController.handleEvent(this.mockOnGrantedEvent);
-
- String message = String.format("I am '%s' and I am the leader of the 'null'", this.host);
- assertThat(this.leaderController.getInfo()).isEqualTo(message);
- }
-
- @Test
- public void shouldHandleRevokedEvent() {
- given(this.mockOnGrantedEvent.getContext()).willReturn(this.mockContext);
-
- this.leaderController.handleEvent(this.mockOnGrantedEvent);
- this.leaderController.handleEvent(this.mockOnRevokedEvent);
-
- String message = String.format("I am '%s' but I am not a leader of the 'null'", this.host);
- assertThat(this.leaderController.getInfo()).isEqualTo(message);
- }
-
- @Test
- public void shouldRevokeLeadership() {
- given(this.mockOnGrantedEvent.getContext()).willReturn(this.mockContext);
-
- this.leaderController.handleEvent(this.mockOnGrantedEvent);
- ResponseEntity responseEntity = this.leaderController.revokeLeadership();
-
- String message = String.format("Leadership revoked for '%s'", this.host);
- assertThat(responseEntity.getBody()).isEqualTo(message);
- assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
- verify(this.mockContext).yield();
- }
-
- @Test
- public void shouldNotRevokeLeadershipIfNotLeader() {
- ResponseEntity responseEntity = this.leaderController.revokeLeadership();
-
- String message = String.format("Cannot revoke leadership because '%s' is not a leader", this.host);
- assertThat(responseEntity.getBody()).isEqualTo(message);
- assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
- verify(this.mockContext, times(0)).yield();
- }
-
-}
diff --git a/spring-cloud-kubernetes-examples/pom.xml b/spring-cloud-kubernetes-examples/pom.xml
deleted file mode 100644
index 7cc4cf36e..000000000
--- a/spring-cloud-kubernetes-examples/pom.xml
+++ /dev/null
@@ -1,52 +0,0 @@
-
-
-
-
- 4.0.0
-
- spring-cloud-kubernetes
- org.springframework.cloud
- 5.0.2-SNAPSHOT
-
-
- spring-cloud-kubernetes-examples
- pom
-
- Spring Cloud Kubernetes :: Examples
- Examples showing how to use features of Spring Cloud Kubernetes.
-
-
-
- kubernetes-leader-election-example
-
-
-
-
-
- org.apache.maven.plugins
- maven-deploy-plugin
- ${maven-deploy-plugin.version}
-
- true
-
-
-
-
-
diff --git a/spring-cloud-kubernetes-fabric8-leader/pom.xml b/spring-cloud-kubernetes-fabric8-leader/pom.xml
index ead8495d7..79bddfdde 100644
--- a/spring-cloud-kubernetes-fabric8-leader/pom.xml
+++ b/spring-cloud-kubernetes-fabric8-leader/pom.xml
@@ -17,10 +17,6 @@
org.springframework.cloud
spring-cloud-kubernetes-fabric8-autoconfig
-
- org.springframework.integration
- spring-integration-core
-
org.springframework.boot
spring-boot-starter-actuator
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfiguration.java b/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfiguration.java
deleted file mode 100644
index 0f511d237..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfiguration.java
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import java.net.UnknownHostException;
-
-import io.fabric8.kubernetes.client.KubernetesClient;
-
-import org.springframework.boot.actuate.autoconfigure.info.ConditionalOnEnabledInfoContributor;
-import org.springframework.boot.actuate.info.InfoContributor;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderInfoContributor;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderInitiator;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderUtils;
-import org.springframework.cloud.kubernetes.commons.leader.election.ConditionalOnLeaderElectionDisabled;
-import org.springframework.context.ApplicationEventPublisher;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.integration.leader.Candidate;
-import org.springframework.integration.leader.DefaultCandidate;
-import org.springframework.integration.leader.event.DefaultLeaderEventPublisher;
-import org.springframework.integration.leader.event.LeaderEventPublisher;
-
-/**
- * @author Gytis Trikleris
- */
-@Configuration(proxyBeanMethods = false)
-@EnableConfigurationProperties(LeaderProperties.class)
-@ConditionalOnBean(KubernetesClient.class)
-@ConditionalOnProperty(value = "spring.cloud.kubernetes.leader.enabled", matchIfMissing = true)
-@ConditionalOnLeaderElectionDisabled
-public class Fabric8LeaderAutoConfiguration {
-
- /*
- * Used for publishing application events that happen: granted, revoked or failed to
- * acquire mutex.
- */
- @Bean
- @ConditionalOnMissingBean(LeaderEventPublisher.class)
- public LeaderEventPublisher defaultLeaderEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
- return new DefaultLeaderEventPublisher(applicationEventPublisher);
- }
-
- /*
- * This can be thought as "self" or the pod that participates in leader election
- * process. The implementation that we return simply logs events that happen during
- * that process.
- */
- @Bean
- public Candidate candidate(LeaderProperties leaderProperties) throws UnknownHostException {
- String id = LeaderUtils.hostName();
- String role = leaderProperties.getRole();
- return new DefaultCandidate(id, role);
- }
-
- /*
- * Add an info contributor with leader information.
- */
- @Bean
- @ConditionalOnClass(InfoContributor.class)
- @ConditionalOnEnabledInfoContributor("leader")
- public LeaderInfoContributor leaderInfoContributor(Fabric8LeadershipController fabric8LeadershipController,
- Candidate candidate) {
- return new LeaderInfoContributor(fabric8LeadershipController, candidate);
- }
-
- /**
- * watches the readiness of the pod. In case of a readiness change, it has to go
- * through leader process again.
- */
- @Bean
- public Fabric8PodReadinessWatcher hostPodWatcher(Candidate candidate, KubernetesClient kubernetesClient,
- Fabric8LeadershipController fabric8LeadershipController) {
- return new Fabric8PodReadinessWatcher(candidate.getId(), kubernetesClient, fabric8LeadershipController);
- }
-
- @Bean
- public Fabric8LeadershipController leadershipController(Candidate candidate, LeaderProperties leaderProperties,
- LeaderEventPublisher leaderEventPublisher, KubernetesClient kubernetesClient) {
- return new Fabric8LeadershipController(candidate, leaderProperties, leaderEventPublisher, kubernetesClient);
- }
-
- @Bean
- public Fabric8LeaderRecordWatcher leaderRecordWatcher(LeaderProperties leaderProperties,
- Fabric8LeadershipController fabric8LeadershipController, KubernetesClient kubernetesClient) {
- return new Fabric8LeaderRecordWatcher(leaderProperties, fabric8LeadershipController, kubernetesClient);
- }
-
- @Bean
- public LeaderInitiator leaderInitiator(LeaderProperties leaderProperties,
- Fabric8LeadershipController fabric8LeadershipController,
- Fabric8LeaderRecordWatcher fabric8LeaderRecordWatcher, Fabric8PodReadinessWatcher hostPodWatcher) {
- return new LeaderInitiator(leaderProperties, fabric8LeadershipController, fabric8LeaderRecordWatcher,
- hostPodWatcher);
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcher.java b/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcher.java
deleted file mode 100644
index dacadebab..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcher.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.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.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 {
-
- private static final LogAccessor LOGGER = new LogAccessor(Fabric8LeaderRecordWatcher.class);
-
- private final ReentrantLock lock = new ReentrantLock();
-
- private final Fabric8LeadershipController fabric8LeadershipController;
-
- private final LeaderProperties leaderProperties;
-
- private final KubernetesClient kubernetesClient;
-
- private volatile Watch configMapWatch;
-
- public Fabric8LeaderRecordWatcher(LeaderProperties leaderProperties,
- Fabric8LeadershipController fabric8LeadershipController, KubernetesClient kubernetesClient) {
- this.fabric8LeadershipController = fabric8LeadershipController;
- this.leaderProperties = leaderProperties;
- this.kubernetesClient = kubernetesClient;
- }
-
- public void start() {
- 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 (configMapWatch != null) {
- guarded(lock, () -> {
- if (configMapWatch != null) {
- LOGGER.debug(() -> "Stopping leader record watcher");
- configMapWatch.close();
- configMapWatch = null;
- }
- });
- }
- }
-
- @Override
- public void eventReceived(Action action, ConfigMap configMap) {
- LOGGER.debug(() -> action + " event received, triggering leadership update");
-
- if (!Action.ERROR.equals(action)) {
- fabric8LeadershipController.update();
- }
- }
-
- @Override
- public void onClose(WatcherException cause) {
- if (cause != null) {
- guarded(lock, () -> {
- LOGGER.warn(() -> "Watcher stopped unexpectedly, will restart because of : " + cause);
- configMapWatch = null;
- start();
- });
- }
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeadershipController.java b/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeadershipController.java
deleted file mode 100644
index 571257379..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeadershipController.java
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import java.util.Map;
-import java.util.concurrent.locks.ReentrantLock;
-
-import io.fabric8.kubernetes.api.model.ConfigMap;
-import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
-import io.fabric8.kubernetes.client.KubernetesClient;
-import io.fabric8.kubernetes.client.KubernetesClientException;
-
-import org.springframework.cloud.kubernetes.commons.leader.Leader;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties;
-import org.springframework.cloud.kubernetes.commons.leader.LeadershipController;
-import org.springframework.cloud.kubernetes.commons.leader.PodReadinessWatcher;
-import org.springframework.core.log.LogAccessor;
-import org.springframework.integration.leader.Candidate;
-import org.springframework.integration.leader.event.LeaderEventPublisher;
-
-import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.guarded;
-
-/**
- * @author Gytis Trikleris
- */
-public class Fabric8LeadershipController extends LeadershipController {
-
- private static final LogAccessor LOGGER = new LogAccessor(Fabric8LeadershipController.class);
-
- private final KubernetesClient kubernetesClient;
-
- private final ReentrantLock lock = new ReentrantLock();
-
- public Fabric8LeadershipController(Candidate candidate, LeaderProperties leaderProperties,
- LeaderEventPublisher leaderEventPublisher, KubernetesClient kubernetesClient) {
- super(candidate, leaderProperties, leaderEventPublisher);
- this.kubernetesClient = kubernetesClient;
- }
-
- @Override
- public void update() {
- guarded(lock, () -> {
- LOGGER.debug(() -> "Checking leader state");
- ConfigMap configMap = getConfigMap();
- if (configMap == null && !leaderProperties.isCreateConfigMap()) {
- LOGGER.warn("ConfigMap '" + leaderProperties.getConfigMapName()
- + "' does not exist and leaderProperties.isCreateConfigMap() "
- + "is false, cannot acquire leadership");
- notifyOnFailedToAcquire();
- return;
- }
- Leader leader = extractLeader(configMap);
-
- if (leader != null && isPodReady(leader.getId())) {
- handleLeaderChange(leader);
- return;
- }
-
- if (leader != null && leader.isCandidate(candidate)) {
- revoke(configMap);
- }
- else {
- acquire(configMap);
- }
- });
-
- }
-
- public void revoke() {
- guarded(lock, () -> {
- ConfigMap configMap = getConfigMap();
- Leader leader = extractLeader(configMap);
-
- if (leader != null && leader.isCandidate(candidate)) {
- revoke(configMap);
- }
- });
- }
-
- private void revoke(ConfigMap configMap) {
- LOGGER.debug(() -> "Trying to revoke leadership from :" + candidate);
-
- try {
- String leaderKey = getLeaderKey();
- removeConfigMapEntry(configMap, leaderKey);
- handleLeaderChange(null);
- }
- catch (KubernetesClientException e) {
- LOGGER.warn("Failure when revoking leadership for : " + candidate + "because : " + e.getMessage());
- }
- }
-
- private void acquire(ConfigMap configMap) {
- LOGGER.debug(() -> "Trying to acquire leadership for :" + candidate);
-
- if (!isPodReady(candidate.getId())) {
- LOGGER.debug("Pod : " + candidate + "is not ready at the moment, cannot acquire leadership");
- return;
- }
-
- try {
- Map data = getLeaderData(candidate);
-
- if (configMap == null) {
- createConfigMap(data);
- }
- else {
- updateConfigMapEntry(configMap, data);
- }
-
- Leader newLeader = new Leader(candidate.getRole(), candidate.getId());
- handleLeaderChange(newLeader);
- }
- catch (KubernetesClientException e) {
- LOGGER.warn(() -> "Failure when acquiring leadership for : " + candidate + " because : " + e.getMessage());
- notifyOnFailedToAcquire();
- }
- }
-
- @Override
- protected PodReadinessWatcher createPodReadinessWatcher(String localLeaderId) {
- return new Fabric8PodReadinessWatcher(localLeaderId, kubernetesClient, this);
- }
-
- private Leader extractLeader(ConfigMap configMap) {
- if (configMap == null) {
- return null;
- }
-
- return extractLeader(configMap.getData());
- }
-
- private boolean isPodReady(String name) {
- return kubernetesClient.pods().withName(name).isReady();
- }
-
- private ConfigMap getConfigMap() {
- return kubernetesClient.configMaps()
- .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
- .withName(leaderProperties.getConfigMapName())
- .get();
- }
-
- private void createConfigMap(Map data) {
- LOGGER.debug(() -> "Creating new config map with data: " + data);
-
- ConfigMap newConfigMap = new ConfigMapBuilder().withNewMetadata()
- .withName(leaderProperties.getConfigMapName())
- .addToLabels(PROVIDER_KEY, PROVIDER)
- .addToLabels(KIND_KEY, KIND)
- .endMetadata()
- .addToData(data)
- .build();
-
- kubernetesClient.configMaps()
- .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
- .resource(newConfigMap)
- .create();
- }
-
- private void updateConfigMapEntry(ConfigMap configMap, Map newData) {
- LOGGER.debug(() -> "Adding new data to config map: " + newData);
- ConfigMap newConfigMap = new ConfigMapBuilder(configMap).addToData(newData).build();
- updateConfigMap(configMap, newConfigMap);
- }
-
- private void removeConfigMapEntry(ConfigMap configMap, String key) {
- LOGGER.debug(() -> "Removing config map entry: " + key);
- ConfigMap newConfigMap = new ConfigMapBuilder(configMap).removeFromData(key).build();
- updateConfigMap(configMap, newConfigMap);
- }
-
- private void updateConfigMap(ConfigMap oldConfigMap, ConfigMap newConfigMap) {
- String oldResourceVersion = oldConfigMap.getMetadata().getResourceVersion();
- kubernetesClient.configMaps()
- .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace()))
- .resource(newConfigMap)
- .lockResourceVersion(oldResourceVersion)
- .update();
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8PodReadinessWatcher.java b/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8PodReadinessWatcher.java
deleted file mode 100644
index 8932cd6b6..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8PodReadinessWatcher.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import java.util.concurrent.locks.ReentrantLock;
-
-import io.fabric8.kubernetes.api.model.Pod;
-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 io.fabric8.kubernetes.client.dsl.PodResource;
-import io.fabric8.kubernetes.client.readiness.Readiness;
-import org.apache.commons.logging.LogFactory;
-
-import org.springframework.cloud.kubernetes.commons.leader.PodReadinessWatcher;
-import org.springframework.core.log.LogAccessor;
-
-import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.guarded;
-
-/**
- * @author Gytis Trikleris
- */
-public class Fabric8PodReadinessWatcher implements PodReadinessWatcher, Watcher {
-
- private static final LogAccessor LOGGER = new LogAccessor(LogFactory.getLog(Fabric8PodReadinessWatcher.class));
-
- private final ReentrantLock lock = new ReentrantLock();
-
- private final String podName;
-
- private final KubernetesClient kubernetesClient;
-
- private final Fabric8LeadershipController fabric8LeadershipController;
-
- private volatile boolean previousState;
-
- private volatile Watch watch;
-
- public Fabric8PodReadinessWatcher(String podName, KubernetesClient kubernetesClient,
- Fabric8LeadershipController fabric8LeadershipController) {
- this.podName = podName;
- this.kubernetesClient = kubernetesClient;
- this.fabric8LeadershipController = fabric8LeadershipController;
- }
-
- @Override
- public void start() {
- if (watch == null) {
- guarded(lock, () -> {
- if (watch == null) {
- LOGGER.debug(() -> "Starting pod readiness watcher for :" + podName);
- PodResource podResource = kubernetesClient.pods().withName(podName);
- previousState = podResource.isReady();
- watch = podResource.watch(this);
- }
- });
- }
- }
-
- @Override
- public void stop() {
- if (watch != null) {
- guarded(lock, () -> {
- if (watch != null) {
- LOGGER.debug(() -> "Stopping pod readiness watcher for :" + podName);
- watch.close();
- watch = null;
- }
- });
- }
- }
-
- @Override
- public void eventReceived(Action action, Pod pod) {
- boolean currentState = Readiness.isPodReady(pod);
- if (previousState != currentState) {
- guarded(lock, () -> {
- if (previousState != currentState) {
- LOGGER.debug(() -> "readiness status changed for pod : " + podName + " to state: " + currentState
- + ", triggering leadership update");
- previousState = currentState;
- fabric8LeadershipController.update();
- }
- });
- }
- }
-
- @Override
- public void onClose(WatcherException cause) {
- if (cause != null) {
- guarded(lock, () -> {
- LOGGER.warn(() -> "Watcher stopped unexpectedly, will restart" + cause.getMessage());
- watch = null;
- start();
- });
- }
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfiguration.java b/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfiguration.java
index a4db68a6c..31334a4aa 100644
--- a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfiguration.java
+++ b/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfiguration.java
@@ -45,9 +45,9 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.log.LogAccessor;
-import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.COORDINATION_GROUP;
-import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.COORDINATION_VERSION;
-import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.LEASE;
+import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils.COORDINATION_GROUP;
+import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils.COORDINATION_VERSION;
+import static org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils.LEASE;
/**
* @author wind57
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-kubernetes-fabric8-leader/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
index c94b99ab8..f045c4084 100644
--- a/spring-cloud-kubernetes-fabric8-leader/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
+++ b/spring-cloud-kubernetes-fabric8-leader/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -1,3 +1,2 @@
-org.springframework.cloud.kubernetes.fabric8.leader.Fabric8LeaderAutoConfiguration
org.springframework.cloud.kubernetes.fabric8.leader.election.Fabric8LeaderElectionCallbacksAutoConfiguration
org.springframework.cloud.kubernetes.fabric8.leader.election.Fabric8LeaderElectionAutoConfiguration
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfigurationTests.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfigurationTests.java
deleted file mode 100644
index 35373df24..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfigurationTests.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import org.junit.jupiter.api.Test;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.boot.test.web.server.LocalManagementPort;
-import org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient;
-import org.springframework.http.MediaType;
-import org.springframework.test.web.reactive.server.WebTestClient;
-
-@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
- properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.autoStartup=false",
- "management.endpoints.web.exposure.include=info", "management.endpoint.info.show-details=always",
- "management.info.kubernetes.enabled=true" })
-@AutoConfigureWebTestClient
-class Fabric8LeaderAutoConfigurationTests {
-
- @LocalManagementPort
- private int port;
-
- @Autowired
- private WebTestClient webClient;
-
- @Test
- void contextLoads() {
- }
-
- @Test
- void infoEndpointShouldContainLeaderElection() {
- webClient.get()
- .uri("http://localhost:{port}/actuator/info", port)
- .accept(MediaType.APPLICATION_JSON)
- .exchange()
- .expectStatus()
- .isOk()
- .expectBody()
- .jsonPath("kubernetes")
- .exists()
- .jsonPath("leaderElection")
- .exists()
- .jsonPath("leaderElection.leaderId")
- .exists();
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderContextTest.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderContextTest.java
deleted file mode 100644
index e000a7911..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderContextTest.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import java.util.Optional;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-import org.springframework.cloud.kubernetes.commons.leader.Leader;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderContext;
-import org.springframework.integration.leader.Candidate;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.verify;
-
-/**
- * @author Gytis Trikleris
- */
-class Fabric8LeaderContextTest {
-
- private final Candidate mockCandidate = Mockito.mock(Candidate.class);
-
- private final Fabric8LeadershipController mockFabric8LeadershipController = Mockito
- .mock(Fabric8LeadershipController.class);
-
- private final Leader mockLeader = Mockito.mock(Leader.class);
-
- private LeaderContext leaderContext;
-
- @BeforeEach
- void beforeEach() {
- leaderContext = new LeaderContext(mockCandidate, mockFabric8LeadershipController);
- }
-
- @Test
- void testIsLeaderWithoutLeader() {
- Mockito.when(mockFabric8LeadershipController.getLocalLeader()).thenReturn(Optional.empty());
- boolean result = leaderContext.isLeader();
- assertThat(result).isFalse();
- }
-
- @Test
- void testIsLeaderWithAnotherLeader() {
- Mockito.when(mockFabric8LeadershipController.getLocalLeader()).thenReturn(Optional.of(mockLeader));
- boolean result = leaderContext.isLeader();
- assertThat(result).isFalse();
- }
-
- @Test
- void testIsLeaderWhenLeader() {
- Mockito.when(mockFabric8LeadershipController.getLocalLeader()).thenReturn(Optional.of(mockLeader));
- Mockito.when(mockLeader.isCandidate(mockCandidate)).thenReturn(true);
- boolean result = this.leaderContext.isLeader();
- assertThat(result).isTrue();
- }
-
- @Test
- void shouldYieldLeadership() {
- leaderContext.yield();
- verify(mockFabric8LeadershipController).revoke();
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderInfoContributorDisabledTests.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderInfoContributorDisabledTests.java
deleted file mode 100644
index 363c929f7..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderInfoContributorDisabledTests.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import org.junit.jupiter.api.Test;
-
-import org.springframework.beans.factory.NoSuchBeanDefinitionException;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.SpringBootConfiguration;
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderInfoContributor;
-import org.springframework.context.ApplicationContext;
-
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-/**
- * Tests that verify LeaderInfoContributor can be disabled via configuration property.
- */
-@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
- properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.autoStartup=false",
- "management.info.leader.enabled=false" })
-class Fabric8LeaderInfoContributorDisabledTests {
-
- @Autowired
- private ApplicationContext context;
-
- /**
- * Test that the LeaderInfoContributor bean is NOT present when
- * management.info.leader.enabled=false
- */
- @Test
- void leaderInfoContributorShouldNotBePresent() {
- assertThatThrownBy(() -> context.getBean(LeaderInfoContributor.class))
- .isInstanceOf(NoSuchBeanDefinitionException.class);
- }
-
- @SpringBootConfiguration
- @EnableAutoConfiguration
- protected static class TestConfig {
-
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderInitiatorTest.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderInitiatorTest.java
deleted file mode 100644
index 3d4400246..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderInitiatorTest.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import java.time.Duration;
-
-import org.awaitility.Awaitility;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-import org.springframework.cloud.kubernetes.commons.leader.LeaderInitiator;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.verify;
-import static org.mockito.internal.verification.VerificationModeFactory.atLeastOnce;
-
-/**
- * @author Gytis Trikleris
- */
-class Fabric8LeaderInitiatorTest {
-
- private final LeaderProperties leaderProperties = new LeaderProperties();
-
- private final Fabric8LeadershipController mockFabric8LeadershipController = Mockito
- .mock(Fabric8LeadershipController.class);
-
- private final Fabric8LeaderRecordWatcher mockFabric8LeaderRecordWatcher = Mockito
- .mock(Fabric8LeaderRecordWatcher.class);
-
- private final Fabric8PodReadinessWatcher mockFabric8PodReadinessWatcher = Mockito
- .mock(Fabric8PodReadinessWatcher.class);
-
- private final Runnable runnable = Mockito.mock(Runnable.class);
-
- private LeaderInitiator leaderInitiator;
-
- @BeforeEach
- void beforeEach() {
- leaderInitiator = new LeaderInitiator(leaderProperties, mockFabric8LeadershipController,
- mockFabric8LeaderRecordWatcher, mockFabric8PodReadinessWatcher);
- }
-
- @AfterEach
- void afterEach() {
- leaderInitiator.stop();
- Mockito.reset(mockFabric8LeadershipController, mockFabric8LeaderRecordWatcher, mockFabric8PodReadinessWatcher);
- }
-
- @Test
- void testIsAutoStartup() {
- assertThat(leaderInitiator.isAutoStartup()).isTrue();
- }
-
- @Test
- void shouldStart() {
- leaderProperties.setUpdatePeriod(Duration.ofMillis(1L));
-
- leaderInitiator.start();
-
- assertThat(leaderInitiator.isRunning()).isTrue();
- verify(mockFabric8LeaderRecordWatcher).start();
- verify(mockFabric8PodReadinessWatcher).start();
-
- Awaitility.await().untilAsserted(() -> verify(mockFabric8LeadershipController, atLeastOnce()).update());
-
- }
-
- @Test
- void shouldStartOnlyOnce() {
- leaderInitiator.start();
- leaderInitiator.start();
-
- verify(mockFabric8LeaderRecordWatcher).start();
- }
-
- @Test
- void shouldStop() {
- leaderInitiator.start();
- leaderInitiator.stop();
-
- assertThat(leaderInitiator.isRunning()).isFalse();
- verify(mockFabric8LeaderRecordWatcher).stop();
- verify(mockFabric8PodReadinessWatcher).start();
- verify(mockFabric8LeadershipController).revoke();
- }
-
- @Test
- void shouldStopOnlyOnce() {
- leaderInitiator.start();
- leaderInitiator.stop();
- leaderInitiator.stop();
-
- verify(mockFabric8LeaderRecordWatcher).stop();
- }
-
- @Test
- void shouldStopAndExecuteCallback() {
- leaderInitiator.start();
- leaderInitiator.stop(runnable);
-
- assertThat(leaderInitiator.isRunning()).isFalse();
- verify(mockFabric8LeaderRecordWatcher).stop();
- verify(mockFabric8PodReadinessWatcher).start();
- verify(mockFabric8LeadershipController).revoke();
- verify(runnable).run();
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcherTest.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcherTest.java
deleted file mode 100644
index 9f72712f2..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcherTest.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import io.fabric8.kubernetes.api.model.ConfigMap;
-import io.fabric8.kubernetes.api.model.ConfigMapList;
-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 io.fabric8.kubernetes.client.dsl.MixedOperation;
-import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
-import io.fabric8.kubernetes.client.dsl.Resource;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties;
-
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-
-/**
- * @author Gytis Trikleris
- */
-class Fabric8LeaderRecordWatcherTest {
-
- private final LeaderProperties mockLeaderProperties = Mockito.mock(LeaderProperties.class);
-
- private final Fabric8LeadershipController mockFabric8LeadershipController = Mockito
- .mock(Fabric8LeadershipController.class);
-
- private final KubernetesClient mockKubernetesClient = Mockito.mock(KubernetesClient.class);
-
- @SuppressWarnings("unchecked")
- private final MixedOperation> mockConfigMapsOperation = Mockito
- .mock(MixedOperation.class);
-
- @SuppressWarnings("unchecked")
- private final NonNamespaceOperation> mockInNamespaceOperation = Mockito
- .mock(NonNamespaceOperation.class);
-
- @SuppressWarnings("unchecked")
- private final Resource mockWithNameResource = Mockito.mock(Resource.class);
-
- private final Watch mockWatch = Mockito.mock(Watch.class);
-
- private final ConfigMap mockConfigMap = Mockito.mock(ConfigMap.class);
-
- private final WatcherException mockKubernetesClientException = Mockito.mock(WatcherException.class);
-
- private Fabric8LeaderRecordWatcher watcher;
-
- @BeforeEach
- void beforeEach() {
- watcher = new Fabric8LeaderRecordWatcher(mockLeaderProperties, mockFabric8LeadershipController,
- mockKubernetesClient);
- }
-
- @Test
- void shouldStartOnce() {
- initStubs();
- watcher.start();
- watcher.start();
- verify(mockWithNameResource).watch(watcher);
- }
-
- @Test
- void shouldStopOnce() {
- initStubs();
- watcher.start();
- watcher.stop();
- watcher.stop();
- verify(mockWatch).close();
- }
-
- @Test
- void shouldHandleEvent() {
- watcher.eventReceived(Watcher.Action.ADDED, mockConfigMap);
- watcher.eventReceived(Watcher.Action.DELETED, mockConfigMap);
- watcher.eventReceived(Watcher.Action.MODIFIED, mockConfigMap);
- verify(mockFabric8LeadershipController, times(3)).update();
- }
-
- @Test
- void shouldIgnoreErrorEvent() {
- watcher.eventReceived(Watcher.Action.ERROR, mockConfigMap);
- verify(mockFabric8LeadershipController, times(0)).update();
- }
-
- @Test
- void shouldHandleClose() {
- initStubs();
- watcher.onClose(mockKubernetesClientException);
- verify(mockWithNameResource).watch(watcher);
- }
-
- @Test
- void shouldIgnoreCloseWithoutCause() {
- watcher.onClose(null);
- verify(mockWithNameResource, times(0)).watch(watcher);
- }
-
- private void initStubs() {
- Mockito.when(mockKubernetesClient.configMaps()).thenReturn(mockConfigMapsOperation);
- Mockito.when(mockConfigMapsOperation.inNamespace(null)).thenReturn(mockInNamespaceOperation);
- Mockito.when(mockInNamespaceOperation.withName(null)).thenReturn(mockWithNameResource);
- Mockito.when(mockWithNameResource.watch(watcher)).thenReturn(mockWatch);
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderTest.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderTest.java
deleted file mode 100644
index d02717769..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderTest.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import org.springframework.cloud.kubernetes.commons.leader.Leader;
-import org.springframework.integration.leader.Candidate;
-import org.springframework.integration.leader.DefaultCandidate;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-/**
- * @author Gytis Trikleris
- */
-class Fabric8LeaderTest {
-
- private static final String ROLE = "test-role";
-
- private static final String ID = "test-id";
-
- private Leader leader;
-
- @BeforeEach
- void before() {
- leader = new Leader(ROLE, ID);
- }
-
- @Test
- void shouldGetRole() {
- assertThat(leader.getRole()).isEqualTo(ROLE);
- }
-
- @Test
- void shouldGetId() {
- assertThat(leader.getId()).isEqualTo(ID);
- }
-
- @Test
- void shouldCheckWithNullCandidate() {
- assertThat(leader.isCandidate(null)).isEqualTo(false);
- }
-
- @Test
- void shouldCheckCandidate() {
- Candidate candidate = new DefaultCandidate(ID, ROLE);
- assertThat(leader.isCandidate(candidate)).isTrue();
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeadershipControllerTest.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeadershipControllerTest.java
deleted file mode 100644
index eb55e0025..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeadershipControllerTest.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import io.fabric8.kubernetes.api.model.ConfigMap;
-import io.fabric8.kubernetes.api.model.ConfigMapList;
-import io.fabric8.kubernetes.client.KubernetesClient;
-import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
-import io.fabric8.kubernetes.client.dsl.Resource;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Mockito;
-
-import org.springframework.boot.test.system.CapturedOutput;
-import org.springframework.boot.test.system.OutputCaptureExtension;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties;
-import org.springframework.integration.leader.Candidate;
-import org.springframework.integration.leader.event.LeaderEventPublisher;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-/**
- * @author Gytis Trikleris
- */
-public class Fabric8LeadershipControllerTest {
-
- private final Candidate mockCandidate = Mockito.mock(Candidate.class);
-
- private final LeaderProperties mockLeaderProperties = Mockito.mock(LeaderProperties.class);
-
- private final LeaderEventPublisher mockLeaderEventPublisher = Mockito.mock(LeaderEventPublisher.class);
-
- private final KubernetesClient mockKubernetesClient = Mockito.mock(KubernetesClient.class,
- Mockito.RETURNS_DEEP_STUBS);
-
- private Fabric8LeadershipController fabric8LeadershipController;
-
- @BeforeEach
- void beforeEach() {
- fabric8LeadershipController = new Fabric8LeadershipController(mockCandidate, mockLeaderProperties,
- mockLeaderEventPublisher, mockKubernetesClient);
- }
-
- @Test
- void shouldGetEmptyLocalLeader() {
- assertThat(fabric8LeadershipController.getLocalLeader().isPresent()).isFalse();
- }
-
- @Test
- @ExtendWith(OutputCaptureExtension.class)
- void whenNonExistentConfigmapAndCreationNotAllowedStopLeadershipAcquire(CapturedOutput output) {
- // given
- String testNamespace = "test-namespace";
- String testConfigmap = "test-configmap";
- @SuppressWarnings("unchecked")
- Resource mockResource = Mockito.mock(Resource.class);
- @SuppressWarnings("unchecked")
- NonNamespaceOperation> mockNonNamespaceOperation = Mockito
- .mock(NonNamespaceOperation.class);
-
- Fabric8LeadershipController fabric8LeadershipController = new Fabric8LeadershipController(mockCandidate,
- mockLeaderProperties, mockLeaderEventPublisher, mockKubernetesClient);
-
- when(mockLeaderProperties.isCreateConfigMap()).thenReturn(false);
- when(mockLeaderProperties.isPublishFailedEvents()).thenReturn(true);
- when(mockLeaderProperties.getConfigMapName()).thenReturn(testConfigmap);
- when(mockKubernetesClient.getNamespace()).thenReturn(testNamespace);
- when(mockLeaderProperties.getNamespace(anyString())).thenReturn(testNamespace);
- when(mockKubernetesClient.configMaps().inNamespace(anyString())).thenReturn(mockNonNamespaceOperation);
- when(mockNonNamespaceOperation.withName(any())).thenReturn(mockResource);
- when(mockResource.get()).thenReturn(null);
-
- // when
- fabric8LeadershipController.update();
-
- // then
- assertThat(output).contains(
- "ConfigMap 'test-configmap' does not exist and leaderProperties.isCreateConfigMap() is false, cannot acquire leadership");
- verify(mockLeaderEventPublisher).publishOnFailedToAcquire(any(), any(), any());
-
- verify(mockKubernetesClient, never()).pods();
- verify(mockCandidate, never()).getId();
- verify(mockLeaderProperties, never()).getLeaderIdPrefix();
- verify(mockLeaderEventPublisher, never()).publishOnGranted(any(), any(), any());
- verify(mockLeaderEventPublisher, never()).publishOnRevoked(any(), any(), any());
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8PodReadinessWatcherTest.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8PodReadinessWatcherTest.java
deleted file mode 100644
index 77007af1f..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8PodReadinessWatcherTest.java
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader;
-
-import io.fabric8.kubernetes.api.model.Pod;
-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.Watch;
-import io.fabric8.kubernetes.client.Watcher;
-import io.fabric8.kubernetes.client.WatcherException;
-import io.fabric8.kubernetes.client.dsl.MixedOperation;
-import io.fabric8.kubernetes.client.dsl.PodResource;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-
-/**
- * @author Gytis Trikleris
- */
-public class Fabric8PodReadinessWatcherTest {
-
- private static final String POD_NAME = "test-pod";
-
- private final Fabric8LeadershipController mockFabric8LeadershipController = Mockito
- .mock(Fabric8LeadershipController.class);
-
- private final KubernetesClient mockKubernetesClient = Mockito.mock(KubernetesClient.class);
-
- @SuppressWarnings("unchecked")
- private final MixedOperation mockPodsOperation = Mockito.mock(MixedOperation.class);
-
- private final PodResource mockPodResource = Mockito.mock(PodResource.class);
-
- private final Pod mockPod = Mockito.mock(Pod.class);
-
- private final PodStatus mockPodStatus = Mockito.mock(PodStatus.class);
-
- private final Watch mockWatch = Mockito.mock(Watch.class);
-
- private final WatcherException mockKubernetesClientException = Mockito.mock(WatcherException.class);
-
- private Fabric8PodReadinessWatcher watcher;
-
- @BeforeEach
- void beforeEach() {
- watcher = new Fabric8PodReadinessWatcher(POD_NAME, mockKubernetesClient, mockFabric8LeadershipController);
- }
-
- @Test
- void shouldStartOnce() {
- initStubs();
- watcher.start();
- watcher.start();
- verify(mockPodResource).watch(watcher);
- }
-
- @Test
- void shouldStopOnce() {
- initStubs();
- watcher.start();
- watcher.stop();
- watcher.stop();
- verify(mockWatch).close();
- }
-
- @Test
- void shouldHandleEventWithStateChange() {
- initStubs();
- Mockito.when(mockPodResource.isReady()).thenReturn(true);
- Mockito.when(mockPod.getStatus()).thenReturn(mockPodStatus);
-
- watcher.start();
- watcher.eventReceived(Watcher.Action.ADDED, mockPod);
- verify(mockFabric8LeadershipController).update();
- }
-
- @Test
- void shouldIgnoreEventIfStateDoesNotChange() {
- initStubs();
- Mockito.when(mockPod.getStatus()).thenReturn(mockPodStatus);
-
- watcher.start();
- watcher.eventReceived(Watcher.Action.ADDED, mockPod);
- verify(mockFabric8LeadershipController, times(0)).update();
- }
-
- @Test
- void shouldHandleClose() {
- initStubs();
- watcher.onClose(mockKubernetesClientException);
- verify(mockPodResource).watch(watcher);
- }
-
- @Test
- void shouldIgnoreCloseWithoutCause() {
- watcher.onClose(null);
- verify(mockPodResource, times(0)).watch(watcher);
- }
-
- private void initStubs() {
- Mockito.when(mockKubernetesClient.pods()).thenReturn(mockPodsOperation);
- Mockito.when(mockPodsOperation.withName(POD_NAME)).thenReturn(mockPodResource);
- Mockito.when(mockPodResource.watch(watcher)).thenReturn(mockWatch);
- }
-
-}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderApp.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderApp.java
index fa224c425..52217891a 100644
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderApp.java
+++ b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderApp.java
@@ -16,49 +16,14 @@
package org.springframework.cloud.kubernetes.fabric8.leader.election;
-import io.fabric8.kubernetes.client.KubernetesClient;
-import io.fabric8.kubernetes.client.dsl.MixedOperation;
-import io.fabric8.kubernetes.client.dsl.PodResource;
-import io.fabric8.kubernetes.client.dsl.Resource;
-import io.fabric8.kubernetes.client.dsl.internal.BaseOperation;
-import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.Lock;
-import org.mockito.Mockito;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.Primary;
-
-@Configuration
+@SpringBootApplication
public class Fabric8LeaderApp {
- @SuppressWarnings({ "rawtypes", "unchecked" })
- @Bean
- KubernetesClient kubernetesClient() {
- KubernetesClient client = Mockito.mock(KubernetesClient.class);
- Mockito.when(client.getNamespace()).thenReturn("a");
-
- MixedOperation mixedOperation = Mockito.mock(MixedOperation.class);
- Mockito.when(client.configMaps()).thenReturn(mixedOperation);
-
- PodResource podResource = Mockito.mock(PodResource.class);
- Mockito.when(podResource.isReady()).thenReturn(true);
-
- Mockito.when(client.pods()).thenReturn(mixedOperation);
- Mockito.when(mixedOperation.withName(Mockito.anyString())).thenReturn(podResource);
-
- Resource resource = Mockito.mock(Resource.class);
-
- BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
- Mockito.when(baseOperation.withName("leaders")).thenReturn(resource);
-
- Mockito.when(mixedOperation.inNamespace("a")).thenReturn(baseOperation);
- return client;
- }
-
- @Bean
- @Primary
- Lock lock() {
- return Mockito.mock(Lock.class);
+ public static void main(String[] args) {
+ SpringApplication.run(Fabric8LeaderApp.class, args);
}
}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderConfiguration.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderConfiguration.java
new file mode 100644
index 000000000..f588654af
--- /dev/null
+++ b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderConfiguration.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2013-present 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
+ *
+ * https://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.fabric8.leader.election;
+
+import io.fabric8.kubernetes.client.KubernetesClient;
+import io.fabric8.kubernetes.client.dsl.MixedOperation;
+import io.fabric8.kubernetes.client.dsl.PodResource;
+import io.fabric8.kubernetes.client.dsl.Resource;
+import io.fabric8.kubernetes.client.dsl.internal.BaseOperation;
+import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.Lock;
+import org.mockito.Mockito;
+
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+
+@Configuration
+@ConditionalOnProperty(value = "use.mock.config", havingValue = "true", matchIfMissing = false)
+public class Fabric8LeaderConfiguration {
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Bean
+ KubernetesClient kubernetesClient() {
+ KubernetesClient client = Mockito.mock(KubernetesClient.class);
+ Mockito.when(client.getNamespace()).thenReturn("a");
+
+ MixedOperation mixedOperation = Mockito.mock(MixedOperation.class);
+ Mockito.when(client.configMaps()).thenReturn(mixedOperation);
+
+ PodResource podResource = Mockito.mock(PodResource.class);
+ Mockito.when(podResource.isReady()).thenReturn(true);
+
+ Mockito.when(client.pods()).thenReturn(mixedOperation);
+ Mockito.when(mixedOperation.withName(Mockito.anyString())).thenReturn(podResource);
+
+ Resource resource = Mockito.mock(Resource.class);
+
+ BaseOperation baseOperation = Mockito.mock(BaseOperation.class);
+ Mockito.when(baseOperation.withName("leaders")).thenReturn(resource);
+
+ Mockito.when(mixedOperation.inNamespace("a")).thenReturn(baseOperation);
+ return client;
+ }
+
+ @Bean
+ @Primary
+ Lock lock() {
+ return Mockito.mock(Lock.class);
+ }
+
+}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfigurationTests.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfigurationTests.java
index 6afd5dade..d471f2c40 100644
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfigurationTests.java
+++ b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionAutoConfigurationTests.java
@@ -16,12 +16,16 @@
package org.springframework.cloud.kubernetes.fabric8.leader.election;
+import java.util.function.BooleanSupplier;
+
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
-import org.springframework.cloud.kubernetes.fabric8.leader.Fabric8LeaderAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Primary;
/**
* tests that ensure 'spring.cloud.kubernetes.leader.election' enabled correct
@@ -37,19 +41,21 @@ class Fabric8LeaderElectionAutoConfigurationTests {
*
* As such:
*
- * - Fabric8LeaderAutoConfiguration must be picked up
- * - Fabric8LeaderElectionAutoConfiguration must not be picked up
+ * - Fabric8LeaderElectionAutoConfiguration is present
+ * - Fabric8LeaderElectionCallbacksAutoConfiguration is present
*
*/
@Test
void leaderElectionAnnotationMissing() {
- new ApplicationContextRunner().withUserConfiguration(Fabric8LeaderApp.class)
- .withConfiguration(AutoConfigurations.of(Fabric8LeaderAutoConfiguration.class,
- Fabric8LeaderElectionAutoConfiguration.class,
- Fabric8LeaderElectionCallbacksAutoConfiguration.class))
+ new ApplicationContextRunner().withUserConfiguration(Fabric8LeaderConfiguration.class)
+ .withAllowBeanDefinitionOverriding(true)
+ .withUserConfiguration(Fabric8LeaderElectionAutoConfiguration.class,
+ Fabric8LeaderElectionCallbacksAutoConfiguration.class, TestConfig.class)
+ .withPropertyValues("spring.main.cloud-platform=KUBERNETES", "use.mock.config=true")
.run(context -> {
- Assertions.assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
- Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
+ // matchIfMissing = true in the annotation, so both are present
+ Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
+ Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionCallbacksAutoConfiguration.class);
});
}
@@ -59,20 +65,20 @@ void leaderElectionAnnotationMissing() {
*
* As such:
*
- * - Fabric8LeaderAutoConfiguration must be picked up
- * - Fabric8LeaderElectionAutoConfiguration must not be picked up
+ * - Fabric8LeaderElectionAutoConfiguration is not present
+ * - Fabric8LeaderElectionCallbacksAutoConfiguration is not present
*
*/
@Test
void leaderElectionAnnotationPresentEqualToFalse() {
- new ApplicationContextRunner().withUserConfiguration(Fabric8LeaderApp.class)
- .withConfiguration(AutoConfigurations.of(Fabric8LeaderAutoConfiguration.class,
- Fabric8LeaderElectionAutoConfiguration.class,
- Fabric8LeaderElectionCallbacksAutoConfiguration.class))
- .withPropertyValues("spring.cloud.kubernetes.leader.election.enabled=false")
+ new ApplicationContextRunner()
+ .withUserConfiguration(Fabric8LeaderConfiguration.class, Fabric8LeaderElectionAutoConfiguration.class,
+ Fabric8LeaderElectionCallbacksAutoConfiguration.class, TestConfig.class)
+ .withPropertyValues("spring.cloud.kubernetes.leader.election.enabled=false",
+ "spring.main.cloud-platform=KUBERNETES")
.run(context -> {
- Assertions.assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
+ Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionCallbacksAutoConfiguration.class);
});
}
@@ -82,21 +88,21 @@ void leaderElectionAnnotationPresentEqualToFalse() {
*
* As such:
*
- * - Fabric8LeaderAutoConfiguration must not be picked up
- * - Fabric8LeaderElectionAutoConfiguration must be picked up
+ * - Fabric8LeaderElectionAutoConfiguration is present
+ * - Fabric8LeaderElectionCallbacksAutoConfiguration is present
*
*/
@Test
void leaderElectionAnnotationPresentEqualToTrue() {
- new ApplicationContextRunner().withUserConfiguration(Fabric8LeaderApp.class)
- .withConfiguration(AutoConfigurations.of(Fabric8LeaderAutoConfiguration.class,
- Fabric8LeaderElectionAutoConfiguration.class,
- Fabric8LeaderElectionCallbacksAutoConfiguration.class))
+ new ApplicationContextRunner()
+ .withUserConfiguration(Fabric8LeaderConfiguration.class, Fabric8LeaderElectionAutoConfiguration.class,
+ Fabric8LeaderElectionCallbacksAutoConfiguration.class, TestConfig.class)
+ .withAllowBeanDefinitionOverriding(true)
.withPropertyValues("spring.cloud.kubernetes.leader.election.enabled=true",
- "spring.main.cloud-platform=kubernetes")
+ "spring.main.cloud-platform=KUBERNETES", "use.mock.config=true")
.run(context -> {
- Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
+ Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionCallbacksAutoConfiguration.class);
});
}
@@ -107,7 +113,6 @@ void leaderElectionAnnotationPresentEqualToTrue() {
*
* As such:
*
- * - Fabric8LeaderAutoConfiguration must not be picked up
* - Fabric8LeaderElectionAutoConfiguration must be picked up
* - Fabric8LeaderElectionInfoContributor must be picked up
*
@@ -115,13 +120,12 @@ void leaderElectionAnnotationPresentEqualToTrue() {
@Test
void leaderInfoContributorPresent() {
new ApplicationContextRunner().withUserConfiguration(Fabric8LeaderApp.class)
- .withConfiguration(AutoConfigurations.of(Fabric8LeaderAutoConfiguration.class,
- Fabric8LeaderElectionAutoConfiguration.class,
+ .withConfiguration(AutoConfigurations.of(Fabric8LeaderElectionAutoConfiguration.class,
Fabric8LeaderElectionCallbacksAutoConfiguration.class))
.withPropertyValues("spring.main.cloud-platform=kubernetes", "management.info.leader.election.enabled=true",
"spring.cloud.kubernetes.leader.election.enabled=true")
+ .withAllowBeanDefinitionOverriding(true)
.run(context -> {
- Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionInfoContributor.class);
});
@@ -142,17 +146,27 @@ void leaderInfoContributorPresent() {
@Test
void leaderInfoContributorMissing() {
new ApplicationContextRunner().withUserConfiguration(Fabric8LeaderApp.class)
- .withConfiguration(AutoConfigurations.of(Fabric8LeaderAutoConfiguration.class,
- Fabric8LeaderElectionAutoConfiguration.class,
+ .withConfiguration(AutoConfigurations.of(Fabric8LeaderElectionAutoConfiguration.class,
Fabric8LeaderElectionCallbacksAutoConfiguration.class))
.withPropertyValues("spring.main.cloud-platform=kubernetes",
"management.info.leader.election.enabled=false",
"spring.cloud.kubernetes.leader.election.enabled=true")
+ .withAllowBeanDefinitionOverriding(true)
.run(context -> {
- Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionInfoContributor.class);
});
}
+ @TestConfiguration
+ static class TestConfig {
+
+ @Bean
+ @Primary
+ BooleanSupplier podReadySupplier() {
+ return () -> false;
+ }
+
+ }
+
}
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInfoContributorIsLeaderTest.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInfoContributorIsLeaderTest.java
index 08b032162..9c206fb66 100644
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInfoContributorIsLeaderTest.java
+++ b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInfoContributorIsLeaderTest.java
@@ -41,7 +41,7 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.server.LocalManagementPort;
import org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderUtils;
+import org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInfoContributorIsNotLeaderTest.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInfoContributorIsNotLeaderTest.java
index 870fc2285..8bbfbe294 100644
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInfoContributorIsNotLeaderTest.java
+++ b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInfoContributorIsNotLeaderTest.java
@@ -41,7 +41,7 @@
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.server.LocalManagementPort;
import org.springframework.boot.webtestclient.autoconfigure.AutoConfigureWebTestClient;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderUtils;
+import org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
diff --git a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderOldAndNewImplementationTests.java b/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderOldAndNewImplementationTests.java
deleted file mode 100644
index 589ed75fe..000000000
--- a/spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderOldAndNewImplementationTests.java
+++ /dev/null
@@ -1,244 +0,0 @@
-/*
- * Copyright 2013-present 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
- *
- * https://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.fabric8.leader.election;
-
-import io.fabric8.kubernetes.api.model.APIGroupList;
-import io.fabric8.kubernetes.api.model.APIGroupListBuilder;
-import io.fabric8.kubernetes.api.model.APIResourceBuilder;
-import io.fabric8.kubernetes.api.model.APIResourceListBuilder;
-import io.fabric8.kubernetes.api.model.ConfigMap;
-import io.fabric8.kubernetes.api.model.GroupVersionForDiscoveryBuilder;
-import io.fabric8.kubernetes.client.KubernetesClient;
-import io.fabric8.kubernetes.client.dsl.MixedOperation;
-import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
-import io.fabric8.kubernetes.client.dsl.PodResource;
-import io.fabric8.kubernetes.client.dsl.Resource;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-import org.springframework.boot.autoconfigure.AutoConfigurations;
-import org.springframework.boot.test.context.TestConfiguration;
-import org.springframework.boot.test.context.runner.ApplicationContextRunner;
-import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration;
-import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration;
-import org.springframework.cloud.kubernetes.fabric8.leader.Fabric8LeaderAutoConfiguration;
-import org.springframework.context.annotation.Bean;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-/**
- * Tests that prove that previous and new leader implementation works based on the flags
- * we set.
- *
- * @author wind57
- */
-class Fabric8LeaderOldAndNewImplementationTests {
-
- private ApplicationContextRunner applicationContextRunner;
-
- /**
- *
- * - 'spring.cloud.kubernetes.leader.enabled' is not set
- * - 'spring.cloud.kubernetes.leader.election.enabled' is not set
- *
- * As such :
- *
- * - 'Fabric8LeaderAutoConfiguration' is active
- * - 'Fabric8LeaderElectionAutoConfiguration' is not active
- *
- */
- @Test
- void noFlagsSet() {
- setup("spring.main.cloud-platform=KUBERNETES");
- applicationContextRunner.run(context -> {
- assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
- assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
- });
- }
-
- /**
- *
- * - 'spring.cloud.kubernetes.leader.enabled' = true
- * - 'spring.cloud.kubernetes.leader.election.enabled' is not set
- *
- * As such :
- *
- * - 'Fabric8LeaderAutoConfiguration' is active
- * - 'Fabric8LeaderElectionAutoConfiguration' is not active
- *
- */
- @Test
- void oldImplementationEnabled() {
- setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.enabled=true");
- applicationContextRunner.run(context -> {
- assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
- assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
- });
- }
-
- /**
- *
- * - 'spring.cloud.kubernetes.leader.enabled' = false
- * - 'spring.cloud.kubernetes.leader.election.enabled' is not set
- *
- * As such :
- *
- * - 'Fabric8LeaderAutoConfiguration' is not active
- * - 'Fabric8LeaderElectionAutoConfiguration' is not active
- *
- */
- @Test
- void oldImplementationDisabled() {
- setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.enabled=false");
- applicationContextRunner.run(context -> {
- assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
- assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
- });
- }
-
- /**
- *
- * - 'spring.cloud.kubernetes.leader.enabled' is not set
- * - 'spring.cloud.kubernetes.leader.election.enabled' = false
- *
- * As such :
- *
- * - 'Fabric8LeaderAutoConfiguration' is active
- * - 'Fabric8LeaderElectionAutoConfiguration' is not active
- *
- */
- @Test
- void newImplementationDisabled() {
- setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.election.enabled=false");
- applicationContextRunner.run(context -> {
- assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
- assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
- });
- }
-
- /**
- *
- * - 'spring.cloud.kubernetes.leader.enabled' is not set
- * - 'spring.cloud.kubernetes.leader.election.enabled' = true
- *
- * As such :
- *
- * - 'Fabric8LeaderAutoConfiguration' is not active
- * - 'Fabric8LeaderElectionAutoConfiguration' is active
- *
- */
- @Test
- void newImplementationEnabled() {
- setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.election.enabled=true");
- applicationContextRunner.run(context -> {
- assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
- assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
- });
- }
-
- /**
- *
- * - 'spring.cloud.kubernetes.leader.enabled' = false
- * - 'spring.cloud.kubernetes.leader.election.enabled' = false
- *
- * As such :
- *
- * - 'Fabric8LeaderAutoConfiguration' is not active
- * - 'Fabric8LeaderElectionAutoConfiguration' is not active
- *
- */
- @Test
- void bothDisabled() {
- setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.enabled=false",
- "spring.cloud.kubernetes.leader.election.enabled=false");
- applicationContextRunner.run(context -> {
- assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
- assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
- });
- }
-
- /**
- *
- * - 'spring.cloud.kubernetes.leader.enabled' = true
- * - 'spring.cloud.kubernetes.leader.election.enabled' = true
- *
- * As such :
- *
- * - 'Fabric8LeaderAutoConfiguration' is not active
- * - 'Fabric8LeaderElectionAutoConfiguration' is active
- *
- * You can't enable both of them, only the new one will work.
- *
- */
- @Test
- void bothEnabled() {
- setup("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.enabled=true",
- "spring.cloud.kubernetes.leader.election.enabled=true");
- applicationContextRunner.run(context -> {
- assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
- assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
- });
- }
-
- private void setup(String... properties) {
- applicationContextRunner = new ApplicationContextRunner()
- .withConfiguration(AutoConfigurations.of(Fabric8LeaderElectionCallbacksAutoConfiguration.class,
- Fabric8AutoConfiguration.class, KubernetesCommonsAutoConfiguration.class,
- Fabric8LeaderElectionAutoConfiguration.class, Fabric8LeaderAutoConfiguration.class))
- .withUserConfiguration(Fabric8LeaderOldAndNewImplementationTests.Configuration.class)
- .withPropertyValues(properties);
- }
-
- @TestConfiguration
- static class Configuration {
-
- @Bean
- @SuppressWarnings({ "rawtypes", "unchecked" })
- KubernetesClient mockKubernetesClient() {
- KubernetesClient client = Mockito.mock(KubernetesClient.class);
-
- Mockito.when(client.getNamespace()).thenReturn("namespace");
-
- MixedOperation mixedOperation = Mockito.mock(MixedOperation.class);
- NonNamespaceOperation nonNamespaceOperation = Mockito.mock(NonNamespaceOperation.class);
- Mockito.when(client.configMaps()).thenReturn(mixedOperation);
-
- Mockito.when(mixedOperation.inNamespace(Mockito.anyString())).thenReturn(nonNamespaceOperation);
- Resource configMapResource = Mockito.mock(Resource.class);
- Mockito.when(nonNamespaceOperation.withName(Mockito.anyString())).thenReturn(configMapResource);
-
- Mockito.when(client.pods()).thenReturn(mixedOperation);
- PodResource podResource = Mockito.mock(PodResource.class);
- Mockito.when(mixedOperation.withName(Mockito.anyString())).thenReturn(podResource);
-
- Mockito.when(client.getApiResources("coordination.k8s.io/v1"))
- .thenReturn(
- new APIResourceListBuilder().withResources(new APIResourceBuilder().withKind("Lease").build())
- .build());
-
- APIGroupList apiGroupList = new APIGroupListBuilder().addNewGroup()
- .withVersions(new GroupVersionForDiscoveryBuilder().withGroupVersion("coordination.k8s.io/v1").build())
- .endGroup()
- .build();
-
- Mockito.when(client.getApiGroups()).thenReturn(apiGroupList);
- return client;
- }
-
- }
-
-}
diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/AbstractLeaderElection.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/AbstractLeaderElection.java
index 7d0657cb3..c5f494f7f 100644
--- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/AbstractLeaderElection.java
+++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/AbstractLeaderElection.java
@@ -35,7 +35,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.system.OutputCaptureExtension;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderUtils;
+import org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils;
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
diff --git a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-leader-election/src/test/java/org/springframework/cloud/kubernetes/client/leader/election/AbstractLeaderElection.java b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-leader-election/src/test/java/org/springframework/cloud/kubernetes/client/leader/election/AbstractLeaderElection.java
index d23e23a05..fafdf4898 100644
--- a/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-leader-election/src/test/java/org/springframework/cloud/kubernetes/client/leader/election/AbstractLeaderElection.java
+++ b/spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-k8s-client-leader-election/src/test/java/org/springframework/cloud/kubernetes/client/leader/election/AbstractLeaderElection.java
@@ -37,7 +37,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.system.OutputCaptureExtension;
-import org.springframework.cloud.kubernetes.commons.leader.LeaderUtils;
+import org.springframework.cloud.kubernetes.commons.leader.election.LeaderUtils;
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;