<p>I'm building Istio/K8s-based platform for controlling traffic routing with NodeJS. I need to be able to programmatically modify Custom Resources and I'd like to use the <a href="https://github.com/kubernetes-client/javascript" rel="nofollow noreferrer">@kubernetes/node-client</a> for that. I wasn't able to find the right API for accessing Custome Resources in docs and the repo. Am I missing something? Thx in adv.</p>
<p>EDIT: When using CustomObjectApi.patchNamespacedCustomObject function, I'm getting the following error back from K8s API:</p>
<p><code>message: 'the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml', reason: 'UnsupportedMediaType', code: 415</code></p>
<p>My Code:</p>
<pre class="lang-js prettyprint-override"><code>const k8sYamls = `${path.resolve(path.dirname(__filename), '..')}/k8sYamls`
const vServiceSpec = read(`${k8sYamls}/${service}/virtual-service.yaml`)
const kc = new k8s.KubeConfig()
kc.loadFromDefault()
const client = kc.makeApiClient(k8s.CustomObjectsApi)
const result = await client.patchNamespacedCustomObject(vServiceSpec.apiVersion.split('/')[0], vServiceSpec.apiVersion.split('/')[1], namespace, 'virtualservices', vServiceSpec.metadata.name, vServiceSpec)
</code></pre>
<p>virtual-service.yaml:</p>
<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: message-service
spec:
  hosts:
  - message-service
  http:
  - name: 'production'
    route:
    - destination:
        host: message-service
      weight: 100
    retries:
      attempts: 3
      perTryTimeout: 2s
      retryOn: 5xx
</code></pre>

Aleksander Nowak
<p>I was using the wrong type for the <code>body</code> object in that method. I got it to work following <a href="https://github.com/kubernetes-client/javascript/blob/master/examples/patch-example.js" rel="nofollow noreferrer">this example</a>.</p>
<pre class="lang-js prettyprint-override"><code>const patch = [
  {
    &quot;op&quot;: &quot;replace&quot;,
    &quot;path&quot;:&quot;/metadata/labels&quot;,
    &quot;value&quot;: {
      &quot;foo&quot;: &quot;bar&quot;
    }
  }
];
const options = { &quot;headers&quot;: { &quot;Content-type&quot;: k8s.PatchUtils.PATCH_FORMAT_JSON_PATCH}};
k8sApi.patchNamespacedPod(res.body.items[0].metadata.name, 'default', patch, undefined, undefined, undefined, undefined, options)
  .then(() =&gt; { console.log(&quot;Patched.&quot;)})
  .catch((err) =&gt; { console.log(&quot;Error: &quot;); console.log(err)});
</code></pre>
