Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 committed Oct 24, 2024
2 parents 7c5a312 + abcccc8 commit ed2080b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public static void assertReloadLogStatements(String left, String right, String a
* create a tar, copy it in the running k3s and load this tar as an image.
*/
public static void loadImage(String image, String tag, String tarName, K3sContainer container) throws Exception {

if (imageAlreadyInK3s(container, tarName)) {
return;
}

// save image
try (SaveImageCmd saveImageCmd = container.getDockerClient().saveImageCmd(image)) {
InputStream imageStream = saveImageCmd.withTag(tag).exec();
Expand Down Expand Up @@ -182,7 +187,7 @@ public static void load(K3sContainer container, String tarName, String imageName
try {
LOG.info("no tars found, will resort to pulling the image");
LOG.info("using : " + imageVersion + " for : " + imageNameForDownload);
pullImage(imageNameForDownload, imageVersion, container);
pullImage(imageNameForDownload, imageVersion, tarName, container);
loadImage(imageNameForDownload, imageVersion, tarName, container);
}
catch (Exception e) {
Expand All @@ -205,7 +210,13 @@ public static void validateImage(String image, K3sContainer container) {
}
}

public static void pullImage(String image, String tag, K3sContainer container) throws InterruptedException {
public static void pullImage(String image, String tag, String tarName, K3sContainer container)
throws InterruptedException {

if (imageAlreadyInK3s(container, tarName)) {
return;
}

try (PullImageCmd pullImageCmd = container.getDockerClient().pullImageCmd(image)) {
pullImageCmd.withTag(tag).start().awaitCompletion();
}
Expand Down Expand Up @@ -265,6 +276,11 @@ private static void loadImageFromPath(String tarName, K3sContainer container) {
}

private static boolean imageAlreadyInK3s(K3sContainer container, String tarName) {

if (tarName == null) {
return false;
}

try {
boolean present = container.execInContainer("sh", "-c", "ctr images list | grep " + tarName)
.getStdout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void createAndWait(String namespace, String name, @Nullable Deployment de
}
else {
String[] image = imageFromDeployment.split(":", 2);
pullImage(image[0], image[1], container);
pullImage(image[0], image[1], name, container);
loadImage(image[0], image[1], name, container);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void createAndWait(String namespace, String name, V1Deployment deployment
}
else {
String[] image = imageFromDeployment.split(":", 2);
pullImage(image[0], image[1], container);
pullImage(image[0], image[1], name, container);
loadImage(image[0], image[1], name, container);
}

Expand Down

0 comments on commit ed2080b

Please sign in to comment.