Skip to content

Commit ef3e9c7

Browse files
authored
Merge pull request #2108 from wind57/fix-incorrect-conditional-on-fabric8-leader-indicator
fix incorrect annotation for fabric8 leader election info contributor
2 parents 4f591a3 + 8908e16 commit ef3e9c7

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.fabric8.kubernetes.client.extended.leaderelection.resourcelock.Lock;
3131
import io.fabric8.kubernetes.client.readiness.Readiness;
3232

33+
import org.springframework.boot.actuate.autoconfigure.info.ConditionalOnEnabledInfoContributor;
3334
import org.springframework.boot.actuate.info.InfoContributor;
3435
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
3536
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -38,7 +39,6 @@
3839
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3940
import org.springframework.boot.cloud.CloudPlatform;
4041
import org.springframework.boot.context.properties.EnableConfigurationProperties;
41-
import org.springframework.boot.health.autoconfigure.contributor.ConditionalOnEnabledHealthIndicator;
4242
import org.springframework.cloud.kubernetes.commons.leader.election.ConditionalOnLeaderElectionEnabled;
4343
import org.springframework.cloud.kubernetes.commons.leader.election.LeaderElectionProperties;
4444
import org.springframework.context.annotation.Bean;
@@ -66,7 +66,7 @@ class Fabric8LeaderElectionAutoConfiguration {
6666

6767
@Bean
6868
@ConditionalOnClass(InfoContributor.class)
69-
@ConditionalOnEnabledHealthIndicator("leader.election")
69+
@ConditionalOnEnabledInfoContributor("leader.election")
7070
Fabric8LeaderElectionInfoContributor leaderElectionInfoContributor(String candidateIdentity,
7171
LeaderElectionConfig leaderElectionConfig, KubernetesClient fabric8KubernetesClient) {
7272
return new Fabric8LeaderElectionInfoContributor(candidateIdentity, leaderElectionConfig,

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

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.boot.autoconfigure.AutoConfigurations;
2323
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2424
import org.springframework.cloud.kubernetes.fabric8.leader.Fabric8LeaderAutoConfiguration;
25-
import org.springframework.cloud.kubernetes.fabric8.leader.Fabric8PodReadinessWatcher;
2625

2726
/**
2827
* tests that ensure 'spring.cloud.kubernetes.leader.election' enabled correct
@@ -38,7 +37,7 @@ class Fabric8LeaderAutoConfigurationTests {
3837
*
3938
* As such:
4039
*
41-
* - Fabric8LeaderAutoConfiguration must be picked up
40+
* - Fabric8LeaderAutoConfiguration must be picked up
4241
* - Fabric8LeaderElectionAutoConfiguration must not be picked up
4342
* </pre>
4443
*/
@@ -49,12 +48,8 @@ void leaderElectionAnnotationMissing() {
4948
Fabric8LeaderElectionAutoConfiguration.class,
5049
Fabric8LeaderElectionCallbacksAutoConfiguration.class))
5150
.run(context -> {
52-
53-
// this one comes from Fabric8LeaderElectionAutoConfiguration
54-
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionInitiator.class);
55-
56-
// this one comes from Fabric8LeaderAutoConfiguration
57-
Assertions.assertThat(context).hasSingleBean(Fabric8PodReadinessWatcher.class);
51+
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
52+
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
5853
});
5954
}
6055

@@ -64,7 +59,7 @@ void leaderElectionAnnotationMissing() {
6459
*
6560
* As such:
6661
*
67-
* - Fabric8LeaderAutoConfiguration must be picked up
62+
* - Fabric8LeaderAutoConfiguration must be picked up
6863
* - Fabric8LeaderElectionAutoConfiguration must not be picked up
6964
* </pre>
7065
*/
@@ -76,22 +71,18 @@ void leaderElectionAnnotationPresentEqualToFalse() {
7671
Fabric8LeaderElectionCallbacksAutoConfiguration.class))
7772
.withPropertyValues("spring.cloud.kubernetes.leader.election.enabled=false")
7873
.run(context -> {
79-
80-
// this one comes from Fabric8LeaderElectionAutoConfiguration
81-
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionInitiator.class);
82-
83-
// this one comes from Fabric8LeaderAutoConfiguration
84-
Assertions.assertThat(context).hasSingleBean(Fabric8PodReadinessWatcher.class);
74+
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderAutoConfiguration.class);
75+
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionAutoConfiguration.class);
8576
});
8677
}
8778

8879
/**
8980
* <pre>
90-
* - spring.cloud.kubernetes.leader.election = false
81+
* - spring.cloud.kubernetes.leader.election = true
9182
*
9283
* As such:
9384
*
94-
* - Fabric8LeaderAutoConfiguration must not be picked up
85+
* - Fabric8LeaderAutoConfiguration must not be picked up
9586
* - Fabric8LeaderElectionAutoConfiguration must be picked up
9687
* </pre>
9788
*/
@@ -104,12 +95,63 @@ void leaderElectionAnnotationPresentEqualToTrue() {
10495
.withPropertyValues("spring.cloud.kubernetes.leader.election.enabled=true",
10596
"spring.main.cloud-platform=kubernetes")
10697
.run(context -> {
98+
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
99+
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
100+
});
101+
}
107102

108-
// this one comes from Fabric8LeaderElectionAutoConfiguration
109-
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionInitiator.class);
103+
/**
104+
* <pre>
105+
* - spring.cloud.kubernetes.leader.election = true
106+
* - management.info.leader.election.enabled = true
107+
*
108+
* As such:
109+
*
110+
* - Fabric8LeaderAutoConfiguration must not be picked up
111+
* - Fabric8LeaderElectionAutoConfiguration must be picked up
112+
* - Fabric8LeaderElectionInfoContributor must be picked up
113+
* </pre>
114+
*/
115+
@Test
116+
void leaderInfoContributorPresent() {
117+
new ApplicationContextRunner().withUserConfiguration(Fabric8LeaderApp.class)
118+
.withConfiguration(AutoConfigurations.of(Fabric8LeaderAutoConfiguration.class,
119+
Fabric8LeaderElectionAutoConfiguration.class,
120+
Fabric8LeaderElectionCallbacksAutoConfiguration.class))
121+
.withPropertyValues("spring.main.cloud-platform=kubernetes", "management.info.leader.election.enabled=true",
122+
"spring.cloud.kubernetes.leader.election.enabled=true")
123+
.run(context -> {
124+
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
125+
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
126+
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionInfoContributor.class);
127+
});
128+
}
110129

111-
// this one comes from Fabric8LeaderAutoConfiguration
112-
Assertions.assertThat(context).doesNotHaveBean(Fabric8PodReadinessWatcher.class);
130+
/**
131+
* <pre>
132+
* - spring.cloud.kubernetes.leader.election = true
133+
* - management.info.leader.election.enabled = false
134+
*
135+
* As such:
136+
*
137+
* - Fabric8LeaderAutoConfiguration must not be picked up
138+
* - Fabric8LeaderElectionAutoConfiguration must be picked up
139+
* - Fabric8LeaderElectionInfoContributor must not be picked up
140+
* </pre>
141+
*/
142+
@Test
143+
void leaderInfoContributorMissing() {
144+
new ApplicationContextRunner().withUserConfiguration(Fabric8LeaderApp.class)
145+
.withConfiguration(AutoConfigurations.of(Fabric8LeaderAutoConfiguration.class,
146+
Fabric8LeaderElectionAutoConfiguration.class,
147+
Fabric8LeaderElectionCallbacksAutoConfiguration.class))
148+
.withPropertyValues("spring.main.cloud-platform=kubernetes",
149+
"management.info.leader.election.enabled=false",
150+
"spring.cloud.kubernetes.leader.election.enabled=true")
151+
.run(context -> {
152+
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderAutoConfiguration.class);
153+
Assertions.assertThat(context).hasSingleBean(Fabric8LeaderElectionAutoConfiguration.class);
154+
Assertions.assertThat(context).doesNotHaveBean(Fabric8LeaderElectionInfoContributor.class);
113155
});
114156
}
115157

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*/
5353
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
5454
properties = { "spring.main.cloud-platform=KUBERNETES", "management.endpoints.web.exposure.include=info",
55-
"management.endpoint.info.show-details=always", "management.info.kubernetes.enabled=true",
55+
"management.endpoint.info.show-details=always",
5656
"spring.cloud.kubernetes.leader.election.enabled=true" })
5757
@AutoConfigureWebTestClient
5858
class Fabric8LeaderElectionInfoContributorIsLeaderTest {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*/
5353
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
5454
properties = { "spring.main.cloud-platform=KUBERNETES", "management.endpoints.web.exposure.include=info",
55-
"management.endpoint.info.show-details=always", "management.info.kubernetes.enabled=true",
55+
"management.endpoint.info.show-details=always",
5656
"spring.cloud.kubernetes.leader.election.enabled=true" })
5757
@AutoConfigureWebTestClient
5858
class Fabric8LeaderElectionInfoContributorIsNotLeaderTest {

0 commit comments

Comments
 (0)