Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use resource pattern resolver to look up CRDs from classpath #196

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -58,23 +56,21 @@ 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 = crdPath + '*' + crdSuffix;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users may find it more useful if they can specify the entire location pattern or even a set of patterns (e.g., META-INF/fabric8/*-v1.yml and META-INF/fabric8/*-v2.yml). This is a breaking change and outside the scope of the fix, so let me leave it for now.

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);
}

}
Loading