Initialize discovery client properly Fixes #517

This commit is contained in:
Ryan Baxter
2021-03-26 15:59:44 -04:00
parent 3f0a211839
commit de8f938586
6 changed files with 94 additions and 0 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.discovery;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.kubernetes.PodUtils;
import org.springframework.context.ApplicationEventPublisher;
/**
* @author Ryan Baxter
*/
public class KubernetesDiscoveryClientHealthIndicatorInitializer
implements InitializingBean {
private PodUtils podUtils;
private ApplicationEventPublisher applicationEventPublisher;
public KubernetesDiscoveryClientHealthIndicatorInitializer(PodUtils podUtils,
ApplicationEventPublisher applicationEventPublisher) {
this.podUtils = podUtils;
this.applicationEventPublisher = applicationEventPublisher;
}
public void initialize() {
this.applicationEventPublisher
.publishEvent(new InstanceRegisteredEvent<>(podUtils.currentPod(), null));
}
@Override
public void afterPropertiesSet() {
this.initialize();
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.kubernetes.discovery;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.After;
import org.junit.Test;
@@ -23,12 +24,14 @@ import org.junit.Test;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.kubernetes.PodUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Oleg Vyukov
@@ -86,6 +89,13 @@ public class KubernetesCatalogServicesWatchConfigurationTest {
return mock(KubernetesClient.class);
}
@Bean
PodUtils podUtils() {
PodUtils podPodUtils = mock(PodUtils.class);
when(podPodUtils.currentPod()).thenReturn(() -> mock(Pod.class));
return podPodUtils;
}
}
}

View File

@@ -16,18 +16,21 @@
package org.springframework.cloud.kubernetes.discovery;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.kubernetes.PodUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Ryan Dawson
@@ -90,6 +93,13 @@ public class KubernetesDiscoveryClientAutoConfigurationPropertiesTests {
return mock(KubernetesClient.class);
}
@Bean
PodUtils podUtils() {
PodUtils podPodUtils = mock(PodUtils.class);
when(podPodUtils.currentPod()).thenReturn(() -> mock(Pod.class));
return podPodUtils;
}
}
}

View File

@@ -0,0 +1,8 @@
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: "*"

View File

@@ -25,6 +25,7 @@ import org.junit.runner.RunWith;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
@RequiresKubernetes
@RunWith(Arquillian.class)
@@ -57,4 +58,11 @@ public class ServicesIT {
.body("serviceId", hasItems("discovery-service-a"));
}
@Test
public void testHealthEndpoint() {
given().baseUri(String.format("%s://%s:%d", PROTOCOL, HOST, PORT))
.contentType("application/json").get("actuator/health").then()
.statusCode(200).body("components.discoveryComposite.status", is("UP"));
}
}

View File

@@ -0,0 +1,8 @@
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: "*"