From 3a4c8b5670edad98b6e0fa7a3a7955b1d1d38be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Boschi?= Date: Tue, 29 Nov 2022 18:00:07 +0100 Subject: [PATCH] fix: leader election manager init when not using config overrider --- .../io/javaoperatorsdk/operator/Operator.java | 2 +- .../operator/OperatorTest.java | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java index e54bcd3ef8..8d7199f727 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java @@ -60,9 +60,9 @@ public Operator(KubernetesClient client, Consumer public Operator(KubernetesClient kubernetesClient, ConfigurationService configurationService) { this.kubernetesClient = kubernetesClient != null ? kubernetesClient : new KubernetesClientBuilder().build(); + ConfigurationServiceProvider.set(configurationService); configurationService.getLeaderElectionConfiguration() .ifPresent(c -> leaderElectionManager.init(c, this.kubernetesClient)); - ConfigurationServiceProvider.set(configurationService); } /** Adds a shutdown hook that automatically calls {@link #stop()} when the app shuts down. */ diff --git a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/OperatorTest.java b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/OperatorTest.java index 1b30d94c69..8a95431885 100644 --- a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/OperatorTest.java +++ b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/OperatorTest.java @@ -1,5 +1,7 @@ package io.javaoperatorsdk.operator; +import java.util.Optional; + import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -12,6 +14,7 @@ import io.javaoperatorsdk.operator.api.config.AbstractConfigurationService; import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider; import io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration; +import io.javaoperatorsdk.operator.api.config.Version; import io.javaoperatorsdk.operator.api.reconciler.Context; import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration; import io.javaoperatorsdk.operator.api.reconciler.Reconciler; @@ -84,6 +87,23 @@ void shouldBeAbleToProvideLeaderElectionConfiguration() { .getLeaderElectionConfiguration().orElseThrow().getIdentity().orElseThrow()); } + @Test + void shouldBeAbleToInitLeaderElectionManagerWithoutOverrider() { + ConfigurationServiceProvider.reset(); + final LeaderElectionConfiguration leaderElectionConfiguration = + new LeaderElectionConfiguration("leader-election-test", "namespace", "identity"); + final AbstractConfigurationService configurationService = + new AbstractConfigurationService(Version.UNKNOWN) { + @Override + public Optional getLeaderElectionConfiguration() { + return Optional.of(leaderElectionConfiguration); + } + }; + new Operator(kubernetesClient, configurationService); + assertEquals("identity", ConfigurationServiceProvider.instance() + .getLeaderElectionConfiguration().orElseThrow().getIdentity().orElseThrow()); + } + @ControllerConfiguration private static class FooReconciler implements Reconciler {