How to use the @kubernetes/client-node.Watch function in @kubernetes/client-node

To help you get started, we’ve selected a few @kubernetes/client-node examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kubernetes-client / javascript / examples / typescript / watch / watch-example.ts View on Github external
import * as k8s from '@kubernetes/client-node';

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const watch = new k8s.Watch(kc);
const req = watch.watch('/api/v1/namespaces',
    // optional query parameters can go here.
    {},
    // callback is called for each received object.
    (type, obj) => {
        if (type === 'ADDED') {
            // tslint:disable-next-line:no-console
            console.log('new object:');
        } else if (type === 'MODIFIED') {
            // tslint:disable-next-line:no-console
            console.log('changed object:');
        } else if (type === 'DELETED') {
            // tslint:disable-next-line:no-console
            console.log('deleted object:');
        } else {
            // tslint:disable-next-line:no-console
github jenkins-x / vscode-jx-tools / src / KubeWatcher.ts View on Github external
connect() {
        if (!this.connected) {
            this.connected = true;

            let kc = new k8s.KubeConfig();
            let configFile = process.env['HOME'] + '/.kube/config';
            try {
                kc.loadFromFile(configFile);
            } catch (e) {
                console.log('error reading ' + configFile + ': ' + e.message);
                throw e;
            }
            
            //let resourceVersion = 0
            let watch = new k8s.Watch(kc);

            // optional query parameters can go here.
            // TODO filter on labels once we add them to Activities
            const queryParameters = {};

            // callback is called for each received object.
            const callback = (type: any, obj: any) => {
                if (type === 'ADDED') {
                    this.notify(CallbackKind.ADD, obj);
                }
                else if (type === 'MODIFIED') {
                    this.notify(CallbackKind.UPDATE, obj);
                }
                else if (type === 'DELETED') {
                    this.notify(CallbackKind.DELETE, obj);
                }
github kyma-project / console / tests / ui-tests / setup / helm-broker / configurer.js View on Github external
async watch(path, queryParams, callbackFn, doneFn, name) {
    const watch = new k8s.Watch(this.kubeConfig);

    const promise = new Promise((resolve, reject) => {
      let req;

      const resolveFn = x => {
        if (req) {
          req.abort();
        }
        resolve(x);
      };

      req = watch.watch(
        path,
        queryParams,
        callbackFn(resolveFn, reject),
        doneFn(resolveFn, reject),
github kubernetes-client / javascript / examples / cache-example.js View on Github external
const k8s = require('@kubernetes/client-node');

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

const path = '/api/v1/namespaces/default/pods';
const watch = new k8s.Watch(kc);
const listFn = (fn) => {
    k8sApi.listNamespacedPod('default')
        .then((res) => {
            fn(res.body.items);
        })
        .catch((err) => {
            console.log(err);
        });
}
const cache = new k8s.ListWatch(path, watch, listFn);

const looper = () => {
    const list = cache.list('default');
    if (list) {
        let names = [];
        for (let i = 0; i < list.length; i++) {
github NERC-CEH / datalab / code / workspaces / infrastructure-api / src / kubeWatcher / kubeWatcher.js View on Github external
function kubeWatcher() {
  logger.info(`Starting kube-watcher, listening for pods labelled "${SELECTOR_LABEL}" on any namespace.`);
  return new k8s.Watch(kubeConfig).watch(watchUrl, selector, eventHandler, errorHandler);
}
github stanford-oval / almond-cloud / training / backends / kubernetes.js View on Github external
constructor() {
        super();

        this._watcher = new k8s.Watch(kc);
        this._req = null;

        this._watchedJobs = new Map;
    }
github ForetagInc / fullstack-ts-boilerplate / apps / api / src / app / common.module.ts View on Github external
      useFactory: (kc: KubeConfig) => new Watch(kc),
      inject: [KubeConfig],