Skip to content
This repository was archived by the owner on Jul 26, 2022. It is now read-only.

Commit 3ee939a

Browse files
authored
fix: use getObjectStream to address deprecation warning in kubernetes-client (#664)
* fix: use getObjectStream to address deprecation warning in kubernetes-client Signed-off-by: Markus Maga <[email protected]> * fix: end stream normally and update tests Signed-off-by: Markus Maga <[email protected]> * fix: mock stream end Signed-off-by: Markus Maga <[email protected]>
1 parent 7852dd6 commit 3ee939a

File tree

4 files changed

+33
-44
lines changed

4 files changed

+33
-44
lines changed

lib/external-secret.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict'
22

3-
const JSONStream = require('json-stream')
4-
53
/**
64
* Creates an FIFO queue which you can put to and take from.
75
* If theres nothing to take it will wait with resolving until
@@ -37,14 +35,11 @@ async function startWatcher ({
3735
while (true) {
3836
logger.debug('Starting watch stream for namespace %s', loggedNamespaceName)
3937

40-
const stream = kubeClient
38+
const stream = await kubeClient
4139
.apis[customResourceManifest.spec.group]
4240
.v1.watch
4341
.namespaces(namespace)[customResourceManifest.spec.names.plural]
44-
.getStream()
45-
46-
const jsonStream = new JSONStream()
47-
stream.pipe(jsonStream)
42+
.getObjectStream()
4843

4944
let timeout
5045
const restartTimeout = () => {
@@ -55,23 +50,23 @@ async function startWatcher ({
5550
const timeMs = watchTimeout
5651
timeout = setTimeout(() => {
5752
logger.info(`No watch event for ${timeMs} ms, restarting watcher for ${loggedNamespaceName}`)
58-
stream.abort()
53+
stream.end()
5954
}, timeMs)
6055
timeout.unref()
6156
}
6257

63-
jsonStream.on('data', (evt) => {
58+
stream.on('data', (evt) => {
6459
eventQueue.put(evt)
6560
restartTimeout()
6661
})
6762

68-
jsonStream.on('error', (err) => {
63+
stream.on('error', (err) => {
6964
logger.warn(err, 'Got error on stream for namespace %s', loggedNamespaceName)
7065
deathQueue.put('ERROR')
7166
clearTimeout(timeout)
7267
})
7368

74-
jsonStream.on('end', () => {
69+
stream.on('end', () => {
7570
deathQueue.put('END')
7671
clearTimeout(timeout)
7772
})
@@ -80,8 +75,7 @@ async function startWatcher ({
8075

8176
logger.info('Stopping watch stream for namespace %s due to event: %s', loggedNamespaceName, deathEvent)
8277
eventQueue.put({ type: 'DELETED_ALL' })
83-
84-
stream.abort()
78+
stream.end()
8579
}
8680
} catch (err) {
8781
logger.error(err, 'Watcher for namespace %s crashed', loggedNamespaceName)

lib/external-secret.test.js

+26-29
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ const { getExternalSecretEvents } = require('./external-secret')
1010
describe('getExternalSecretEvents', () => {
1111
let kubeClientMock
1212
let watchedNamespaces
13-
let externalSecretsApiMock
1413
let fakeCustomResourceManifest
1514
let loggerMock
16-
let mockedStream
15+
let externalsecrets
1716

1817
beforeEach(() => {
1918
fakeCustomResourceManifest = {
@@ -24,12 +23,10 @@ describe('getExternalSecretEvents', () => {
2423
}
2524
}
2625
}
27-
externalSecretsApiMock = sinon.mock()
2826

29-
mockedStream = new Readable()
30-
mockedStream._read = () => { }
31-
32-
externalSecretsApiMock.get = sinon.stub()
27+
externalsecrets = {
28+
getObjectStream: () => undefined
29+
}
3330

3431
kubeClientMock = {
3532
apis: {
@@ -38,9 +35,7 @@ describe('getExternalSecretEvents', () => {
3835
watch: {
3936
namespaces: () => {
4037
return {
41-
externalsecrets: {
42-
getStream: () => mockedStream
43-
}
38+
externalsecrets
4439
}
4540
}
4641
}
@@ -69,6 +64,27 @@ describe('getExternalSecretEvents', () => {
6964
spec: { backendType: 'secretsManager', data: [] }
7065
}
7166

67+
const fakeStream = Readable.from([
68+
{
69+
type: 'MODIFIED',
70+
object: fakeExternalSecretObject
71+
},
72+
{
73+
type: 'ADDED',
74+
object: fakeExternalSecretObject
75+
},
76+
{
77+
type: 'DELETED',
78+
object: fakeExternalSecretObject
79+
},
80+
{
81+
type: 'DELETED_ALL'
82+
}
83+
])
84+
85+
fakeStream.end = sinon.stub()
86+
externalsecrets.getObjectStream = () => fakeStream
87+
7288
const events = getExternalSecretEvents({
7389
kubeClient: kubeClientMock,
7490
watchedNamespaces: watchedNamespaces,
@@ -77,25 +93,6 @@ describe('getExternalSecretEvents', () => {
7793
watchTimeout: 5000
7894
})
7995

80-
mockedStream.push(`${JSON.stringify({
81-
type: 'MODIFIED',
82-
object: fakeExternalSecretObject
83-
})}\n`)
84-
85-
mockedStream.push(`${JSON.stringify({
86-
type: 'ADDED',
87-
object: fakeExternalSecretObject
88-
})}\n`)
89-
90-
mockedStream.push(`${JSON.stringify({
91-
type: 'DELETED',
92-
object: fakeExternalSecretObject
93-
})}\n`)
94-
95-
mockedStream.push(`${JSON.stringify({
96-
type: 'DELETED_ALL'
97-
})}\n`)
98-
9996
const modifiedEvent = await events.next()
10097
expect(modifiedEvent.value.type).is.equal('MODIFIED')
10198
expect(modifiedEvent.value.object).is.deep.equal(fakeExternalSecretObject)

package-lock.json

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"aws-sdk": "^2.628.0",
3939
"express": "^4.17.1",
4040
"js-yaml": "^3.14.1",
41-
"json-stream": "^1.0.0",
4241
"kubernetes-client": "^9.0.0",
4342
"lodash": "^4.17.21",
4443
"make-promises-safe": "^5.1.0",

0 commit comments

Comments
 (0)