示例#1
0
    constructor(logger, clientConfig, defaultNamespace = 'default') {
        this.logger = logger;
        this.defaultNamespace = defaultNamespace;

        if (clientConfig) {
            this.client = new Client({
                config: clientConfig
            });
        } else if (process.env.KUBERNETES_SERVICE_HOST && process.env.KUBERNETES_SERVICE_PORT) {
            // configures the client when running inside k8s
            try {
                this.client = new Client({ config: config.getInCluster() });
            } catch (e) {
                logger.error(`Unable to create k8s Client using getInCluster: ${e}`);
                // throw e;
            }
        } else {
            // configures the client from ~/.kube/config when running outside k8s
            try {
                this.client = new Client({ config: config.fromKubeconfig() });
            } catch (e) {
                logger.error(`Unable to create k8s Client using fromKubeconfig: ${e}`);
                // throw e;
            }
        }
    }
示例#2
0
async function main () {
  try {
    const client = new Client({ config: config.fromKubeconfig(), version: '1.9' })

    // Pod with single container
    let res = await client.api.v1.namespaces('namespace_name').pods('pod_name').exec.post({
      qs: {
        command: ['ls', '-al'],
        stdout: true,
        stderr: true
      }
    })
    console.log(res.body)
    console.log(res.messages)

    // Pod with multiple containers /must/ specify a container
    res = await client.api.v1.namespaces('namespace_name').pods('pod_name').exec.post({
      qs: {
        command: ['ls', '-al'],
        container: 'container_name',
        stdout: true,
        stderr: true
      }
    })
    console.log(res.body)
  } catch (err) {
    console.error('Error: ', err)
  }
}
示例#3
0
async function main () {
  try {
    const client = new Client({ config: config.fromKubeconfig(), version: '1.9' })

    // Pod with single container
    let logs = await client.api.v1.namespaces('namespace_name').pods('pod_name').log.get()
    console.log(logs.body)

    // Pod with multiple containers
    logs = await client.api.v1.namespaces('namespace_name').pods('pod_name').log.get({
      qs: {
        container: 'container_name'
      }
    })
    console.log(logs.body)
  } catch (err) {
    console.error('Error: ', err)
  }
}
示例#4
0
'use strict';

const Client = require('kubernetes-client').Client;
const config = require('kubernetes-client').config;
const logger = require('../lib/logger');

var client;

if (process.env.NODE_ENV === 'production') {
	client = new Client({config: config.getInCluster()});
} else { // For development we use our local kubeconfig
	client = new Client({config: config.fromKubeconfig()});
}

module.exports.pods = async namespace => {
	await client.loadSpec();
	return await client.api.v1.namespaces(namespace).pods.get()
		.then(result => {
			return result;
		})
		.catch(err => {
			logger(err, this);
			return err;
		});
};
示例#5
0
function getConfig(context) {
  return isRunningInK8s()
    ? config.getInCluster()
    : config.fromKubeconfig(null, context);
}