GithubHelp home page GithubHelp logo

kube-watch's Introduction

kube-watch

Kubernetes Watch API for node.

Forked from subk/kube-watch

$ npm i @baxmusic/kube-watch

new KubeWatch(resource, options) -> EventEmitter

import KubeWatch from '@baxmusic/kube-watch';

const pods = new KubeWatch('pods', {
  url: 'http://kube-api-server'   // Kubernetes API URL
});

pods
  .on('added', event => {
    console.log('Pod %s added to namespace %s with IP address %s',
      event.metadata.name, event.metadata.namespace, event.status.podIP);
  })
  .on('modified', event => {
    // do something...
  })
  .on('deleted', event => {
    // ..do something else..
  })
  .on('error', err => {
    console.error('Error %d: %s', err.code, err.message);
  });

By default, kube-watch will first request Kubernetes API to fetch the last resourceVersion for requested resource. See Kubernetes documentation for more details.
If you want to specify resourceVersion manually, see Query Parameters section.

See Kubernetes API documentation for more details.

API v1

  • namespaces
  • endpoints
  • events
  • limitranges
  • persistentvolumeclaims
  • persistentvolumes
  • pods
  • podtemplates
  • replicationcontrollers
  • resourcequotas
  • secrets
  • serviceaccounts
  • service

API v1beta1

  • horizontalpodautoscalers
  • ingresses
  • jobs

API version will be automatically selected depending on requested resource.

Watch all services in namespace public :

const services = new KubeWatch('services', {
  url: 'http://kube-api-server',
  namespace: 'public'
});

Only watch service named www in namespace public :

const services = new KubeWatch('services', {
  url: 'http://kube-api-server',
  namespace: 'public',
  name: 'www'
});

You can filter which events will be emitted using events option.
By default, kube-watch will emit all k8s events: added, modified, deleted.

const namespaces = new KubeWatch('namespaces', {
  url: 'http://kube-api-server',
  events: [ 'added' ]   // watcher will only emit 'added' event
});

These extra query parameters are supported as option :

  • labelSelector
  • fieldSelector
  • resourceVersion
  • timeoutSeconds
const services = new KubeWatch('services', {
  url: 'http://kube-api-server',
  labelSelector: 'public-http',
  fieldSelector: 'event.status.podIP',
  resourceVersion: '6587423'
});

See documentation for more details about these options.

HTTP requests are performed using request package.
Pass custom options using request property.

const services = new KubeWatch('services', {
  url: 'http://kube-api-server',
  request: {
    timeout: 30000    // change HTTP request timeout
  }
});

See request's http authentication

const services = new KubeWatch('services', {
  url: 'http://kube-api-server',
  request: {
    auth: {
      user: 'foobar'
      pass: 'el8'
    }
  }
});

See request's TLS/SSL support.

const services = new KubeWatch('services', {
  url: 'http://kube-api-server',
  request: {
    cert: fs.readFileSync(certFile),
    key: fs.readFileSync(keyFile),
    passphrase: 'password',
    ca: fs.readFileSync(caFile)
  }
});

Run test.js in watch mode :

$ env KUBE_API_SERVER=http://kube-api-server \
    npm run test:watch

Single run :

$ env KUBE_API_SERVER=http://kube-api-server \
    npm run test:single

Run tests using minikube to simulate workload.
See test/run-test.sh.

$ npm test
  • Improve test suites by simulating workload

kube-watch's People

Contributors

leroy0211 avatar ryuheechul avatar subk avatar

Watchers

Daniel Kraaij avatar James Cloos avatar  avatar Daniel Kraaij avatar  avatar  avatar

kube-watch's Issues

Add 'end' event when a watch is terminated

Add an end event for when a watch is terminated.

Use case:

When watching is terminated, the service will no longer be watching changes. When using health-checks, this can trigger an unhealthy pod so k8s will restart the pod.

Update documentation to watch "forever"

The documentation needs some updating, because watching "forever" is not built-in. But it's possible by listening to the end event and starting a new watch based on the previously given resourceVersion.

Maybe we need to have it built-in by default.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.