csharp icon indicating copy to clipboard operation
csharp copied to clipboard

[KubernetesClient.Aot] .NET 8 AOT compatibility for Scale functions

Open guillaume-chervet opened this issue 1 year ago • 15 comments

I @tg123 ,

I'am using KubernetesClient.Aot --version 13.0.12 Thank you so much for your help !

Just an issue to trace it. It would be awesome to implement AOT compatibility with Scale (StatefulSet and Deployment) functions :)

2024-02-02 09:22:36 fail: SlimFaas.ScaleReplicasWorker[0]
2024-02-02 09:22:36       Global Error in ScaleReplicasWorker
2024-02-02 09:22:36       System.ArgumentNullException: Value cannot be null. (Parameter 'jsonTypeInfo')
2024-02-02 09:22:36          at System.Text.Json.ThrowHelper.ThrowArgumentNullException(String)
2024-02-02 09:22:36          at k8s.Kubernetes.SendRequest[T](String, HttpMethod, IReadOnlyDictionary`2, T, CancellationToken)
2024-02-02 09:22:36          at k8s.AbstractKubernetes.IAppsV1Operations_PatchNamespacedDeploymentScaleWithHttpMessagesAsync[T](V1Patch, String, String, String, String, String, Nullable`1, Nullable`1, IReadOnlyDictionary`2, CancellationToken)
2024-02-02 09:22:36          at k8s.AbstractKubernetes.k8s.IAppsV1Operations.PatchNamespacedDeploymentScaleWithHttpMessagesAsync(V1Patch, String, String, String, String, String, Nullable`1, Nullable`1, IReadOnlyDictionary`2, CancellationToken)
2024-02-02 09:22:36          at k8s.AppsV1OperationsExtensions.PatchNamespacedDeploymentScaleAsync(IAppsV1Operations, V1Patch, String, String, String , String , String , Nullable`1 , Nullable`1 , CancellationToken )
2024-02-02 09:22:36          at SlimFaas.Kubernetes.KubernetesService.ScaleAsync(ReplicaRequest request)
2024-02-02 09:22:36          at SlimFaas.ReplicasService.CheckScaleAsync(String kubeNamespace)
2024-02-02 09:22:36          at SlimFaas.ScaleReplicasWorker.ExecuteAsync(CancellationToken stoppingToken)

The code : image

guillaume-chervet avatar Feb 05 '24 13:02 guillaume-chervet

the patch banned for aot is because that patch is so dynamic

i looked into it but did not have better idea any suggestion?

tg123 avatar Feb 05 '24 17:02 tg123

Hi @tg123 is patch not just a string? Does it require json formatting?

guillaume-chervet avatar Feb 05 '24 18:02 guillaume-chervet

hi @tg123 , I look at the code but I did not find any PatchNamespaceFunction. I do not very understand how is organized the whole code.

Do you have a youtube or some notes to help to understand this repository?

guillaume-chervet avatar Feb 10 '24 13:02 guillaume-chervet

here is the reason why V1Patch is not easy to fit AOT

https://github.com/kubernetes-client/csharp/blob/cdf5398e2d017f4687a15ba5f644aac47b469fc7/src/KubernetesClient/Models/V1PatchJsonConverter.cs#L24 V1Patch.body is a type-less object which AOT does not like

a workaround is to make V1Patch body string only but it breaks the compatibility with the non-aot sdk

tg123 avatar Feb 11 '24 07:02 tg123

I am for adding a specific method for AOT. Every project i have converted to .NET 8, i had to remove dynamic behavior in favor of performance and final ram cost benefice. Do you think it is possible to do it? @tg123

guillaume-chervet avatar Feb 11 '24 08:02 guillaume-chervet

imho, dynamic will still play an important role in future .net projects. keep patient, a bit busy there days, will get it done as soon as i got time

tg123 avatar Feb 15 '24 04:02 tg123

Thank you @tg123 ,

I do not know how to help you. It would be awesome to have a solution :)

guillaume-chervet avatar Feb 15 '24 18:02 guillaume-chervet

hi @tg123 , when do you think you will have some time ?

How can I help you?

guillaume-chervet avatar Feb 20 '24 09:02 guillaume-chervet

thanks @guillaume-chervet

if you can port https://github.com/kubernetes-client/csharp/blob/master/src/KubernetesClient/Models/V1Patch.cs and https://github.com/kubernetes-client/csharp/blob/master/src/KubernetesClient/Models/V1PatchJsonConverter.cs to AOT project, then patch will be good

i am thinking to support string only, but the behavior changed from non-aot project

tg123 avatar Feb 21 '24 06:02 tg123

Thank you @tg123 , i will try to look at. I am lacking of time too :(

guillaume-chervet avatar Feb 22 '24 05:02 guillaume-chervet

hi @tg123 , I do knot understand well the project.

Another idea, is it possible to retrieve an HttpClient already configured to pass authentication header ? So it will be possible to build AOT compatible rest request.

guillaume-chervet avatar Feb 27 '24 21:02 guillaume-chervet

see here https://github.com/kubernetes-client/csharp/discussions/1216

tg123 avatar Feb 29 '24 05:02 tg123

Thank you very much @tg123 . It works, I can activate Trimming now !

    public async Task<ReplicaRequest?> ScaleAsync(ReplicaRequest request)
    {
        try
        {
            using k8s.Kubernetes client = new(_k8SConfig);
            string patchString = $"{{\"spec\": {{\"replicas\": {request.Replicas}}}}}";
            var httpContent = new StringContent(patchString, Encoding.UTF8, "application/merge-patch+json");
            // we need to get the base uri, as it's not set on the HttpClient
            switch (request.PodType)
            {
                case PodType.Deployment:
                    {
                        var url = string.Concat(client.BaseUri, $"apis/apps/v1/namespaces/{request.Namespace}/deployments/{request.Deployment}/scale" );
                        HttpRequestMessage httpRequest = new(HttpMethod.Patch,
                            new Uri(url));
                        httpRequest.Content = httpContent;
                        if ( client.Credentials != null )
                        {
                            await client.Credentials.ProcessHttpRequestAsync( httpRequest, CancellationToken.None );
                        }
                        var response = await client.HttpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);
                        if(response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new HttpOperationException("Error while scaling deployment");
                        }
                        break;
                    }
                case PodType.StatefulSet:
                    {
                        var url = string.Concat(client.BaseUri, $"apis/apps/v1/namespaces/{request.Namespace}/statefulsets/{request.Deployment}/scale" );
                        HttpRequestMessage httpRequest = new(HttpMethod.Patch,
                            new Uri(url));
                        httpRequest.Content = httpContent;
                        if ( client.Credentials != null )
                        {
                            await client.Credentials.ProcessHttpRequestAsync( httpRequest, CancellationToken.None );
                        }
                        var response = await client.HttpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead );
                        if(response.StatusCode != HttpStatusCode.OK)
                        {
                            throw new HttpOperationException("Error while scaling deployment");
                        }
                        break;
                    }
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
        catch (HttpOperationException e)
        {
            _logger.LogError(e, "Error while scaling kubernetes deployment {RequestDeployment}", request.Deployment);
            return request;
        }
        return request;
    }

guillaume-chervet avatar Feb 29 '24 20:02 guillaume-chervet

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar May 29 '24 20:05 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Jun 28 '24 20:06 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-triage-robot avatar Jul 28 '24 21:07 k8s-triage-robot

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

k8s-ci-robot avatar Jul 28 '24 21:07 k8s-ci-robot