<p>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.</p>
<pre><code>public static async Task&lt;V1Scale&gt; 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&lt;V1Scale&gt;();
            
                // Set the new number of repplcias
                jsonPatch.Replace(e =&gt; e.Spec.Replicas, request.Replicas);
                // Creat the patch

                var patch = new V1Patch(jsonPatch,V1Patch.PatchType.ApplyPatch);
                var list = client.ListNamespacedPod(&quot;default&quot;);
                
                //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, &quot;M&quot;, 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; }
    }
</code></pre>


<p>The <code>JsonPatchDocument&lt;T&gt;</code> you are using generates Json Patch, but you are specifying ApplyPatch.</p>
<p>Edit as of 2022-04-19:<br />
The <a href="https://github.com/kubernetes-client/csharp" rel="nofollow noreferrer">kubernetes-client/csharp</a> library changed the serializer to <code>System.Text.Json</code> and this doesn't support <code>JsonPatchDocument&lt;T&gt;</code> serialization, hence we have to do it beforehand</p>
<h4><code>From version 7 of the client library:</code></h4>
<pre class="lang-cs prettyprint-override"><code>var jsonPatch = new JsonPatchDocument&lt;V1Scale&gt;();
jsonPatch.ContractResolver = new DefaultContractResolver
{
    NamingStrategy = new CamelCaseNamingStrategy()
};
jsonPatch.Replace(e =&gt; 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);
</code></pre>
<h4><code>Works until version 6 of the client library:</code></h4>
<p>Either of these should work:</p>
<p>Json Patch:</p>
<pre class="lang-cs prettyprint-override"><code>var jsonPatch = new JsonPatchDocument&lt;V1Scale&gt;();
jsonPatch.Replace(e =&gt; e.Spec.Replicas, request.Replicas);
var patch = new V1Patch(jsonPatch, V1Patch.PatchType.JsonPatch);
await client.PatchNamespacedDeploymentScaleAsync(patch, request.Deployment, request.Namespace);
</code></pre>
<p>Json Merge Patch:</p>
<pre class="lang-cs prettyprint-override"><code>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);
</code></pre>
<h3>About the different patch options:</h3>
<p>They are described here (official C# client):
<a href="https://github.com/kubernetes-client/csharp/blob/master/src/KubernetesClient/Kubernetes.Header.cs#L32" rel="nofollow noreferrer">https://github.com/kubernetes-client/csharp/blob/master/src/KubernetesClient/Kubernetes.Header.cs#L32</a></p>
<ul>
<li>Nice article about the difference between JSON Patch and JSON Merge Patch: <a href="https://erosb.github.io/post/json-patch-vs-merge-patch/" rel="nofollow noreferrer">https://erosb.github.io/post/json-patch-vs-merge-patch/</a></li>
<li>Strategic merge patch is a custom k8s version of Json Patch:
<a href="https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md" rel="nofollow noreferrer">https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md</a></li>
<li>And ApplyPatch is the &quot;server side apply&quot; that replaces it in yaml format:
<a href="https://kubernetes.io/docs/reference/using-api/server-side-apply/" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/using-api/server-side-apply/</a></li>
</ul>
