@pubgcorp/kubectl-node

1.5.0 • Public • Published

kubectl-node

Limited and partial implementation of kubectl.

⚠️ Caution ⚠️

"kubectl-node" uses server-side-apply to modify resources. Modification by "kubectl-node" may cause conflict.

Install

# NPM
npm install @pubgcorp/kubectl-node

# YARN
yarn add @pubgcorp/kubectl-node

Basic Usage

Create kubectl instance

Javascript

const fs = require("fs");
const os = require("os");
const yaml = require("js-yaml"); // if you use js-yaml, need to install

const { NewKubectl } = require("@pubgcorp/kubectl-node");
// kubeconfig sample

const kubeconfig = yaml.load(fs.readFileSync(`${os.homedir()}/.kube/config`, "utf8"));
const kubectl = NewKubectl(kubeconfig)

Typescript

import * as fs from "fs"
import * as os from "os"
import * as yaml from "js-yaml" // if you use js-yaml, need to install

import { NewKubectl, KubeConfig } from "@pubgcorp/kubectl-node";
// kubeconfig sample

const kubeconfig = yaml.load(fs.readFileSync(`${os.homedir()}/.kube/config`, "utf8")) as KubeConfig;
const kubectl = NewKubectl(kubeconfig)

kubectl api-resources, api-versions, version

Get api-resources, api-versions and version from cluster

const apiResources = await kubectl.apiResources();
const apiVersions = await kubectl.apiVersions();
const version = await kubectl.version();

kubectl get

Get single resource

// get without namespace (search `default` namespace by default);
//  => equivalant to `kubectl.get('deployment', 'nginx', { namespace: "default" })`;
const result = await kubectl.get('deployment', 'nginx');
// get with namespace
const result = await kubectl.get('deployment', 'nginx', { namespace: 'my-namespace' });

kubectl list

Get collection of resources

// list without namespace (search `default` namespace by default);
const result = await kubectl.list('deployment');
// list with namespace
const namespace = "my-namespace";
const result = await kubectl.list('deployment', { namespace });
// list all resources in all namespaces
const result = await kubectl.list('deployment', { allNamespaces: true });
// list with labelSelector
const result1 = await kubectl.list('deployment', { allNamespaces: true, labelSelector: { app: 'nginx' }});
const result2 = await kubectl.list('deployment', { allNamespaces: true, labelSelector: { app: { op: 'In', values: ['nginx'] } }});
// list with fieldSelector
const result1 = await kubectl.list('pod', { allNamespaces: true, fieldSelector: [{ path: 'spec.nodeName', op: 'Equal', value: 'docker-desktop' }] })
const result2 = await kubectl.list('pod', { allNamespaces: true, fieldSelector: [{ path: 'status.podIP', op: 'NotEqual', value: '10.1.0.8' }] })
const result3 = await kubectl.list('pod', { allNamespaces: true, labelSelector: { tier: 'control-plane' }, fieldSelector: [{ path: 'status.podIP', op: 'NotEqual', value: '10.1.0.8' }] })

kubectl scale

Scale single resource if resource"s spec.replicas exists and spec.replicas is number

// scale without namespace
const result = await kubectl.scale('deployment', 'nginx', 2);
// scale with namespace
const namespace = "my-namespace";
const result = await kubectl.scale('deployment', 'nginx', 2, { namespace });

kubectl patch

Patch single resource

Sequence

  1. Get resource from remort
  2. Apply json-patch locally
  3. Apply the modification through server-side-apply patch.

See json-patch for additional information.

// patch without namespace
const result = await kubectl.patch('deployment', 'nginx', [{ op: "add", path: "/metadata/annotations", value: { key1: "value1" } }]);
// patch with namespace
const namespace = "my-namespace";
const result = await kubectl.scale('deployment', 'nginx', [{ op: "add", path: "/metadata/annotations", value: { key1: "value1" } }], { namespace });

kubectl delete

Delete single resource

// delete without namespace
const result = await kubectl.delete('deployment', 'nginx');
// delete with namespace
const namespace = "my-namespace";
const result = await kubectl.delete('deployment', 'nginx', { namespace });

kubectl apply

const objects = [
    {
        "apiVersion": "apps/v1",
        "kind": "Deployment",
        "metadata": {
            "labels": {
                "app": "nginx"
            },
            "name": "nginx",
            "namespace": "default"
        },
        "spec": {
            "selector": {
                "matchLabels": {
                    "app": "nginx"
                }
            },
            "template": {
                "metadata": {
                    "labels": {
                        "app": "nginx"
                    }
                },
                "spec": {
                    "containers": [
                        {
                            "image": "nginx",
                            "name": "nginx"
                        }
                    ]
                }
            }
        }
    },
    {
        "apiVersion": "apps/v1",
        "kind": "Deployment",
        "metadata": {
            "labels": {
                "app": "nginx"
            },
            "name": "nginx",
            "namespace": "my-namespace",
        },
        "spec": {
            "selector": {
                "matchLabels": {
                    "k8s-app": "nginx"
                }
            },
            "template": {
                "metadata": {
                    "labels": {
                        "k8s-app": "nginx"
                    }
                },
                "spec": {
                    "containers": [
                        {
                            "image": "nginx",
                            "name": "nginx"
                        }
                    ]
                }
            }
        }
    }
]
const result = await kubectl.apply(objects);

To-Dos

  • [ ] (Not sure) Support list resources using get (= conbine list and get)
  • [ ] (Not sure) Support delete with labelSelect
  • [ ] (Not sure) Support other methods (create, label, annotate, taint, drain, cordon, uncordon, etc.)

Readme

Keywords

none

Package Sidebar

Install

npm i @pubgcorp/kubectl-node

Weekly Downloads

4

Version

1.5.0

License

MIT

Unpacked Size

69.2 kB

Total Files

42

Last publish

Collaborators

  • dirty49374
  • mnthe
  • bitofsky
  • pril
  • talkwithraon
  • byeonggukgong
  • dalpark
  • pubg-suchanpark
  • feelyoung-jay