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