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

Commit

Permalink
refactor: use factory for poller in daemon to reduce dependency craze
Browse files Browse the repository at this point in the history
  • Loading branch information
Flydiverny committed Nov 3, 2019
1 parent 1bd6ec4 commit d9bff6f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 36 deletions.
14 changes: 10 additions & 4 deletions bin/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand Down
26 changes: 3 additions & 23 deletions lib/daemon.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict'

/* eslint-disable no-console */

const Poller = require('./poller')

/** Daemon class. */
class Daemon {
/**
Expand All @@ -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 = {}
}
Expand Down Expand Up @@ -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()
}
Expand Down
21 changes: 12 additions & 9 deletions lib/daemon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})

Expand All @@ -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',
Expand All @@ -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)
})
})
50 changes: 50 additions & 0 deletions lib/poller-factory.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d9bff6f

Please sign in to comment.