File tree Expand file tree Collapse file tree 8 files changed +150
-0
lines changed
examples/kubectl/equivalents Expand file tree Collapse file tree 8 files changed +150
-0
lines changed Original file line number Diff line number Diff line change
1
+ import * as k8s from '@kubernetes/client-node' ;
2
+ import { readFileSync } from 'node:fs' ;
3
+
4
+ const kc = new k8s . KubeConfig ( ) ;
5
+ kc . loadFromDefault ( ) ;
6
+
7
+ const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
8
+
9
+ // This code is the JavaScript equivalent of `kubectl apply -f namespace.yaml`.
10
+
11
+ try {
12
+ const namespaceYaml = k8s . loadYaml ( readFileSync ( './namespace.yaml' , 'utf8' ) ) ;
13
+ const createdNamespace = await k8sApi . createNamespace ( { body : namespaceYaml } ) ;
14
+ console . log ( 'New namespace created:' , createdNamespace ) ;
15
+ } catch ( err ) {
16
+ console . error ( err ) ;
17
+ }
Original file line number Diff line number Diff line change
1
+ import * as k8s from '@kubernetes/client-node' ;
2
+
3
+ const kc = new k8s . KubeConfig ( ) ;
4
+ kc . loadFromDefault ( ) ;
5
+
6
+ const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
7
+
8
+ // This code is the JavaScript equivalent of `kubectl create namespace test`.
9
+
10
+ try {
11
+ const namespace = {
12
+ metadata : {
13
+ name : 'test' ,
14
+ } ,
15
+ } ;
16
+ const createdNamespace = await k8sApi . createNamespace ( { body : namespace } ) ;
17
+ console . log ( 'New namespace created:' , createdNamespace ) ;
18
+ } catch ( err ) {
19
+ console . error ( err ) ;
20
+ }
Original file line number Diff line number Diff line change
1
+ import * as k8s from '@kubernetes/client-node' ;
2
+
3
+ const kc = new k8s . KubeConfig ( ) ;
4
+ kc . loadFromDefault ( ) ;
5
+
6
+ const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
7
+
8
+ // This code is the JavaScript equivalent of `kubectl get ns`.
9
+
10
+ try {
11
+ const namespaces = await k8sApi . listNamespace ( ) ;
12
+ namespaces . items . forEach ( ( namespace ) => console . log ( namespace . metadata . name ) ) ;
13
+ } catch ( err ) {
14
+ console . error ( err ) ;
15
+ }
Original file line number Diff line number Diff line change
1
+ apiVersion : v1
2
+ kind : Namespace
3
+ metadata :
4
+ name : test
Original file line number Diff line number Diff line change
1
+ import * as k8s from '@kubernetes/client-node' ;
2
+
3
+ const kc = new k8s . KubeConfig ( ) ;
4
+ kc . loadFromDefault ( ) ;
5
+
6
+ const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
7
+
8
+ // This code is the JavaScript equivalent of `kubectl run demo-pod --image=nginx --namespace=default`.
9
+
10
+ const pod = {
11
+ metadata : {
12
+ name : 'demo-pod' ,
13
+ } ,
14
+ spec : {
15
+ containers : [
16
+ {
17
+ name : 'nginx-container' ,
18
+ image : 'nginx' ,
19
+ } ,
20
+ ] ,
21
+ } ,
22
+ } ;
23
+ const namespace = 'default' ;
24
+
25
+ try {
26
+ const createdPod = await k8sApi . createNamespacedPod ( {
27
+ namespace,
28
+ body : pod ,
29
+ } ) ;
30
+ console . log ( 'Created pod:' , createdPod ) ;
31
+ } catch ( err ) {
32
+ console . error ( err ) ;
33
+ }
Original file line number Diff line number Diff line change
1
+ import * as k8s from '@kubernetes/client-node' ;
2
+
3
+ const kc = new k8s . KubeConfig ( ) ;
4
+ kc . loadFromDefault ( ) ;
5
+
6
+ const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
7
+
8
+ // This code is the JavaScript equivalent of `kubectl get pods --namespace=default`.
9
+
10
+ try {
11
+ const pods = await k8sApi . listNamespacedPod ( { namespace : 'default' } ) ;
12
+
13
+ pods . items . forEach ( ( pod ) => console . log ( pod . metadata . name ) ) ;
14
+ } catch ( err ) {
15
+ console . error ( err ) ;
16
+ }
Original file line number Diff line number Diff line change
1
+ import * as k8s from '@kubernetes/client-node' ;
2
+
3
+ const kc = new k8s . KubeConfig ( ) ;
4
+ kc . loadFromDefault ( ) ;
5
+
6
+ const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
7
+
8
+ // This code is the JavaScript equivalent of `kubectl create resourcequota my-quota --hard=pods=3`.
9
+
10
+ try {
11
+ const quota = {
12
+ metadata : {
13
+ name : 'my-quota' ,
14
+ } ,
15
+ spec : {
16
+ hard : {
17
+ pods : '3' ,
18
+ } ,
19
+ } ,
20
+ } ;
21
+ const createdQuota = await k8sApi . createNamespacedResourceQuota ( {
22
+ namespace : 'default' ,
23
+ body : quota ,
24
+ } ) ;
25
+
26
+ console . log ( 'Created quota:' , createdQuota ) ;
27
+ } catch ( err ) {
28
+ console . error ( err ) ;
29
+ }
Original file line number Diff line number Diff line change
1
+ import * as k8s from '@kubernetes/client-node' ;
2
+
3
+ const kc = new k8s . KubeConfig ( ) ;
4
+ kc . loadFromDefault ( ) ;
5
+
6
+ const k8sApi = kc . makeApiClient ( k8s . CoreV1Api ) ;
7
+
8
+ // This code is the JavaScript equivalent of `kubectl get resourcequotas --all-namespaces`.
9
+
10
+ try {
11
+ const resourceQuotas = await k8sApi . listResourceQuotaForAllNamespaces ( ) ;
12
+
13
+ resourceQuotas . items . forEach ( ( quota ) => console . log ( quota . metadata . name ) ) ;
14
+ } catch ( err ) {
15
+ console . error ( err ) ;
16
+ }
You can’t perform that action at this time.
0 commit comments