Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const k8s = require('@kubernetes/client-node');
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
var namespace = {
metadata: {
name: 'test'
}
};
k8sApi.createNamespace(namespace).then(
(response) => {
console.log('Created namespace');
console.log(response);
k8sApi.readNamespace(namespace.metadata.name).then(
(response) => {
console.log(response);
k8sApi.deleteNamespace(
namespace.metadata.name, {} /* delete options */);
async kurl(root: any, args: any, context: Context): Promise {
context.requireSingleTenantSession();
// this isn't stored in the database, it's read in realtime
// from the cluster
try {
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const res = await k8sApi.listNode();
const nodes = _.map(res.body.items, async (item) => {
let usageStats = {
availableCPUs: -1,
availableMemory: -1,
availablePods: -1
};
const address = _.find(item.status!.addresses || [], { type: "InternalIP" });
if (address) {
try {
const nodeIP = address.address;
let uri = `http://${nodeIP}:10255/stats/summary`;
const options: RequestPromiseOptions = {
method: "GET",
};
async function containerStarted(container: K8sContainer, attempts: number = 240): Promise {
let core: k8s.CoreV1Api;
try {
core = container.config.makeApiClient(k8s.CoreV1Api);
} catch (e) {
e.message = `Failed to create Kubernetes core API client: ${e.message}`;
container.log.write(e.message);
throw e;
}
const sleepTime = 500; // ms
for (let i = 0; i < attempts; i++) {
await sleep(500);
const pod = (await core.readNamespacedPod(container.pod, container.ns)).body;
const containerStatus = pod.status.containerStatuses.find(c => c.name === container.name);
if (containerStatus && (!!_.get(containerStatus, "state.running.startedAt") || !!_.get(containerStatus, "state.terminated"))) {
const message = `Container '${container.name}' started`;
container.log.write(message);
return;
}
const { Pod, Container } = require('kubernetes-models/v1')
const k8s = require('@kubernetes/client-node')
const kc = new k8s.KubeConfig()
// Using the default credentials for kubectl
kc.loadFromDefault()
const k8sApi = kc.makeApiClient(k8s.CoreV1Api)
function createPod(environment = 'production') {
return new Pod({
metadata: {
name: 'test-pod',
},
spec: {
containers: [
new Container({
name: 'test-container',
image: 'nginx',
env: [{ name: 'ENV', value: environment }],
}),
],
},
})
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();
testListEndpointsForAllNamespaces();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const k8s = require("@kubernetes/client-node");
const Table = require("easy-table");
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const appsV1Api = kc.makeApiClient(k8s.AppsV1Api);
const coreV1Api = kc.makeApiClient(k8s.CoreV1Api);
const roles = ['blue', 'green'];
getDeployments().catch(console.error.bind(console));
function getDeployments() {
return __awaiter(this, void 0, void 0, function* () {
const deploymentsRes = yield appsV1Api.listNamespacedDeployment('default');
let deployments = [];
for (const deployment of deploymentsRes.body.items) {
let role = deployment.spec.template.metadata.labels.role;
if (role && roles.includes(role)) {
deployments.push({
name: deployment.metadata.name,
role,
status: deployment.status.conditions[0].status,
image: deployment.spec.template.spec.containers[0].image,
ports: [],
services: []
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');
constructor() {
const kc = new k8s.KubeConfig();
kc.loadFromCluster();
this.k8sAppsApi = kc.makeApiClient(k8s.AppsV1Api);
this.k8sCoreApi = kc.makeApiClient(k8s.CoreV1Api);
}
async function loadService(kubeConfig, namespace, serviceName) {
const coreApi = kubeConfig.makeApiClient(k8s.CoreV1Api)
try {
return (await coreApi.readNamespacedService(serviceName, namespace)).body
} catch (error) {
throw k8nApiPrettyError(error, { _object: `Service "${serviceName}"` })
}
}
const k8s = require('@kubernetes/client-node');
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
//const k8sApi2 = kc.makeApiClient(k8s.ExtensionsV1beta1Api);
let data = {};
async function fetchLocal(data={}){
await k8sApi.listNamespacedPod('default').then((res) => {
const metaDat = {};
for(let meta in res.body.items[0].metadata){
if(res.body.items[0].metadata[meta]){
metaDat[meta] = res.body.items[0].metadata[meta]
}
}