js-kube-api

A javascript interface for Kubernetes API

Usage no npm install needed!

<script type="module">
  import jsKubeApi from 'https://cdn.skypack.dev/js-kube-api';
</script>

README

k8sNodeApi

A javascript interface for Kubernetes API

Maintenance HitCount npm

Getting Started

For those that want to use Nodejs to interface the Kubernetes API, here is been developed a API using native nodejs modules, to be used as in-cluster pods that triggers jobs.

Prerequisites

  • nodejs 12.x +

Installing

npm install k8snodeapi --save

Testing

This project uses Mocha-Chai combination in /test folder

npm test

A server example

const { Crud } = require('4crud')
const Client = require('../k8sInClient')

const server = new Crud()
const jobClient = new Client()

server
  .get((req, res) => {
    jobClient.read('default', req.search.get('name'), (response) => {
      res.statusCode = 200
      res.setHeader('Content-Type', 'application/json')
      res.write(JSON.stringify(response))
      res.end()
    })
  })
  .post((req, res) => {
    jobClient.create('default', req.body, (response) => {
      res.statusCode = 200
      res.setHeader('Content-Type', 'application/json')
      res.write(JSON.stringify(response))
      res.end()
    })
  })
  .delete((req, res) => {
    jobClient.delete('default', req.search.get('name'), (response) => {
      res.statusCode = 200
      res.setHeader('Content-Type', 'application/json')
      res.write(JSON.stringify(response))
      res.end()
    })
  })
  .start(3000) //start server at port 3000

POST / PUT requests localhost:3000 with JSON data:

const jobYaml = {
  "apiVersion": "batch/v1",
  "kind": "Job",
  "metadata": {
      "name": "pi-with-ttl"
  },
  "spec": {
      "ttlSecondsAfterFinished": 10,
      "template": {
          "spec": {
              "containers": [{
                  "name": "pi",
                  "image": "perl",
                  "command": [
                      "perl",
                      "-Mbignum=bpi",
                      "-wle",
                      "print bpi(2000)"
                  ]
              }],
              "restartPolicy": "Never"
          }
      }
  }
}

GET / DELETE requests to localhost:3000?name=pi-with-ttl

Running

npm start

Release History

  • 0.0.1
    • CHANGE: Work in progress

Authors

  • GH3S - Initial work - gh3s@protonmail.ch

License

This project is licensed under the MIT License - see the LICENSE file for details

Contributing

  1. Fork it (https://github.com/gh3s/k8snodeapi/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request