Skip to content

Commit 2fcaf66

Browse files
committed
wip
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 246035c commit 2fcaf66

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/expectation/ExpectationManager.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ public class ExpectationManager<P extends HasMetadata> {
3333
protected final ConcurrentHashMap<ResourceID, RegisteredExpectation<P>> registeredExpectations =
3434
new ConcurrentHashMap<>();
3535

36+
/**
37+
* Checks if the expectation holds, if not sets the expectation with the given timeout.
38+
*
39+
* @return false, if the expectation is already fulfilled, therefore, not registered. Returns true
40+
* if expectation is not met and set with a timeout.
41+
*/
42+
public boolean checkAndSetExpectation(
43+
P primary, Context<P> context, Duration timeout, Expectation<P> expectation) {
44+
var fulfilled = expectation.isFulfilled(primary, context);
45+
if (fulfilled) {
46+
return false;
47+
} else {
48+
setExpectation(primary, timeout, expectation);
49+
return true;
50+
}
51+
}
52+
53+
/**
54+
* Sets a target expectation with given timeout.
55+
*
56+
* @param primary resource
57+
* @param timeout of expectation
58+
* @param expectation to check
59+
*/
60+
// we might consider in the future to throw an exception if an expectation is already set
3661
public void setExpectation(P primary, Duration timeout, Expectation<P> expectation) {
3762
registeredExpectations.put(
3863
ResourceID.fromResource(primary),

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/expectation/ExpectationManagerTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,28 @@ void setExpectationShouldReplaceExistingExpectation() {
181181

182182
assertThat(expectationManager.getExpectation(configMap)).contains(expectation2);
183183
}
184+
185+
@Test
186+
void checkAndSetExpectationAlreadyMet() {
187+
Expectation<ConfigMap> expectation = mock(Expectation.class);
188+
when(expectation.isFulfilled(any(), any())).thenReturn(true);
189+
190+
var res =
191+
expectationManager.checkAndSetExpectation(
192+
configMap, mock(Context.class), Duration.ofMinutes(5), expectation);
193+
assertThat(res).isFalse();
194+
assertThat(expectationManager.getExpectation(configMap)).isEmpty();
195+
}
196+
197+
@Test
198+
void checkAndSetExpectationNotMet() {
199+
Expectation<ConfigMap> expectation = mock(Expectation.class);
200+
when(expectation.isFulfilled(any(), any())).thenReturn(false);
201+
202+
var res =
203+
expectationManager.checkAndSetExpectation(
204+
configMap, mock(Context.class), Duration.ofMinutes(5), expectation);
205+
assertThat(res).isTrue();
206+
assertThat(expectationManager.getExpectation(configMap)).isPresent();
207+
}
184208
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/expectation/ExpectationReconciler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ public UpdateControl<ExpectationCustomResource> reconcile(
6868
var deployment = context.getSecondaryResource(Deployment.class);
6969
if (deployment.isEmpty()) {
7070
createDeployment(primary, context);
71-
expectationManager.setExpectation(
72-
primary, Duration.ofSeconds(timeout), deploymentReadyExpectation(context));
73-
return UpdateControl.noUpdate();
71+
var set =
72+
expectationManager.checkAndSetExpectation(
73+
primary, context, Duration.ofSeconds(timeout), deploymentReadyExpectation(context));
74+
if (set) {
75+
return UpdateControl.noUpdate();
76+
}
7477
} else {
7578
// checks the expectation if it is fulfilled also removes it,
7679
// in your logic you might add a next expectation based on your workflow

0 commit comments

Comments
 (0)