-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into generalized_match
- Loading branch information
Showing
8 changed files
with
179 additions
and
56 deletions.
There are no files selected for viewing
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
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
61 changes: 61 additions & 0 deletions
61
.../main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfigurationBuilder.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,61 @@ | ||
package io.javaoperatorsdk.operator.api.config; | ||
|
||
import java.time.Duration; | ||
|
||
import io.fabric8.kubernetes.client.extended.leaderelection.LeaderCallbacks; | ||
|
||
import static io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration.*; | ||
|
||
public final class LeaderElectionConfigurationBuilder { | ||
|
||
private String leaseName; | ||
private String leaseNamespace; | ||
private String identity; | ||
private Duration leaseDuration = LEASE_DURATION_DEFAULT_VALUE; | ||
private Duration renewDeadline = RENEW_DEADLINE_DEFAULT_VALUE; | ||
private Duration retryPeriod = RETRY_PERIOD_DEFAULT_VALUE; | ||
private LeaderCallbacks leaderCallbacks; | ||
|
||
private LeaderElectionConfigurationBuilder(String leaseName) { | ||
this.leaseName = leaseName; | ||
} | ||
|
||
public static LeaderElectionConfigurationBuilder aLeaderElectionConfiguration(String leaseName) { | ||
return new LeaderElectionConfigurationBuilder(leaseName); | ||
} | ||
|
||
public LeaderElectionConfigurationBuilder withLeaseNamespace(String leaseNamespace) { | ||
this.leaseNamespace = leaseNamespace; | ||
return this; | ||
} | ||
|
||
public LeaderElectionConfigurationBuilder withIdentity(String identity) { | ||
this.identity = identity; | ||
return this; | ||
} | ||
|
||
public LeaderElectionConfigurationBuilder withLeaseDuration(Duration leaseDuration) { | ||
this.leaseDuration = leaseDuration; | ||
return this; | ||
} | ||
|
||
public LeaderElectionConfigurationBuilder withRenewDeadline(Duration renewDeadline) { | ||
this.renewDeadline = renewDeadline; | ||
return this; | ||
} | ||
|
||
public LeaderElectionConfigurationBuilder withRetryPeriod(Duration retryPeriod) { | ||
this.retryPeriod = retryPeriod; | ||
return this; | ||
} | ||
|
||
public LeaderElectionConfigurationBuilder withLeaderCallbacks(LeaderCallbacks leaderCallbacks) { | ||
this.leaderCallbacks = leaderCallbacks; | ||
return this; | ||
} | ||
|
||
public LeaderElectionConfiguration build() { | ||
return new LeaderElectionConfiguration(leaseName, leaseNamespace, leaseDuration, renewDeadline, | ||
retryPeriod, identity, leaderCallbacks); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...ork-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/IndexDiscriminator.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,50 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; | ||
|
||
/** | ||
* Uses a custom index of {@link InformerEventSource} to access the target resource. The index needs | ||
* to be explicitly created when the event source is defined. This approach improves the performance | ||
* to access the resource. | ||
*/ | ||
public class IndexDiscriminator<R extends HasMetadata, P extends HasMetadata> | ||
implements ResourceDiscriminator<R, P> { | ||
|
||
private final String indexName; | ||
private final String eventSourceName; | ||
private final Function<P, String> keyMapper; | ||
|
||
public IndexDiscriminator(String indexName, Function<P, String> keyMapper) { | ||
this(indexName, null, keyMapper); | ||
} | ||
|
||
public IndexDiscriminator(String indexName, String eventSourceName, | ||
Function<P, String> keyMapper) { | ||
this.indexName = indexName; | ||
this.eventSourceName = eventSourceName; | ||
this.keyMapper = keyMapper; | ||
} | ||
|
||
@Override | ||
public Optional<R> distinguish(Class<R> resource, | ||
P primary, | ||
Context<P> context) { | ||
|
||
InformerEventSource<R, P> eventSource = | ||
(InformerEventSource<R, P>) context | ||
.eventSourceRetriever() | ||
.getResourceEventSourceFor(resource, eventSourceName); | ||
var resources = eventSource.byIndex(indexName, keyMapper.apply(primary)); | ||
if (resources.isEmpty()) { | ||
return Optional.empty(); | ||
} else if (resources.size() > 1) { | ||
throw new IllegalStateException("More than one resource found"); | ||
} else { | ||
return Optional.of(resources.get(0)); | ||
} | ||
} | ||
} |
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
41 changes: 0 additions & 41 deletions
41
...c/test/java/io/javaoperatorsdk/operator/sample/indexdiscriminator/IndexDiscriminator.java
This file was deleted.
Oops, something went wrong.
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
14 changes: 14 additions & 0 deletions
14
...st/java/io/javaoperatorsdk/operator/sample/indexdiscriminator/TestIndexDiscriminator.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,14 @@ | ||
package io.javaoperatorsdk.operator.sample.indexdiscriminator; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.javaoperatorsdk.operator.api.reconciler.IndexDiscriminator; | ||
|
||
import static io.javaoperatorsdk.operator.sample.indexdiscriminator.IndexDiscriminatorTestReconciler.configMapKeyFromPrimary; | ||
|
||
public class TestIndexDiscriminator | ||
extends IndexDiscriminator<ConfigMap, IndexDiscriminatorTestCustomResource> { | ||
|
||
public TestIndexDiscriminator(String indexName, String nameSuffix) { | ||
super(indexName, p -> configMapKeyFromPrimary(p, nameSuffix)); | ||
} | ||
} |