This commit is contained in:
erabii
2021-07-13 13:56:02 -04:00
committed by GitHub
parent 0637e96529
commit b380f463a5
4 changed files with 145 additions and 26 deletions

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2013-2021 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;
import io.fabric8.kubernetes.api.model.Pod;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.info.ConditionalOnEnabledInfoContributor;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesEnabled;
import org.springframework.cloud.kubernetes.commons.PodUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author wind57
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(HealthIndicator.class)
@ConditionalOnKubernetesEnabled
public class Fabric8ActuatorConfiguration {
@Bean
@ConditionalOnEnabledHealthIndicator("kubernetes")
public Fabric8HealthIndicator kubernetesHealthIndicator(PodUtils<Pod> podUtils) {
return new Fabric8HealthIndicator(podUtils);
}
@Bean
@ConditionalOnEnabledInfoContributor("kubernetes")
public Fabric8InfoContributor kubernetesInfoContributor(PodUtils<Pod> podUtils) {
return new Fabric8InfoContributor(podUtils);
}
}

View File

@@ -18,7 +18,6 @@ package org.springframework.cloud.kubernetes.fabric8;
import java.time.Duration;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
@@ -26,16 +25,11 @@ import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.info.ConditionalOnEnabledInfoContributor;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.kubernetes.commons.ConditionalOnKubernetesEnabled;
import org.springframework.cloud.kubernetes.commons.KubernetesClientProperties;
import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration;
import org.springframework.cloud.kubernetes.commons.PodUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -123,22 +117,4 @@ public class Fabric8AutoConfiguration {
return new Fabric8PodUtils(client);
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(HealthIndicator.class)
protected static class KubernetesActuatorConfiguration {
@Bean
@ConditionalOnEnabledHealthIndicator("kubernetes")
public Fabric8HealthIndicator kubernetesHealthIndicator(PodUtils<Pod> podUtils) {
return new Fabric8HealthIndicator(podUtils);
}
@Bean
@ConditionalOnEnabledInfoContributor("kubernetes")
public Fabric8InfoContributor kubernetesInfoContributor(PodUtils<Pod> podUtils) {
return new Fabric8InfoContributor(podUtils);
}
}
}

View File

@@ -1,6 +1,6 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration\
org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration,\
org.springframework.cloud.kubernetes.fabric8.Fabric8ActuatorConfiguration
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.cloud.kubernetes.fabric8.profile.Fabric8ProfileEnvironmentPostProcessor

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2013-2021 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.config;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.ReactiveHealthContributorRegistry;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.kubernetes.fabric8.config.example.App;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
/**
* @author wind57
*/
public class Fabric8ActuatorTests {
@Nested
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = {
"management.health.kubernetes.enabled=false", "management.endpoint.health.show-details=always",
"management.endpoint.health.show-components=always", "management.endpoints.web.exposure.include=health" })
public class DisabledHealthTest {
@Autowired
private ReactiveHealthContributorRegistry registry;
@Autowired
private WebTestClient webClient;
@Value("${local.server.port}")
private int port;
@Test
public void healthEndpointShouldContainKubernetes() {
this.webClient.get().uri("http://localhost:{port}/actuator/health", this.port)
.accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk().expectBody(String.class)
.value(not(containsString("kubernetes")));
Assertions.assertNull(registry.getContributor("kubernetes"),
"reactive kubernetes contributor must NOT be present when 'management.health.kubernetes.enabled=false'");
}
}
@Nested
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = App.class, properties = {
"management.health.kubernetes.enabled=true", "management.endpoint.health.show-details=always",
"management.endpoint.health.show-components=always", "management.endpoints.web.exposure.include=health" })
public class EnabledHealthTest {
@Autowired
private WebTestClient webClient;
@Autowired
private ReactiveHealthContributorRegistry registry;
@Value("${local.server.port}")
private int port;
@Test
public void healthEndpointShouldContainKubernetes() {
this.webClient.get().uri("http://localhost:{port}/actuator/health", this.port)
.accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk().expectBody(String.class)
.value(containsString("kubernetes"));
Assertions.assertNotNull(registry.getContributor("kubernetes"),
"reactive kubernetes contributor must be present when 'management.health.kubernetes.enabled=true'");
}
}
}