npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

k8s-es5

v1.0.3

Published

Node.js client library for Google's Kubernetes Kubectl And API

Downloads

5

Readme

Nodejs Kubernetes client

Node.js client library for Google's Kubernetes Kubectl And API

build

    git clone https://github.com/Goyoo/node-k8s-client.git
    npm install
    npm run build

#test for test please install minikube

    
    mocha test

Install:

    npm install k8s

Usage

Create client

var K8s = require('k8s')

// use kubectl

var kubectl = K8s.kubectl({
    endpoint:  'http://192.168.10.10:8080'
    , namespace: 'namespace'
    , binary: '/usr/local/bin/kubectl'
})

//use restful api
var kubeapi = K8s.api({
	endpoint: 'http://192.168.10.10:8080'
	, version: '/api/v1'
})

// Configure using kubeconfig
var kube = K8s.kubectl({
	binary: '/bin/kubectl'
	,kubeconfig: '/etc/cluster1.yaml'
	,version: '/api/v1'
});

Options

endpoint : URL for API

version : API Version

binary : Path to binary file

kubeconfig : Path to kubeconfig

:auth See below authentication section

:strictSSL If set to false, use of the API will not validate SSL certificate. Defualt is true.

Authentication

Authentication to REST API is done via the auth option. Currently supported authentication method types are username/password, token and client certificate. Presence of authentication details is checked in this order so if a token is specified as well as a client certificate then a token will be used.

Username/password:

{
  "auth": {
    "username": "admin",
    "password": "123123"
  }
}

Token:

{
  "auth": {
    "token": "hcc927ndkcka12"
  }
}

Client certificate:

{
  "auth": {
    "clientKey": fs.readFileSync('k8s-client-key.pem'),
    "clientCert": fs.readFileSync('k8s-client-cert.pem'),
    "caCert": fs.readFileSync('k8s-ca-crt.pem')
  }
}

kubeAPI

using callback

// method GET
kubeapi.get('namespaces/default/replicationcontrollers', function(err, data){})

// method POST
kubeapi.post('namespaces/default/replicationcontrollers', require('./rc/nginx-rc.json'), function(err, data){})
// method PUT
kubeapi.put('namespaces/default/replicationcontrollers/nginx', require('./rc/nginx-rc.json'), function(err, data){})
// method PATCH
kubeapi.patch('namespaces/default/replicationcontrollers/nginx', [{ op: 'replace', path: '/spec/replicas', value: 2 }], function(err, data){})
// method DELETE
kubeapi.delete('namespaces/default/replicationcontrollers/nginx', function(err, data){})

using promise

// method GET
kubeapi.get('namespaces/default/replicationcontrollers').then(function(data){}).catch(function(err){})
// method POST
kubeapi.post('namespaces/default/replicationcontrollers', require('./rc/nginx-rc.json')).then(function(data){}).catch(function(err){})
// method PUT
kubeapi.put('namespaces/default/replicationcontrollers/nginx', require('./rc/nginx-rc.json')).then(function(data){}).catch(function(err){})
// method PATCH
kubeapi.patch('namespaces/default/replicationcontrollers/nginx', [{ op: 'replace', path: '/spec/replicas', value: 2 }]).then(function(data){}).catch(function(err){})
// method DELETE
kubeapi.delete('namespaces/default/replicationcontrollers/nginx').then(function(data){}).catch(function(err){})

using async/await


!async function()
{
    try
    {
        // method GET
        const data1 = await kubeapi.get('namespaces/default/replicationcontrollers')
        // method POST
        const data2 = await kubeapi.post('namespaces/default/replicationcontrollers', require('./rc/nginx-rc.json'))
        // method PUT
        const data3 = await kubeapi.put('namespaces/default/replicationcontrollers/nginx', require('./rc/nginx-rc.json'))
        // method PATCH
        const data4 = await kubeapi.patch('namespaces/default/replicationcontrollers/nginx', [{ op: 'replace', path: '/spec/replicas', value: 2 }])
        // method DELETE
        const data5 = await kubeapi.delete('namespaces/default/replicationcontrollers/nginx')
    }
    catch(err){
        console.log(err)
    }
}()

method GET -> watch

using callback
var res = kubeapi.watch('watch/namespaces/default/pods', function(data){
	// message
}, function(err){
	// exit
}, [timeout])
using rxjs
kubeapi.watch('watch/namespaces/default/pods', [timeout]).subscribe(data=>{
    // message
}, err=>{
    // exit
})

kubectl (callback, promise, async/await)

example

    //kubectl['type']['action]([arguments], [flags], [callback]): Promise

    //callback
    kubect.pod.delete('pod_name', function(err, data){})
    kubect.pod.delete('pod_name', ['--grace-period=0'], function(err, data){})
    //promise
    kubect.pod.delete('pod_name').then()
    kubect.pod.delete('pod_name', ['--grace-period=0']).then()
    //async/await
    const data = await kubect.pod.delete('pod_name')
    const data = await kubect.pod.delete('pod_name',['--grace-period=0'])

excute command

    kubectl.command('get pod pod_name --output=json', function(err, data){})
    kubectl.command('get pod pod_name --output=json').then()
    const data = await kubectl.command('get pod pod_name --output=json')

Pods

get pod list

kubectl.pod.list(function(err, pods){})

//selector
var label = { name: nginx }
kubectl.pod.list(label, function(err, pods){})

get pod

kubectl.pod.get('nginx', function(err, pod){})

// label selector
kubectl.pod.list({ app: 'nginx' }, function(err, pods){}) 

create a pod

kubectl.pod.create('/:path/pods/nginx.yaml'), function(err, data){})

delete a pod

kubectl.pod.delete('nginx', function(err, data){})

log

kubectl.pod.log('pod_id1 pod_id2 pod_id3', function(err, log){})

ReplicationController

get rc list

kubectl.rc.list(function(err, pods){})

get a rc

kubectl.rc.get('nginx', function(err, pod){})

create a rc

kubectl.rc.create('/:path/pods/nginx.yaml'), function(err, data){})

delete a rc

kubectl.rc.delete('nginx', function(err, data){})

rolling-update by image name

kubectl.rc.rollingUpdate('nginx', 'nginx:vserion', function(err, data){})

rolling-update by file

kubectl.rc.rollingUpdateByFile('nginx', '/:path/rc/nginx-v2.yaml', function(err, data){})

change replicas

kubectl.rc.scale('nginx', 3, function(err, data){})

Service

get service list

kubectl.service.list(function(err, pods){})

get a service

kubectl.service.get('nginx', function(err, pod){})

create a service

kubectl.service.create('/:path/service/nginx.yaml'), function(err, data){})

delete a service

kubectl.service.delete('nginx', function(err, data){})

Node

get node list

kubectl.node.list(function(err, pods){})

get a node

kubectl.node.get('nginx', function(err, pod){})

create a node

kubectl.node.create('/:path/nodes/node1.yaml'), function(err, data){})

delete a node

kubectl.node.delete('nginx', function(err, data){})

Namespace

    kubectl.namespace['fn']

Daemonset

    kubectl.daemonset['fn']

Deployment

    kubectl.deployment['fn']

Secrets

    kubectl.secrets['fn']

endpoint

    kubectl.endpoint['fn']

ingress

    kubectl.ingress['fn']