Skip to content

feat: allow easier configuration of matcher #2760

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -37,10 +37,18 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
implements ConfiguredDependentResource<KubernetesDependentResourceConfig<R>> {

private static final Logger log = LoggerFactory.getLogger(KubernetesDependentResource.class);

@SuppressWarnings("rawtypes")
private static final SSABasedGenericKubernetesResourceMatcher defaultMatcher =
SSABasedGenericKubernetesResourceMatcher.getInstance();

private final boolean garbageCollected = this instanceof GarbageCollected;
private KubernetesDependentResourceConfig<R> kubernetesDependentResourceConfig;
private volatile Boolean useSSA;

@SuppressWarnings("unchecked")
private SSABasedGenericKubernetesResourceMatcher<R> matcher = defaultMatcher;

public KubernetesDependentResource(Class<R> resourceType) {
this(resourceType, null);
}
Expand All @@ -54,6 +62,13 @@ public void configureWith(KubernetesDependentResourceConfig<R> config) {
this.kubernetesDependentResourceConfig = config;
}

public void setMatcher(SSABasedGenericKubernetesResourceMatcher<R> matcher) {
if (this.matcher != defaultMatcher) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure how big the matcher object can be, but this could be a boolean.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure I'm following

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean you don't need to keep the defaultMatcherin the field if it's only used to verify that the matcher is set only once. It can be a boolean that is flipped when first set so it's only taking a byte (usually) instead of the size of the defaultMatcher object.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, figured it out, I have done that change locally, checking with QOSDK before pushing

throw new IllegalStateException("Can only set a matcher once.");
}
this.matcher = matcher;
}

@SuppressWarnings("unused")
public R create(R desired, P primary, Context<P> context) {
if (useSSA(context)) {
Expand Down Expand Up @@ -111,9 +126,7 @@ public Result<R> match(R actualResource, R desired, P primary, Context<P> contex
final boolean matches;
addMetadata(true, actualResource, desired, primary, context);
if (useSSA(context)) {
matches =
SSABasedGenericKubernetesResourceMatcher.getInstance()
.matches(actualResource, desired, context);
matches = matcher.matches(actualResource, desired, context);
} else {
matches =
GenericKubernetesResourceMatcher.match(desired, actualResource, false, false, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.javaoperatorsdk.operator.api.reconciler.Context;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -39,6 +40,7 @@ void setup() {
when(mockedContext.getClient()).thenReturn(client);

final var configurationService = mock(ConfigurationService.class);
when(configurationService.shouldUseSSA(any(), any(), any())).thenReturn(true);
final var controllerConfiguration = mock(ControllerConfiguration.class);
when(controllerConfiguration.getConfigurationService()).thenReturn(configurationService);
when(controllerConfiguration.fieldManager()).thenReturn("controller");
Expand Down Expand Up @@ -239,17 +241,31 @@ void testSanitizeState_daemonSetWithResources_withMismatch() {
@ParameterizedTest
@ValueSource(booleans = {true, false})
void testCustomMatcher_returnsExpectedMatchBasedOnReadOnlyLabel(boolean readOnly) {
var dr = new ConfigMapDR();
var desiredConfigMap =
loadResource("configmap.empty-owner-reference-desired.yaml", ConfigMap.class);
desiredConfigMap.getData().put("key1", "another value");
var actualConfigMap = loadResource("configmap.empty-owner-reference.yaml", ConfigMap.class);
actualConfigMap.getMetadata().getLabels().put("readonly", Boolean.toString(readOnly));

var matcher = new ReadOnlyAwareMatcher<ConfigMap>();
assertThat(matcher.matches(actualConfigMap, desiredConfigMap, mockedContext))
ConfigMap ignoredPrimary = null;
assertThat(
dr.match(
actualConfigMap,
desiredConfigMap,
ignoredPrimary,
(Context<ConfigMap>) mockedContext)
.matched())
.isEqualTo(readOnly);
}

private static class ConfigMapDR extends KubernetesDependentResource<ConfigMap, ConfigMap> {
public ConfigMapDR() {
super(ConfigMap.class);
setMatcher(new ReadOnlyAwareMatcher<>());
}
}

private static class ReadOnlyAwareMatcher<T extends HasMetadata>
extends SSABasedGenericKubernetesResourceMatcher<T> {
@Override
Expand Down