|
26 | 26 | import io.fabric8.kubernetes.api.model.HasMetadata; |
27 | 27 | import io.javaoperatorsdk.operator.api.config.ConfigurationServiceOverrider; |
28 | 28 | import io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider; |
| 29 | +import io.javaoperatorsdk.operator.api.config.LeaderElectionConfigurationBuilder; |
29 | 30 | import io.javaoperatorsdk.operator.config.loader.provider.AgregatePrirityListConfigProvider; |
30 | 31 | import io.javaoperatorsdk.operator.config.loader.provider.EnvVarConfigProvider; |
31 | 32 | import io.javaoperatorsdk.operator.config.loader.provider.SystemPropertyConfigProvider; |
@@ -101,6 +102,17 @@ public static ConfigLoader getDefault() { |
101 | 102 | Boolean.class, |
102 | 103 | ConfigurationServiceOverrider::withCloneSecondaryResourcesWhenGettingFromCache)); |
103 | 104 |
|
| 105 | + // --------------------------------------------------------------------------- |
| 106 | + // Operator-level leader-election property keys |
| 107 | + // --------------------------------------------------------------------------- |
| 108 | + static final String LEADER_ELECTION_ENABLED_KEY = "leader-election.enabled"; |
| 109 | + static final String LEADER_ELECTION_LEASE_NAME_KEY = "leader-election.lease-name"; |
| 110 | + static final String LEADER_ELECTION_LEASE_NAMESPACE_KEY = "leader-election.lease-namespace"; |
| 111 | + static final String LEADER_ELECTION_IDENTITY_KEY = "leader-election.identity"; |
| 112 | + static final String LEADER_ELECTION_LEASE_DURATION_KEY = "leader-election.lease-duration"; |
| 113 | + static final String LEADER_ELECTION_RENEW_DEADLINE_KEY = "leader-election.renew-deadline"; |
| 114 | + static final String LEADER_ELECTION_RETRY_PERIOD_KEY = "leader-election.retry-period"; |
| 115 | + |
104 | 116 | // --------------------------------------------------------------------------- |
105 | 117 | // Controller-level retry property suffixes |
106 | 118 | // --------------------------------------------------------------------------- |
@@ -166,7 +178,15 @@ public ConfigLoader( |
166 | 178 | * no binding has a matching value, preserving the previous behavior. |
167 | 179 | */ |
168 | 180 | public Consumer<ConfigurationServiceOverrider> applyConfigs() { |
169 | | - return buildConsumer(OPERATOR_BINDINGS, operatorKeyPrefix); |
| 181 | + Consumer<ConfigurationServiceOverrider> consumer = |
| 182 | + buildConsumer(OPERATOR_BINDINGS, operatorKeyPrefix); |
| 183 | + |
| 184 | + Consumer<ConfigurationServiceOverrider> leaderElectionStep = |
| 185 | + buildLeaderElectionConsumer(operatorKeyPrefix); |
| 186 | + if (leaderElectionStep != null) { |
| 187 | + consumer = consumer.andThen(leaderElectionStep); |
| 188 | + } |
| 189 | + return consumer; |
170 | 190 | } |
171 | 191 |
|
172 | 192 | /** |
@@ -226,6 +246,60 @@ private <R extends HasMetadata> Consumer<ControllerConfigurationOverrider<R>> bu |
226 | 246 | }; |
227 | 247 | } |
228 | 248 |
|
| 249 | + /** |
| 250 | + * If leader election is explicitly disabled via {@code leader-election.enabled=false}, returns |
| 251 | + * {@code null}. Otherwise, if at least one leader-election property is present (with {@code |
| 252 | + * leader-election.lease-name} being required), returns a {@link Consumer} that builds a {@link |
| 253 | + * io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration} via {@link |
| 254 | + * LeaderElectionConfigurationBuilder} and applies it to the overrider. Returns {@code null} when |
| 255 | + * no leader-election properties are present at all. |
| 256 | + */ |
| 257 | + private Consumer<ConfigurationServiceOverrider> buildLeaderElectionConsumer(String prefix) { |
| 258 | + Optional<Boolean> enabled = |
| 259 | + configProvider.getValue(prefix + LEADER_ELECTION_ENABLED_KEY, Boolean.class); |
| 260 | + if (enabled.isPresent() && !enabled.get()) { |
| 261 | + return null; |
| 262 | + } |
| 263 | + |
| 264 | + Optional<String> leaseName = |
| 265 | + configProvider.getValue(prefix + LEADER_ELECTION_LEASE_NAME_KEY, String.class); |
| 266 | + Optional<String> leaseNamespace = |
| 267 | + configProvider.getValue(prefix + LEADER_ELECTION_LEASE_NAMESPACE_KEY, String.class); |
| 268 | + Optional<String> identity = |
| 269 | + configProvider.getValue(prefix + LEADER_ELECTION_IDENTITY_KEY, String.class); |
| 270 | + Optional<Duration> leaseDuration = |
| 271 | + configProvider.getValue(prefix + LEADER_ELECTION_LEASE_DURATION_KEY, Duration.class); |
| 272 | + Optional<Duration> renewDeadline = |
| 273 | + configProvider.getValue(prefix + LEADER_ELECTION_RENEW_DEADLINE_KEY, Duration.class); |
| 274 | + Optional<Duration> retryPeriod = |
| 275 | + configProvider.getValue(prefix + LEADER_ELECTION_RETRY_PERIOD_KEY, Duration.class); |
| 276 | + |
| 277 | + if (leaseName.isEmpty() |
| 278 | + && leaseNamespace.isEmpty() |
| 279 | + && identity.isEmpty() |
| 280 | + && leaseDuration.isEmpty() |
| 281 | + && renewDeadline.isEmpty() |
| 282 | + && retryPeriod.isEmpty()) { |
| 283 | + return null; |
| 284 | + } |
| 285 | + |
| 286 | + return overrider -> { |
| 287 | + var builder = |
| 288 | + LeaderElectionConfigurationBuilder.aLeaderElectionConfiguration( |
| 289 | + leaseName.orElseThrow( |
| 290 | + () -> |
| 291 | + new IllegalStateException( |
| 292 | + "leader-election.lease-name must be set when configuring leader" |
| 293 | + + " election"))); |
| 294 | + leaseNamespace.ifPresent(builder::withLeaseNamespace); |
| 295 | + identity.ifPresent(builder::withIdentity); |
| 296 | + leaseDuration.ifPresent(builder::withLeaseDuration); |
| 297 | + renewDeadline.ifPresent(builder::withRenewDeadline); |
| 298 | + retryPeriod.ifPresent(builder::withRetryPeriod); |
| 299 | + overrider.withLeaderElectionConfiguration(builder.build()); |
| 300 | + }; |
| 301 | + } |
| 302 | + |
229 | 303 | /** |
230 | 304 | * Iterates {@code bindings} and, for each one whose key (optionally prefixed by {@code |
231 | 305 | * keyPrefix}) is present in the {@link ConfigProvider}, accumulates a call to the binding's |
|
0 commit comments