forked from quarkiverse/quarkus-operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make it possible to delay controller registration
Controllers were previously automatically registered with the operator as soon as they were processed. This feature makes it possible for the operator to wait for a CDI event before registering the controller. This was initially implemented in operator-framework/java-operator-sdk#344 by @FroMage.
- Loading branch information
Showing
12 changed files
with
376 additions
and
109 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
...nt/src/main/java/io/quarkiverse/operatorsdk/deployment/HybridControllerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package io.quarkiverse.operatorsdk.deployment; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.AnnotationValue; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.Type; | ||
import org.jboss.jandex.Type.Kind; | ||
|
||
import io.javaoperatorsdk.operator.ControllerUtils; | ||
import io.javaoperatorsdk.operator.api.Controller; | ||
import io.javaoperatorsdk.operator.api.ResourceController; | ||
import io.javaoperatorsdk.operator.api.config.RetryConfiguration; | ||
import io.quarkiverse.operatorsdk.runtime.DelayRegistrationUntil; | ||
import io.quarkiverse.operatorsdk.runtime.ExternalConfiguration; | ||
import io.quarkiverse.operatorsdk.runtime.ExternalControllerConfiguration; | ||
|
||
/** | ||
* Encapsulates controller configuration values that might come from either annotation or external | ||
* properties file. | ||
*/ | ||
class HybridControllerConfiguration { | ||
|
||
private final ValueExtractor extractor; | ||
private final String name; | ||
private final ExternalControllerConfiguration extConfig; | ||
private final AnnotationInstance controllerAnnotation; | ||
private final AnnotationInstance delayRegistrationAnnotation; | ||
|
||
/** | ||
* Creates a new HybridControllerConfiguration | ||
* | ||
* @param resourceControllerClassName the fully-qualified name of the associated {@link | ||
* ResourceController} class | ||
* @param externalConfiguration the external configuration | ||
* @param info the {@link ClassInfo} from which information needs to be | ||
* extracted | ||
*/ | ||
public HybridControllerConfiguration( | ||
String resourceControllerClassName, | ||
ExternalConfiguration externalConfiguration, | ||
ClassInfo info) { | ||
this.controllerAnnotation = info.classAnnotation( | ||
DotName.createSimple(Controller.class.getName())); | ||
this.delayRegistrationAnnotation = info.classAnnotation( | ||
DotName.createSimple(DelayRegistrationUntil.class.getName())); | ||
// retrieve the controller's name | ||
final var defaultControllerName = ControllerUtils | ||
.getDefaultResourceControllerName(resourceControllerClassName); | ||
this.name = ValueExtractor.annotationValueOrDefault( | ||
controllerAnnotation, "name", AnnotationValue::asString, () -> defaultControllerName); | ||
|
||
this.extConfig = externalConfiguration.controllers.get(name); | ||
this.extractor = new ValueExtractor(extConfig); | ||
} | ||
|
||
String name() { | ||
return name; | ||
} | ||
|
||
String finalizer(final String crdName) { | ||
return extractor.extract( | ||
controllerAnnotation, c -> c.finalizer, | ||
"finalizerName", | ||
AnnotationValue::asString, | ||
() -> ControllerUtils.getDefaultFinalizerName(crdName)); | ||
} | ||
|
||
boolean generationAware() { | ||
return extractor.extract( | ||
controllerAnnotation, c -> c.generationAware, | ||
"generationAwareEventProcessing", | ||
AnnotationValue::asBoolean, | ||
() -> true); | ||
} | ||
|
||
String[] namespaces() { | ||
return extractor.extract( | ||
controllerAnnotation, c -> c.namespaces.map(l -> l.toArray(new String[0])), | ||
"namespaces", | ||
AnnotationValue::asStringArray, | ||
() -> new String[] {}); | ||
} | ||
|
||
RetryConfiguration retryConfiguration() { | ||
return extConfig == null ? null : RetryConfigurationResolver.resolve(extConfig.retry); | ||
} | ||
|
||
Type eventType() { | ||
return extractor.extract( | ||
delayRegistrationAnnotation, c -> c.delayRegistrationUntilEvent | ||
.filter(s -> void.class.getName().equals(s)) | ||
.map(DotName::createSimple) | ||
.map(dn -> Type.create(dn, Kind.CLASS)), | ||
"event", | ||
AnnotationValue::asClass, | ||
() -> null); | ||
} | ||
|
||
boolean delayedRegistration() { | ||
return extractor.extract( | ||
delayRegistrationAnnotation, | ||
c -> c.delayRegistrationUntilEvent.map(s -> void.class.getName().equals(s)), | ||
"event", | ||
v -> v.asClass().kind() != Kind.VOID, | ||
() -> false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.