From 89f94c7318d9c17d7fe736b84abbf7cb70d55768 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Thu, 7 Nov 2024 15:59:24 +0100 Subject: [PATCH] refactor: load external CRDs from CRD generation step Fixes #867 Signed-off-by: Chris Laprun --- .../bundle/deployment/BundleProcessor.java | 37 +------------------ .../deployment/CRDGenerationBuildStep.java | 24 ++++++++++++ .../deployment/UnownedCRDInfoBuildItem.java | 2 +- .../operatorsdk/runtime/CRDUtils.java | 11 ++++++ ...quarkus-operator-sdk-bundle-generator.adoc | 19 ---------- ...bundle-generator_quarkus.operator-sdk.adoc | 19 ---------- .../pages/includes/quarkus-operator-sdk.adoc | 19 ++++++++++ ...kus-operator-sdk_quarkus.operator-sdk.adoc | 19 ++++++++++ 8 files changed, 75 insertions(+), 75 deletions(-) rename {bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle => core/deployment/src/main/java/io/quarkiverse/operatorsdk}/deployment/UnownedCRDInfoBuildItem.java (86%) diff --git a/bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/BundleProcessor.java b/bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/BundleProcessor.java index 15220535..0e097e4d 100644 --- a/bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/BundleProcessor.java +++ b/bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/BundleProcessor.java @@ -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; @@ -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; @@ -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 EMPTY_SET = Set.of(); private static ReconcilerAugmentedClassInfo augmentReconcilerInfo( ReconcilerAugmentedClassInfo reconcilerInfo) { @@ -114,35 +108,6 @@ private static String getBundleName(AnnotationInstance csvMetadata, String defau } } - @BuildStep(onlyIf = IsGenerationEnabled.class) - UnownedCRDInfoBuildItem unownedCRDInfo(BuildTimeOperatorConfiguration operatorConfiguration, - CurateOutcomeBuildItem appInfoBuildItem) { - final Optional> 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, diff --git a/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CRDGenerationBuildStep.java b/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CRDGenerationBuildStep.java index 5e262e9c..3c4f4e50 100644 --- a/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CRDGenerationBuildStep.java +++ b/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/CRDGenerationBuildStep.java @@ -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; @@ -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 { @@ -94,4 +99,23 @@ GeneratedCRDInfoBuildItem generateCRDs( return new GeneratedCRDInfoBuildItem(crdInfo); } + + @BuildStep + UnownedCRDInfoBuildItem unownedCRDInfo(BuildTimeOperatorConfiguration operatorConfiguration, + CurateOutcomeBuildItem appInfoBuildItem) { + final Optional> 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); + } } diff --git a/bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/UnownedCRDInfoBuildItem.java b/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/UnownedCRDInfoBuildItem.java similarity index 86% rename from bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/UnownedCRDInfoBuildItem.java rename to core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/UnownedCRDInfoBuildItem.java index 73a52ff8..d6a1b939 100644 --- a/bundle-generator/deployment/src/main/java/io/quarkiverse/operatorsdk/bundle/deployment/UnownedCRDInfoBuildItem.java +++ b/core/deployment/src/main/java/io/quarkiverse/operatorsdk/deployment/UnownedCRDInfoBuildItem.java @@ -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; diff --git a/core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/CRDUtils.java b/core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/CRDUtils.java index bffba9eb..e50c9428 100644 --- a/core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/CRDUtils.java +++ b/core/runtime/src/main/java/io/quarkiverse/operatorsdk/runtime/CRDUtils.java @@ -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; @@ -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: diff --git a/docs/modules/ROOT/pages/includes/quarkus-operator-sdk-bundle-generator.adoc b/docs/modules/ROOT/pages/includes/quarkus-operator-sdk-bundle-generator.adoc index 805a8d6b..91eb43ff 100644 --- a/docs/modules/ROOT/pages/includes/quarkus-operator-sdk-bundle-generator.adoc +++ b/docs/modules/ROOT/pages/includes/quarkus-operator-sdk-bundle-generator.adoc @@ -110,25 +110,6 @@ endif::add-copy-button-to-env-var[] |Map | -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 -| - |=== diff --git a/docs/modules/ROOT/pages/includes/quarkus-operator-sdk-bundle-generator_quarkus.operator-sdk.adoc b/docs/modules/ROOT/pages/includes/quarkus-operator-sdk-bundle-generator_quarkus.operator-sdk.adoc index 805a8d6b..91eb43ff 100644 --- a/docs/modules/ROOT/pages/includes/quarkus-operator-sdk-bundle-generator_quarkus.operator-sdk.adoc +++ b/docs/modules/ROOT/pages/includes/quarkus-operator-sdk-bundle-generator_quarkus.operator-sdk.adoc @@ -110,25 +110,6 @@ endif::add-copy-button-to-env-var[] |Map | -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 -| - |=== diff --git a/docs/modules/ROOT/pages/includes/quarkus-operator-sdk.adoc b/docs/modules/ROOT/pages/includes/quarkus-operator-sdk.adoc index 73bfddb2..44eeb0ad 100644 --- a/docs/modules/ROOT/pages/includes/quarkus-operator-sdk.adoc +++ b/docs/modules/ROOT/pages/includes/quarkus-operator-sdk.adoc @@ -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] diff --git a/docs/modules/ROOT/pages/includes/quarkus-operator-sdk_quarkus.operator-sdk.adoc b/docs/modules/ROOT/pages/includes/quarkus-operator-sdk_quarkus.operator-sdk.adoc index 73bfddb2..44eeb0ad 100644 --- a/docs/modules/ROOT/pages/includes/quarkus-operator-sdk_quarkus.operator-sdk.adoc +++ b/docs/modules/ROOT/pages/includes/quarkus-operator-sdk_quarkus.operator-sdk.adoc @@ -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]