Skip to content

Commit

Permalink
refactor: load external CRDs from CRD generation step
Browse files Browse the repository at this point in the history
Fixes #867

Signed-off-by: Chris Laprun <claprun@redhat.com>
  • Loading branch information
metacosm committed Nov 8, 2024
1 parent 3120324 commit 89f94c7
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Predicate;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
Expand All @@ -38,17 +36,14 @@
import io.quarkiverse.operatorsdk.common.ReconciledAugmentedClassInfo;
import io.quarkiverse.operatorsdk.common.ReconcilerAugmentedClassInfo;
import io.quarkiverse.operatorsdk.deployment.GeneratedCRDInfoBuildItem;
import io.quarkiverse.operatorsdk.deployment.UnownedCRDInfoBuildItem;
import io.quarkiverse.operatorsdk.deployment.VersionBuildItem;
import io.quarkiverse.operatorsdk.runtime.BuildTimeOperatorConfiguration;
import io.quarkiverse.operatorsdk.runtime.CRDInfo;
import io.quarkiverse.operatorsdk.runtime.CRDInfos;
import io.quarkiverse.operatorsdk.runtime.CRDUtils;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.GeneratedFileSystemResourceBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.deployment.pkg.builditem.JarBuildItem;
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.kubernetes.deployment.KubernetesConfig;
Expand All @@ -63,7 +58,6 @@ public class BundleProcessor {
private static final DotName CSV_METADATA = DotName.createSimple(CSVMetadata.class.getName());
private static final String BUNDLE = "bundle";
private static final String DEFAULT_PROVIDER_NAME = System.getProperty("user.name");
private static final Set<String> EMPTY_SET = Set.of();

private static ReconcilerAugmentedClassInfo augmentReconcilerInfo(
ReconcilerAugmentedClassInfo reconcilerInfo) {
Expand Down Expand Up @@ -114,35 +108,6 @@ private static String getBundleName(AnnotationInstance csvMetadata, String defau
}
}

@BuildStep(onlyIf = IsGenerationEnabled.class)
UnownedCRDInfoBuildItem unownedCRDInfo(BuildTimeOperatorConfiguration operatorConfiguration,
CurateOutcomeBuildItem appInfoBuildItem) {
final Optional<List<String>> maybeExternalCRDs = operatorConfiguration.crd().externalCRDLocations();
final var crds = new CRDInfos();
if (maybeExternalCRDs.isPresent()) {
final var moduleRoot = appInfoBuildItem.getApplicationModel().getApplicationModule().getModuleDir().toPath();
maybeExternalCRDs.get().parallelStream()
.filter(Predicate.not(String::isBlank))
.map(String::trim)
.forEach(crdLocation -> {
final var crdPath = moduleRoot.resolve(crdLocation);
final var crd = loadFrom(crdPath);
crds.addCRDInfoFor(crd.getCrdName(), crd.getCrdSpecVersion(), crd);
});
}
return new UnownedCRDInfoBuildItem(crds);
}

private CRDInfo loadFrom(Path crdPath) {
try {
final var crd = CRDUtils.loadFrom(crdPath);
final var crdName = crd.getMetadata().getName();
return new CRDInfo(crdName, CRDUtils.DEFAULT_CRD_SPEC_VERSION, crdPath.toFile().getAbsolutePath(), EMPTY_SET);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@SuppressWarnings({ "unused" })
@BuildStep(onlyIf = IsGenerationEnabled.class)
CSVMetadataBuildItem gatherCSVMetadata(KubernetesConfig kubernetesConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;

import org.jboss.logging.Logger;

Expand All @@ -11,10 +14,12 @@
import io.quarkiverse.operatorsdk.runtime.CRDGenerationInfo;
import io.quarkiverse.operatorsdk.runtime.CRDInfo;
import io.quarkiverse.operatorsdk.runtime.CRDInfos;
import io.quarkiverse.operatorsdk.runtime.CRDUtils;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;

class CRDGenerationBuildStep {
Expand Down Expand Up @@ -94,4 +99,23 @@ GeneratedCRDInfoBuildItem generateCRDs(

return new GeneratedCRDInfoBuildItem(crdInfo);
}

@BuildStep
UnownedCRDInfoBuildItem unownedCRDInfo(BuildTimeOperatorConfiguration operatorConfiguration,
CurateOutcomeBuildItem appInfoBuildItem) {
final Optional<List<String>> maybeExternalCRDs = operatorConfiguration.crd().externalCRDLocations();
final var crds = new CRDInfos();
if (maybeExternalCRDs.isPresent()) {
final var moduleRoot = appInfoBuildItem.getApplicationModel().getApplicationModule().getModuleDir().toPath();
maybeExternalCRDs.get().parallelStream()
.filter(Predicate.not(String::isBlank))
.map(String::trim)
.forEach(crdLocation -> {
final var crdPath = moduleRoot.resolve(crdLocation);
final var crd = CRDUtils.loadFromAsCRDInfo(crdPath);
crds.addCRDInfoFor(crd.getCrdName(), crd.getCrdSpecVersion(), crd);
});
}
return new UnownedCRDInfoBuildItem(crds);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkiverse.operatorsdk.bundle.deployment;
package io.quarkiverse.operatorsdk.deployment;

import io.quarkiverse.operatorsdk.runtime.CRDInfos;
import io.quarkus.builder.item.SimpleBuildItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;

import org.jboss.logging.Logger;

Expand Down Expand Up @@ -56,6 +57,16 @@ public static CustomResourceDefinition loadFrom(Path crdPath) throws IOException
return crd;
}

public static CRDInfo loadFromAsCRDInfo(Path crdPath) {
try {
final var crd = loadFrom(crdPath);
final var crdName = crd.getMetadata().getName();
return new CRDInfo(crdName, DEFAULT_CRD_SPEC_VERSION, crdPath.toFile().getAbsolutePath(), Collections.emptySet());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void apply(KubernetesClient client, String v, Object crd) {
switch (v) {
case DEFAULT_CRD_SPEC_VERSION:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,6 @@ endif::add-copy-button-to-env-var[]
|Map<String,String>
|

a|icon:lock[title=Fixed at build time] [[quarkus-operator-sdk-bundle-generator_quarkus-operator-sdk-bundle-external-crd-locations]] [.property-path]##link:#quarkus-operator-sdk-bundle-generator_quarkus-operator-sdk-bundle-external-crd-locations[`quarkus.operator-sdk.bundle.external-crd-locations`]##

[.description]
--
A comma-separated list of paths where external CRDs that need to be referenced for non-generated custom resources. Typical use cases where this might be needed include when custom resource implementations are located in a different module than the controller implementation or when the CRDs are not generated at all (e.g. in integration cases where your operator needs to deal with 3rd party custom resources).

Paths can be either absolute or relative, in which case they will be resolved from the current module root directory.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_OPERATOR_SDK_BUNDLE_EXTERNAL_CRD_LOCATIONS+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_OPERATOR_SDK_BUNDLE_EXTERNAL_CRD_LOCATIONS+++`
endif::add-copy-button-to-env-var[]
--
|list of string
|

|===


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,6 @@ endif::add-copy-button-to-env-var[]
|Map<String,String>
|

a|icon:lock[title=Fixed at build time] [[quarkus-operator-sdk-bundle-generator_quarkus-operator-sdk-bundle-external-crd-locations]] [.property-path]##link:#quarkus-operator-sdk-bundle-generator_quarkus-operator-sdk-bundle-external-crd-locations[`quarkus.operator-sdk.bundle.external-crd-locations`]##

[.description]
--
A comma-separated list of paths where external CRDs that need to be referenced for non-generated custom resources. Typical use cases where this might be needed include when custom resource implementations are located in a different module than the controller implementation or when the CRDs are not generated at all (e.g. in integration cases where your operator needs to deal with 3rd party custom resources).

Paths can be either absolute or relative, in which case they will be resolved from the current module root directory.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_OPERATOR_SDK_BUNDLE_EXTERNAL_CRD_LOCATIONS+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_OPERATOR_SDK_BUNDLE_EXTERNAL_CRD_LOCATIONS+++`
endif::add-copy-button-to-env-var[]
--
|list of string
|

|===


Expand Down
19 changes: 19 additions & 0 deletions docs/modules/ROOT/pages/includes/quarkus-operator-sdk.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ endif::add-copy-button-to-env-var[]
|list of string
|

a|icon:lock[title=Fixed at build time] [[quarkus-operator-sdk_quarkus-operator-sdk-crd-external-crd-locations]] [.property-path]##link:#quarkus-operator-sdk_quarkus-operator-sdk-crd-external-crd-locations[`quarkus.operator-sdk.crd.external-crd-locations`]##

[.description]
--
A comma-separated list of paths where external CRDs that need to be referenced for non-generated custom resources. Typical use cases where this might be needed include when custom resource implementations are located in a different module than the controller implementation or when the CRDs are not generated at all (e.g. in integration cases where your operator needs to deal with 3rd party custom resources).

Paths can be either absolute or relative, in which case they will be resolved from the current module root directory.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_OPERATOR_SDK_CRD_EXTERNAL_CRD_LOCATIONS+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_OPERATOR_SDK_CRD_EXTERNAL_CRD_LOCATIONS+++`
endif::add-copy-button-to-env-var[]
--
|list of string
|

a|icon:lock[title=Fixed at build time] [[quarkus-operator-sdk_quarkus-operator-sdk-generation-aware]] [.property-path]##link:#quarkus-operator-sdk_quarkus-operator-sdk-generation-aware[`quarkus.operator-sdk.generation-aware`]##

[.description]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,25 @@ endif::add-copy-button-to-env-var[]
|list of string
|

a|icon:lock[title=Fixed at build time] [[quarkus-operator-sdk_quarkus-operator-sdk-crd-external-crd-locations]] [.property-path]##link:#quarkus-operator-sdk_quarkus-operator-sdk-crd-external-crd-locations[`quarkus.operator-sdk.crd.external-crd-locations`]##

[.description]
--
A comma-separated list of paths where external CRDs that need to be referenced for non-generated custom resources. Typical use cases where this might be needed include when custom resource implementations are located in a different module than the controller implementation or when the CRDs are not generated at all (e.g. in integration cases where your operator needs to deal with 3rd party custom resources).

Paths can be either absolute or relative, in which case they will be resolved from the current module root directory.


ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_OPERATOR_SDK_CRD_EXTERNAL_CRD_LOCATIONS+++[]
endif::add-copy-button-to-env-var[]
ifndef::add-copy-button-to-env-var[]
Environment variable: `+++QUARKUS_OPERATOR_SDK_CRD_EXTERNAL_CRD_LOCATIONS+++`
endif::add-copy-button-to-env-var[]
--
|list of string
|

a|icon:lock[title=Fixed at build time] [[quarkus-operator-sdk_quarkus-operator-sdk-generation-aware]] [.property-path]##link:#quarkus-operator-sdk_quarkus-operator-sdk-generation-aware[`quarkus.operator-sdk.generation-aware`]##

[.description]
Expand Down

0 comments on commit 89f94c7

Please sign in to comment.