Skip to content

fix: fix lastAttempt value on first try when maxAttempt is zero #1397

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 15 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import io.javaoperatorsdk.operator.OperatorException;
import io.javaoperatorsdk.operator.ReconcilerUtils;
import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
import io.javaoperatorsdk.operator.api.reconciler.Constants;
import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
Expand All @@ -31,6 +30,7 @@
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
import io.javaoperatorsdk.operator.processing.retry.NoRetry;
import io.javaoperatorsdk.operator.processing.retry.Retry;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.DEFAULT_NAMESPACES_SET;
Expand Down Expand Up @@ -151,10 +151,14 @@ public RateLimiter getRateLimiter() {
}

@Override
public Retry getRetry() {
public Optional<Retry> getRetry() {
final Class<? extends Retry> retryClass = annotation.retry();
return Utils.instantiateAndConfigureIfNeeded(retryClass, Retry.class,
Utils.contextFor(this, null, null), this::configureFromAnnotatedReconciler);
if (NoRetry.class.equals(retryClass)) {
return Optional.empty();
}

return Optional.of(Utils.instantiateAndConfigureIfNeeded(retryClass, Retry.class,
Utils.contextFor(this, null, null), this::configureFromAnnotatedReconciler));
}


Expand Down Expand Up @@ -221,12 +225,15 @@ public List<DependentResourceSpec> getDependentResources() {
throw new IllegalArgumentException(
"A DependentResource named '" + name + "' already exists: " + spec);
}
var eventSourceName = dependent.useEventSourceWithName();
eventSourceName = Constants.NO_VALUE_SET.equals(eventSourceName) ? null : eventSourceName;
final var context = Utils.contextFor(this, dependentType, null);
spec = new DependentResourceSpec(dependentType, config, name,
Set.of(dependent.dependsOn()),
Utils.instantiate(dependent.readyPostcondition(), Condition.class, context),
Utils.instantiate(dependent.reconcilePrecondition(), Condition.class, context),
Utils.instantiate(dependent.deletePostcondition(), Condition.class, context));
Utils.instantiate(dependent.deletePostcondition(), Condition.class, context),
eventSourceName);
specsMap.put(name, spec);
}

Expand Down Expand Up @@ -256,17 +263,16 @@ private Object createKubernetesResourceConfig(Class<? extends DependentResource>
OnUpdateFilter<? extends HasMetadata> onUpdateFilter = null;
OnDeleteFilter<? extends HasMetadata> onDeleteFilter = null;
GenericFilter<? extends HasMetadata> genericFilter = null;
ResourceDiscriminator<?, ? extends HasMetadata> resourceDiscriminator = null;
if (kubeDependent != null) {
if (!Arrays.equals(KubernetesDependent.DEFAULT_NAMESPACES,
kubeDependent.namespaces())) {
namespaces = Set.of(kubeDependent.namespaces());
configuredNS = true;
}

final var fromAnnotation = kubeDependent.labelSelector();
labelSelector = Constants.NO_VALUE_SET.equals(fromAnnotation) ? null : fromAnnotation;


final var context =
Utils.contextFor(this, dependentType, null);
onAddFilter = Utils.instantiate(kubeDependent.onAddFilter(), OnAddFilter.class, context);
Expand All @@ -276,10 +282,15 @@ private Object createKubernetesResourceConfig(Class<? extends DependentResource>
Utils.instantiate(kubeDependent.onDeleteFilter(), OnDeleteFilter.class, context);
genericFilter =
Utils.instantiate(kubeDependent.genericFilter(), GenericFilter.class, context);

resourceDiscriminator =
Utils.instantiate(kubeDependent.resourceDiscriminator(),
ResourceDiscriminator.class, context);
}

config =
new KubernetesDependentResourceConfig(namespaces, labelSelector, configuredNS, onAddFilter,
new KubernetesDependentResourceConfig(namespaces, labelSelector, configuredNS,
resourceDiscriminator, onAddFilter,
onUpdateFilter, onDeleteFilter, genericFilter);

return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ default boolean isGenerationAware() {

String getAssociatedReconcilerClassName();

default Retry getRetry() {
default Optional<Retry> getRetry() {
final var configuration = getRetryConfiguration();
return !RetryConfiguration.DEFAULT.equals(configuration)
return Optional.of(!RetryConfiguration.DEFAULT.equals(configuration)
? GenericRetry.fromConfiguration(configuration)
: GenericRetry.DEFAULT; // NOSONAR
: GenericRetry.DEFAULT); // NOSONAR
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ControllerConfigurationOverrider<R extends HasMetadata> {
private String finalizer;
private boolean generationAware;
private Set<String> namespaces;
private Retry retry;
private Optional<Retry> retry;
private String labelSelector;
private ResourceEventFilter<R> customResourcePredicate;
private final ControllerConfiguration<R> original;
Expand Down Expand Up @@ -113,12 +113,17 @@ public ControllerConfigurationOverrider<R> watchingAllNamespaces() {
*/
@Deprecated(forRemoval = true)
public ControllerConfigurationOverrider<R> withRetry(RetryConfiguration retry) {
this.retry = GenericRetry.fromConfiguration(retry);
this.retry = Optional.of(GenericRetry.fromConfiguration(retry));
return this;
}

public ControllerConfigurationOverrider<R> withRetry(Retry retry) {
this.retry = retry;
this.retry = Optional.of(retry);
return this;
}

public ControllerConfigurationOverrider<R> withoutRetry() {
this.retry = Optional.empty();
return this;
}

Expand Down Expand Up @@ -174,7 +179,7 @@ private void replaceConfig(String name, Object newConfig, DependentResourceSpec<
namedDependentResourceSpecs.put(name,
new DependentResourceSpec<>(current.getDependentResourceClass(), newConfig, name,
current.getDependsOn(), current.getReadyCondition(), current.getReconcileCondition(),
current.getDeletePostCondition()));
current.getDeletePostCondition(), current.getUseEventSourceWithName().orElse(null)));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -220,7 +225,8 @@ public ControllerConfiguration<R> build() {
KubernetesDependentResourceConfig c) {
return new DependentResourceSpec(spec.getDependentResourceClass(),
c.setNamespaces(namespaces), name, spec.getDependsOn(), spec.getReadyCondition(),
spec.getReconcileCondition(), spec.getDeletePostCondition());
spec.getReconcileCondition(), spec.getDeletePostCondition(),
(String) spec.getUseEventSourceWithName().orElse(null));
}

public static <R extends HasMetadata> ControllerConfigurationOverrider<R> override(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class DefaultControllerConfiguration<R extends HasMetadata>
private final String crdName;
private final String finalizer;
private final boolean generationAware;
private final Retry retry;
private final Optional<Retry> retry;
private final ResourceEventFilter<R> resourceEventFilter;
private final List<DependentResourceSpec> dependents;
private final Duration reconciliationMaxInterval;
Expand All @@ -40,7 +40,7 @@ public DefaultControllerConfiguration(
String finalizer,
boolean generationAware,
Set<String> namespaces,
Retry retry,
Optional<Retry> retry,
String labelSelector,
ResourceEventFilter<R> resourceEventFilter,
Class<R> resourceClass,
Expand Down Expand Up @@ -93,7 +93,7 @@ public String getAssociatedReconcilerClassName() {
}

@Override
public Retry getRetry() {
public Optional<Retry> getRetry() {
return retry;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ public class DependentResourceSpec<T extends DependentResource<?, ?>, C> {

private final Condition<?, ?> deletePostCondition;

private final String useEventSourceWithName;

public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig,
String name, Set<String> dependsOn, Condition<?, ?> readyCondition,
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition) {
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition,
String useEventSourceWithName) {
this.dependentResourceClass = dependentResourceClass;
this.dependentResourceConfig = dependentResourceConfig;
this.name = name;
this.dependsOn = dependsOn;
this.readyCondition = readyCondition;
this.reconcileCondition = reconcileCondition;
this.deletePostCondition = deletePostCondition;
this.useEventSourceWithName = useEventSourceWithName;
}

public Class<T> getDependentResourceClass() {
Expand Down Expand Up @@ -89,4 +93,8 @@ public Condition getReconcileCondition() {
public Condition getDeletePostCondition() {
return deletePostCondition;
}

public Optional<String> getUseEventSourceWithName() {
return Optional.ofNullable(useEventSourceWithName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public <P extends HasMetadata> InformerConfigurationBuilder<R> withNamespacesInh
}

/**
* Whether or not the associated informer should track changes made to the parent
* Whether the associated informer should track changes made to the parent
* {@link io.javaoperatorsdk.operator.processing.Controller}'s namespaces configuration.
*
* @param followChanges {@code true} to reconfigure the associated informer when the parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ManagedDependentResourceContext;
import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever;

public interface Context<P extends HasMetadata> {

Optional<RetryInfo> getRetryInfo();

default <T> Optional<T> getSecondaryResource(Class<T> expectedType) {
return getSecondaryResource(expectedType, null);
default <R> Optional<R> getSecondaryResource(Class<R> expectedType) {
return getSecondaryResource(expectedType, (String) null);
}

<T> Set<T> getSecondaryResources(Class<T> expectedType);
<R> Set<R> getSecondaryResources(Class<R> expectedType);

<T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventSourceName);
@Deprecated(forRemoval = true)
<R> Optional<R> getSecondaryResource(Class<R> expectedType, String eventSourceName);

<R> Optional<R> getSecondaryResource(Class<R> expectedType,
ResourceDiscriminator<R, P> discriminator);

ControllerConfiguration<P> getControllerConfiguration();

ManagedDependentResourceContext managedDependentResourceContext();

EventSourceRetriever<P> eventSourceRetriever();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DefaultManagedDependentResourceContext;
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ManagedDependentResourceContext;
import io.javaoperatorsdk.operator.processing.Controller;
import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever;

public class DefaultContext<P extends HasMetadata> implements Context<P> {

Expand Down Expand Up @@ -47,6 +48,12 @@ public <T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventS
.getSecondaryResource(primaryResource);
}

@Override
public <R> Optional<R> getSecondaryResource(Class<R> expectedType,
ResourceDiscriminator<R, P> discriminator) {
return discriminator.distinguish(expectedType, primaryResource, this);
}

@Override
public ControllerConfiguration<P> getControllerConfiguration() {
return controllerConfiguration;
Expand All @@ -57,6 +64,11 @@ public ManagedDependentResourceContext managedDependentResourceContext() {
return defaultManagedDependentResourceContext;
}

@Override
public EventSourceRetriever<P> eventSourceRetriever() {
return controller.getEventSourceManager();
}

public DefaultContext<P> setRetryInfo(RetryInfo retryInfo) {
this.retryInfo = retryInfo;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.javaoperatorsdk.operator.api.reconciler;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;

/**
* An interface that a {@link Reconciler} can implement to have the SDK register the provided
Expand Down Expand Up @@ -39,6 +43,22 @@ static Map<String, EventSource> nameEventSources(EventSource... eventSources) {
return eventSourceMap;
}

@SuppressWarnings("unchecked,rawtypes")
static <K extends HasMetadata> Map<String, EventSource> nameEventSourcesFromDependentResource(
EventSourceContext<K> context, DependentResource... dependentResources) {

if (dependentResources != null) {
Map<String, EventSource> eventSourceMap = new HashMap<>(dependentResources.length);
for (DependentResource dependentResource : dependentResources) {
Optional<ResourceEventSource> es = dependentResource.eventSource(context);
es.ifPresent(e -> eventSourceMap.put(generateNameFor(e), e));
}
return eventSourceMap;
} else {
return Collections.emptyMap();
}
}

/**
* This is for the use case when the event sources are not access explicitly by name in the
* reconciler.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.javaoperatorsdk.operator.api.reconciler;

import java.util.Optional;

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

public interface ResourceDiscriminator<R, P extends HasMetadata> {

Optional<R> distinguish(Class<R> resource, P primary, Context<P> context);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.ResourceID;

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

private final Function<P, ResourceID> mapper;

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

@Override
public Optional<R> distinguish(Class<R> resource, P primary, Context<P> context) {
var resourceID = mapper.apply(primary);
return context.getSecondaryResources(resource).stream()
.filter(resourceID::isSameResource)
.findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.javaoperatorsdk.operator.api.reconciler.dependent;

import io.javaoperatorsdk.operator.api.reconciler.Constants;
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.NO_VALUE_SET;
Expand Down Expand Up @@ -57,4 +58,13 @@
* one can be
*/
String[] dependsOn() default {};

/**
* Setting here a name of the event source means that dependent resource will use an event source
* registered with that name. So won't create one. This is helpful if more dependent resources
* created for the same type, and want to share a common event source.
*
* @return event source name (if any) provided by the dependent resource should be used.
*/
String useEventSourceWithName() default NO_VALUE_SET;
}
Loading