diff --git a/README.md b/README.md index eb71118..5c74a17 100644 --- a/README.md +++ b/README.md @@ -150,19 +150,23 @@ const main = async () => { if (isKubernetesCredentialsPresent()) { const resourceObserver = createResourceObserver(); - resourceObserver.observe((podResourceSpecification, podResourceUsage) => { - for (const containerResourceSpecification of podResourceSpecification.containers) { - if (containerResourceSpecification.resources.limits && containerResourceSpecification.resources.limits.memory) { - const containerResourceUsage = podResourceUsage.containers.find((container) => { - return container.name === containerResourceSpecification.name; - }); - - if (!containerResourceUsage) { - throw new Error('Unexpected state.'); - } - - if (containerResourceUsage.usage.memory / containerResourceSpecification.resources.limits.memory > MAXIMUM_MEMORY_USAGE) { - lightship.shutdown(); + resourceObserver.observe((error, podResourceSpecification, podResourceUsage) => { + if (error) { + // Handle error. + } else { + for (const containerResourceSpecification of podResourceSpecification.containers) { + if (containerResourceSpecification.resources.limits && containerResourceSpecification.resources.limits.memory) { + const containerResourceUsage = podResourceUsage.containers.find((container) => { + return container.name === containerResourceSpecification.name; + }); + + if (!containerResourceUsage) { + throw new Error('Unexpected state.'); + } + + if (containerResourceUsage.usage.memory / containerResourceSpecification.resources.limits.memory > MAXIMUM_MEMORY_USAGE) { + lightship.shutdown(); + } } } } diff --git a/package.json b/package.json index 519efe8..cf88b47 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "dependencies": { "bytes-iec": "^3.1.0", "got": "^9.6.0", - "roarr": "^2.14.6" + "roarr": "^2.14.6", + "serialize-error": "^5.0.0" }, "description": "Retrieves & observes Kubernetes Pod resource (CPU, memory) utilisation.", "devDependencies": { diff --git a/src/factories/createResourceObserver.js b/src/factories/createResourceObserver.js index b292d18..f3a0ccd 100644 --- a/src/factories/createResourceObserver.js +++ b/src/factories/createResourceObserver.js @@ -1,5 +1,8 @@ // @flow +import { + serializeError, +} from 'serialize-error'; import { getCredentials, getPodResourceSpecification, @@ -69,15 +72,25 @@ export default () => { const context = await ready; const podResourceSpecification = context.podResourceSpecification; - const podResourceUsage = await getPodResourceUsage(); - log.debug({ - podResourceSpecification, - podResourceUsage, - }, 'observed resource usage'); + try { + const podResourceUsage = await getPodResourceUsage(); + + log.debug({ + podResourceSpecification, + podResourceUsage, + }, 'observed resource usage'); + + // eslint-disable-next-line callback-return + callback(null, podResourceSpecification, podResourceUsage); + } catch (error) { + log.error({ + error: serializeError(error), + }, 'cannot get resource usage'); - // eslint-disable-next-line callback-return - callback(podResourceSpecification, podResourceUsage); + // eslint-disable-next-line callback-return + callback(error, null, null); + } timeout = setTimeout(() => { tick(); diff --git a/src/types.js b/src/types.js index 8480522..19cfec8 100644 --- a/src/types.js +++ b/src/types.js @@ -36,4 +36,8 @@ export type PodResourceSpecificationType = {| +name: string, |}; -export type IntervalCallbackType = (podResourceSpecification: PodResourceSpecificationType, podResourceUsage: PodResourceUsageType) => void; +export type IntervalCallbackType = ( + error: Error | null, + podResourceSpecification: PodResourceSpecificationType | null, + podResourceUsage: PodResourceUsageType | null +) => void;