-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1254 from Ananya2001-an/refactor-examples
chore: add kubectl equivalent examples
- Loading branch information
Showing
8 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
const fs = require('fs'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
/** | ||
* This sample code is Javascript equivalent to `kubectl apply -f namespace.yml` | ||
* You can find the sample namespace.yml file in the examples/resources folder | ||
*/ | ||
|
||
const namespaceCreateYaml = async () => { | ||
try { | ||
const namespaceYaml = k8s.loadYaml(fs.readFileSync('../../resources/namespace.yml')); | ||
const createdNamespace = await k8sApi.createNamespace(namespaceYaml); | ||
console.log('New namespace created: ', createdNamespace.body); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
namespaceCreateYaml(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
/** | ||
* This sample code is Javascript equivalent to `kubectl create namespace test` | ||
*/ | ||
|
||
const namespaceCreate = async () => { | ||
try { | ||
const namespace = { | ||
metadata: { | ||
name: 'test', | ||
}, | ||
}; | ||
const createdNamespace = await k8sApi.createNamespace(namespace); | ||
console.log('New namespace created: ', createdNamespace.body); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
namespaceCreate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
/** | ||
* This sample code is Javascript equivalent to `kubectl get ns` | ||
*/ | ||
|
||
const namespaceList = async () => { | ||
try { | ||
const namespaces = await k8sApi.listNamespace(); | ||
namespaces.body.items.map(namespace => console.log(namespace.metadata.name)) | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
namespaceList(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
/** | ||
* This sample code is Javascript equivalent to `kubectl run demo-pod --image=nginx --namespace=default` | ||
*/ | ||
|
||
const podCreate = async () => { | ||
const pod = { | ||
metadata:{ | ||
name: "demo-pod" | ||
}, | ||
spec:{ | ||
containers:[{ | ||
name: "nginx-container", | ||
image:"nginx" | ||
}] | ||
} | ||
} | ||
try{ | ||
const createdPod = await k8sApi.createNamespacedPod('default', pod) | ||
console.log("Created pod: " + createdPod.body) | ||
|
||
} | ||
catch(err){ | ||
console.error(err); | ||
} | ||
}; | ||
|
||
podCreate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
/** | ||
* This sample code is Javascript equivalent to `kubectl get pods --namespace=default` | ||
*/ | ||
|
||
const podFilterByNamespace = async () => { | ||
try { | ||
const pods = await k8sApi.listNamespacedPod('default'); | ||
pods.body.items.map(pod => console.log(pod.metadata.name)) | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}; | ||
|
||
podFilterByNamespace(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
/** | ||
* This sample code is Javascript equivalent to `kubectl create resourcequota my-quota --hard=pods=3` | ||
*/ | ||
|
||
const resourceQuotaCreate = async () => { | ||
try{ | ||
const quota = { | ||
metadata:{ | ||
name: "my-quota" | ||
}, | ||
spec:{ | ||
hard:{ | ||
pods:"3" | ||
} | ||
} | ||
} | ||
const createdQuota = await k8sApi.createNamespacedResourceQuota("default", quota); | ||
console.log("Created quota: " + createdQuota.body) | ||
} | ||
catch(err){ | ||
console.error(err); | ||
} | ||
}; | ||
|
||
resourceQuotaCreate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const k8s = require('@kubernetes/client-node'); | ||
|
||
const kc = new k8s.KubeConfig(); | ||
kc.loadFromDefault(); | ||
|
||
const k8sApi = kc.makeApiClient(k8s.CoreV1Api); | ||
|
||
/** | ||
* This sample code is Javascript equivalent to `kubectl get resourcequotas --all-namespaces` | ||
*/ | ||
|
||
const resourceQuotaList = async () => { | ||
try{ | ||
const resourceQuotas = await k8sApi.listResourceQuotaForAllNamespaces(); | ||
resourceQuotas.body.items.map(quota => console.log(quota.metadata.name)) | ||
} | ||
catch(err){ | ||
console.error(err); | ||
} | ||
}; | ||
|
||
resourceQuotaList(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: test |