Skip to content

feat: platform condition #2065

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 16 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.4-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.4-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.4-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.4-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 @@ -230,6 +230,7 @@ private static List<DependentResourceSpec> dependentResources(
Utils.instantiate(dependent.readyPostcondition(), Condition.class, context),
Utils.instantiate(dependent.reconcilePrecondition(), Condition.class, context),
Utils.instantiate(dependent.deletePostcondition(), Condition.class, context),
Utils.instantiate(dependent.activationCondition(), Condition.class, context),
eventSourceName);
specsMap.put(dependentName, spec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.CustomResource;
Expand All @@ -19,6 +21,7 @@
import io.javaoperatorsdk.operator.api.monitoring.Metrics;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResourceFactory;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
import io.javaoperatorsdk.operator.processing.dependent.workflow.ManagedWorkflowFactory;

import static io.javaoperatorsdk.operator.api.config.ExecutorServiceManager.newThreadPoolExecutor;
Expand Down Expand Up @@ -335,7 +338,7 @@ default ExecutorServiceManager getExecutorServiceManager() {
* resources are created/updated and match was change to use
* <a href="https://kubernetes.io/docs/reference/using-api/server-side-apply/">Server-Side
* Apply</a> (SSA) by default.
*
* <p>
* SSA based create/update can be still used with the legacy matching, just overriding the match
* method of Kubernetes Dependent Resource.
*
Expand All @@ -345,4 +348,20 @@ default boolean ssaBasedCreateUpdateMatchForDependentResources() {
return true;
}

/**
* Returns the set of default resources for which Server-Side Apply (SSA) will not be used, even
* if it is the default behavior for dependent resources as specified by
* {@link #ssaBasedCreateUpdateMatchForDependentResources()}. The exception to this is in the case
* where the use of SSA is explicitly enabled on the dependent resource directly using
* {@link KubernetesDependent#useSSA()}.
* <p>
* By default, SSA is disabled for {@link ConfigMap} and {@link Secret} resources.
*
* @return The set of resource types for which SSA will not be used
* @since 4.4.0
*/
default Set<Class<? extends HasMetadata>> defaultNonSSAResource() {
return Set.of(ConfigMap.class, Secret.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.api.monitoring.Metrics;

Expand All @@ -35,6 +36,7 @@ public class ConfigurationServiceOverrider {
private Duration cacheSyncTimeout;
private ResourceClassResolver resourceClassResolver;
private Boolean ssaBasedCreateUpdateMatchForDependentResources;
private Set<Class<? extends HasMetadata>> defaultNonSSAResource;

ConfigurationServiceOverrider(ConfigurationService original) {
this.original = original;
Expand Down Expand Up @@ -150,6 +152,12 @@ public ConfigurationServiceOverrider withSSABasedCreateUpdateMatchForDependentRe
return this;
}

public ConfigurationServiceOverrider withDefaultNonSSAResource(
Set<Class<? extends HasMetadata>> defaultNonSSAResource) {
this.defaultNonSSAResource = defaultNonSSAResource;
return this;
}

public ConfigurationService build() {
return new BaseConfigurationService(original.getVersion(), cloner, client) {
@Override
Expand Down Expand Up @@ -256,6 +264,12 @@ public boolean ssaBasedCreateUpdateMatchForDependentResources() {
? ssaBasedCreateUpdateMatchForDependentResources
: super.ssaBasedCreateUpdateMatchForDependentResources();
}

@Override
public Set<Class<? extends HasMetadata>> defaultNonSSAResource() {
return defaultNonSSAResource != null ? defaultNonSSAResource
: super.defaultNonSSAResource();
}
};
}

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 @@ -22,18 +22,21 @@ public class DependentResourceSpec<R, P extends HasMetadata> {

private final Condition<?, ?> deletePostCondition;

private final Condition<?, ?> activationCondition;

private final String useEventSourceWithName;

public DependentResourceSpec(Class<? extends DependentResource<R, P>> dependentResourceClass,
String name, Set<String> dependsOn, Condition<?, ?> readyCondition,
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition,
String useEventSourceWithName) {
Condition<?, ?> activationCondition, String useEventSourceWithName) {
this.dependentResourceClass = dependentResourceClass;
this.name = name;
this.dependsOn = dependsOn;
this.readyCondition = readyCondition;
this.reconcileCondition = reconcileCondition;
this.deletePostCondition = deletePostCondition;
this.activationCondition = activationCondition;
this.useEventSourceWithName = useEventSourceWithName;
}

Expand Down Expand Up @@ -87,6 +90,11 @@ public Condition getDeletePostCondition() {
return deletePostCondition;
}

@SuppressWarnings("rawtypes")
public Condition getActivationCondition() {
return activationCondition;
}

public Optional<String> getUseEventSourceWithName() {
return Optional.ofNullable(useEventSourceWithName);
}
Expand Down
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
Expand Up @@ -10,6 +10,7 @@
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ManagedDependentResourceContext;
import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever;
import io.javaoperatorsdk.operator.processing.event.source.IndexerResourceCache;

public interface Context<P extends HasMetadata> {

Expand Down Expand Up @@ -43,4 +44,12 @@ <R> Optional<R> getSecondaryResource(Class<R> expectedType,
* ExecutorService initialized by framework for workflows. Used for workflow standalone mode.
*/
ExecutorService getWorkflowExecutorService();

/**
* Retrieves the primary resource cache.
*
* @return the {@link IndexerResourceCache} associated with the associated {@link Reconciler} for
* this context
*/
IndexedResourceCache<P> getPrimaryCache();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public <T> Set<T> getSecondaryResources(Class<T> expectedType) {
return getSecondaryResourcesAsStream(expectedType).collect(Collectors.toSet());
}

@Override
public IndexedResourceCache<P> getPrimaryCache() {
return controller.getEventSourceManager().getControllerResourceEventSource();
}

@Override
public <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType) {
return controller.getEventSourceManager().getResourceEventSourcesFor(expectedType).stream()
Expand Down
Loading