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

Commit

Permalink
feat: remember last poll thru poller restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
Flydiverny committed Jul 23, 2019
1 parent 067b6a9 commit f6399c5
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions lib/poller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const LAST_POLL = 'externalsecret.kubernetes-client.io/last-poll'

/**
* Kubernetes secret descriptor.
* @typedef {Object} SecretDescriptor
Expand Down Expand Up @@ -58,7 +60,10 @@ class Poller {
name: secretDescriptor.name,
ownerReferences: [
this._ownerReference
]
],
annotations: {
[LAST_POLL]: `${Date.now()}`
}
},
type: 'Opaque',
data
Expand All @@ -71,12 +76,15 @@ class Poller {
*/
async _poll () {
this._logger.info('running poll')

try {
await this._upsertKubernetesSecret()
} catch (err) {
this._logger.error('failure while polling the secrets')
this._logger.error(err)
}

this._setNextPoll()
}

/**
Expand All @@ -100,14 +108,22 @@ class Poller {

/**
* Checks if secret exists, if not trigger a poll
* If secret already exists check when it was last polled and set timeout for next poll
*/
async _checkForSecret () {
const secretDescriptor = this._secretDescriptor
const secretName = secretDescriptor.name
const kubeNamespace = this._kubeClient.api.v1.namespaces(this._namespace)

try {
await kubeNamespace.secrets(secretName).get()
const {
body: { metadata: { annotations = {} } = {} } = {}
} = await kubeNamespace.secrets(secretName).get()

const lastPoll = parseInt(annotations[LAST_POLL] || '0', 10)
const nextPollIn = Math.max(lastPoll - (Date.now() - this._intervalMilliseconds), 0)

this._setNextPoll(nextPollIn)
} catch (err) {
if (err.statusCode === 404) {
this._logger.info('Secret does not exist, polling right away')
Expand All @@ -116,6 +132,16 @@ class Poller {
}
}

_setNextPoll (nextPollIn = this._intervalMilliseconds) {
if (this._interval) {
clearTimeout(this._interval)
this._interval = null
}

this._interval = setTimeout(this._poll.bind(this), nextPollIn)
this._logger.debug('Next poll for %s in %s in %s', this._secretDescriptor.name, this._namespace, nextPollIn)
}

/**
* Start poller.
* @param {boolean} forcePoll - Trigger poll right away
Expand All @@ -124,14 +150,14 @@ class Poller {
start ({ forcePoll = false } = {}) {
if (this._interval) return this

this._logger.debug('starting poller')

if (forcePoll) {
this._poll()
} else {
this._checkForSecret()
}

this._logger.info('starting poller')
this._interval = setInterval(this._poll.bind(this), this._intervalMilliseconds)
return this
}

Expand All @@ -141,8 +167,8 @@ class Poller {
*/
stop () {
if (!this._interval) return this
this._logger.info('stopping poller')
clearInterval(this._interval)
this._logger.debug('stopping poller')
clearTimeout(this._interval)
this._interval = null
return this
}
Expand Down

0 comments on commit f6399c5

Please sign in to comment.