File tree 2 files changed +23
-0
lines changed
2 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -126,6 +126,7 @@ specifications:
126
126
[ examples/] ( examples/ ) has snippets for using kubernetes-client:
127
127
128
128
* 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 )
129
130
* Create a ` client ` from your kube-apiserver's swagger.json:
130
131
[ client-from-apiserver-swagger.js] ( ./examples/client-from-apiserver-swagger.js )
131
132
* Create a ` client ` from one of the included Swagger specifications:
Original file line number Diff line number Diff line change
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 ( ) ;
You can’t perform that action at this time.
0 commit comments