Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'kubernetes-client' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
async function main () {
try {
const client = new Client({
config: {
url: process.env.K8S_CLUSTER_HOST,
auth: {
user: process.env.K8S_USER,
pass: process.env.K8S_PASSWORD
},
insecureSkipTlsVerify: true
},
version: process.env.K8S_CLUSTER_VERSION
})
//
// Fetch all the pods
const pods = await client.api.v1.pods.get()
pods.body.items.forEach((item) => {
console.log(item.metadata)
//
// You can create a Deployment and Service to experiment with using the
// coalmine example:
//
// $ kubectl apply -f examples/coalmine-deploy.json
// $ kubectl expose deployment coalmine --type=NodePort --selector='app=coalmine,state=stable'
// $ minikube service coalmine --url
//
const Client = require('kubernetes-client').Client
const config = require('kubernetes-client').config
const JSONStream = require('json-stream')
const namespace = 'default'
const deployment = 'coalmine'
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' })
function watchPod (pod) {
const podClient = client.api.v1.namespaces(namespace).pods(pod)
const stream = podClient.log.getStream({ qs: { follow: true } })
const jsonStream = new JSONStream()
stream.pipe(jsonStream)
jsonStream.on('data', async object => {
console.log('Log event:', JSON.stringify(object, null, 2))
if (object.level === 'error') {
console.warn(`Error in ${pod}`)
await podClient.patch({
body: {
metadata: {
labels: {
state: 'failed'
async function main () {
try {
const client = new Client({ version: '1.9' })
//
// Get all the Namespaces.
//
const namespaces = await client.api.v1.namespaces.get()
console.log('Namespaces: ', namespaces)
//
// Create a new Deployment.
//
const create = await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest })
console.log('Create: ', create)
//
// Fetch the Deployment we just created.
//
async function main () {
try {
const client = new Client({
config: config.fromKubeconfig(),
version: '1.12'
})
//
// Add endpoints to our client
//
client.addCustomResourceDefinition(vpa)
//
// List all the resources of the new type
//
const all = await client.apis['autoscaling.k8s.io'].v1beta2.namespaces('default').verticalpodautoscalers.get()
console.log('All VPAs: ', all)
//
async function main () {
try {
const client = new Client({ config: config.fromKubeconfig(), version: '1.10' })
// Create a deployment
const create = await client.apis.apps.v1.ns('default').deploy.post({ body: deploymentManifest })
console.log('Create: ', create)
// Update the deployment
// Change the image from nginx:1.7.9 to nginx:1.9.1
const updateImage = await client.apis.apps.v1.ns('default').deploy('nginx-deployment').patch({
body: {
spec: {
template: {
spec: {
containers: [{
name: 'nginx',
image: 'nginx:1.9.1'
}]
async function main () {
try {
const client = new Client({ config: config.fromKubeconfig() })
//
// Load the /swagger.json from the kube-apiserver specified in config.fromKubeconfig()
//
await client.loadSpec()
const create = await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest })
console.log('Result: ', create)
} catch (err) {
console.error('Error: ', err)
}
}
async init () {
const backend = new Request(this.apiServerConfig)
const client = new Client({ backend })
await client.loadSpec()
// If "frameworkcontroller.microsoft.com" is not found, try to load it from crd.
if (client.apis['frameworkcontroller.microsoft.com'] === undefined) {
const frameworkDef = await client.apis['apiextensions.k8s.io'].v1beta1.customresourcedefinitions('frameworks.frameworkcontroller.microsoft.com').get()
client.addCustomResourceDefinition(frameworkDef.body)
}
this.client = client
}
global.codewind = {
RUNNING_IN_K8S: false,
MULTI_USER: (process.env.MULTI_USER == 'true') || false,
EXTENSIONS: (process.env.EXTENSIONS == 'true') || false,
// Workspace location *inside* the our container.
// Volume is mounted from WORKSPACE_DIRECTORY in docker-compose.yaml.
CODEWIND_WORKSPACE: '/codewind-workspace/',
CODEWIND_TEMP_WORKSPACE: '/cw-temp/',
// temporary mounted workspace directory retained for projects that still require it
MOUNTED_WORKSPACE: '/mounted-workspace'
};
// find if running in kubernetes and build up a whitelist of allowed origins
try {
k8Client = new K8Client({ config: k8config.getInCluster(), version: '1.9' });
}
catch (err) {
log.info('Codewind does not appear to be running in k8s')
global.codewind.RUNNING_IN_K8S = false;
}
const originsWhitelist = []
try {
if (k8Client) {
log.info('Codewind is running in k8s');
global.codewind.RUNNING_IN_K8S = true;
global.codewind.k8Client = k8Client;
// get current ingress path - it is passed in an env var
// https://github.com/eclipse/codewind-che-plugin/blob/master/codewind-che-sidecar/scripts/kube/codewind_template.yaml#L135
const INGRESS_HOST_ENVVAR = 'CHE_INGRESS_HOST';
import { Operation } from "../projects/operation";
const execAsync = promisify(exec);
import * as path from "path";
import * as logger from "./logger";
import { ContainerStates } from "../projects/constants";
import * as processManager from "./processManager";
import { ProcessResult } from "./processManager";
import { ProjectInfo } from "../projects/Project";
import * as projectExtensions from "../extensions/projectExtensions";
const Client = require("kubernetes-client").Client; // tslint:disable-line:no-require-imports
const config = require("kubernetes-client").config; // tslint:disable-line:no-require-imports
let k8sClient: any = undefined;
if (process.env.IN_K8) {
k8sClient = new Client({ config: config.getInCluster(), version: "1.9"});
}
const KUBE_NAMESPACE = process.env.KUBE_NAMESPACE || "default";
export interface PodInfo {
podName: string;
ip: string;
serviceName: string;
exposedPort: string;
internalPort: string;
podPorts: string[];
}
/**
* @function
* @description Get kube pod info.
// Check to see if we are passing in a username/password
const accessToken = await getTokenFromBasicAuth({ insecureSkipTlsVerify: kubeconfig.insecureSkipTlsVerify, url: kubeconfig.url, user: kubeconfig.auth.username || kubeconfig.auth.user, password: kubeconfig.auth.password || kubeconfig.auth.pass });
kubeconfig = {
url: kubeconfig.url,
auth: {
bearer: accessToken
},
insecureSkipTlsVerify: 'insecureSkipTlsVerify' in kubeconfig ? Boolean(kubeconfig.insecureSkipTlsVerify) : false
};
}
}
}
const kbconfig = new KubeConfig();
const client = new Client({ backend: kubeconfig || kbconfig.loadFromDefault(), spec, getNames: getNames });
// CRD with the service instance stuff, but only to this client, not the cluster
client.addCustomResourceDefinition(serviceCatalogCRD);
return client;
}