Skip to content

Commit

Permalink
feat: install a shutdown hook if termination timeout is set
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Laprun <[email protected]>
  • Loading branch information
metacosm committed Mar 26, 2024
1 parent 34ec03d commit 5a349ba
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class ConfigurationServiceRecorder {

static final Logger log = Logger.getLogger(ConfigurationServiceRecorder.class.getName());

@SuppressWarnings({ "rawtypes", "unchecked" })
@SuppressWarnings({ "rawtypes" })
public Supplier<QuarkusConfigurationService> configurationServiceSupplier(Version version,
Map<String, QuarkusControllerConfiguration> configurations,
CRDGenerationInfo crdInfo, RunTimeOperatorConfiguration runTimeConfiguration,
BuildTimeOperatorConfiguration buildTimeConfiguration) {
final var maxThreads = runTimeConfiguration.concurrentReconciliationThreads
.orElse(ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER);
final var timeout = runTimeConfiguration.terminationTimeoutSeconds
.orElse(ConfigurationService.DEFAULT_TERMINATION_TIMEOUT_SECONDS);
.orElse(QuarkusConfigurationService.UNSET_TERMINATION_TIMEOUT_SECONDS);
final var workflowThreads = runTimeConfiguration.concurrentWorkflowThreads
.orElse(ConfigurationService.DEFAULT_WORKFLOW_EXECUTOR_THREAD_NUMBER);
final var cacheSyncTimeout = runTimeConfiguration.cacheSyncTimeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static io.quarkiverse.operatorsdk.runtime.CRDUtils.applyCRD;

import java.time.Duration;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.Produces;
Expand Down Expand Up @@ -34,8 +36,7 @@ public class OperatorProducer {
@DefaultBean
@ApplicationScoped
Operator operator(QuarkusConfigurationService configuration, Instance<Reconciler<? extends HasMetadata>> reconcilers) {
if (configuration.getVersion() instanceof Version) {
final var version = ((Version) configuration.getVersion());
if (configuration.getVersion() instanceof Version version) {
log.info("Quarkus Java Operator SDK extension {}", version.getExtensionCompleteVersion());
}

Expand All @@ -52,6 +53,12 @@ Operator operator(QuarkusConfigurationService configuration, Instance<Reconciler
operator.register(reconciler);
}

// if we set a termination timeout, install a shutdown hook
final var terminationTimeoutSeconds = configuration.getTerminationTimeoutSeconds();
if (QuarkusConfigurationService.UNSET_TERMINATION_TIMEOUT_SECONDS != terminationTimeoutSeconds) {
operator.installShutdownHook(Duration.ofSeconds(terminationTimeoutSeconds));
}

return operator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class QuarkusConfigurationService extends AbstractConfigurationService im
DependentResourceFactory<QuarkusControllerConfiguration<?>>,
ManagedWorkflowFactory<QuarkusControllerConfiguration<?>> {
private static final Logger log = LoggerFactory.getLogger(QuarkusConfigurationService.class);
public static final int UNSET_TERMINATION_TIMEOUT_SECONDS = -1;
private final CRDGenerationInfo crdInfo;
private final int concurrentReconciliationThreads;
private final int terminationTimeout;
Expand Down Expand Up @@ -129,7 +130,6 @@ public int concurrentReconciliationThreads() {
return this.concurrentReconciliationThreads;
}

@Override
public int getTerminationTimeoutSeconds() {
return terminationTimeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public class RunTimeOperatorConfiguration {
public Optional<Integer> concurrentReconciliationThreads;

/**
* Amount of seconds the SDK waits for reconciliation threads to terminate before shutting down.
* Amount of seconds the SDK waits for reconciliation threads to terminate before shutting down. Setting this value will
* install a shutdown hook to wait for termination (causing
* {@link io.javaoperatorsdk.operator.Operator#installShutdownHook(Duration)} to be called with
* `Duration.ofSeconds(terminationTimeoutSeconds)`).
*/
@ConfigItem
public Optional<Integer> terminationTimeoutSeconds;
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/includes/quarkus-operator-sdk.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ a| [[quarkus-operator-sdk_quarkus-operator-sdk-termination-timeout-seconds]]`lin

[.description]
--
Amount of seconds the SDK waits for reconciliation threads to terminate before shutting down.
Amount of seconds the SDK waits for reconciliation threads to terminate before shutting down. Setting this value will install a shutdown hook to wait for termination (causing `io.javaoperatorsdk.operator.Operator++#++installShutdownHook(Duration)` to be called with `Duration.ofSeconds(terminationTimeoutSeconds)`).

ifdef::add-copy-button-to-env-var[]
Environment variable: env_var_with_copy_button:+++QUARKUS_OPERATOR_SDK_TERMINATION_TIMEOUT_SECONDS+++[]
Expand Down

0 comments on commit 5a349ba

Please sign in to comment.