Skip to content

Commit

Permalink
feat(client-node): add support for watch streaming (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
Silas Boyd-Wickizer committed May 29, 2019
1 parent 41809f4 commit abdaa2a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
22 changes: 21 additions & 1 deletion backends/kubernetes-client-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const pascalcase = require('pascalcase')
const { Readable } = require('stream')

//
// https://github.com/kubernetes-client/javascript
Expand Down Expand Up @@ -28,7 +29,26 @@ class ClientNodeBackend {
}

async getWatchObjectStream (options) {
throw new Error('getWatchObjectStream is not implemented')
const watch = new this.client.Watch(this.kubeconfig)

const stream = new Readable({
objectMode: true,
read: () => { /* .watch callback pushes to stream below */ },
destroy: (err, cb) => {
req.destroy(err)
req.abort()
cb(err)
}
})

const req = watch.watch(
options.pathname,
Object.assign({}, options.qs, options.parameters),
(type, object) => stream.push({ type, object }),
err => stream.destroy(err)
)

return stream
}

http (options) {
Expand Down
9 changes: 7 additions & 2 deletions test-integration/stream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const fs = require('fs')
const yaml = require('js-yaml')

Expand Down Expand Up @@ -44,14 +45,18 @@ describe('test-integration/stream', () => {

const stream = await client.api.v1.watch.namespaces(namespace).pods.getObjectStream()

let gotData = false
await new Promise((resolve, reject) => {
stream.on('data', data => {
gotData = true
expect(['ADDED', 'DELETED', 'MODIFIED']).to.include(data.type)
expect(data.object).to.be.an('object')
stream.destroy()
resolve()
})
stream.on('error', err => {
stream.abort()
reject(err)
stream.destroy()
if (!gotData) reject(err)
})
})
})
Expand Down

0 comments on commit abdaa2a

Please sign in to comment.