From 49f26f85c4189644f59d58edb1cf3f3d6dfb1be9 Mon Sep 17 00:00:00 2001 From: Markus Maga Date: Sun, 3 Nov 2019 15:51:14 +0100 Subject: [PATCH] refactor: use factory for poller in daemon to reduce dependency craze --- bin/daemon.js | 14 ++++++++---- lib/daemon.js | 26 +++------------------- lib/daemon.test.js | 21 ++++++++++-------- lib/poller-factory.js | 50 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 lib/poller-factory.js diff --git a/bin/daemon.js b/bin/daemon.js index 1574dfc7..c155cf55 100755 --- a/bin/daemon.js +++ b/bin/daemon.js @@ -12,6 +12,7 @@ const Daemon = require('../lib/daemon') const MetricsServer = require('../lib/metrics-server') const Metrics = require('../lib/metrics') const { getExternalSecretEvents } = require('../lib/external-secret') +const PollerFactory = require('../lib/poller-factory') const { backends, @@ -40,14 +41,19 @@ async function main () { const registry = Prometheus.register const metrics = new Metrics({ registry }) - const daemon = new Daemon({ + const pollerFactory = new PollerFactory({ backends, - externalSecretEvents, kubeClient, - logger, metrics, pollerIntervalMilliseconds, - customResourceManifest + customResourceManifest, + logger + }) + + const daemon = new Daemon({ + externalSecretEvents, + logger, + pollerFactory }) const metricsServer = new MetricsServer({ diff --git a/lib/daemon.js b/lib/daemon.js index 0205c008..79abc3c4 100644 --- a/lib/daemon.js +++ b/lib/daemon.js @@ -1,9 +1,5 @@ 'use strict' -/* eslint-disable no-console */ - -const Poller = require('./poller') - /** Daemon class. */ class Daemon { /** @@ -15,21 +11,13 @@ class Daemon { * @param {number} pollerIntervalMilliseconds - Interval time in milliseconds for polling secret properties. */ constructor ({ - backends, externalSecretEvents, - kubeClient, logger, - metrics, - pollerIntervalMilliseconds, - customResourceManifest + pollerFactory }) { - this._backends = backends - this._kubeClient = kubeClient this._externalSecretEvents = externalSecretEvents this._logger = logger - this._metrics = metrics - this._pollerIntervalMilliseconds = pollerIntervalMilliseconds - this._customResourceManifest = customResourceManifest + this._pollerFactory = pollerFactory this._pollers = {} } @@ -68,15 +56,7 @@ class Daemon { throw new Error(`Poller for ${descriptor.id} already exists`) } - const poller = new Poller({ - backends: this._backends, - intervalMilliseconds: this._pollerIntervalMilliseconds, - kubeClient: this._kubeClient, - logger: this._logger, - metrics: this._metrics, - customResourceManifest: this._customResourceManifest, - externalSecret: descriptor.externalSecret - }) + const poller = this._pollerFactory.createPoller(descriptor) this._pollers[descriptor.id] = poller.start() } diff --git a/lib/daemon.test.js b/lib/daemon.test.js index b2bbfc2c..f5ed89cb 100644 --- a/lib/daemon.test.js +++ b/lib/daemon.test.js @@ -5,19 +5,27 @@ const { expect } = require('chai') const sinon = require('sinon') const Daemon = require('./daemon') -const Poller = require('./poller') describe('Daemon', () => { let daemon let loggerMock + let pollerMock + let pollerFactory beforeEach(() => { loggerMock = sinon.mock() loggerMock.info = sinon.stub() + pollerMock = sinon.mock() + pollerMock.start = sinon.stub().returns(pollerMock) + pollerMock.stop = sinon.stub().returns(pollerMock) + + pollerFactory = sinon.mock() + pollerFactory.createPoller = sinon.stub().returns(pollerMock) + daemon = new Daemon({ logger: loggerMock, - pollerIntervalMilliseconds: 0 + pollerFactory }) }) @@ -26,11 +34,6 @@ describe('Daemon', () => { }) it('starts new pollers for external secrets', async () => { - sinon.stub(Poller.prototype, 'start') - .callsFake(function () { return this }) - sinon.stub(Poller.prototype, 'stop') - .callsFake(function () { return this }) - const fakeExternalSecretEvents = (async function * () { yield { type: 'ADDED', @@ -49,7 +52,7 @@ describe('Daemon', () => { await daemon.start() daemon.stop() - expect(Poller.prototype.start.called).to.equal(true) - expect(Poller.prototype.stop.called).to.equal(true) + expect(pollerMock.start.called).to.equal(true) + expect(pollerMock.stop.called).to.equal(true) }) }) diff --git a/lib/poller-factory.js b/lib/poller-factory.js new file mode 100644 index 00000000..21627c2d --- /dev/null +++ b/lib/poller-factory.js @@ -0,0 +1,50 @@ +'use strict' + +const Poller = require('./poller') + +class PollerFactory { + /** + * Create PollerFactory. + * @param {Object} backends - Backends for fetching secret properties. + * @param {Object} kubeClient - Client for interacting with kubernetes cluster. + * @param {Object} metrics - Metrics client + * @param {Object} customResourceManifest - CRD manifest + * @param {Object} logger - Logger for logging stuff. + * @param {number} pollerIntervalMilliseconds - Interval time in milliseconds for polling secret properties. + */ + constructor ({ + backends, + kubeClient, + metrics, + pollerIntervalMilliseconds, + customResourceManifest, + logger + }) { + this._logger = logger + this._metrics = metrics + this._backends = backends + this._kubeClient = kubeClient + this._pollerIntervalMilliseconds = pollerIntervalMilliseconds + this._customResourceManifest = customResourceManifest + } + + /** + * Create poller + * @param {Object} externalSecret - External Secret custom resource oject + */ + createPoller ({ externalSecret }) { + const poller = new Poller({ + backends: this._backends, + intervalMilliseconds: this._pollerIntervalMilliseconds, + kubeClient: this._kubeClient, + logger: this._logger, + metrics: this._metrics, + customResourceManifest: this._customResourceManifest, + externalSecret + }) + + return poller + } +} + +module.exports = PollerFactory