Skip to content

Commit

Permalink
fix(k8s): deduplicate ns creation during init
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thsig committed Apr 19, 2019
1 parent a727ace commit 316f9a6
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions garden-service/src/plugins/kubernetes/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down

0 comments on commit 316f9a6

Please sign in to comment.