<p>I'm trying to restart my kubernetes deployment via the kubernetes api using the
@kubernetes/client-node Library. I'm not using deployment scale because i only need one deployment (db and service container) per app.</p>
<p>I also tried to restart a single container inside the deployment via exec (/sbin/reboot or kill), but it seems to not work with the nodejs library because it fails to upgrade to websocket connection, what is needed by the kubernetes exec endpoint as it seems.  The other idea was to restart the whole deployment by setting the scale to 0 and then 1 again. But I dont get it working via the nodejs library. I tried to find an example for that, but was not successful.</p>
<p>The rolling restart is not working for me, becuase my application doesnt support multiple instances.</p>
<p>i tried it like this to scale</p>
<pre><code>await k8sApi.patchNamespacedDeploymentScale(`mydeployment-name`, 'default', {
spec: { replicas: 0 },
});

await k8sApi.patchNamespacedDeploymentScale(`mydeployment-name`, 'default', {
spec: { replicas: 1 },
});
</code></pre>
<p>and to reboot the container i tried this</p>
<pre><code>await coreV1Api.connectPostNamespacedPodExec(
podName,
'default',
'/sbin/reboot',
'web',
false,
false,
false,
false
);
</code></pre>
<hr />
<p>Extra input:</p>
<p>When trying to use patchNamespacedDeployment i get the following error back by kubernetes api:</p>
<pre><code>statusCode: 415,
statusMessage: 'Unsupported Media Type',
</code></pre>
<p>And response body:</p>
<pre><code>V1Scale {
apiVersion: 'v1',
kind: 'Status',
metadata: V1ObjectMeta {
annotations: undefined,
clusterName: undefined,
creationTimestamp: undefined,
deletionGracePeriodSeconds: undefined,
deletionTimestamp: undefined,
finalizers: undefined,
generateName: undefined,
generation: undefined,
labels: undefined,
managedFields: undefined,
name: undefined,
namespace: undefined,
ownerReferences: undefined,
resourceVersion: undefined,
selfLink: undefined,
uid: undefined
},
spec: undefined,
status: V1ScaleStatus { replicas: undefined, selector: undefined }
</code></pre>
<p>when trying the exec approach i get the following response:</p>
<pre><code>kind: 'Status',
apiVersion: 'v1',
metadata: {},
status: 'Failure',
message: 'Upgrade request required',
reason: 'BadRequest',
code: 400
</code></pre>
<p>i already looked the upgrade request error up, and it seems like the library isnt aware of this, because the library was generated from function footprints or something, so it is not aware of websockets.</p>

<p>Really seems like there is a bug in the node Kubernetes client library.
On PATCH requests it should set the content type to &quot;application/json-patch+json&quot; but instead it sends the content type as &quot;application/json&quot;.
Thats why you get unsupported media type back by the api.</p>
<p>Furthermore you need to use the JSON Patch format for the body you send: <a href="http://jsonpatch.com" rel="noreferrer">http://jsonpatch.com</a></p>
<p>To manually set the content type you can pass custom headers to the function call.</p>
<p>This worked for me:</p>
<pre><code>const patch = [
    {
      op: 'replace',
      path: '/spec/replicas',
      value: 0,
    },
  ];
    
await k8sApi.patchNamespacedDeployment(
    `mydeployment-name`,
    'default',
    patch,
    undefined,
    undefined,
    undefined,
    undefined,
    { headers: { 'content-type': 'application/json-patch+json' } }
  );
</code></pre>
<p>After some google searching I found that this problem is already existing since 2018: <a href="https://github.com/kubernetes-client/javascript/issues/19" rel="noreferrer">https://github.com/kubernetes-client/javascript/issues/19</a></p>
