Skip to content

Commit 2014d27

Browse files
cfellin1silasbw
authored andcommittedAug 1, 2018
docs(apply): doc pattern for kubectl apply -f (#305)
Closes #64
1 parent b640e1a commit 2014d27

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ specifications:
126126
[examples/](examples/) has snippets for using kubernetes-client:
127127

128128
* The basic usage example from above: [basic.js](./examples/basic.js)
129+
* Use error handling to simulate `kubectl apply -f`: [apply-deploy.js](./examples/apply-deploy.js)
129130
* Create a `client` from your kube-apiserver's swagger.json:
130131
[client-from-apiserver-swagger.js](./examples/client-from-apiserver-swagger.js)
131132
* Create a `client` from one of the included Swagger specifications:

‎examples/apply-deploy.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Use this pattern to simulate kubectl apply -f; create a Deployment or replace it if it already exists.
3+
//
4+
const Client = require('kubernetes-client').Client;
5+
const config = require('kubernetes-client').config;
6+
7+
const deploymentManifest = require('./nginx-deployment.json');
8+
9+
async function applyDeploy() {
10+
const client = new Client({ config: config.fromKubeconfig(), version: '1.9' });
11+
12+
try {
13+
const create = await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest });
14+
console.log('Create:', create);
15+
} catch (err) {
16+
if (err.code !== 409) throw err;
17+
const replace = await client.apis.apps.v1.namespaces('default').deployments('nginx-deployment').put({ body: deploymentManifest });
18+
console.log('Replace:', replace);
19+
}
20+
}
21+
22+
applyDeploy();

0 commit comments

Comments
 (0)
Please sign in to comment.