Skip to content
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

[artifact-manager] (#552) Implement exception handling during deletion of Custom Resource which is in use #555

Merged
merged 10 commits into from
Dec 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ public void handleError(ClientHttpResponse response) throws IOException {
}

throw new HttpClientErrorException(response.getStatusCode(),
"Invalid/Unreachable FQDN or IP address: " + messageBuilder.toString());
"Error (" + messageBuilder.toString()
+ ") while processing request, FQDN or IP may be incorrect or the server may be down.");
}

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ private boolean tryDeleteCustomResource(final String customResourceName, final S
restClient.deleteCustomResource(customResourceName, existingObjectId);
customResourceJsonElement.remove("id");
customResourceJsonElement.add("id", new JsonPrimitive(existingObjectId));
} catch (HttpClientErrorException ex) {
} catch (Exception ex) {
if (isCustomResourceActiveAttached(ex)) {
// Do warn the user, but don't fail.
logger.debug(
Expand Down Expand Up @@ -390,14 +390,34 @@ private static boolean isCustomResourceExistingInDifferentOrganization(
HttpStatus status = HttpStatus.valueOf(clientException.getStatusCode().value());
final String magicMessage = "Custom resource with the same resource type already exists! Use a different resource type name!";
String message = clientException.getMessage();
return (status == HttpStatus.BAD_REQUEST && message != null && message.indexOf(magicMessage) != -1);
return (status == HttpStatus.BAD_REQUEST && message != null && message.contains(magicMessage));
}

private static boolean isCustomResourceActiveAttached(final HttpClientErrorException clientException) {
HttpStatus status = HttpStatus.valueOf(clientException.getStatusCode().value());
/**
* Check if the exception is due to active resources attached to the custom
* resource.
*
* The reason why we fetch all the messages is that the exception can be nested,
* which it is currently.
*
* @param clientException - The exception to check
* @return true if the exception is due to active resources attached to the
* custom
*/
private static boolean isCustomResourceActiveAttached(final Exception clientException) {
final String magicMessage = "Resource type cannot be deleted as there are active resources attached to it";
String message = clientException.getMessage();
return (status == HttpStatus.BAD_REQUEST && message != null && message.indexOf(magicMessage) != -1);

Throwable e = clientException;
StringBuilder builder = new StringBuilder(e.getMessage()).append("\n");

Throwable cause = e.getCause();
while (cause != null) {
builder.append(cause.getMessage()).append("\n");
cause = cause.getCause();
}

String message = builder.toString();
return message.contains(magicMessage);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions docs/versions/latest/Release.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@

## Improvements

### *Custom Resources will now get updated correctly if in use*

When a custom resource is in use, we attempt to do an update after a failed deletion (default behavior as we want to recreate it).

#### Previous Behavior

This used to fail since the exception was not caught and was also burried inside of another exception.

#### New Behavior

The exception is now caught and the update is attempted.

[//]: # (### *Improvement Name* )
[//]: # (Talk ONLY regarding the improvement)
[//]: # (Optional But higlhy recommended)
Expand Down
Loading