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..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,21 +1,20 @@ package io.javaoperatorsdk.operator.springboot.starter; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.net.URISyntaxException; +import java.nio.file.Paths; 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; @@ -58,23 +57,22 @@ 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 = Paths.get(crdPath, '*' + crdSuffix).toString(); 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); - } - }