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 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 @@ -37,10 +37,15 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
implements ConfiguredDependentResource<KubernetesDependentResourceConfig<R>> {

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

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

private SSABasedGenericKubernetesResourceMatcher<R> matcher =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be this rather part of the configuration?

The current approach is that name and class and config are the params and configuration contains everything else. This might go there just as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Wanted to keep it simple first but it definitely could go on the config.

SSABasedGenericKubernetesResourceMatcher.getInstance();
private volatile boolean matcherSet = false;

public KubernetesDependentResource(Class<R> resourceType) {
this(resourceType, null);
}
Expand All @@ -49,11 +54,26 @@ public KubernetesDependentResource(Class<R> resourceType, String name) {
super(resourceType, name);
}

public KubernetesDependentResource(
Class<R> resourceType, String name, SSABasedGenericKubernetesResourceMatcher<R> matcher) {
this(resourceType, name);
this.matcher = matcher;
matcherSet = true;
}

@Override
public void configureWith(KubernetesDependentResourceConfig<R> config) {
this.kubernetesDependentResourceConfig = config;
}

public void setMatcher(SSABasedGenericKubernetesResourceMatcher<R> matcher) {
if (matcherSet) {
throw new IllegalStateException("Can only set a matcher once.");
}
this.matcher = matcher;
matcherSet = true;
}

@SuppressWarnings("unused")
public R create(R desired, P primary, Context<P> context) {
if (useSSA(context)) {
Expand Down Expand Up @@ -111,9 +131,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,30 @@ 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, "foo", new ReadOnlyAwareMatcher<>());
}
}

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