Skip to content

Commit

Permalink
align InstanceRegisteredEvent for all 3 blocking discovery clients (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Nov 29, 2023
1 parent ca89d28 commit 11430e0
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package org.springframework.cloud.kubernetes.discovery;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.health.DiscoveryClientHealthIndicatorProperties;
import org.springframework.cloud.kubernetes.commons.PodUtils;
import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnSpringCloudKubernetesBlockingDiscovery;
import org.springframework.cloud.kubernetes.commons.discovery.ConditionalOnSpringCloudKubernetesBlockingDiscoveryHealthInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthIndicatorInitializer;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -54,13 +53,16 @@ KubernetesDiscoveryClient kubernetesDiscoveryClient(RestTemplate restTemplate,
}

@Bean
@ConditionalOnSpringCloudKubernetesBlockingDiscoveryHealthInitializer
InitializingBean indicatorInitializer(ApplicationEventPublisher applicationEventPublisher,
ApplicationContext applicationContext) {
InitializingBean bean = () -> applicationEventPublisher
.publishEvent(new InstanceRegisteredEvent<>(applicationContext.getId(), null));
return bean;
@ConditionalOnMissingBean
PodUtils<?> kubernetesDiscoveryPodUtils() {
return new KubernetesDiscoveryPodUtils();
}

@Bean
@ConditionalOnSpringCloudKubernetesBlockingDiscoveryHealthInitializer
KubernetesDiscoveryClientHealthIndicatorInitializer indicatorInitializer(PodUtils<?> podUtils,
ApplicationEventPublisher applicationEventPublisher) {
return new KubernetesDiscoveryClientHealthIndicatorInitializer(podUtils, applicationEventPublisher);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2013-2023 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 java.util.function.Supplier;

import org.springframework.cloud.kubernetes.commons.PodUtils;

/**
* @author wind57
*/
final class KubernetesDiscoveryPodUtils implements PodUtils<Object> {

@Override
public Supplier<Object> currentPod() {
// we don't really have a way to get the pod here
return () -> null;
}

@Override
public boolean isInsideKubernetes() {
// this bean is used in a config that is annotated
// with @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES),
// so safe to return true here.
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2013-2023 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.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;

import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthIndicatorInitializer.RegisteredEventSource;

/**
* test that asserts the type of published event for blocking discovery.
*
* @author wind57
*/
@SpringBootTest(
properties = { "spring.main.cloud-platform=kubernetes", "spring.cloud.config.enabled=false",
"spring.cloud.kubernetes.discovery.discovery-server-url=http://example" },
classes = BlockingDiscoveryHealthPublishedEventTest.HealthEventListenerConfiguration.class)
class BlockingDiscoveryHealthPublishedEventTest {

private static boolean caught;

@Test
void test() {
Assertions.assertTrue(caught);
}

@TestConfiguration
static class HealthEventListenerConfiguration {

@Bean
HealthEventListener healthEventListener() {
return new HealthEventListener();
}

}

private static class HealthEventListener implements ApplicationListener<InstanceRegisteredEvent<?>> {

@Override
public void onApplicationEvent(InstanceRegisteredEvent<?> event) {
caught = true;
Assertions.assertTrue(event.getSource() instanceof RegisteredEventSource);
RegisteredEventSource registeredEventSource = (RegisteredEventSource) event.getSource();
Assertions.assertTrue(registeredEventSource.inside());
Assertions.assertNull(registeredEventSource.pod());
Assertions.assertEquals(registeredEventSource.cloudPlatform(), "kubernetes");
}

}

}

0 comments on commit 11430e0

Please sign in to comment.