Skip to content

Commit

Permalink
Use resource pattern resolver to look up CRDs from classpath (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalab authored Jan 8, 2025
1 parent 8db5b3a commit ae82068
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
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.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;

Expand Down Expand Up @@ -57,23 +56,22 @@ public DefaultCRDApplier(KubernetesClient kubernetesClient, List<CRDTransformer>
@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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}

0 comments on commit ae82068

Please sign in to comment.