Skip to content

Commit

Permalink
Remove high level filtering and return an array of cr classes instead…
Browse files Browse the repository at this point in the history
… of CustomResourceInfo's
  • Loading branch information
baloo42 committed May 9, 2024
1 parent e22a3c7 commit 3ac2865
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.fabric8.crd.generator.collector;

import io.fabric8.crdv2.generator.CustomResourceInfo;
import io.fabric8.kubernetes.api.model.HasMetadata;
import org.jboss.jandex.IndexView;
import org.slf4j.Logger;
Expand All @@ -30,10 +29,11 @@
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Collects Custom Resource class files from various places and loads them by using
* {@link CustomResourceCollector#findCustomResources()}.
* {@link CustomResourceCollector#findCustomResourceClasses()}.
*/
public class CustomResourceCollector {

Expand All @@ -45,7 +45,6 @@ public class CustomResourceCollector {
private final Set<String> customResourceClassNames = new HashSet<>();

private final List<Predicate<String>> classNamePredicates = new LinkedList<>();
private final List<Predicate<CustomResourceInfo>> customResourceInfoPredicates = new LinkedList<>();

public CustomResourceCollector withParentClassLoader(ClassLoader classLoader) {
this.customResourceClassLoader.withParentClassLoader(classLoader);
Expand Down Expand Up @@ -125,51 +124,7 @@ public CustomResourceCollector withExcludePackages(Collection<String> packages)
return this;
}

public CustomResourceCollector withIncludeGroups(Collection<String> groups) {
if (groups != null) {
groups.stream()
.filter(Objects::nonNull)
.map(group -> (Predicate<CustomResourceInfo>) cr -> group.equals(cr.group()))
.reduce(Predicate::or)
.ifPresent(customResourceInfoPredicates::add);
}
return this;
}

public CustomResourceCollector withExcludeGroups(Collection<String> groups) {
if (groups != null) {
groups.stream()
.filter(Objects::nonNull)
.map(groupToFilter -> (Predicate<CustomResourceInfo>) cr -> !groupToFilter.equals(cr.group()))
.reduce(Predicate::or)
.ifPresent(customResourceInfoPredicates::add);
}
return this;
}

public CustomResourceCollector withIncludeVersions(Collection<String> versions) {
if (versions != null) {
versions.stream()
.filter(Objects::nonNull)
.map(versionToFilter -> (Predicate<CustomResourceInfo>) cr -> versionToFilter.equals(cr.version()))
.reduce(Predicate::or)
.ifPresent(customResourceInfoPredicates::add);
}
return this;
}

public CustomResourceCollector withExcludeVersions(Collection<String> versions) {
if (versions != null) {
versions.stream()
.filter(Objects::nonNull)
.map(versionToFilter -> (Predicate<CustomResourceInfo>) cr -> !versionToFilter.equals(cr.version()))
.reduce(Predicate::or)
.ifPresent(customResourceInfoPredicates::add);
}
return this;
}

public CustomResourceInfo[] findCustomResources() {
private List<Class<? extends HasMetadata>> findCustomResourceClassesAsList() {
Set<String> customResourcesClassNames = new HashSet<>(customResourceClassNames);

// use indices only if custom resource class names are not explicitly given
Expand All @@ -184,23 +139,20 @@ public CustomResourceInfo[] findCustomResources() {
.reduce(Predicate::and)
.orElse(className -> true);

Predicate<CustomResourceInfo> customResourceInfoPredicate = customResourceInfoPredicates.stream()
.reduce(Predicate::and)
.orElse(info -> true);

CustomResourceInfo[] infos = customResourcesClassNames.stream()
List<Class<? extends HasMetadata>> customResourceClasses = customResourcesClassNames.stream()
.filter(classNamePredicate)
.map(customResourceClassLoader::loadCustomResourceClass)
.map(this::createCustomResourceInfo)
.filter(customResourceInfoPredicate)
.toArray(CustomResourceInfo[]::new);
.collect(Collectors.toList());

log.debug("Found {} custom resource classes after filtering", infos.length);
return infos;
log.debug("Found {} custom resource classes after filtering", customResourceClasses.size());
return customResourceClasses;
}

private CustomResourceInfo createCustomResourceInfo(Class<? extends HasMetadata> customResourceClass) {
return CustomResourceInfo.fromClass(customResourceClass);
public Class<? extends HasMetadata>[] findCustomResourceClasses() {
Collection<Class<? extends HasMetadata>> customResourceClasses = findCustomResourceClassesAsList();
@SuppressWarnings("unchecked")
Class<? extends HasMetadata>[] array = (Class<? extends HasMetadata>[]) new Class<?>[customResourceClasses.size()];
return customResourceClasses.toArray(array);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package io.fabric8.crd.generator.collector;

import io.fabric8.crd.generator.collector.examples.MyCustomResource;
import io.fabric8.crdv2.generator.CustomResourceInfo;
import io.fabric8.kubernetes.api.model.HasMetadata;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.IndexWriter;
Expand All @@ -36,31 +36,29 @@ class CustomResourceCollectorTest {
// must be adjusted if new test custom resources are added
private static final int CR_COUNT_ALL = 7;
private static final int CR_COUNT_V1_PKG = 2;
private static final int CR_COUNT_V1_VERSION = 2;
private static final int CR_COUNT_OTHER_GROUP = 1;

@Test
void explicitClass_thenNoScanAndFind() {
CustomResourceCollector collector = new CustomResourceCollector();
collector.withFileToIndex(new File("target/classes"));
collector.withCustomResourceClass(MyCustomResource.class.getName());
CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(1, infos.length);
}

@Test
void scanClassDirWithNoCRs_thenFindZero() {
CustomResourceCollector collector = new CustomResourceCollector();
collector.withFileToIndex(new File("target/classes"));
CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(0, infos.length);
}

@Test
void scanClassDirWithCRs_thenFindAll() {
CustomResourceCollector collector = new CustomResourceCollector();
collector.withFileToIndex(new File("target/test-classes"));
CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(CR_COUNT_ALL, infos.length);
}

Expand All @@ -70,7 +68,7 @@ void scanClassDirWithCRsAndFilterByPackageIncludes_thenFind() {
collector.withFileToIndex(new File("target/test-classes"));
collector.withIncludePackages(
Collections.singletonList("io.fabric8.crd.generator.collector.examples.v1"));
CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(CR_COUNT_V1_PKG, infos.length);
}

Expand All @@ -80,51 +78,15 @@ void scanClassDirWithCRsAndFilterByPackageExcludes_thenFind() {
collector.withFileToIndex(new File("target/test-classes"));
collector.withExcludePackages(
Collections.singletonList("io.fabric8.crd.generator.collector.examples.v1"));
CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(CR_COUNT_ALL - CR_COUNT_V1_PKG, infos.length);
}

@Test
void scanClassDirWithCRsAndFilterByVersionIncludes_thenFind() {
CustomResourceCollector collector = new CustomResourceCollector();
collector.withFileToIndex(new File("target/test-classes"));
collector.withIncludeVersions(Collections.singletonList("v1"));
CustomResourceInfo[] infos = collector.findCustomResources();
assertEquals(CR_COUNT_V1_VERSION, infos.length);
}

@Test
void scanClassDirWithCRsAndFilterByVersionExcludes_thenFind() {
CustomResourceCollector collector = new CustomResourceCollector();
collector.withFileToIndex(new File("target/test-classes"));
collector.withExcludeVersions(Collections.singletonList("v1"));
CustomResourceInfo[] infos = collector.findCustomResources();
assertEquals(CR_COUNT_ALL - CR_COUNT_V1_VERSION, infos.length);
}

@Test
void scanClassDirWithCRsAndFilterByGroupIncludes_thenFind() {
CustomResourceCollector collector = new CustomResourceCollector();
collector.withFileToIndex(new File("target/test-classes"));
collector.withIncludeGroups(Collections.singletonList("other.samples.fabric8.io"));
CustomResourceInfo[] infos = collector.findCustomResources();
assertEquals(CR_COUNT_OTHER_GROUP, infos.length);
}

@Test
void scanClassDirWithCRsAndFilterByGroupExcludes_thenFind() {
CustomResourceCollector collector = new CustomResourceCollector();
collector.withFileToIndex(new File("target/test-classes"));
collector.withExcludeGroups(Collections.singletonList("other.samples.fabric8.io"));
CustomResourceInfo[] infos = collector.findCustomResources();
assertEquals(CR_COUNT_ALL - CR_COUNT_OTHER_GROUP, infos.length);
}

@Test
void indexWithCR_thenFindCRFromIndex() throws IOException {
CustomResourceCollector collector = new CustomResourceCollector();
collector.withIndex(Index.of(MyCustomResource.class));
CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(1, infos.length);
}

Expand Down Expand Up @@ -152,7 +114,7 @@ void classDirWithCRsAndIndex_thenFindOnlyCRFromIndex(@TempDir File tempDir) thro
CustomResourceCollector collector = new CustomResourceCollector();
collector.withFileToIndex(tempDir);

CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(1, infos.length);
}

Expand All @@ -163,7 +125,7 @@ void classDirWithCRsAndIndexAndForceScan_thenFindAll(@TempDir File tempDir) thro
collector.withFileToIndex(tempDir);
collector.withForceIndex(true);

CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(2, infos.length);
}

Expand All @@ -172,7 +134,7 @@ void provideIndexWithCRAndForceScan_thenFindOnlyCRFromIndex() throws IOException
CustomResourceCollector collector = new CustomResourceCollector();
collector.withIndex(Index.of(MyCustomResource.class));
collector.withForceIndex(true);
CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(1, infos.length);
}

Expand All @@ -198,12 +160,8 @@ void checkNullsafe() {

collector.withIncludePackages(null);
collector.withExcludePackages(null);
collector.withIncludeGroups(null);
collector.withExcludeGroups(null);
collector.withIncludeVersions(null);
collector.withExcludeVersions(null);

CustomResourceInfo[] infos = collector.findCustomResources();
Class<? extends HasMetadata>[] infos = collector.findCustomResourceClasses();
assertEquals(0, infos.length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import io.fabric8.crd.generator.collector.CustomResourceCollector;
import io.fabric8.crdv2.generator.CRDGenerationInfo;
import io.fabric8.crdv2.generator.CRDGenerator;
import io.fabric8.crdv2.generator.CustomResourceInfo;
import io.fabric8.kubernetes.api.model.HasMetadata;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -54,7 +54,7 @@ public class CrdGeneratorMojo extends AbstractMojo {
* If set, scanning is disabled.
*/
@Parameter(property = "fabric8.crd-generator.customResourceClasses")
private List<String> customResourceClasses = new LinkedList<>();
private List<String> customResourceClassNames = new LinkedList<>();

/**
* Dependencies which should be scanned for Custom Resources.
Expand Down Expand Up @@ -139,14 +139,10 @@ public void execute() throws MojoExecutionException {
.withFilesToIndex(filesToIndex)
.withForceIndex(forceIndex)
.withIncludePackages(inclusions.getPackages())
.withIncludeGroups(inclusions.getGroups())
.withIncludeVersions(inclusions.getVersions())
.withExcludePackages(exclusions.getPackages())
.withExcludeGroups(exclusions.getGroups())
.withExcludeVersions(exclusions.getVersions())
.withCustomResourceClasses(customResourceClasses);
.withCustomResourceClasses(customResourceClassNames);

CustomResourceInfo[] customResourceInfos = customResourceCollector.findCustomResources();
Class<? extends HasMetadata>[] customResourceClasses = customResourceCollector.findCustomResourceClasses();

try {
Files.createDirectories(outputDirectory.toPath());
Expand All @@ -155,7 +151,7 @@ public void execute() throws MojoExecutionException {
}

CRDGenerator crdGenerator = new CRDGenerator()
.customResources(customResourceInfos)
.customResourceClasses(customResourceClasses)
.withParallelGenerationEnabled(parallel)
.withImplicitPreserveUnknownFields(implicitPreserveUnknownFields)
.inOutputDir(outputDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

public class FilterSet {
private List<String> packages;
private List<String> versions;
private List<String> groups;

public List<String> getPackages() {
return packages;
Expand All @@ -29,20 +27,4 @@ public List<String> getPackages() {
public void setPackages(List<String> packages) {
this.packages = packages;
}

public List<String> getVersions() {
return versions;
}

public void setVersions(List<String> versions) {
this.versions = versions;
}

public List<String> getGroups() {
return groups;
}

public void setGroups(List<String> groups) {
this.groups = groups;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ void checkFindingJarArchiveForDependency(@TempDir File tempDir) throws MojoExecu
given(mock.withForceIndex(anyBoolean())).willReturn(mock);
given(mock.withCustomResourceClasses(any())).willReturn(mock);
given(mock.withIncludePackages(any())).willReturn(mock);
given(mock.withIncludeGroups(any())).willReturn(mock);
given(mock.withIncludeVersions(any())).willReturn(mock);
given(mock.withExcludePackages(any())).willReturn(mock);
given(mock.withExcludeGroups(any())).willReturn(mock);
given(mock.withExcludeVersions(any())).willReturn(mock);
})) {
crdGeneratorMojo.execute();
assertTrue(captor.getValue().contains(dummyJar));
Expand Down

0 comments on commit 3ac2865

Please sign in to comment.