-
Notifications
You must be signed in to change notification settings - Fork 219
feat: use a predicate to select the custom resource for which a reconcile should be triggered #454
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...ator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/Event.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
package io.javaoperatorsdk.operator.processing.event; | ||
|
||
import io.fabric8.kubernetes.client.CustomResource; | ||
import java.util.function.Predicate; | ||
|
||
public interface Event { | ||
|
||
/** | ||
* @return the UID of the the {@link CustomResource} for which a reconcile loop should be | ||
* triggered. | ||
* @deprecated use {@link #getCustomResourcesSelector()} | ||
*/ | ||
@Deprecated | ||
String getRelatedCustomResourceUid(); | ||
|
||
/** | ||
* The selector used to determine the {@link CustomResource} for which a reconcile loop should be | ||
* triggered. | ||
*/ | ||
Predicate<CustomResource> getCustomResourcesSelector(); | ||
|
||
/** @return the {@link EventSource} that has generated the event. */ | ||
EventSource getEventSource(); | ||
} |
125 changes: 125 additions & 0 deletions
125
...core/src/test/java/io/javaoperatorsdk/operator/processing/CustomResourceSelectorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package io.javaoperatorsdk.operator.processing; | ||
|
||
import static io.javaoperatorsdk.operator.TestUtils.testCustomResource; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.doCallRealMethod; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.timeout; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.fabric8.kubernetes.client.Watcher; | ||
import io.javaoperatorsdk.operator.api.config.ConfigurationService; | ||
import io.javaoperatorsdk.operator.processing.event.AbstractEvent; | ||
import io.javaoperatorsdk.operator.processing.event.DefaultEventSourceManager; | ||
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent; | ||
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
class CustomResourceSelectorTest { | ||
|
||
public static final int FAKE_CONTROLLER_EXECUTION_DURATION = 250; | ||
public static final int SEPARATE_EXECUTION_TIMEOUT = 450; | ||
|
||
private final EventDispatcher eventDispatcherMock = mock(EventDispatcher.class); | ||
private final CustomResourceCache customResourceCache = new CustomResourceCache(); | ||
|
||
private final DefaultEventSourceManager defaultEventSourceManagerMock = | ||
mock(DefaultEventSourceManager.class); | ||
|
||
private final DefaultEventHandler defaultEventHandler = | ||
new DefaultEventHandler( | ||
eventDispatcherMock, | ||
"Test", | ||
null, | ||
ConfigurationService.DEFAULT_RECONCILIATION_THREADS_NUMBER); | ||
|
||
@BeforeEach | ||
public void setup() { | ||
defaultEventHandler.setEventSourceManager(defaultEventSourceManagerMock); | ||
|
||
// todo: remove | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above… |
||
when(defaultEventSourceManagerMock.getCache()).thenReturn(customResourceCache); | ||
doCallRealMethod().when(defaultEventSourceManagerMock).getLatestResource(any()); | ||
doCallRealMethod().when(defaultEventSourceManagerMock).getLatestResources(any()); | ||
doCallRealMethod().when(defaultEventSourceManagerMock).getLatestResourceUids(any()); | ||
doCallRealMethod().when(defaultEventSourceManagerMock).cacheResource(any(), any()); | ||
doAnswer( | ||
invocation -> { | ||
final var resourceId = (String) invocation.getArgument(0); | ||
customResourceCache.cleanup(resourceId); | ||
return null; | ||
}) | ||
.when(defaultEventSourceManagerMock) | ||
.cleanup(any()); | ||
} | ||
|
||
@Test | ||
public void dispatchEventsWithPredicate() { | ||
TestCustomResource cr1 = testCustomResource(UUID.randomUUID().toString()); | ||
cr1.getSpec().setValue("1"); | ||
TestCustomResource cr2 = testCustomResource(UUID.randomUUID().toString()); | ||
cr2.getSpec().setValue("2"); | ||
TestCustomResource cr3 = testCustomResource(UUID.randomUUID().toString()); | ||
cr3.getSpec().setValue("3"); | ||
|
||
customResourceCache.cacheResource(cr1); | ||
customResourceCache.cacheResource(cr2); | ||
customResourceCache.cacheResource(cr3); | ||
|
||
defaultEventHandler.handleEvent( | ||
new AbstractEvent( | ||
c -> { | ||
var tcr = ((TestCustomResource) c); | ||
return Objects.equals("1", tcr.getSpec().getValue()) | ||
|| Objects.equals("3", tcr.getSpec().getValue()); | ||
}, | ||
null) {}); | ||
|
||
verify(eventDispatcherMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(2)) | ||
.handleExecution(any()); | ||
|
||
waitMinimalTime(); | ||
|
||
ArgumentCaptor<ExecutionScope> executionScopeArgumentCaptor = | ||
ArgumentCaptor.forClass(ExecutionScope.class); | ||
|
||
verify(eventDispatcherMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(2)) | ||
.handleExecution(executionScopeArgumentCaptor.capture()); | ||
|
||
assertThat(executionScopeArgumentCaptor.getAllValues()) | ||
.hasSize(2) | ||
.allSatisfy( | ||
s -> { | ||
assertThat(s.getEvents()).isNotEmpty().hasOnlyElementsOfType(AbstractEvent.class); | ||
assertThat(s) | ||
.satisfiesAnyOf( | ||
e -> Objects.equals(cr1.getMetadata().getUid(), e.getCustomResourceUid()), | ||
e -> Objects.equals(cr3.getMetadata().getUid(), e.getCustomResourceUid())); | ||
}); | ||
} | ||
|
||
private void waitMinimalTime() { | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
private CustomResourceEvent prepareCREvent() { | ||
return prepareCREvent(UUID.randomUUID().toString()); | ||
} | ||
|
||
private CustomResourceEvent prepareCREvent(String uid) { | ||
TestCustomResource customResource = testCustomResource(uid); | ||
customResourceCache.cacheResource(customResource); | ||
return new CustomResourceEvent(Watcher.Action.MODIFIED, customResource, null); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these methods marked as
todo: remove
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copied from the other methods that were there, I don't know what was the rationale of the old comments, should I remove the comment or you have a plan to remove thos methods ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll try to figure out what's going on there… :)