Search in sources :

Example 1 with CRDGenerationInfo

use of io.quarkiverse.operatorsdk.runtime.CRDGenerationInfo in project quarkus-operator-sdk by quarkiverse.

the class CRDGeneration method generate.

/**
 * Generates the CRD in the location specified by the output target, using the specified CRD generation configuration
 *
 * @param outputTarget the {@link OutputTargetBuildItem} specifying where the CRDs should be generated
 * @param crdConfig the {@link CRDConfiguration} specifying how the CRDs should be generated
 * @param validateCustomResources whether the SDK should check if the CRDs are properly deployed on the server
 * @param existing the already known CRDInfos
 * @return a {@link CRDGenerationInfo} detailing information about the CRD generation
 */
CRDGenerationInfo generate(OutputTargetBuildItem outputTarget, CRDConfiguration crdConfig, boolean validateCustomResources, Map<String, Map<String, CRDInfo>> existing) {
    // initialize CRDInfo with existing data to always have a full view even if we don't generate anything
    final var converted = new HashMap<>(existing);
    // record which CRDs got generated so that we only apply the changed ones
    final var generated = new HashSet<String>();
    if (needGeneration) {
        final String outputDirName = crdConfig.outputDirectory;
        final var outputDir = outputTarget.getOutputDirectory().resolve(outputDirName).toFile();
        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }
        // generate CRDs with detailed information
        final var info = generator.forCRDVersions(crdConfig.versions).inOutputDir(outputDir).detailedGenerate();
        final var crdDetailsPerNameAndVersion = info.getCRDDetailsPerNameAndVersion();
        crdDetailsPerNameAndVersion.forEach((crdName, initialVersionToCRDInfoMap) -> {
            OperatorSDKProcessor.log.infov("Generated {0} CRD:", crdName);
            generated.add(crdName);
            final var versions = crMappings.getResourceInfos(crdName);
            final var versionToCRDInfo = converted.computeIfAbsent(crdName, s -> new HashMap<>());
            initialVersionToCRDInfoMap.forEach((version, crdInfo) -> {
                final var filePath = crdInfo.getFilePath();
                OperatorSDKProcessor.log.infov("  - {0} -> {1}", version, filePath);
                versionToCRDInfo.put(version, new CRDInfo(crdInfo.getCrdName(), version, filePath, crdInfo.getDependentClassNames(), versions));
            });
        });
    }
    return new CRDGenerationInfo(crdConfig.apply, validateCustomResources, converted, generated);
}
Also used : HashMap(java.util.HashMap) CRDGenerationInfo(io.quarkiverse.operatorsdk.runtime.CRDGenerationInfo) CRDInfo(io.quarkiverse.operatorsdk.runtime.CRDInfo) HashSet(java.util.HashSet)

Example 2 with CRDGenerationInfo

use of io.quarkiverse.operatorsdk.runtime.CRDGenerationInfo in project quarkus-operator-sdk by quarkiverse.

the class OperatorSDKProcessor method createConfigurationServiceAndOperator.

@BuildStep
ConfigurationServiceBuildItem createConfigurationServiceAndOperator(OutputTargetBuildItem outputTarget, CombinedIndexBuildItem combinedIndexBuildItem, BuildProducer<AdditionalBeanBuildItem> additionalBeans, BuildProducer<ReflectiveClassBuildItem> reflectionClasses, BuildProducer<ForceNonWeakReflectiveClassBuildItem> forcedReflectionClasses, BuildProducer<GeneratedCRDInfoBuildItem> generatedCRDInfo, LiveReloadBuildItem liveReload) {
    final CRDConfiguration crdConfig = buildTimeConfiguration.crd;
    final boolean validateCustomResources = ConfigurationUtils.shouldValidateCustomResources(buildTimeConfiguration.checkCRDAndValidateLocalModel, buildTimeConfiguration.crd.validate, log);
    // apply should imply generate: we cannot apply if we're not generating!
    final var crdGeneration = new CRDGeneration(crdConfig.generate || crdConfig.apply);
    final var index = combinedIndexBuildItem.getIndex();
    final List<QuarkusControllerConfiguration> controllerConfigs = ClassUtils.getKnownReconcilers(index, log).map(ci -> createControllerConfiguration(ci, additionalBeans, reflectionClasses, forcedReflectionClasses, index, crdGeneration, liveReload)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    // retrieve the known CRD information to make sure we always have a full view
    var storedCRDInfos = liveReload.getContextObject(ContextStoredCRDInfos.class);
    if (storedCRDInfos == null) {
        storedCRDInfos = new ContextStoredCRDInfos();
    }
    CRDGenerationInfo crdInfo = crdGeneration.generate(outputTarget, crdConfig, validateCustomResources, storedCRDInfos.getExisting());
    storedCRDInfos.putAll(crdInfo.getCrds());
    // record CRD generation info in context for future use
    liveReload.setContextObject(ContextStoredCRDInfos.class, storedCRDInfos);
    additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(OperatorProducer.class));
    // if the app doesn't provide a main class, add the AppEventListener
    if (index.getAllKnownImplementors(DotName.createSimple(QuarkusApplication.class.getName())).isEmpty()) {
        additionalBeans.produce(AdditionalBeanBuildItem.builder().addBeanClass(AppEventListener.class).setDefaultScope(DotName.createSimple(Singleton.class.getName())).setUnremovable().build());
    }
    generatedCRDInfo.produce(new GeneratedCRDInfoBuildItem(crdInfo));
    return new ConfigurationServiceBuildItem(Version.loadFromProperties(), controllerConfigs);
}
Also used : ConfigurationServiceRecorder(io.quarkiverse.operatorsdk.runtime.ConfigurationServiceRecorder) UnremovableBeanBuildItem(io.quarkus.arc.deployment.UnremovableBeanBuildItem) JandexUtil(io.quarkus.deployment.util.JandexUtil) RunTimeOperatorConfiguration(io.quarkiverse.operatorsdk.runtime.RunTimeOperatorConfiguration) Version(io.quarkiverse.operatorsdk.runtime.Version) ClassInfo(org.jboss.jandex.ClassInfo) CombinedIndexBuildItem(io.quarkus.deployment.builditem.CombinedIndexBuildItem) RECONCILER(io.quarkiverse.operatorsdk.common.Constants.RECONCILER) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) MetricsCapabilityBuildItem(io.quarkus.deployment.metrics.MetricsCapabilityBuildItem) BooleanSupplier(java.util.function.BooleanSupplier) Reconciler(io.javaoperatorsdk.operator.api.reconciler.Reconciler) AdditionalBeanBuildItem(io.quarkus.arc.deployment.AdditionalBeanBuildItem) ObserverConfiguratorBuildItem(io.quarkus.arc.deployment.ObserverRegistrationPhaseBuildItem.ObserverConfiguratorBuildItem) BuildException(io.quarkus.builder.BuildException) FeatureBuildItem(io.quarkus.deployment.builditem.FeatureBuildItem) Operator(io.javaoperatorsdk.operator.Operator) ClassUtils.loadClass(io.quarkiverse.operatorsdk.common.ClassUtils.loadClass) ConfigurationUtils(io.quarkiverse.operatorsdk.common.ConfigurationUtils) CRDGenerationInfo(io.quarkiverse.operatorsdk.runtime.CRDGenerationInfo) Instance(javax.enterprise.inject.Instance) AnnotationValue(org.jboss.jandex.AnnotationValue) CUSTOM_RESOURCE(io.quarkiverse.operatorsdk.common.Constants.CUSTOM_RESOURCE) NoOpMetricsProvider(io.quarkiverse.operatorsdk.runtime.NoOpMetricsProvider) ObserverRegistrationPhaseBuildItem(io.quarkus.arc.deployment.ObserverRegistrationPhaseBuildItem) ExecutionTime(io.quarkus.deployment.annotations.ExecutionTime) ForceNonWeakReflectiveClassBuildItem(io.quarkus.deployment.builditem.nativeimage.ForceNonWeakReflectiveClassBuildItem) DecoratorBuildItem(io.quarkus.kubernetes.spi.DecoratorBuildItem) CDI(javax.enterprise.inject.spi.CDI) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) APPLICATION_SCOPED(io.quarkus.arc.processor.DotNames.APPLICATION_SCOPED) List(java.util.List) AnnotationInstance(org.jboss.jandex.AnnotationInstance) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) QuarkusControllerConfiguration(io.quarkiverse.operatorsdk.runtime.QuarkusControllerConfiguration) ResultHandle(io.quarkus.gizmo.ResultHandle) CustomResource(io.fabric8.kubernetes.client.CustomResource) SyntheticBeanBuildItem(io.quarkus.arc.deployment.SyntheticBeanBuildItem) ReflectiveClassBuildItem(io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem) Record(io.quarkus.deployment.annotations.Record) AppEventListener(io.quarkiverse.operatorsdk.runtime.AppEventListener) ClassUtils(io.quarkiverse.operatorsdk.common.ClassUtils) Logger(org.jboss.logging.Logger) MethodCreator(io.quarkus.gizmo.MethodCreator) DotName(org.jboss.jandex.DotName) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) MetricsFactory(io.quarkus.runtime.metrics.MetricsFactory) IndexDependencyBuildItem(io.quarkus.deployment.builditem.IndexDependencyBuildItem) OperatorProducer(io.quarkiverse.operatorsdk.runtime.OperatorProducer) BuildStep(io.quarkus.deployment.annotations.BuildStep) ResourceInfo(io.quarkiverse.operatorsdk.common.ResourceInfo) AssignableResultHandle(io.quarkus.gizmo.AssignableResultHandle) IndexView(org.jboss.jandex.IndexView) ConfigurationService(io.javaoperatorsdk.operator.api.config.ConfigurationService) MethodDescriptor(io.quarkus.gizmo.MethodDescriptor) BuildTimeOperatorConfiguration(io.quarkiverse.operatorsdk.runtime.BuildTimeOperatorConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CONTROLLER_CONFIGURATION(io.quarkiverse.operatorsdk.common.Constants.CONTROLLER_CONFIGURATION) ReconcilerUtils(io.javaoperatorsdk.operator.ReconcilerUtils) ObserverConfigurator(io.quarkus.arc.processor.ObserverConfigurator) CRDConfiguration(io.quarkiverse.operatorsdk.runtime.CRDConfiguration) OutputTargetBuildItem(io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem) DelayRegistrationUntil(io.quarkiverse.operatorsdk.runtime.DelayRegistrationUntil) QuarkusApplication(io.quarkus.runtime.QuarkusApplication) QuarkusConfigurationService(io.quarkiverse.operatorsdk.runtime.QuarkusConfigurationService) Collections(java.util.Collections) LiveReloadBuildItem(io.quarkus.deployment.builditem.LiveReloadBuildItem) Optional(java.util.Optional) QuarkusControllerConfiguration(io.quarkiverse.operatorsdk.runtime.QuarkusControllerConfiguration) QuarkusApplication(io.quarkus.runtime.QuarkusApplication) CRDConfiguration(io.quarkiverse.operatorsdk.runtime.CRDConfiguration) CRDGenerationInfo(io.quarkiverse.operatorsdk.runtime.CRDGenerationInfo) OperatorProducer(io.quarkiverse.operatorsdk.runtime.OperatorProducer) Singleton(javax.inject.Singleton) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

CRDGenerationInfo (io.quarkiverse.operatorsdk.runtime.CRDGenerationInfo)2 HashMap (java.util.HashMap)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 CustomResource (io.fabric8.kubernetes.client.CustomResource)1 Operator (io.javaoperatorsdk.operator.Operator)1 ReconcilerUtils (io.javaoperatorsdk.operator.ReconcilerUtils)1 ConfigurationService (io.javaoperatorsdk.operator.api.config.ConfigurationService)1 Reconciler (io.javaoperatorsdk.operator.api.reconciler.Reconciler)1 ClassUtils (io.quarkiverse.operatorsdk.common.ClassUtils)1 ClassUtils.loadClass (io.quarkiverse.operatorsdk.common.ClassUtils.loadClass)1 ConfigurationUtils (io.quarkiverse.operatorsdk.common.ConfigurationUtils)1 CONTROLLER_CONFIGURATION (io.quarkiverse.operatorsdk.common.Constants.CONTROLLER_CONFIGURATION)1 CUSTOM_RESOURCE (io.quarkiverse.operatorsdk.common.Constants.CUSTOM_RESOURCE)1 RECONCILER (io.quarkiverse.operatorsdk.common.Constants.RECONCILER)1 ResourceInfo (io.quarkiverse.operatorsdk.common.ResourceInfo)1 AppEventListener (io.quarkiverse.operatorsdk.runtime.AppEventListener)1 BuildTimeOperatorConfiguration (io.quarkiverse.operatorsdk.runtime.BuildTimeOperatorConfiguration)1 CRDConfiguration (io.quarkiverse.operatorsdk.runtime.CRDConfiguration)1 CRDInfo (io.quarkiverse.operatorsdk.runtime.CRDInfo)1