Skip to content

Commit 4ec9c18

Browse files
committed
Add the ability to disable leader info contributor
Fixes #2085
1 parent 720487d commit 4ec9c18

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

docs/modules/ROOT/pages/leader-election.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,29 @@ To specify the name of the configmap used for leader election use the following
2525
----
2626
spring.cloud.kubernetes.leader.config-map-name=leader
2727
----
28+
29+
== Leader Election Info Contributor
30+
31+
Spring Cloud Kubernetes Leader includes an `InfoContributor` which adds leader election information to
32+
Spring Boot's `/actuator/info` endpoint. This contributor provides information about the current leader,
33+
including the leader ID, role, and whether the current application instance is the leader.
34+
35+
Example output:
36+
[source,json]
37+
----
38+
{
39+
"leaderElection": {
40+
"leaderId": "my-app-pod-1",
41+
"role": "my-role",
42+
"isLeader": true
43+
}
44+
}
45+
----
46+
47+
You can disable this `InfoContributor` by setting `management.info.leade.enabled`
48+
to `false` in `application.[properties | yaml]`:
49+
50+
[source,properties]
51+
----
52+
management.info.leader.enabled=false
53+
----

spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import io.fabric8.kubernetes.client.KubernetesClient;
2222

23+
import org.springframework.boot.actuate.autoconfigure.info.ConditionalOnEnabledInfoContributor;
2324
import org.springframework.boot.actuate.info.InfoContributor;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -74,6 +75,7 @@ public Candidate candidate(LeaderProperties leaderProperties) throws UnknownHost
7475
*/
7576
@Bean
7677
@ConditionalOnClass(InfoContributor.class)
78+
@ConditionalOnEnabledInfoContributor("leader")
7779
public LeaderInfoContributor leaderInfoContributor(Fabric8LeadershipController fabric8LeadershipController,
7880
Candidate candidate) {
7981
return new LeaderInfoContributor(fabric8LeadershipController, candidate);

spring-cloud-kubernetes-fabric8-leader/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderAutoConfigurationTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.springframework.http.MediaType;
2727
import org.springframework.test.web.reactive.server.WebTestClient;
2828

29-
import static org.hamcrest.Matchers.containsString;
30-
3129
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
3230
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.autoStartup=false",
3331
"management.endpoints.web.exposure.include=info", "management.endpoint.info.show-details=always",
@@ -52,8 +50,13 @@ void infoEndpointShouldContainLeaderElection() {
5250
.exchange()
5351
.expectStatus()
5452
.isOk()
55-
.expectBody(String.class)
56-
.value(containsString("kubernetes"));
53+
.expectBody()
54+
.jsonPath("kubernetes")
55+
.exists()
56+
.jsonPath("leaderElection")
57+
.exists()
58+
.jsonPath("leaderElection.leaderId")
59+
.exists();
5760
}
5861

5962
@SpringBootConfiguration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.leader;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.SpringBootConfiguration;
24+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
25+
import org.springframework.boot.test.context.SpringBootTest;
26+
import org.springframework.cloud.kubernetes.commons.leader.LeaderInfoContributor;
27+
import org.springframework.context.ApplicationContext;
28+
29+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
30+
31+
/**
32+
* Tests that verify LeaderInfoContributor can be disabled via configuration property.
33+
*/
34+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
35+
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.leader.autoStartup=false",
36+
"management.info.leader.enabled=false" })
37+
class Fabric8LeaderInfoContributorDisabledTests {
38+
39+
@Autowired
40+
private ApplicationContext context;
41+
42+
/**
43+
* Test that the LeaderInfoContributor bean is NOT present when
44+
* management.info.leader.enabled=false
45+
*/
46+
@Test
47+
void leaderInfoContributorShouldNotBePresent() {
48+
assertThatThrownBy(() -> context.getBean(LeaderInfoContributor.class))
49+
.isInstanceOf(NoSuchBeanDefinitionException.class);
50+
}
51+
52+
@SpringBootConfiguration
53+
@EnableAutoConfiguration
54+
protected static class TestConfig {
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)