diff --git a/src/plugins/kubernetes/actions.ts b/src/plugins/kubernetes/actions.ts index 70283dcbc7..a14fa65f7c 100644 --- a/src/plugins/kubernetes/actions.ts +++ b/src/plugins/kubernetes/actions.ts @@ -39,7 +39,7 @@ import { ContainerModule, helpers, } from "../container" -import { values, every, uniq } from "lodash" +import { uniq } from "lodash" import { deserializeValues, serializeValues, splitFirst, sleep } from "../../util/util" import { ServiceStatus } from "../../types/service" import { joiIdentifier } from "../../types/common" @@ -47,7 +47,6 @@ import { coreApi, } from "./api" import { - createNamespace, getAppNamespace, getMetadataNamespace, getAllGardenNamespaces, @@ -89,50 +88,17 @@ export async function getEnvironmentStatus({ ctx, provider }: GetEnvironmentStat throw err } - const statusDetail: { [key: string]: boolean } = { - namespaceReady: false, - metadataNamespaceReady: false, - } - - const metadataNamespace = await getMetadataNamespace(ctx, provider) - const namespacesStatus = await coreApi(context).listNamespace() - const namespace = await getAppNamespace(ctx, provider) - - for (const n of namespacesStatus.body.items) { - if (n.metadata.name === namespace && n.status.phase === "Active") { - statusDetail.namespaceReady = true - } - - if (n.metadata.name === metadataNamespace && n.status.phase === "Active") { - statusDetail.metadataNamespaceReady = true - } - } - - let configured = every(values(statusDetail)) + await getMetadataNamespace(ctx, provider) + await getAppNamespace(ctx, provider) return { - configured, - detail: statusDetail, + configured: true, + detail: {}, } } -export async function configureEnvironment( - { ctx, provider, status, logEntry }: ConfigureEnvironmentParams, -) { - const context = provider.config.context - - if (!status.detail.namespaceReady) { - const ns = await getAppNamespace(ctx, provider) - logEntry && logEntry.setState({ section: "kubernetes", msg: `Creating namespace ${ns}` }) - await createNamespace(context, ns) - } - - if (!status.detail.metadataNamespaceReady) { - const ns = await getMetadataNamespace(ctx, provider) - logEntry && logEntry.setState({ section: "kubernetes", msg: `Creating namespace ${ns}` }) - await createNamespace(context, ns) - } - +export async function configureEnvironment({ }: ConfigureEnvironmentParams) { + // this happens implicitly in the `getEnvironmentStatus()` function return {} } diff --git a/src/plugins/kubernetes/kubernetes.ts b/src/plugins/kubernetes/kubernetes.ts index 628bbd2d22..04d5e74ddc 100644 --- a/src/plugins/kubernetes/kubernetes.ts +++ b/src/plugins/kubernetes/kubernetes.ts @@ -9,7 +9,7 @@ import * as Joi from "joi" import { joiIdentifier, - validate + validate, } from "../../types/common" import { GardenPlugin, diff --git a/src/plugins/kubernetes/namespace.ts b/src/plugins/kubernetes/namespace.ts index 0d0e7a656b..552c8431be 100644 --- a/src/plugins/kubernetes/namespace.ts +++ b/src/plugins/kubernetes/namespace.ts @@ -18,18 +18,33 @@ import { import { name as providerName } from "./kubernetes" import { AuthenticationError } from "../../exceptions" -export async function createNamespace(context: string, namespace: string) { - // TODO: the types for all the create functions in the library are currently broken - await coreApi(context).createNamespace({ - apiVersion: "v1", - kind: "Namespace", - metadata: { - name: namespace, - annotations: { - "garden.io/generated": "true", - }, - }, - }) +const created: { [name: string]: boolean } = {} + +export async function ensureNamespace(context: string, namespace: string) { + if (!created[namespace]) { + const namespacesStatus = await coreApi(context).listNamespace() + + for (const n of namespacesStatus.body.items) { + if (n.status.phase === "Active") { + created[n.metadata.name] = true + } + } + + if (!created[namespace]) { + // TODO: the types for all the create functions in the library are currently broken + await coreApi(context).createNamespace({ + apiVersion: "v1", + kind: "Namespace", + metadata: { + name: namespace, + annotations: { + "garden.io/generated": "true", + }, + }, + }) + created[namespace] = true + } + } } export async function getNamespace(ctx: PluginContext, provider: KubernetesProvider, suffix?: string) { @@ -57,7 +72,13 @@ export async function getNamespace(ctx: PluginContext, provider: KubernetesProvi namespace = `garden--${username}--${ctx.projectName}` } - return suffix ? `${namespace}--${suffix}` : namespace + if (suffix) { + namespace = `${namespace}--${suffix}` + } + + await ensureNamespace(provider.config.context, namespace) + + return namespace } export async function getAppNamespace(ctx: PluginContext, provider: KubernetesProvider) {