|  | 
| 17 | 17 | package org.springframework.cloud.kubernetes.integration.tests.commons; | 
| 18 | 18 | 
 | 
| 19 | 19 | import java.io.File; | 
| 20 |  | -import java.io.IOException; | 
| 21 | 20 | import java.io.InputStream; | 
| 22 | 21 | import java.nio.charset.StandardCharsets; | 
| 23 | 22 | import java.nio.file.Files; | 
|  | 
| 41 | 40 | import org.testcontainers.k3s.K3sContainer; | 
| 42 | 41 | 
 | 
| 43 | 42 | import org.springframework.core.io.ClassPathResource; | 
| 44 |  | -import org.springframework.util.ReflectionUtils; | 
| 45 | 43 | import org.springframework.util.StreamUtils; | 
| 46 | 44 | import org.springframework.util.StringUtils; | 
| 47 | 45 | 
 | 
|  | 
| 59 | 57 |  */ | 
| 60 | 58 | public final class Commons { | 
| 61 | 59 | 
 | 
|  | 60 | +	private static String POM_VERSION; | 
|  | 61 | + | 
| 62 | 62 | 	private static final Log LOG = LogFactory.getLog(Commons.class); | 
| 63 | 63 | 
 | 
| 64 | 64 | 	private Commons() { | 
| @@ -149,6 +149,11 @@ public static void loadImage(String image, String tag, String tarName, K3sContai | 
| 149 | 149 | 	 * either get the tar from '/tmp/docker/images', or pull the image. | 
| 150 | 150 | 	 */ | 
| 151 | 151 | 	public static void load(K3sContainer container, String tarName, String imageNameForDownload, String imageVersion) { | 
|  | 152 | + | 
|  | 153 | +		if (imageAlreadyInK3s(container, tarName)) { | 
|  | 154 | +			return; | 
|  | 155 | +		} | 
|  | 156 | + | 
| 152 | 157 | 		File dockerImagesRootDir = Paths.get(TMP_IMAGES).toFile(); | 
| 153 | 158 | 		if (dockerImagesRootDir.exists() && dockerImagesRootDir.isDirectory()) { | 
| 154 | 159 | 			File[] tars = dockerImagesRootDir.listFiles(); | 
| @@ -204,22 +209,22 @@ public static void pullImage(String image, String tag, K3sContainer container) t | 
| 204 | 209 | 		try (PullImageCmd pullImageCmd = container.getDockerClient().pullImageCmd(image)) { | 
| 205 | 210 | 			pullImageCmd.withTag(tag).start().awaitCompletion(); | 
| 206 | 211 | 		} | 
| 207 |  | - | 
| 208 | 212 | 	} | 
| 209 | 213 | 
 | 
| 210 | 214 | 	public static String pomVersion() { | 
| 211 |  | -		try (InputStream in = new ClassPathResource(KUBERNETES_VERSION_FILE).getInputStream()) { | 
| 212 |  | -			String version = StreamUtils.copyToString(in, StandardCharsets.UTF_8); | 
| 213 |  | -			if (StringUtils.hasText(version)) { | 
| 214 |  | -				version = version.trim(); | 
|  | 215 | +		if (POM_VERSION == null) { | 
|  | 216 | +			try (InputStream in = new ClassPathResource(KUBERNETES_VERSION_FILE).getInputStream()) { | 
|  | 217 | +				String version = StreamUtils.copyToString(in, StandardCharsets.UTF_8); | 
|  | 218 | +				if (StringUtils.hasText(version)) { | 
|  | 219 | +					POM_VERSION = version.trim(); | 
|  | 220 | +				} | 
|  | 221 | +			} | 
|  | 222 | +			catch (Exception e) { | 
|  | 223 | +				throw new RuntimeException(e); | 
| 215 | 224 | 			} | 
| 216 |  | -			return version; | 
| 217 |  | -		} | 
| 218 |  | -		catch (IOException e) { | 
| 219 |  | -			ReflectionUtils.rethrowRuntimeException(e); | 
| 220 | 225 | 		} | 
| 221 |  | -		// not reachable since exception rethrown at runtime | 
| 222 |  | -		return null; | 
|  | 226 | + | 
|  | 227 | +		return POM_VERSION; | 
| 223 | 228 | 	} | 
| 224 | 229 | 
 | 
| 225 | 230 | 	/** | 
| @@ -259,4 +264,23 @@ private static void loadImageFromPath(String tarName, K3sContainer container) { | 
| 259 | 264 | 		}); | 
| 260 | 265 | 	} | 
| 261 | 266 | 
 | 
|  | 267 | +	private static boolean imageAlreadyInK3s(K3sContainer container, String tarName) { | 
|  | 268 | +		try { | 
|  | 269 | +			boolean present = container.execInContainer("sh", "-c", "ctr images list | grep " + tarName) | 
|  | 270 | +				.getStdout() | 
|  | 271 | +				.contains(tarName); | 
|  | 272 | +			if (present) { | 
|  | 273 | +				System.out.println("image : " + tarName + " already in k3s, skipping"); | 
|  | 274 | +				return true; | 
|  | 275 | +			} | 
|  | 276 | +			else { | 
|  | 277 | +				System.out.println("image : " + tarName + " not in k3s"); | 
|  | 278 | +				return false; | 
|  | 279 | +			} | 
|  | 280 | +		} | 
|  | 281 | +		catch (Exception e) { | 
|  | 282 | +			throw new RuntimeException(e); | 
|  | 283 | +		} | 
|  | 284 | +	} | 
|  | 285 | + | 
| 262 | 286 | } | 
0 commit comments