Skip to content

Commit

Permalink
feat: expose logic to determine whether a kube dependent resource use…
Browse files Browse the repository at this point in the history
…s SSA or not (#2516)

Signed-off-by: Chris Laprun <[email protected]>
  • Loading branch information
metacosm authored Aug 27, 2024
1 parent f8e965c commit 7b0870a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
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.kubernetes.KubernetesDependentResource;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.ResourceUpdaterMatcher;
import io.javaoperatorsdk.operator.processing.dependent.workflow.ManagedWorkflowFactory;

/** An interface from which to retrieve configuration information. */
Expand Down Expand Up @@ -358,6 +361,32 @@ default boolean ssaBasedCreateUpdateMatchForDependentResources() {
return true;
}

/**
* This is mostly useful as an integration point for downstream projects to be able to reuse the
* logic used to determine whether a given {@link KubernetesDependentResource} should use SSA or
* not.
*
* @param dependentResource the {@link KubernetesDependentResource} under consideration
* @return {@code true} if SSA should be used for
* @param <R> the dependent resource type
* @param <P> the primary resource type
* @since 4.9.4
*/
default <R extends HasMetadata, P extends HasMetadata> boolean shouldUseSSA(
KubernetesDependentResource<R, P> dependentResource) {
if (dependentResource instanceof ResourceUpdaterMatcher) {
return false;
}
Optional<Boolean> useSSAConfig = dependentResource.configuration()
.flatMap(KubernetesDependentResourceConfig::useSSA);
// don't use SSA for certain resources by default, only if explicitly overriden
if (useSSAConfig.isEmpty()
&& defaultNonSSAResource().contains(dependentResource.resourceType())) {
return false;
}
return useSSAConfig.orElse(ssaBasedCreateUpdateMatchForDependentResources());
}

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class GenericKubernetesDependentResource<P extends HasMetadata>
extends KubernetesDependentResource<GenericKubernetesResource, P> {

private GroupVersionKind groupVersionKind;
private final GroupVersionKind groupVersionKind;

public GenericKubernetesDependentResource(GroupVersionKind groupVersionKind) {
super(GenericKubernetesResource.class);
Expand All @@ -19,6 +19,7 @@ protected InformerConfiguration.InformerConfigurationBuilder<GenericKubernetesRe
return InformerConfiguration.from(groupVersionKind);
}

@SuppressWarnings("unused")
public GroupVersionKind getGroupVersionKind() {
return groupVersionKind;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
private final ResourceUpdaterMatcher<R> updaterMatcher;
private final boolean garbageCollected = this instanceof GarbageCollected;
private KubernetesDependentResourceConfig<R> kubernetesDependentResourceConfig;

private final boolean usingCustomResourceUpdateMatcher;
private volatile Boolean useSSA;

@SuppressWarnings("unchecked")
public KubernetesDependentResource(Class<R> resourceType) {
super(resourceType);

usingCustomResourceUpdateMatcher = this instanceof ResourceUpdaterMatcher;
updaterMatcher = usingCustomResourceUpdateMatcher
updaterMatcher = this instanceof ResourceUpdaterMatcher
? (ResourceUpdaterMatcher<R>) this
: GenericResourceUpdaterMatcher.updaterMatcherFor(resourceType);
}
Expand Down Expand Up @@ -194,18 +192,10 @@ protected void addMetadata(boolean forMatch, R actualResource, final R target, P
}

protected boolean useSSA(Context<P> context) {
if (usingCustomResourceUpdateMatcher) {
return false;
}
Optional<Boolean> useSSAConfig =
configuration().flatMap(KubernetesDependentResourceConfig::useSSA);
var configService = context.getControllerConfiguration().getConfigurationService();
// don't use SSA for certain resources by default, only if explicitly overriden
if (useSSAConfig.isEmpty() && configService.defaultNonSSAResource().contains(resourceType())) {
return false;
if (useSSA == null) {
useSSA = context.getControllerConfiguration().getConfigurationService().shouldUseSSA(this);
}
return useSSAConfig.orElse(context.getControllerConfiguration().getConfigurationService()
.ssaBasedCreateUpdateMatchForDependentResources());
return useSSA;
}

private boolean usePreviousAnnotation(Context<P> context) {
Expand Down

0 comments on commit 7b0870a

Please sign in to comment.