Skip to content

Commit

Permalink
Close Fabric8 Client When Context Is Stopped (#1780)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Baxter authored and ryanjbaxter committed Oct 23, 2024
1 parent 36c190e commit 4d55f69
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import org.springframework.boot.BootstrapRegistry;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.CollectionUtils;
Expand All @@ -60,6 +61,8 @@ public final class ConfigUtils {
|| sourceName.endsWith("-" + activeProfile + ".yaml")
|| sourceName.endsWith("-" + activeProfile + ".properties");

private static final ApplicationListener<?> NO_OP = (e) -> { };

private ConfigUtils() {
}

Expand Down Expand Up @@ -329,17 +332,23 @@ private static Map<String, String> decodeData(Map<String, String> data) {
}

public static <T> void registerSingle(ConfigurableBootstrapContext bootstrapContext, Class<T> cls, T instance,
String name) {
String name, ApplicationListener<?> listener) {
bootstrapContext.registerIfAbsent(cls, BootstrapRegistry.InstanceSupplier.of(instance));
bootstrapContext.addCloseListener(event -> {
if (event.getApplicationContext().getBeanFactory().getSingleton(name) == null) {
event.getApplicationContext()
.getBeanFactory()
.registerSingleton(name, event.getBootstrapContext().get(cls));
event.getApplicationContext().addApplicationListener(listener);
}
});
}

public static <T> void registerSingle(ConfigurableBootstrapContext bootstrapContext, Class<T> cls, T instance,
String name) {
registerSingle(bootstrapContext, cls, instance, name, NO_OP);
}

/**
* append prefix to the keys and return a new Map with the new values.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import java.time.Duration;

import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -31,6 +33,8 @@
import org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;

/**
* Auto configuration for Kubernetes.
Expand Down Expand Up @@ -117,4 +121,11 @@ public Fabric8PodUtils kubernetesPodUtils(KubernetesClient client) {
return new Fabric8PodUtils(client);
}

@EventListener
void onContextClosed(ContextClosedEvent event) {
// Clean up any open connections from the KubernetesClient when the context is closed
BeanFactoryUtils.beansOfTypeIncludingAncestors(event.getApplicationContext(), KubernetesClient.class).values()
.forEach(Client::close);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.springframework.cloud.kubernetes.commons.config.SecretsConfigProperties;
import org.springframework.cloud.kubernetes.commons.config.SecretsPropertySourceLocator;
import org.springframework.cloud.kubernetes.fabric8.Fabric8AutoConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.env.Environment;

import static org.springframework.cloud.kubernetes.commons.config.ConfigUtils.registerSingle;
Expand Down Expand Up @@ -90,7 +92,8 @@ private KubernetesClient registerConfigAndClient(ConfigurableBootstrapContext bo
registerSingle(bootstrapContext, Config.class, config, "fabric8Config");

KubernetesClient kubernetesClient = new Fabric8AutoConfiguration().kubernetesClient(config);
registerSingle(bootstrapContext, KubernetesClient.class, kubernetesClient, "configKubernetesClient");
registerSingle(bootstrapContext, KubernetesClient.class, kubernetesClient, "configKubernetesClient",
(ApplicationListener<ContextClosedEvent>) event -> kubernetesClient.close());
return kubernetesClient;
}

Expand Down

0 comments on commit 4d55f69

Please sign in to comment.