Skip to content

Commit

Permalink
examples: port missing examples from master to main
Browse files Browse the repository at this point in the history
This commit ports the remaining examples from the master branch
to the main branch.

Fixes: #1907
  • Loading branch information
cjihrig committed Jan 4, 2025
1 parent 622a23e commit 4b98e59
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 0 deletions.
17 changes: 17 additions & 0 deletions examples/kubectl/equivalents/namespace-create-yaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as k8s from '@kubernetes/client-node';
import { readFileSync } from 'node:fs';

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

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

// This code is the JavaScript equivalent of `kubectl apply -f namespace.yaml`.

try {
const namespaceYaml = k8s.loadYaml(readFileSync('./namespace.yaml', 'utf8'));
const createdNamespace = await k8sApi.createNamespace({ body: namespaceYaml });
console.log('New namespace created:', createdNamespace);
} catch (err) {
console.error(err);
}
20 changes: 20 additions & 0 deletions examples/kubectl/equivalents/namespace-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as k8s from '@kubernetes/client-node';

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

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

// This code is the JavaScript equivalent of `kubectl create namespace test`.

try {
const namespace = {
metadata: {
name: 'test',
},
};
const createdNamespace = await k8sApi.createNamespace({ body: namespace });
console.log('New namespace created:', createdNamespace);
} catch (err) {
console.error(err);
}
15 changes: 15 additions & 0 deletions examples/kubectl/equivalents/namespace-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as k8s from '@kubernetes/client-node';

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

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

// This code is the JavaScript equivalent of `kubectl get ns`.

try {
const namespaces = await k8sApi.listNamespace();
namespaces.items.forEach((namespace) => console.log(namespace.metadata.name));
} catch (err) {
console.error(err);
}
4 changes: 4 additions & 0 deletions examples/kubectl/equivalents/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: test
33 changes: 33 additions & 0 deletions examples/kubectl/equivalents/pod-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as k8s from '@kubernetes/client-node';

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

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

// This code is the JavaScript equivalent of `kubectl run demo-pod --image=nginx --namespace=default`.

const pod = {
metadata: {
name: 'demo-pod',
},
spec: {
containers: [
{
name: 'nginx-container',
image: 'nginx',
},
],
},
};
const namespace = 'default';

try {
const createdPod = await k8sApi.createNamespacedPod({
namespace,
body: pod,
});
console.log('Created pod:', createdPod);
} catch (err) {
console.error(err);
}
16 changes: 16 additions & 0 deletions examples/kubectl/equivalents/pod-filter-by-namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as k8s from '@kubernetes/client-node';

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

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

// This code is the JavaScript equivalent of `kubectl get pods --namespace=default`.

try {
const pods = await k8sApi.listNamespacedPod({ namespace: 'default' });

pods.items.forEach((pod) => console.log(pod.metadata.name));
} catch (err) {
console.error(err);
}
29 changes: 29 additions & 0 deletions examples/kubectl/equivalents/resourceQuota-create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as k8s from '@kubernetes/client-node';

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

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

// This code is the JavaScript equivalent of `kubectl create resourcequota my-quota --hard=pods=3`.

try {
const quota = {
metadata: {
name: 'my-quota',
},
spec: {
hard: {
pods: '3',
},
},
};
const createdQuota = await k8sApi.createNamespacedResourceQuota({
namespace: 'default',
body: quota,
});

console.log('Created quota:', createdQuota);
} catch (err) {
console.error(err);
}
16 changes: 16 additions & 0 deletions examples/kubectl/equivalents/resourceQuota-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as k8s from '@kubernetes/client-node';

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

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

// This code is the JavaScript equivalent of `kubectl get resourcequotas --all-namespaces`.

try {
const resourceQuotas = await k8sApi.listResourceQuotaForAllNamespaces();

resourceQuotas.items.forEach((quota) => console.log(quota.metadata.name));
} catch (err) {
console.error(err);
}

0 comments on commit 4b98e59

Please sign in to comment.