How do I create a Kubernetes Custom Resource using javascript client

6/20/2021

My custom definition

apiVersion: something.com/v1alpha1
 kind: MyKind
 metadata:
   name: test
 spec:
   size: 1
   image: myimage
   

Here is an answer that shows how to create a deployment using a javascript client. However, I need to create a custom resource using a javascript client

-- Atul
javascript
kubernetes
kubernetes-apiserver
kubernetes-custom-resources
node.js

2 Answers

6/21/2021

All the client libraries are auto-generated from the same underlying IDL so it works like in Go, createNamespacedCustomObject. You can also use the raw API directly too.

-- coderanger
Source: StackOverflow

6/22/2021
const k8s = require('@kubernetes/client-node')
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sClient = kc.makeApiClient(k8s.CustomObjectsApi);

var body = {
    "apiVersion": "something.com/v1alpha1",
    "kind": "MyKind",
    "metadata": {
        "name": "mycustomobject",
    },
    "spec": {
        "size": "1",
        "image": "myimage"
    }
}

k8sClient.createNamespacedCustomObject('something.com','v1alpha1','default','mykinds', body)
    .then((res)=>{
        console.log(res)
    })
    .catch((err)=>{
        console.log(err)
    })
-- Atul
Source: StackOverflow