-
Notifications
You must be signed in to change notification settings - Fork 236
feat: expectation pattern support #2941
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
csviri
wants to merge
23
commits into
next
Choose a base branch
from
expectation-simple
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
5fab265
feat: expectation pattern support
csviri cadf200
wip
csviri 2492ffa
wip
csviri 1c048a3
wip
csviri 82ec539
unit tests
csviri fa7a0d0
wip
csviri 4e3475f
wip
csviri 43bb65e
wip
csviri f1eb6c8
format
csviri 20482b3
docs
csviri 7f5f329
wip
csviri 3c9d2a0
fix
csviri 00ec4ac
Update docs/content/en/docs/documentation/reconciler.md
csviri 608d41a
Update docs/content/en/docs/documentation/reconciler.md
csviri 5a620a6
Update docs/content/en/docs/documentation/reconciler.md
csviri b260319
Update docs/content/en/docs/documentation/reconciler.md
csviri 17f1e80
Update docs/content/en/docs/documentation/reconciler.md
csviri 98f92ba
changes from code review
csviri 52900ea
Apply suggestion from @xstefank
xstefank 1a70de0
Apply suggestions from code review
xstefank c39d80f
javadoc for expectation
csviri ef31d50
Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
csviri 5cb0064
fixes from CR
csviri 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
51 changes: 51 additions & 0 deletions
51
...rk-core/src/main/java/io/javaoperatorsdk/operator/processing/expectation/Expectation.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,51 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.processing.expectation; | ||
|
|
||
| import java.util.function.BiPredicate; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Experimental; | ||
|
|
||
| import static io.javaoperatorsdk.operator.api.reconciler.Experimental.API_MIGHT_CHANGE; | ||
|
|
||
| @Experimental(API_MIGHT_CHANGE) | ||
| public interface Expectation<P extends HasMetadata> { | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| String UNNAMED = "unnamed"; | ||
|
|
||
| boolean isFulfilled(P primary, Context<P> context); | ||
|
|
||
| default String name() { | ||
| return UNNAMED; | ||
| } | ||
|
|
||
| static <P extends HasMetadata> Expectation<P> createExpectation( | ||
| String name, BiPredicate<P, Context<P>> predicate) { | ||
| return new Expectation<>() { | ||
| @Override | ||
| public String name() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isFulfilled(P primary, Context<P> context) { | ||
| return predicate.test(primary, context); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
143 changes: 143 additions & 0 deletions
143
.../src/main/java/io/javaoperatorsdk/operator/processing/expectation/ExpectationManager.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,143 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.processing.expectation; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.LocalDateTime; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
| import io.javaoperatorsdk.operator.api.reconciler.Experimental; | ||
| import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
|
|
||
| import static io.javaoperatorsdk.operator.api.reconciler.Experimental.API_MIGHT_CHANGE; | ||
|
|
||
| @Experimental(API_MIGHT_CHANGE) | ||
| public class ExpectationManager<P extends HasMetadata> { | ||
|
|
||
| protected final ConcurrentHashMap<ResourceID, RegisteredExpectation<P>> registeredExpectations = | ||
| new ConcurrentHashMap<>(); | ||
|
|
||
| /** | ||
| * Checks if the expectation holds, if not sets the expectation with the given timeout. | ||
| * | ||
| * @return false, if the expectation is already fulfilled, therefore, not registered. Returns true | ||
| * if expectation is not met and set with a timeout. | ||
| */ | ||
| public boolean checkAndSetExpectation( | ||
| P primary, Context<P> context, Duration timeout, Expectation<P> expectation) { | ||
| var fulfilled = expectation.isFulfilled(primary, context); | ||
| if (fulfilled) { | ||
| return false; | ||
| } else { | ||
| setExpectation(primary, timeout, expectation); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets a target expectation with given timeout. | ||
| * | ||
| * @param primary resource | ||
| * @param timeout of expectation | ||
| * @param expectation to set | ||
| */ | ||
| // we might consider in the future to throw an exception if an expectation is already set | ||
| public void setExpectation(P primary, Duration timeout, Expectation<P> expectation) { | ||
| registeredExpectations.put( | ||
| ResourceID.fromResource(primary), | ||
| new RegisteredExpectation<>(LocalDateTime.now(), timeout, expectation)); | ||
| } | ||
|
|
||
| /** | ||
| * Checks on expectation with provided name. Return the expectation result. If the result of | ||
| * expectation is fulfilled, the expectation is automatically removed; | ||
| */ | ||
| public ExpectationResult<P> checkExpectation( | ||
| String expectationName, P primary, Context<P> context) { | ||
| var resourceID = ResourceID.fromResource(primary); | ||
| var exp = registeredExpectations.get(ResourceID.fromResource(primary)); | ||
| if (exp != null && expectationName.equals(exp.expectation().name())) { | ||
| return checkExpectation(exp, resourceID, primary, context); | ||
| } else { | ||
| return checkExpectation(null, resourceID, primary, context); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks if actual expectation is fulfilled. Return the expectation result. If the result of | ||
| * expectation is fulfilled, the expectation is automatically removed; | ||
| */ | ||
| public ExpectationResult<P> checkExpectation(P primary, Context<P> context) { | ||
| var resourceID = ResourceID.fromResource(primary); | ||
| var exp = registeredExpectations.get(ResourceID.fromResource(primary)); | ||
| return checkExpectation(exp, resourceID, primary, context); | ||
| } | ||
|
|
||
| private ExpectationResult<P> checkExpectation( | ||
| RegisteredExpectation<P> exp, ResourceID resourceID, P primary, Context<P> context) { | ||
| if (exp == null) { | ||
| return new ExpectationResult<>(null, null); | ||
| } | ||
| if (exp.expectation().isFulfilled(primary, context)) { | ||
| registeredExpectations.remove(resourceID); | ||
| return new ExpectationResult<>(exp.expectation(), ExpectationStatus.FULFILLED); | ||
| } else if (exp.isTimedOut()) { | ||
| // we don't remove the expectation so user knows about it's state | ||
| return new ExpectationResult<>(exp.expectation(), ExpectationStatus.TIMED_OUT); | ||
| } else { | ||
| return new ExpectationResult<>(exp.expectation(), ExpectationStatus.NOT_YET_FULFILLED); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Returns true if there is an expectation for the primary resource, but it is not yet fulfilled | ||
| * neither timed out. | ||
| * The intention behind this is that you can exit reconciliation early with a simple check | ||
| * if true. | ||
| * */ | ||
| public boolean ongoingExpectationPresent(P primary, Context<P> context) { | ||
| var exp = registeredExpectations.get(ResourceID.fromResource(primary)); | ||
| if (exp == null) { | ||
| return false; | ||
| } | ||
| return !exp.isTimedOut() && !exp.expectation().isFulfilled(primary, context); | ||
| } | ||
|
|
||
| public boolean isExpectationPresent(P primary) { | ||
| return registeredExpectations.containsKey(ResourceID.fromResource(primary)); | ||
| } | ||
|
|
||
| public boolean isExpectationPresent(String name, P primary) { | ||
| var exp = registeredExpectations.get(ResourceID.fromResource(primary)); | ||
| return exp != null && name.equals(exp.expectation().name()); | ||
| } | ||
|
|
||
| public Optional<Expectation<P>> getExpectation(P primary) { | ||
| var regExp = registeredExpectations.get(ResourceID.fromResource(primary)); | ||
| return Optional.ofNullable(regExp).map(RegisteredExpectation::expectation); | ||
| } | ||
|
|
||
| public Optional<String> getExpectationName(P primary) { | ||
| return getExpectation(primary).map(Expectation::name); | ||
| } | ||
|
|
||
| public void removeExpectation(P primary) { | ||
| registeredExpectations.remove(ResourceID.fromResource(primary)); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...e/src/main/java/io/javaoperatorsdk/operator/processing/expectation/ExpectationResult.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,42 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.processing.expectation; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
|
|
||
| public record ExpectationResult<P extends HasMetadata>( | ||
| Expectation<P> expectation, ExpectationStatus status) { | ||
|
|
||
| public boolean isFulfilled() { | ||
| return status == ExpectationStatus.FULFILLED; | ||
| } | ||
|
|
||
| public boolean isTimedOut() { | ||
| return status == ExpectationStatus.TIMED_OUT; | ||
| } | ||
|
|
||
| public boolean isExpectationPresent() { | ||
| return expectation != null; | ||
| } | ||
|
|
||
| public boolean isNotPresentOrFulfilled() { | ||
| return !isExpectationPresent() || isFulfilled(); | ||
| } | ||
|
|
||
| public String name() { | ||
| return expectation.name(); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...e/src/main/java/io/javaoperatorsdk/operator/processing/expectation/ExpectationStatus.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,22 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.processing.expectation; | ||
|
|
||
| public enum ExpectationStatus { | ||
| FULFILLED, | ||
| NOT_YET_FULFILLED, | ||
xstefank marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TIMED_OUT | ||
| } | ||
Oops, something went wrong.
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.
delete this empty line