Skip to content
Draft
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 @@ -447,19 +447,6 @@ default Set<Class<? extends HasMetadata>> defaultNonSSAResource() {
return defaultNonSSAResources();
}

/**
* If a javaoperatorsdk.io/previous annotation should be used so that the operator sdk can detect
* events from its own updates of dependent resources and then filter them.
*
* <p>Disable this if you want to react to your own dependent resource updates
*
* @return if special annotation should be used for dependent resource to filter events
* @since 4.5.0
*/
default boolean previousAnnotationForDependentResourcesEventFiltering() {
return true;
}

/**
* For dependent resources, the framework can add an annotation to filter out events resulting
* directly from the framework's operation. There are, however, some resources that do not follow
Expand Down Expand Up @@ -491,18 +478,16 @@ default Set<Class<? extends HasMetadata>> withPreviousAnnotationForDependentReso

/**
* If the event logic should parse the resourceVersion to determine the ordering of dependent
* resource events. This is typically not needed.
* resource events.
*
* <p>Disabled by default as Kubernetes does not support, and discourages, this interpretation of
* resourceVersions. Enable only if your api server event processing seems to lag the operator
* logic, and you want to further minimize the amount of work done / updates issued by the
* operator.
* <p>Enabled by default as Kubernetes does support this interpretation of resourceVersions.
* Disable only if your api server provides non comparable resource versions..
*
* @return if resource version should be parsed (as integer)
* @since 4.5.0
*/
default boolean parseResourceVersionsForEventFilteringAndCaching() {
return false;
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,6 @@ public Set<Class<? extends HasMetadata>> defaultNonSSAResources() {
defaultNonSSAResource, ConfigurationService::defaultNonSSAResources);
}

@Override
public boolean previousAnnotationForDependentResourcesEventFiltering() {
return overriddenValueOrDefault(
previousAnnotationForDependentResources,
ConfigurationService::previousAnnotationForDependentResourcesEventFiltering);
}

@Override
public boolean parseResourceVersionsForEventFilteringAndCaching() {
return overriddenValueOrDefault(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,21 @@ class DefaultInformerEventSourceConfiguration<R extends HasMetadata>
private final GroupVersionKind groupVersionKind;
private final InformerConfiguration<R> informerConfig;
private final KubernetesClient kubernetesClient;
private final boolean comparableResourceVersions;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add this also to @Informer annotation


protected DefaultInformerEventSourceConfiguration(
GroupVersionKind groupVersionKind,
PrimaryToSecondaryMapper<?> primaryToSecondaryMapper,
SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper,
InformerConfiguration<R> informerConfig,
KubernetesClient kubernetesClient) {
KubernetesClient kubernetesClient,
boolean comparableResourceVersions) {
this.informerConfig = Objects.requireNonNull(informerConfig);
this.groupVersionKind = groupVersionKind;
this.primaryToSecondaryMapper = primaryToSecondaryMapper;
this.secondaryToPrimaryMapper = secondaryToPrimaryMapper;
this.kubernetesClient = kubernetesClient;
this.comparableResourceVersions = comparableResourceVersions;
}

@Override
Expand Down Expand Up @@ -135,6 +138,11 @@ public Optional<GroupVersionKind> getGroupVersionKind() {
public Optional<KubernetesClient> getKubernetesClient() {
return Optional.ofNullable(kubernetesClient);
}

@Override
public boolean parseResourceVersionsForEventFilteringAndCaching() {
return this.comparableResourceVersions;
}
}

@SuppressWarnings({"unused", "UnusedReturnValue"})
Expand All @@ -148,6 +156,7 @@ class Builder<R extends HasMetadata> {
private PrimaryToSecondaryMapper<?> primaryToSecondaryMapper;
private SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private KubernetesClient kubernetesClient;
private boolean comparableResourceVersions = true;

private Builder(Class<R> resourceClass, Class<? extends HasMetadata> primaryResourceClass) {
this(resourceClass, primaryResourceClass, null);
Expand Down Expand Up @@ -285,6 +294,11 @@ public Builder<R> withFieldSelector(FieldSelector fieldSelector) {
return this;
}

public Builder<R> parseResourceVersionsForEventFilteringAndCaching(boolean parse) {
this.comparableResourceVersions = parse;
return this;
}

public void updateFrom(InformerConfiguration<R> informerConfig) {
if (informerConfig != null) {
final var informerConfigName = informerConfig.getName();
Expand Down Expand Up @@ -324,7 +338,10 @@ public InformerEventSourceConfiguration<R> build() {
HasMetadata.getKind(primaryResourceClass),
false)),
config.build(),
kubernetesClient);
kubernetesClient,
comparableResourceVersions);
}
}

boolean parseResourceVersionsForEventFilteringAndCaching();
}
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ public static <P extends HasMetadata> P addFinalizerWithSSA(
}
}

public static int compareResourceVersions(HasMetadata h1, HasMetadata h2) {
return compareResourceVersions(
h1.getMetadata().getResourceVersion(), h2.getMetadata().getResourceVersion());
}

public static int compareResourceVersions(String v1, String v2) {
var v1Length = v1.length();
if (v1Length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
private final boolean garbageCollected = this instanceof GarbageCollected;
private KubernetesDependentResourceConfig<R> kubernetesDependentResourceConfig;
private volatile Boolean useSSA;
private volatile Boolean usePreviousAnnotationForEventFiltering;

public KubernetesDependentResource() {}

Expand All @@ -72,6 +71,27 @@ public void configureWith(KubernetesDependentResourceConfig<R> config) {
this.kubernetesDependentResourceConfig = config;
}

@Override
protected R handleCreate(R desired, P primary, Context<P> context) {
return eventSource()
.orElseThrow()
.updateAndCacheResource(
desired,
context,
toCreate -> KubernetesDependentResource.super.handleCreate(toCreate, primary, context));
}

@Override
protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
return eventSource()
.orElseThrow()
.updateAndCacheResource(
desired,
context,
toUpdate ->
KubernetesDependentResource.super.handleUpdate(actual, toUpdate, primary, context));
}

@SuppressWarnings("unused")
public R create(R desired, P primary, Context<P> context) {
if (useSSA(context)) {
Expand Down Expand Up @@ -158,14 +178,6 @@ protected void addMetadata(
} else {
annotations.remove(InformerEventSource.PREVIOUS_ANNOTATION_KEY);
}
} else if (usePreviousAnnotation(context)) { // set a new one
eventSource()
.orElseThrow()
.addPreviousAnnotation(
Optional.ofNullable(actualResource)
.map(r -> r.getMetadata().getResourceVersion())
.orElse(null),
target);
}
addReferenceHandlingMetadata(target, primary);
}
Expand All @@ -181,22 +193,6 @@ protected boolean useSSA(Context<P> context) {
return useSSA;
}

private boolean usePreviousAnnotation(Context<P> context) {
if (usePreviousAnnotationForEventFiltering == null) {
usePreviousAnnotationForEventFiltering =
context
.getControllerConfiguration()
.getConfigurationService()
.previousAnnotationForDependentResourcesEventFiltering()
&& !context
.getControllerConfiguration()
.getConfigurationService()
.withPreviousAnnotationForDependentResourcesBlocklist()
.contains(this.resourceType());
}
return usePreviousAnnotationForEventFiltering;
}

@Override
protected void handleDelete(P primary, R secondary, Context<P> context) {
if (secondary != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ public class ControllerEventSource<T extends HasMetadata>

@SuppressWarnings({"unchecked", "rawtypes"})
public ControllerEventSource(Controller<T> controller) {
super(NAME, controller.getCRClient(), controller.getConfiguration(), false);
super(
NAME,
controller.getCRClient(),
controller.getConfiguration(),
controller
.getConfiguration()
.getConfigurationService()
.parseResourceVersionsForEventFilteringAndCaching());
this.controller = controller;

final var config = controller.getConfiguration();
Expand Down
Loading