'how to scale the AKS Deployments using C#
I have tried using the Patch Class to scale the Deployments but unable to do so. Please let me know how to do it. i have researched a lot but no proper docs on it/Example to achieve.
public static async Task<V1Scale> Scale([FromBody] ReplicaRequest request)
{
try
{
// Use the config object to create a client.
using (var client = new Kubernetes(config))
{
// Create a json patch for the replicas
var jsonPatch = new JsonPatchDocument<V1Scale>();
// Set the new number of repplcias
jsonPatch.Replace(e => e.Spec.Replicas, request.Replicas);
// Creat the patch
var patch = new V1Patch(jsonPatch,V1Patch.PatchType.ApplyPatch);
var list = client.ListNamespacedPod("default");
//client.PatchNamespacedReplicaSetStatus(patch, request.Deployment, request.Namespace);
//var _result = await client.PatchNamespacedDeploymentScaleAsync(patch, request.Deployment, request.Namespace,null, null,true,default);
await client.PatchNamespacedDeploymentScaleAsync(patch, request.Deployment, request.Namespace,null, "M", true, default);
}
}
catch (Microsoft.Rest.HttpOperationException e)
{
Console.WriteLine(e.Response.Content);
}
return null;
}
public class ReplicaRequest
{
public string Deployment { get; set; }
public string Namespace { get; set; }
public int Replicas { get; set; }
}
Solution 1:[1]
The JsonPatchDocument<T>
you are using generates Json Patch, but you are specifying ApplyPatch.
Edit as of 2022-04-19:
The kubernetes-client/csharp library changed the serializer to System.Text.Json
and this doesn't support JsonPatchDocument<T>
serialization, hence we have to do it beforehand
From version 7 of the client library:
var jsonPatch = new JsonPatchDocument<V1Scale>();
jsonPatch.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
};
jsonPatch.Replace(e => e.Spec.Replicas, request.Replicas);
var jsonPatchString = Newtonsoft.Json.JsonConvert.SerializeObject(jsonPatch);
var patch = new V1Patch(jsonPatchString, V1Patch.PatchType.JsonPatch);
await client.PatchNamespacedDeploymentScaleAsync(patch, request.Deployment, request.Namespace);
Works until version 6 of the client library:
Either of these should work:
Json Patch:
var jsonPatch = new JsonPatchDocument<V1Scale>();
jsonPatch.Replace(e => e.Spec.Replicas, request.Replicas);
var patch = new V1Patch(jsonPatch, V1Patch.PatchType.JsonPatch);
await client.PatchNamespacedDeploymentScaleAsync(patch, request.Deployment, request.Namespace);
Json Merge Patch:
var jsonMergePatch = new V1Scale { Spec = new V1ScaleSpec { Replicas = request.Replicas } };
var patch = new V1Patch(jsonMergePatch, V1Patch.PatchType.MergePatch);
await client.PatchNamespacedDeploymentScaleAsync(patch, request.Deployment, request.Namespace);
About the different patch options:
They are described here (official C# client): https://github.com/kubernetes-client/csharp/blob/master/src/KubernetesClient/Kubernetes.Header.cs#L32
- Nice article about the difference between JSON Patch and JSON Merge Patch: https://erosb.github.io/post/json-patch-vs-merge-patch/
- Strategic merge patch is a custom k8s version of Json Patch: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md
- And ApplyPatch is the "server side apply" that replaces it in yaml format: https://kubernetes.io/docs/reference/using-api/server-side-apply/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |