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

[bug]: Kubernetes Delete methods arent handling Status return objects and are throwing JsonSerialization Exceptions #547

Open
ewassef opened this issue Mar 19, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@ewassef
Copy link
Contributor

ewassef commented Mar 19, 2023

Describe the bug

When calling the IKubernetesClient.Delete<T> method, the Serializer is sending in a TResource of the type we are deleting and expecting the same back. However, It seems that in 1.23+ this method returns the following json

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Success",
  "details": {
    "name": "unittest-1-geo-failover-primary",
    "group": "externaldns.k8s.io",
    "kind": "dnsendpoints",
    "uid": "8657a1d8-f518-4408-8a23-0662983f25d9"
  }
}

To reproduce

Simply call the Delete on a custom object and observe

Expected behavior

A successful deletion without an exception

Screenshots

No response

Additional Context

This will actually successfully delete, but then throw a serialization exception. If this is in a Reconcile, then the next time it will get called, it will succeed as the NotFound status code is handled. However, in a finalizer, the finalizer fails

@ewassef ewassef added the bug Something isn't working label Mar 19, 2023
@slacki123
Copy link
Contributor

slacki123 commented May 18, 2023

I also found the same issue. As you mentioned, it looks like the "Delete<T>" method returns a status object, eg.

{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success","details":{"name":"appname","group":"apps","kind":"deployments","uid":"eaa477f2-1979-4808-9369-25232794b18c"}}

when trying to deserialize within the Delete<T> method, it ultimately does the following operation:

JsonSerializer.Deserialize<T>(theStatusJson)

Which is the root of the error...

Not ideal, but if in a hurry, you can always implement own delete method...

    public static async Task CustomDelete<TResource>(this IKubernetesClient client, string name, string @namespace)
        where TResource : IKubernetesObject<V1ObjectMeta>
    {
        var definition = EntityDefinition.FromType<TResource>();
        try
        {
            var status = await (
                string.IsNullOrWhiteSpace(@namespace)
                ? client.ApiClient.CustomObjects.DeleteClusterCustomObjectWithHttpMessagesAsync(
                    definition.Group,
                    definition.Version,
                    definition.Plural,
                    name)
                    .ConfigureAwait(false)
                : client.ApiClient.CustomObjects.DeleteNamespacedCustomObjectWithHttpMessagesAsync(
                        definition.Group,
                        definition.Version,
                        @namespace,
                        definition.Plural,
                        name)
                    .ConfigureAwait(false)
                );
        }
        catch (HttpOperationException e) when (e.Response.StatusCode == HttpStatusCode.NotFound)
        {
        }

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants