How to use the @kubernetes/client-node.BatchV1Api 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 hyperflow-wms / hyperflow / functions / k8sCommand.js View on Github external
async function k8sCommand(ins, outs, context, cb) {

  let handlerStart = Date.now();
  console.log("[DEBUG] K8sInvoke called.");
  // let cluster = await getCluster();
  // const token = await getGCPToken();

  const kc = new k8s.KubeConfig();
  kc.loadFromDefault(); // loadFromString(JSON.stringify(kconfig))

  const k8sApi = kc.makeApiClient(k8s.BatchV1Api);
  try {
    var command = 'hflow-job-execute ' + context.taskId + ' ' + context.redis_url;
    var containerName = process.env.HF_VAR_WORKER_CONTAINER;
    var volumePath = '/work_dir';
    var jobName = Math.random().toString(36).substring(7);
    //var jobName = context.name + "_" + context.taskId.replace(/:/g, '_');

    // Load definition of the the worker job pod
    // File 'job-template.yaml' should be provided externally during deployment
    var job_template_path = process.env.HF_VAR_JOB_TEMPLATE_PATH || "./job-template.yaml";
    var jobYaml = fs.readFileSync(job_template_path, 'utf8');
    //var job = yaml.safeLoad(eval('`'+jobYaml+'`')); // this works, but eval unsafe

    // use string replacement instead of eval to evaluate job template
    // 'params' should contain values for variables to be replaced in job template yaml
    var params = {
github atomist / sdm-core / lib / pack / k8s / KubernetesGoalScheduler.ts View on Github external
export async function deleteJob(job: { name: string, namespace: string }): Promise {
    try {
        const kc = loadKubeConfig();
        const batch = kc.makeApiClient(k8s.BatchV1Api);

        await batch.readNamespacedJob(job.name, job.namespace);
        try {
            await batch.deleteNamespacedJob(
                job.name,
                job.namespace,
                { propagationPolicy: "Foreground" } as any);
        } catch (e) {
            logger.warn(`Failed to delete k8s jobs '${job.namespace}:${job.name}': ${
                prettyPrintError(e)}`);
        }
    } catch (e) {
        // This is ok to ignore because the job doesn't exist any more
    }
}
github stanford-oval / almond-cloud / training / backends / kubernetes.js View on Github external
// Copyright 2019 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna 
//
// See COPYING for details
"use strict";

const k8s = require('@kubernetes/client-node');
const Tp = require('thingpedia');

const Config = require('../../config');

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

const k8sApi = kc.makeApiClient(k8s.BatchV1Api);
const watcher = new class JobWatcher extends Tp.Helpers.RefCounted {
    constructor() {
        super();

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

        this._watchedJobs = new Map;
    }

    watch(jobName, callbacks) {
        this._watchedJobs.set(jobName, callbacks);
        // Number of tries to watch job status. Setting to a negative number will try indefinitely.
        this._numTriesLeft = parseInt(Config.TRAINING_WATCH_NUM_TRIES || 5);
    }
github stanford-oval / almond-cloud / tests / unit / test_k8s_api.js View on Github external
],
    contexts: [
        {
            cluster: 'cluster',
            user: 'user',
        }
    ],
    users: [
        {
            name: 'user',
        }
    ],
};
const kc = new k8s.KubeConfig();
Object.assign(kc, fakeConfig);
const k8sApi = kc.makeApiClient(k8s.BatchV1Api);
const k8sCore = kc.makeApiClient(k8s.CoreV1Api);

function testDeleteNamespacedJob() {
    const args = getArgs(k8sApi.deleteNamespacedJob);
    assert.strictEqual(args[0], 'name');
    assert.strictEqual(args[1], 'namespace');
    assert.strictEqual(args[6], 'propagationPolicy');
}

function testListEndpointsForAllNamespaces() {
    const args = getArgs(k8sCore.listEndpointsForAllNamespaces)
    assert.strictEqual(args[2], 'fieldSelector');
}

function main() {
    testDeleteNamespacedJob();
github atomist / sdm-core / lib / pack / k8s / KubernetesGoalScheduler.ts View on Github external
export async function listJobs(labelSelector?: string): Promise {
    const kc = loadKubeConfig();
    const batch = kc.makeApiClient(k8s.BatchV1Api);

    if (configurationValue("sdm.k8s.job.singleNamespace", true)) {
        const podNs = await readNamespace();
        return (await batch.listNamespacedJob(
            podNs,
            undefined,
            undefined,
            undefined,
            undefined,
            labelSelector,
        )).body.items;
    } else {
        return (await batch.listJobForAllNamespaces(
            undefined,
            undefined,
            undefined,