diff --git a/crd-generator/collector/src/main/java/io/fabric8/crd/generator/collector/CustomResourceCollector.java b/crd-generator/collector/src/main/java/io/fabric8/crd/generator/collector/CustomResourceCollector.java index d47771f1f93..8cd38dad0a0 100644 --- a/crd-generator/collector/src/main/java/io/fabric8/crd/generator/collector/CustomResourceCollector.java +++ b/crd-generator/collector/src/main/java/io/fabric8/crd/generator/collector/CustomResourceCollector.java @@ -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; @@ -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 { @@ -45,7 +45,6 @@ public class CustomResourceCollector { private final Set customResourceClassNames = new HashSet<>(); private final List> classNamePredicates = new LinkedList<>(); - private final List> customResourceInfoPredicates = new LinkedList<>(); public CustomResourceCollector withParentClassLoader(ClassLoader classLoader) { this.customResourceClassLoader.withParentClassLoader(classLoader); @@ -125,51 +124,7 @@ public CustomResourceCollector withExcludePackages(Collection packages) return this; } - public CustomResourceCollector withIncludeGroups(Collection groups) { - if (groups != null) { - groups.stream() - .filter(Objects::nonNull) - .map(group -> (Predicate) cr -> group.equals(cr.group())) - .reduce(Predicate::or) - .ifPresent(customResourceInfoPredicates::add); - } - return this; - } - - public CustomResourceCollector withExcludeGroups(Collection groups) { - if (groups != null) { - groups.stream() - .filter(Objects::nonNull) - .map(groupToFilter -> (Predicate) cr -> !groupToFilter.equals(cr.group())) - .reduce(Predicate::or) - .ifPresent(customResourceInfoPredicates::add); - } - return this; - } - - public CustomResourceCollector withIncludeVersions(Collection versions) { - if (versions != null) { - versions.stream() - .filter(Objects::nonNull) - .map(versionToFilter -> (Predicate) cr -> versionToFilter.equals(cr.version())) - .reduce(Predicate::or) - .ifPresent(customResourceInfoPredicates::add); - } - return this; - } - - public CustomResourceCollector withExcludeVersions(Collection versions) { - if (versions != null) { - versions.stream() - .filter(Objects::nonNull) - .map(versionToFilter -> (Predicate) cr -> !versionToFilter.equals(cr.version())) - .reduce(Predicate::or) - .ifPresent(customResourceInfoPredicates::add); - } - return this; - } - - public CustomResourceInfo[] findCustomResources() { + private List> findCustomResourceClassesAsList() { Set customResourcesClassNames = new HashSet<>(customResourceClassNames); // use indices only if custom resource class names are not explicitly given @@ -184,23 +139,20 @@ public CustomResourceInfo[] findCustomResources() { .reduce(Predicate::and) .orElse(className -> true); - Predicate customResourceInfoPredicate = customResourceInfoPredicates.stream() - .reduce(Predicate::and) - .orElse(info -> true); - - CustomResourceInfo[] infos = customResourcesClassNames.stream() + List> 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 customResourceClass) { - return CustomResourceInfo.fromClass(customResourceClass); + public Class[] findCustomResourceClasses() { + Collection> customResourceClasses = findCustomResourceClassesAsList(); + @SuppressWarnings("unchecked") + Class[] array = (Class[]) new Class[customResourceClasses.size()]; + return customResourceClasses.toArray(array); } } diff --git a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/CustomResourceCollectorTest.java b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/CustomResourceCollectorTest.java index 1d52def0f3c..5d5df6c0445 100644 --- a/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/CustomResourceCollectorTest.java +++ b/crd-generator/collector/src/test/java/io/fabric8/crd/generator/collector/CustomResourceCollectorTest.java @@ -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; @@ -36,15 +36,13 @@ 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[] infos = collector.findCustomResourceClasses(); assertEquals(1, infos.length); } @@ -52,7 +50,7 @@ void explicitClass_thenNoScanAndFind() { void scanClassDirWithNoCRs_thenFindZero() { CustomResourceCollector collector = new CustomResourceCollector(); collector.withFileToIndex(new File("target/classes")); - CustomResourceInfo[] infos = collector.findCustomResources(); + Class[] infos = collector.findCustomResourceClasses(); assertEquals(0, infos.length); } @@ -60,7 +58,7 @@ void scanClassDirWithNoCRs_thenFindZero() { void scanClassDirWithCRs_thenFindAll() { CustomResourceCollector collector = new CustomResourceCollector(); collector.withFileToIndex(new File("target/test-classes")); - CustomResourceInfo[] infos = collector.findCustomResources(); + Class[] infos = collector.findCustomResourceClasses(); assertEquals(CR_COUNT_ALL, infos.length); } @@ -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[] infos = collector.findCustomResourceClasses(); assertEquals(CR_COUNT_V1_PKG, infos.length); } @@ -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[] 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[] infos = collector.findCustomResourceClasses(); assertEquals(1, infos.length); } @@ -152,7 +114,7 @@ void classDirWithCRsAndIndex_thenFindOnlyCRFromIndex(@TempDir File tempDir) thro CustomResourceCollector collector = new CustomResourceCollector(); collector.withFileToIndex(tempDir); - CustomResourceInfo[] infos = collector.findCustomResources(); + Class[] infos = collector.findCustomResourceClasses(); assertEquals(1, infos.length); } @@ -163,7 +125,7 @@ void classDirWithCRsAndIndexAndForceScan_thenFindAll(@TempDir File tempDir) thro collector.withFileToIndex(tempDir); collector.withForceIndex(true); - CustomResourceInfo[] infos = collector.findCustomResources(); + Class[] infos = collector.findCustomResourceClasses(); assertEquals(2, infos.length); } @@ -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[] infos = collector.findCustomResourceClasses(); assertEquals(1, infos.length); } @@ -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[] infos = collector.findCustomResourceClasses(); assertEquals(0, infos.length); } diff --git a/crd-generator/maven-plugin/src/main/java/io/fabric8/crd/generator/maven/plugin/CrdGeneratorMojo.java b/crd-generator/maven-plugin/src/main/java/io/fabric8/crd/generator/maven/plugin/CrdGeneratorMojo.java index b541afed10e..f775d7ed414 100644 --- a/crd-generator/maven-plugin/src/main/java/io/fabric8/crd/generator/maven/plugin/CrdGeneratorMojo.java +++ b/crd-generator/maven-plugin/src/main/java/io/fabric8/crd/generator/maven/plugin/CrdGeneratorMojo.java @@ -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; @@ -54,7 +54,7 @@ public class CrdGeneratorMojo extends AbstractMojo { * If set, scanning is disabled. */ @Parameter(property = "fabric8.crd-generator.customResourceClasses") - private List customResourceClasses = new LinkedList<>(); + private List customResourceClassNames = new LinkedList<>(); /** * Dependencies which should be scanned for Custom Resources. @@ -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[] customResourceClasses = customResourceCollector.findCustomResourceClasses(); try { Files.createDirectories(outputDirectory.toPath()); @@ -155,7 +151,7 @@ public void execute() throws MojoExecutionException { } CRDGenerator crdGenerator = new CRDGenerator() - .customResources(customResourceInfos) + .customResourceClasses(customResourceClasses) .withParallelGenerationEnabled(parallel) .withImplicitPreserveUnknownFields(implicitPreserveUnknownFields) .inOutputDir(outputDirectory); diff --git a/crd-generator/maven-plugin/src/main/java/io/fabric8/crd/generator/maven/plugin/FilterSet.java b/crd-generator/maven-plugin/src/main/java/io/fabric8/crd/generator/maven/plugin/FilterSet.java index 8ffdc8d997a..d70d887f98b 100644 --- a/crd-generator/maven-plugin/src/main/java/io/fabric8/crd/generator/maven/plugin/FilterSet.java +++ b/crd-generator/maven-plugin/src/main/java/io/fabric8/crd/generator/maven/plugin/FilterSet.java @@ -19,8 +19,6 @@ public class FilterSet { private List packages; - private List versions; - private List groups; public List getPackages() { return packages; @@ -29,20 +27,4 @@ public List getPackages() { public void setPackages(List packages) { this.packages = packages; } - - public List getVersions() { - return versions; - } - - public void setVersions(List versions) { - this.versions = versions; - } - - public List getGroups() { - return groups; - } - - public void setGroups(List groups) { - this.groups = groups; - } } diff --git a/crd-generator/maven-plugin/src/test/java/io/fabric8/crd/generator/maven/plugin/CrdGeneratorMojoTest.java b/crd-generator/maven-plugin/src/test/java/io/fabric8/crd/generator/maven/plugin/CrdGeneratorMojoTest.java index b1e53de5845..388f1a5608d 100644 --- a/crd-generator/maven-plugin/src/test/java/io/fabric8/crd/generator/maven/plugin/CrdGeneratorMojoTest.java +++ b/crd-generator/maven-plugin/src/test/java/io/fabric8/crd/generator/maven/plugin/CrdGeneratorMojoTest.java @@ -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));