Handle conflict properly.

This commit is contained in:
Michael Lipp 2025-02-02 12:10:22 +01:00
parent 21108771d9
commit d5e589709f
3 changed files with 100 additions and 82 deletions

View file

@ -193,42 +193,41 @@ public class K8sGenericStub<O extends KubernetesObject,
}
/**
* Updates the object's status, retrying for the given number of times
* if the update fails due to a conflict.
* Updates the object's status.
*
* @param object the current state of the object (passed to `status`)
* @param status function that returns the new status
* @param retries the retries
* @return the updated model or empty if not successful
* @return the updated model or empty if the object was not found
* @throws ApiException the api exception
*/
@SuppressWarnings("PMD.AssignmentInOperand")
public Optional<O> updateStatus(O object,
Function<O, Object> status, int retries) throws ApiException {
while (true) {
try {
return K8s.optional(api.updateStatus(object, status));
} catch (ApiException e) {
if (HttpURLConnection.HTTP_CONFLICT != e.getCode()
|| retries-- <= 0) {
throw e;
}
}
}
public Optional<O> updateStatus(O object, Function<O, Object> status)
throws ApiException {
return K8s.optional(api.updateStatus(object, status));
}
/**
* Updates the object's status, retrying up to 16 times if there
* is a conflict.
* Gets the object and updates the status. In case of conflict, retries
* up to `retries` times.
*
* @param object the current state of the object (passed to `status`)
* @param status function that returns the new status
* @return the updated model or empty if not successful
* @param status the status
* @param retries the retries in case of conflict
* @return the updated model or empty if the object was not found
* @throws ApiException the api exception
*/
public Optional<O> updateStatus(O object,
Function<O, Object> status) throws ApiException {
return updateStatus(object, status, 16);
@SuppressWarnings({ "PMD.AssignmentInOperand", "PMD.UnusedAssignment" })
public Optional<O> updateStatus(Function<O, Object> status, int retries)
throws ApiException {
try {
return updateStatus(api.get(namespace, name).throwsApiException()
.getObject(), status);
} catch (ApiException e) {
if (HttpURLConnection.HTTP_CONFLICT != e.getCode()
|| retries-- <= 0) {
throw e;
}
}
return Optional.empty();
}
/**
@ -241,8 +240,7 @@ public class K8sGenericStub<O extends KubernetesObject,
*/
public Optional<O> updateStatus(Function<O, Object> status)
throws ApiException {
return updateStatus(
api.get(namespace, name).throwsApiException().getObject(), status);
return updateStatus(status, 16);
}
/**