Skip to content

Commit d9edc53

Browse files
committed
refactor: better naming
Signed-off-by: Chris Laprun <metacosm@gmail.com>
1 parent 35d5357 commit d9edc53

File tree

7 files changed

+31
-15
lines changed

7 files changed

+31
-15
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/DefaultContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public DefaultContext<P> setRetryInfo(RetryInfo retryInfo) {
164164
}
165165

166166
@SuppressWarnings("unchecked")
167-
public <R> R desiredStateFor(
167+
public <R> R getOrComputeDesiredStateFor(
168168
DependentResource<R, P> dependentResource, Function<P, R> desiredStateComputer) {
169169
return (R)
170170
desiredStates.computeIfAbsent(

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractDependentResource.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected ReconcileResult<R> reconcile(P primary, R actualResource, Context<P> c
8686
if (creatable() || updatable()) {
8787
if (actualResource == null) {
8888
if (creatable) {
89-
var desired = doDesired(context);
89+
var desired = getOrComputeDesired(context);
9090
throwIfNull(desired, primary, "Desired");
9191
logForOperation("Creating", primary, desired);
9292
var createdResource = handleCreate(desired, primary, context);
@@ -96,7 +96,8 @@ protected ReconcileResult<R> reconcile(P primary, R actualResource, Context<P> c
9696
if (updatable()) {
9797
final Matcher.Result<R> match = match(actualResource, primary, context);
9898
if (!match.matched()) {
99-
final var desired = match.computedDesired().orElseGet(() -> doDesired(context));
99+
final var desired =
100+
match.computedDesired().orElseGet(() -> getOrComputeDesired(context));
100101
throwIfNull(desired, primary, "Desired");
101102
logForOperation("Updating", primary, desired);
102103
var updatedResource = handleUpdate(actualResource, desired, primary, context);
@@ -212,10 +213,25 @@ protected R desired(P primary, Context<P> context) {
212213
+ " updated");
213214
}
214215

215-
protected R doDesired(Context<P> context) {
216+
/**
217+
* Retrieves the desired state from the {@link Context} if it has already been computed or calls
218+
* {@link #desired(HasMetadata, Context)} and stores its result in the context for further use.
219+
* This ensures that {@code desired} is only called once per reconciliation to avoid unneeded
220+
* processing and supports scenarios where idempotent computation of the desired state is not
221+
* feasible.
222+
*
223+
* <p>Note that this method should normally only be called by the SDK itself and exclusively (i.e.
224+
* {@link #desired(HasMetadata, Context)} should not be called directly by the SDK) whenever the
225+
* desired state is needed to ensure it is properly cached for the current reconciliation.
226+
*
227+
* @param context the {@link Context} in scope for the current reconciliation
228+
* @return the desired state associated with this dependent resource based on the currently
229+
* in-scope primary resource as found in the context
230+
*/
231+
protected R getOrComputeDesired(Context<P> context) {
216232
assert context instanceof DefaultContext<P>;
217233
DefaultContext<P> defaultContext = (DefaultContext<P>) context;
218-
return defaultContext.desiredStateFor(this, p -> desired(p, defaultContext));
234+
return defaultContext.getOrComputeDesiredStateFor(this, p -> desired(p, defaultContext));
219235
}
220236

221237
public void delete(P primary, Context<P> context) {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected void handleExplicitStateCreation(P primary, R created, Context<P> cont
105105

106106
@Override
107107
public Matcher.Result<R> match(R resource, P primary, Context<P> context) {
108-
var desired = doDesired(context);
108+
var desired = getOrComputeDesired(context);
109109
return Matcher.Result.computed(resource.equals(desired), desired);
110110
}
111111

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesResourceMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static <R extends HasMetadata, P extends HasMetadata> Matcher.Result<R> m
138138
Context<P> context,
139139
boolean labelsAndAnnotationsEquality,
140140
String... ignorePaths) {
141-
final var desired = dependentResource.doDesired(context);
141+
final var desired = dependentResource.getOrComputeDesired(context);
142142
return match(desired, actualResource, labelsAndAnnotationsEquality, context, ignorePaths);
143143
}
144144

@@ -150,7 +150,7 @@ public static <R extends HasMetadata, P extends HasMetadata> Matcher.Result<R> m
150150
boolean specEquality,
151151
boolean labelsAndAnnotationsEquality,
152152
String... ignorePaths) {
153-
final var desired = dependentResource.doDesired(context);
153+
final var desired = dependentResource.getOrComputeDesired(context);
154154
return match(
155155
desired, actualResource, labelsAndAnnotationsEquality, specEquality, context, ignorePaths);
156156
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public R update(R actual, R desired, P primary, Context<P> context) {
123123

124124
@Override
125125
public Result<R> match(R actualResource, P primary, Context<P> context) {
126-
final var desired = doDesired(context);
126+
final var desired = getOrComputeDesired(context);
127127
return match(actualResource, desired, primary, context);
128128
}
129129

@@ -301,16 +301,16 @@ protected Optional<R> selectTargetSecondaryResource(
301301
* @return id of the target managed resource
302302
*/
303303
protected ResourceID targetSecondaryResourceID(P primary, Context<P> context) {
304-
return ResourceID.fromResource(doDesired(context));
304+
return ResourceID.fromResource(getOrComputeDesired(context));
305305
}
306306

307307
protected boolean addOwnerReference() {
308308
return garbageCollected;
309309
}
310310

311311
@Override
312-
protected R doDesired(Context<P> context) {
313-
return super.doDesired(context);
312+
protected R getOrComputeDesired(Context<P> context) {
313+
return super.getOrComputeDesired(context);
314314
}
315315

316316
@Override

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/AbstractDependentResourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AbstractDependentResourceTest {
3434

3535
private static final TestCustomResource PRIMARY = new TestCustomResource();
3636
private static final DefaultContext<TestCustomResource> CONTEXT =
37-
new DefaultContext<>(mock(), mock(), PRIMARY);
37+
new DefaultContext<>(mock(), mock(), PRIMARY, false, false);
3838

3939
@Test
4040
void throwsExceptionIfDesiredIsNullOnCreate() {

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesResourceMatcherTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public TestContext() {
4747
}
4848

4949
public TestContext(HasMetadata primary) {
50-
super(mock(), mock(), primary);
50+
super(mock(), mock(), primary, false, false);
5151
}
5252

5353
@Override
@@ -91,7 +91,7 @@ void doesNotMatchRemovedValues() {
9191
final var localContext = new TestContext(createPrimary("removed"));
9292
assertThat(
9393
GenericKubernetesResourceMatcher.match(
94-
dependentResource.doDesired(localContext), actual, localContext)
94+
dependentResource.getOrComputeDesired(localContext), actual, localContext)
9595
.matched())
9696
.withFailMessage("Removing values in metadata should lead to a mismatch")
9797
.isFalse();

0 commit comments

Comments
 (0)