Skip to content

Commit 7b281d7

Browse files
committed
add executeSynchronousCallWithConflictRetry to CallBuilder
1 parent 9a2d2a6 commit 7b281d7

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

operator/src/main/java/oracle/kubernetes/operator/helpers/CallBuilder.java

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,65 @@ public VersionInfo readVersionCode() throws ApiException {
166166
requestParams, ((client, params) -> new VersionApi(client).getCode()));
167167
}
168168

169+
/**
170+
* Class extended by callers to {@link
171+
* #executeSynchronousCallWithConflictRetry(RequestParamsBuilder, SynchronousCallFactory,
172+
* ConflictRetry)} for building the RequestParams to be passed to {@link
173+
* #executeSynchronousCall(RequestParams, SynchronousCallFactory)}.
174+
*
175+
* @param <T> Type of kubernetes object to be passed to the API
176+
*/
177+
abstract static class RequestParamsBuilder<T> {
178+
T body;
179+
180+
public RequestParamsBuilder(T body) {
181+
this.body = body;
182+
}
183+
184+
abstract RequestParams buildRequestParams();
185+
186+
void setBody(T body) {
187+
this.body = body;
188+
}
189+
}
190+
191+
private <T> T executeSynchronousCallWithConflictRetry(
192+
RequestParamsBuilder requestParamsBuilder,
193+
SynchronousCallFactory<T> factory,
194+
ConflictRetry<T> conflictRetry)
195+
throws ApiException {
196+
int retryCount = 0;
197+
while (retryCount == 0 || retryCount < maxRetryCount) {
198+
retryCount++;
199+
RequestParams requestParams = requestParamsBuilder.buildRequestParams();
200+
try {
201+
return executeSynchronousCall(requestParams, factory);
202+
} catch (ApiException apiException) {
203+
boolean retry = false;
204+
if (apiException.getCode() == HTTP_CONFLICT
205+
&& conflictRetry != null
206+
&& retryCount < maxRetryCount) {
207+
T body = conflictRetry.getUpdatedObject();
208+
if (body != null) {
209+
requestParamsBuilder.setBody(body);
210+
retry = true;
211+
LOGGER.fine(
212+
MessageKeys.SYNC_RETRY,
213+
requestParams.call,
214+
apiException.getCode(),
215+
apiException.getMessage(),
216+
retryCount,
217+
maxRetryCount);
218+
}
219+
}
220+
if (!retry) {
221+
throw apiException;
222+
}
223+
}
224+
}
225+
return null;
226+
}
227+
169228
private <T> T executeSynchronousCall(
170229
RequestParams requestParams, SynchronousCallFactory<T> factory) throws ApiException {
171230
return DISPATCHER.execute(factory, requestParams, helper);
@@ -317,34 +376,16 @@ public Step readDomainAsync(String name, String namespace, ResponseStep<Domain>
317376
public Domain replaceDomainWithConflictRetry(
318377
String uid, String namespace, Domain body, ConflictRetry<Domain> conflictRetry)
319378
throws ApiException {
320-
int retryCount = 0;
321-
while (retryCount == 0 || retryCount < maxRetryCount) {
322-
retryCount++;
323-
try {
324-
return replaceDomain(uid, namespace, body);
325-
} catch (ApiException apiException) {
326-
boolean retry = false;
327-
if (apiException.getCode() == HTTP_CONFLICT
328-
&& conflictRetry != null
329-
&& retryCount < maxRetryCount) {
330-
body = conflictRetry.getUpdatedObject();
331-
if (body != null) {
332-
retry = true;
333-
LOGGER.fine(
334-
MessageKeys.SYNC_RETRY,
335-
"replaceDomain",
336-
apiException.getCode(),
337-
apiException.getMessage(),
338-
retryCount,
339-
maxRetryCount);
379+
return executeSynchronousCallWithConflictRetry(
380+
new RequestParamsBuilder<Domain>(body) {
381+
382+
@Override
383+
RequestParams buildRequestParams() {
384+
return new RequestParams("replaceDomain", namespace, uid, body);
340385
}
341-
}
342-
if (!retry) {
343-
throw apiException;
344-
}
345-
}
346-
}
347-
return null;
386+
},
387+
REPLACE_DOMAIN_CALL,
388+
conflictRetry);
348389
}
349390

350391
/**

0 commit comments

Comments
 (0)