Skip to content

feat: access controller event source #2051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion caffeine-bounded-cache-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>java-operator-sdk</artifactId>
<groupId>io.javaoperatorsdk</groupId>
<version>4.4.3-SNAPSHOT</version>
<version>4.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion micrometer-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>java-operator-sdk</artifactId>
<groupId>io.javaoperatorsdk</groupId>
<version>4.4.3-SNAPSHOT</version>
<version>4.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion operator-framework-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.javaoperatorsdk</groupId>
<artifactId>operator-framework-bom</artifactId>
<version>4.4.3-SNAPSHOT</version>
<version>4.5.0-SNAPSHOT</version>
<name>Operator SDK - Bill of Materials</name>
<packaging>pom</packaging>
<description>Java SDK for implementing Kubernetes operators</description>
Expand Down
2 changes: 1 addition & 1 deletion operator-framework-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>java-operator-sdk</artifactId>
<version>4.4.3-SNAPSHOT</version>
<version>4.5.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,28 @@ private void init(LeaderElectionConfiguration config) {
config.getLeaseDuration(),
config.getRenewDeadline(),
config.getRetryPeriod(),
leaderCallbacks(),
leaderCallbacks(config),
true,
config.getLeaseName()))
.build();
}



private LeaderCallbacks leaderCallbacks() {
private LeaderCallbacks leaderCallbacks(LeaderElectionConfiguration config) {
return new LeaderCallbacks(
this::startLeading,
this::stopLeading,
leader -> log.info("New leader with identity: {}", leader));
() -> {
config.getLeaderCallbacks().ifPresent(LeaderCallbacks::onStartLeading);
LeaderElectionManager.this.startLeading();
},
() -> {
config.getLeaderCallbacks().ifPresent(LeaderCallbacks::onStopLeading);
LeaderElectionManager.this.stopLeading();
},
leader -> {
config.getLeaderCallbacks().ifPresent(cb -> cb.onNewLeader(leader));
log.info("New leader with identity: {}", leader);
});
}

private void startLeading() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.time.Duration;
import java.util.Optional;

import io.fabric8.kubernetes.client.extended.leaderelection.LeaderCallbacks;

public class LeaderElectionConfiguration {

public static final Duration LEASE_DURATION_DEFAULT_VALUE = Duration.ofSeconds(15);
Expand All @@ -17,13 +19,15 @@ public class LeaderElectionConfiguration {
private final Duration renewDeadline;
private final Duration retryPeriod;

private final LeaderCallbacks leaderCallbacks;

public LeaderElectionConfiguration(String leaseName, String leaseNamespace, String identity) {
this(
leaseName,
leaseNamespace,
LEASE_DURATION_DEFAULT_VALUE,
RENEW_DEADLINE_DEFAULT_VALUE,
RETRY_PERIOD_DEFAULT_VALUE, identity);
RETRY_PERIOD_DEFAULT_VALUE, identity, null);
}

public LeaderElectionConfiguration(String leaseName, String leaseNamespace) {
Expand All @@ -32,7 +36,7 @@ public LeaderElectionConfiguration(String leaseName, String leaseNamespace) {
leaseNamespace,
LEASE_DURATION_DEFAULT_VALUE,
RENEW_DEADLINE_DEFAULT_VALUE,
RETRY_PERIOD_DEFAULT_VALUE, null);
RETRY_PERIOD_DEFAULT_VALUE, null, null);
}

public LeaderElectionConfiguration(String leaseName) {
Expand All @@ -41,7 +45,7 @@ public LeaderElectionConfiguration(String leaseName) {
null,
LEASE_DURATION_DEFAULT_VALUE,
RENEW_DEADLINE_DEFAULT_VALUE,
RETRY_PERIOD_DEFAULT_VALUE, null);
RETRY_PERIOD_DEFAULT_VALUE, null, null);
}

public LeaderElectionConfiguration(
Expand All @@ -50,7 +54,7 @@ public LeaderElectionConfiguration(
Duration leaseDuration,
Duration renewDeadline,
Duration retryPeriod) {
this(leaseName, leaseNamespace, leaseDuration, renewDeadline, retryPeriod, null);
this(leaseName, leaseNamespace, leaseDuration, renewDeadline, retryPeriod, null, null);
}

public LeaderElectionConfiguration(
Expand All @@ -59,13 +63,15 @@ public LeaderElectionConfiguration(
Duration leaseDuration,
Duration renewDeadline,
Duration retryPeriod,
String identity) {
String identity,
LeaderCallbacks leaderCallbacks) {
this.leaseName = leaseName;
this.leaseNamespace = leaseNamespace;
this.leaseDuration = leaseDuration;
this.renewDeadline = renewDeadline;
this.retryPeriod = retryPeriod;
this.identity = identity;
this.leaderCallbacks = leaderCallbacks;
}

public Optional<String> getLeaseNamespace() {
Expand All @@ -91,4 +97,8 @@ public Duration getRetryPeriod() {
public Optional<String> getIdentity() {
return Optional.ofNullable(identity);
}

public Optional<LeaderCallbacks> getLeaderCallbacks() {
return Optional.ofNullable(leaderCallbacks);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class BaseControl<T extends BaseControl<T>> {
private Long scheduleDelay = null;

public T rescheduleAfter(long delay) {
this.scheduleDelay = delay;
rescheduleAfter(Duration.ofMillis(delay));
return (T) this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.javaoperatorsdk.operator.api.reconciler;

import java.time.Duration;
import java.util.Optional;

import io.fabric8.kubernetes.api.model.HasMetadata;

public class ErrorStatusUpdateControl<P extends HasMetadata> {
public class ErrorStatusUpdateControl<P extends HasMetadata>
extends BaseControl<ErrorStatusUpdateControl<P>> {

private final P resource;
private final boolean patch;
private boolean noRetry = false;


public static <T extends HasMetadata> ErrorStatusUpdateControl<T> patchStatus(T resource) {
return new ErrorStatusUpdateControl<>(resource, true);
}
Expand Down Expand Up @@ -49,4 +50,16 @@ public boolean isNoRetry() {
public boolean isPatch() {
return patch;
}

/**
* If re-scheduled using this method, it is not considered as retry, it effectively cancels retry.
*
* @param delay for next execution
* @return ErrorStatusUpdateControl
*/
@Override
public ErrorStatusUpdateControl<P> rescheduleAfter(Duration delay) {
withNoRetry();
return super.rescheduleAfter(delay);
}
}
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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,41 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.Cache;

public class ResourceIDMatcherDiscriminator<R extends HasMetadata, P extends HasMetadata>
implements ResourceDiscriminator<R, P> {


private final String eventSourceName;
private final Function<P, ResourceID> mapper;

public ResourceIDMatcherDiscriminator(Function<P, ResourceID> mapper) {
this(null, mapper);
}

public ResourceIDMatcherDiscriminator(String eventSourceName, Function<P, ResourceID> mapper) {
this.eventSourceName = eventSourceName;
this.mapper = mapper;
}

@SuppressWarnings("unchecked")
@Override
public Optional<R> distinguish(Class<R> resource, P primary, Context<P> context) {
var resourceID = mapper.apply(primary);
return context.getSecondaryResourcesAsStream(resource)
.filter(resourceID::isSameResource)
.findFirst();
if (eventSourceName != null) {
return ((Cache<R>) context.eventSourceRetriever().getResourceEventSourceFor(resource,
eventSourceName))
.get(resourceID);
} else {
var eventSources = context.eventSourceRetriever().getResourceEventSourcesFor(resource);
if (eventSources.size() == 1) {
return ((Cache<R>) eventSources.get(0)).get(resourceID);
} else {
return context.getSecondaryResourcesAsStream(resource)
.filter(resourceID::isSameResource)
.findFirst();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface DependentResource<R, P extends HasMetadata> {
* @param eventSourceContext context of event source initialization
* @return an optional event source
*/
default Optional<ResourceEventSource<R, P>> eventSource(
default Optional<? extends ResourceEventSource<R, P>> eventSource(
EventSourceContext<P> eventSourceContext) {
return Optional.empty();
}
Expand Down

This file was deleted.

Loading