Kubernetes Watch API for node.
$ npm i kube-watchnew KubeWatch(resource, options) -> EventEmitter
import KubeWatch from '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
namespacesendpointseventslimitrangespersistentvolumeclaimspersistentvolumespodspodtemplatesreplicationcontrollersresourcequotassecretsserviceaccountsservice
API v1beta1
horizontalpodautoscalersingressesjobs
KubeWatch will attempt to automatically select the correct api version depending on requested resource, but you can override it like so:
const ingresses = new KubeWatch('ingresses', {
url: 'http://kube-api-server',
version: 'extensions/v1beta1'
});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 :
labelSelectorfieldSelectorresourceVersiontimeoutSeconds
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:watchSingle run :
$ env KUBE_API_SERVER=http://kube-api-server \
npm run test:singleRun tests using minikube to simulate workload.
See test/run-test.sh.
$ npm test- Improve test suites by simulating workload