From dc98471e7ea3b61a2dd6bdd455894a1f5c1f7faa Mon Sep 17 00:00:00 2001 From: Koki Kato Date: Fri, 27 Dec 2024 17:57:43 +0900 Subject: [PATCH 1/3] Use resource pattern resolver to look up CRDs from classpath --- .../springboot/starter/CRDApplier.java | 34 ++++++++----------- .../springboot/starter/CRDApplierTest.java | 12 ------- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java b/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java index f9cfdf7..8ee0398 100644 --- a/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java +++ b/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java @@ -1,24 +1,22 @@ package io.javaoperatorsdk.operator.springboot.starter; -import java.io.File; -import java.io.FileInputStream; +import static java.util.Arrays.stream; +import static java.util.stream.Collectors.joining; +import static org.slf4j.LoggerFactory.getLogger; + import java.io.IOException; -import java.net.URISyntaxException; import java.util.List; import java.util.function.UnaryOperator; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.client.KubernetesClient; -import static java.util.Arrays.stream; -import static java.util.Objects.requireNonNull; -import static java.util.stream.Collectors.joining; -import static org.slf4j.LoggerFactory.getLogger; - @FunctionalInterface public interface CRDApplier { @@ -58,23 +56,21 @@ public DefaultCRDApplier(KubernetesClient kubernetesClient, List @Override public void apply() { log.debug("Uploading CRDs with suffix {} under {}", crdSuffix, crdPath); - stream(findFiles()).forEach(this::applyCrd); + stream(findResources()).forEach(this::applyCrd); } - private File[] findFiles() { - var resource = requireNonNull( - getClass().getResource(crdPath), - "Could not find the configured CRD path"); - + private Resource[] findResources() { + final var resourceResolver = new PathMatchingResourcePatternResolver(); + final var resourceLocationPattern = crdPath + '*' + crdSuffix; try { - return new File(resource.toURI()).listFiles((ignored, name) -> name.endsWith(crdSuffix)); - } catch (URISyntaxException e) { - throw new RuntimeException(e); + return resourceResolver.getResources(resourceLocationPattern); + } catch (IOException e) { + throw new RuntimeException("could not find CRD resources from the location pattern: " + resourceLocationPattern); } } - private void applyCrd(File crdFile) { - try (var is = new FileInputStream(crdFile)) { + private void applyCrd(Resource crdResource) { + try (var is = crdResource.getInputStream()) { var crds = kubernetesClient.load(is).items().stream().map(crdTransformer).toList(); kubernetesClient.resourceList(crds).createOrReplace(); diff --git a/starter/src/test/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplierTest.java b/starter/src/test/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplierTest.java index 9809173..3feb998 100644 --- a/starter/src/test/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplierTest.java +++ b/starter/src/test/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplierTest.java @@ -16,7 +16,6 @@ import io.javaoperatorsdk.operator.springboot.starter.model.TestResource; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -61,15 +60,4 @@ void shouldNotUploadWhenNoCrdsFound() { verifyNoInteractions(kubernetesClient); } - - @Test - void shouldThrowWhenBadPath() { - crdPath = "badPath"; - assertThatThrownBy(() -> applier().apply()) - .isInstanceOf(NullPointerException.class) - .hasMessage("Could not find the configured CRD path"); - - verifyNoInteractions(kubernetesClient); - } - } From 18b343e98633a8b9f4e1b92296f2597b40d45b24 Mon Sep 17 00:00:00 2001 From: Koki Kato Date: Tue, 7 Jan 2025 12:20:14 +0900 Subject: [PATCH 2/3] Fix spotless format violations --- .../operator/springboot/starter/CRDApplier.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java b/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java index 8ee0398..522071a 100644 --- a/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java +++ b/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java @@ -1,9 +1,5 @@ package io.javaoperatorsdk.operator.springboot.starter; -import static java.util.Arrays.stream; -import static java.util.stream.Collectors.joining; -import static org.slf4j.LoggerFactory.getLogger; - import java.io.IOException; import java.util.List; import java.util.function.UnaryOperator; @@ -17,6 +13,10 @@ import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.client.KubernetesClient; +import static java.util.Arrays.stream; +import static java.util.stream.Collectors.joining; +import static org.slf4j.LoggerFactory.getLogger; + @FunctionalInterface public interface CRDApplier { @@ -65,7 +65,8 @@ private Resource[] findResources() { try { return resourceResolver.getResources(resourceLocationPattern); } catch (IOException e) { - throw new RuntimeException("could not find CRD resources from the location pattern: " + resourceLocationPattern); + throw new RuntimeException( + "could not find CRD resources from the location pattern: " + resourceLocationPattern); } } From c792d957d4b84dab81d898c5152f1f3387fc35dc Mon Sep 17 00:00:00 2001 From: Koki Kato Date: Wed, 8 Jan 2025 11:08:57 +0900 Subject: [PATCH 3/3] Fix path pattern concatenation --- .../operator/springboot/starter/CRDApplier.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java b/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java index 522071a..dadfe21 100644 --- a/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java +++ b/starter/src/main/java/io/javaoperatorsdk/operator/springboot/starter/CRDApplier.java @@ -1,6 +1,7 @@ package io.javaoperatorsdk.operator.springboot.starter; import java.io.IOException; +import java.nio.file.Paths; import java.util.List; import java.util.function.UnaryOperator; @@ -61,7 +62,7 @@ public void apply() { private Resource[] findResources() { final var resourceResolver = new PathMatchingResourcePatternResolver(); - final var resourceLocationPattern = crdPath + '*' + crdSuffix; + final var resourceLocationPattern = Paths.get(crdPath, '*' + crdSuffix).toString(); try { return resourceResolver.getResources(resourceLocationPattern); } catch (IOException e) {