Add info contributor for leader election. (#334)

Fixes gh-293
This commit is contained in:
Mark Anderson
2019-02-22 17:18:35 +00:00
committed by Spencer Gibb
parent 2212b45aaf
commit 70ecb5529b
8 changed files with 221 additions and 19 deletions

View File

@@ -82,6 +82,11 @@
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
@@ -108,11 +113,6 @@
<artifactId>spock-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>

View File

@@ -19,19 +19,20 @@ package org.springframework.cloud.kubernetes;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.example.App;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.Matchers.containsString;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = {
@@ -43,6 +44,9 @@ public class HealthIndicatorTest {
private static KubernetesClient mockClient;
@Autowired
private WebTestClient webClient;
@Value("${local.server.port}")
private int port;
@@ -62,10 +66,9 @@ public class HealthIndicatorTest {
@Test
public void healthEndpointShouldContainKubernetes() {
RestAssured.baseURI = String.format("http://localhost:%d/actuator/health",
this.port);
given().contentType("application/json").get().then().statusCode(200)
.body(containsString("kubernetes"));
this.webClient.get().uri("http://localhost:{port}/actuator/health", this.port)
.accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
.expectBody(String.class).value(containsString("kubernetes"));
}
}

View File

@@ -19,19 +19,20 @@ package org.springframework.cloud.kubernetes;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.server.mock.KubernetesServer;
import io.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.example.App;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.Matchers.containsString;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class)
@@ -42,6 +43,9 @@ public class InfoContributorTest {
private static KubernetesClient mockClient;
@Autowired
private WebTestClient webClient;
@Value("${local.server.port}")
private int port;
@@ -61,10 +65,9 @@ public class InfoContributorTest {
@Test
public void infoEndpointShouldContainKubernetes() {
RestAssured.baseURI = String.format("http://localhost:%d/actuator/info",
this.port);
given().contentType("application/json").get().then().statusCode(200)
.body(containsString("kubernetes"));
this.webClient.get().uri("http://localhost:{port}/actuator/info", this.port)
.accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
.expectBody(String.class).value(containsString("kubernetes"));
}
}

View File

@@ -37,11 +37,21 @@
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -21,7 +21,9 @@ import java.net.UnknownHostException;
import io.fabric8.kubernetes.client.KubernetesClient;
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;
@@ -90,4 +92,11 @@ public class LeaderAutoConfiguration {
leaderRecordWatcher, hostPodWatcher);
}
@Bean
@ConditionalOnClass(InfoContributor.class)
public LeaderInfoContributor leaderInfoContributor(
LeadershipController leadershipController, Candidate candidate) {
return new LeaderInfoContributor(leadershipController, candidate);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2019-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.leader;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.springframework.boot.actuate.info.Info.Builder;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.integration.leader.Candidate;
public class LeaderInfoContributor implements InfoContributor {
private final LeadershipController leadershipController;
private final Candidate candidate;
public LeaderInfoContributor(LeadershipController leadershipController,
Candidate candidate) {
this.leadershipController = leadershipController;
this.candidate = candidate;
}
@Override
public void contribute(Builder builder) {
Map<String, Object> details = new HashMap<>();
Optional<Leader> leader = leadershipController.getLocalLeader();
if (leader.isPresent()) {
Leader l = leader.get();
details.put("leaderId", l.getId());
details.put("role", l.getRole());
details.put("isLeader", l.isCandidate(candidate));
}
else {
details.put("leaderId", "Unknown");
}
builder.withDetail("leaderElection", details);
}
}

View File

@@ -19,10 +19,16 @@ package org.springframework.cloud.kubernetes.leader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.hamcrest.Matchers.containsString;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
@@ -31,10 +37,23 @@ import org.springframework.test.context.junit4.SpringRunner;
})
public class LeaderAutoConfigurationTests {
@Value("${local.server.port}")
private int port;
@Autowired
private WebTestClient webClient;
@Test
public void contextLoads() {
}
@Test
public void infoEndpointShouldContainLeaderElection() {
this.webClient.get().uri("http://localhost:{port}/actuator/info", this.port)
.accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
.expectBody(String.class).value(containsString("kubernetes"));
}
@SpringBootConfiguration
@EnableAutoConfiguration
protected static class TestConfig {

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2019-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.kubernetes.leader;
import java.util.Map;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.actuate.info.Info;
import org.springframework.integration.leader.Candidate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@RunWith(MockitoJUnitRunner.class)
public class LeaderInfoContributorTest {
@Mock
private Candidate mockCandidate;
@Mock
private LeadershipController mockLeadershipController;
@Mock
private Leader mockLeader;
private LeaderInfoContributor leaderInfoContributor;
@Before
public void before() {
this.leaderInfoContributor = new LeaderInfoContributor(
this.mockLeadershipController, this.mockCandidate);
}
@SuppressWarnings("unchecked")
@Test
public void infoWithoutLeader() {
Info.Builder builder = new Info.Builder();
leaderInfoContributor.contribute(builder);
Map<String, Object> details = (Map<String, Object>) builder.build()
.get("leaderElection");
assertThat(details).containsEntry("leaderId", "Unknown");
}
@SuppressWarnings("unchecked")
@Test
public void infoWhenLeader() {
given(this.mockLeadershipController.getLocalLeader())
.willReturn(Optional.of(this.mockLeader));
given(this.mockLeader.isCandidate(this.mockCandidate)).willReturn(true);
given(this.mockLeader.getRole()).willReturn("testRole");
given(this.mockLeader.getId()).willReturn("id");
Info.Builder builder = new Info.Builder();
leaderInfoContributor.contribute(builder);
Map<String, Object> details = (Map<String, Object>) builder.build()
.get("leaderElection");
assertThat(details).containsEntry("isLeader", true);
assertThat(details).containsEntry("leaderId", "id");
assertThat(details).containsEntry("role", "testRole");
}
@SuppressWarnings("unchecked")
@Test
public void infoWhenAnotherIsLeader() {
given(this.mockLeadershipController.getLocalLeader())
.willReturn(Optional.of(this.mockLeader));
given(this.mockLeader.getRole()).willReturn("testRole");
given(this.mockLeader.getId()).willReturn("id");
Info.Builder builder = new Info.Builder();
leaderInfoContributor.contribute(builder);
Map<String, Object> details = (Map<String, Object>) builder.build()
.get("leaderElection");
assertThat(details).containsEntry("isLeader", false);
assertThat(details).containsEntry("leaderId", "id");
assertThat(details).containsEntry("role", "testRole");
}
}