dotnet-operator-sdk icon indicating copy to clipboard operation
dotnet-operator-sdk copied to clipboard

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

Open ewassef opened this issue 1 year ago • 1 comments

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<T> 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 avatar Mar 19 '23 18:03 ewassef

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)
        {
        }

    }

slacki123 avatar May 18 '23 15:05 slacki123