From 316f9a6762e07506d0d471f0dad72a1e5cbe1e0d Mon Sep 17 00:00:00 2001 From: Thorarinn Sigurdsson Date: Fri, 19 Apr 2019 10:39:52 +0200 Subject: [PATCH] fix(k8s): deduplicate ns creation during init Before this fix, environment initialization for the OpenFAAS plugin would fail because a second call to ensureNamespace was made before the first call (with the same namespace name) was able to register that the namespace had been created. This created a race condition. This was fixed by maintaining a "pending" status to deduplicate such requests. --- garden-service/src/plugins/kubernetes/namespace.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/garden-service/src/plugins/kubernetes/namespace.ts b/garden-service/src/plugins/kubernetes/namespace.ts index d7d600fab3..402036b538 100644 --- a/garden-service/src/plugins/kubernetes/namespace.ts +++ b/garden-service/src/plugins/kubernetes/namespace.ts @@ -15,22 +15,24 @@ import { getPackageVersion } from "../../util/util" import { LogEntry } from "../../logger/log-entry" const GARDEN_VERSION = getPackageVersion() -const created: { [name: string]: boolean } = {} +type CreateNamespaceStatus = "pending" | "created" +const created: { [name: string]: CreateNamespaceStatus } = {} export async function ensureNamespace(api: KubeApi, namespace: string) { if (!created[namespace]) { + created[namespace] = "pending" const namespacesStatus = await api.core.listNamespace() for (const n of namespacesStatus.body.items) { if (n.status.phase === "Active") { - created[n.metadata.name] = true + created[n.metadata.name] = "created" } } - if (!created[namespace]) { + if (created[namespace] !== "created") { // TODO: the types for all the create functions in the library are currently broken await createNamespace(api, namespace) - created[namespace] = true + created[namespace] = "created" } } }