From 07031128f7ee1cee3f24be313808df25ff9b0094 Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Mon, 22 Feb 2021 23:10:30 +0100 Subject: [PATCH] improvement(k8s): get rid of separate metadata namespace It no longer serves a meaningful purpose, was just a relic, cluttering up our clusters. Nice and simple, we just don't use it anymore. --- core/src/plugins/kubernetes/init.ts | 13 ++------ core/src/plugins/kubernetes/kubernetes.ts | 10 +++--- core/src/plugins/kubernetes/namespace.ts | 36 +++++---------------- core/src/plugins/kubernetes/secrets.ts | 8 ++--- core/src/plugins/kubernetes/task-results.ts | 8 ++--- docs/reference/providers/kubernetes.md | 2 +- 6 files changed, 26 insertions(+), 51 deletions(-) diff --git a/core/src/plugins/kubernetes/init.ts b/core/src/plugins/kubernetes/init.ts index 664a3e9654..fdda0a188f 100644 --- a/core/src/plugins/kubernetes/init.ts +++ b/core/src/plugins/kubernetes/init.ts @@ -7,13 +7,7 @@ */ import { KubeApi, KubernetesError } from "./api" -import { - getAppNamespace, - prepareNamespaces, - deleteNamespaces, - getMetadataNamespace, - getSystemNamespace, -} from "./namespace" +import { getAppNamespace, prepareNamespaces, deleteNamespaces, getSystemNamespace } from "./namespace" import { KubernetesPluginContext, KubernetesConfig, KubernetesProvider, ProviderSecretRef } from "./config" import { prepareSystemServices, getSystemServiceStatus, getSystemGarden } from "./system" import { GetEnvironmentStatusParams, EnvironmentStatus } from "../../types/plugin/provider/getEnvironmentStatus" @@ -314,11 +308,10 @@ export async function cleanupEnvironment({ ctx, log }: CleanupEnvironmentParams) const k8sCtx = ctx const api = await KubeApi.factory(log, ctx, k8sCtx.provider) const namespace = await getAppNamespace(k8sCtx, log, k8sCtx.provider) - const metadataNamespace = await getMetadataNamespace(k8sCtx, log, k8sCtx.provider) // Here, we only want to delete namespaces generated by Garden. const namespacesToDelete = ( - await Bluebird.map([namespace, metadataNamespace], async (ns) => { + await Bluebird.map([namespace], async (ns) => { try { const annotations = (await api.core.readNamespace(ns)).metadata.annotations || {} return annotations[gardenAnnotationKey("generated")] === "true" ? ns : null @@ -336,7 +329,7 @@ export async function cleanupEnvironment({ ctx, log }: CleanupEnvironmentParams) return {} } - let nsDescription + let nsDescription: string if (namespacesToDelete.length === 1) { nsDescription = `namespace ${namespacesToDelete[0]}` } else { diff --git a/core/src/plugins/kubernetes/kubernetes.ts b/core/src/plugins/kubernetes/kubernetes.ts index 0394ee7645..cb4a3fa85a 100644 --- a/core/src/plugins/kubernetes/kubernetes.ts +++ b/core/src/plugins/kubernetes/kubernetes.ts @@ -10,7 +10,7 @@ import Bluebird from "bluebird" import { createGardenPlugin } from "../../types/plugin/plugin" import { helmHandlers } from "./helm/handlers" -import { getAppNamespace, getMetadataNamespace, getSystemNamespace } from "./namespace" +import { getAppNamespace, getSystemNamespace } from "./namespace" import { getSecret, setSecret, deleteSecret } from "./secrets" import { getEnvironmentStatus, prepareEnvironment, cleanupEnvironment } from "./init" import { containerHandlers } from "./container/handlers" @@ -164,8 +164,7 @@ export async function debugInfo({ ctx, log, includeProject }: GetDebugInfoParams const namespacesList = [systemNamespace, systemMetadataNamespace] if (includeProject) { const appNamespace = await getAppNamespace(k8sCtx, log, k8sCtx.provider) - const appMetadataNamespace = await getMetadataNamespace(k8sCtx, log, k8sCtx.provider) - namespacesList.push(appNamespace, appMetadataNamespace) + namespacesList.push(appNamespace) } const namespaces = await Bluebird.map(namespacesList, async (ns) => { const nsEntry = entry.info({ section: ns, msg: "collecting namespace configuration", status: "active" }) @@ -191,7 +190,10 @@ export async function debugInfo({ ctx, log, includeProject }: GetDebugInfoParams const outputsSchema = joi.object().keys({ "app-namespace": joiIdentifier().required().description("The primary namespace used for resource deployments."), "default-hostname": joi.string().description("The default hostname configured on the provider."), - "metadata-namespace": joiIdentifier().required().description("The namespace used for Garden metadata."), + "metadata-namespace": joiIdentifier() + .required() + .description("The namespace used for Garden metadata (currently always the same as app-namespace).") + .meta({ deprecated: true }), }) const localKubernetesUrl = getProviderUrl("local-kubernetes") diff --git a/core/src/plugins/kubernetes/namespace.ts b/core/src/plugins/kubernetes/namespace.ts index bcfc48db8c..f328ae13e9 100644 --- a/core/src/plugins/kubernetes/namespace.ts +++ b/core/src/plugins/kubernetes/namespace.ts @@ -6,7 +6,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import Bluebird from "bluebird" import { intersection, cloneDeep } from "lodash" import { PluginContext } from "../../plugin-context" @@ -100,7 +99,6 @@ interface GetNamespaceParams { override?: NamespaceConfig ctx: PluginContext provider: KubernetesProvider - suffix?: string skipCreate?: boolean } @@ -108,21 +106,9 @@ interface GetNamespaceParams { * Resolves a namespace name given project context, provider config, and a (usually undefined) override, and then * ensures it exists in the target cluster (unless skipCreate=true). */ -// TODO: this feels convoluted (=a lot of parameters per line of function code), so let's consider refactoring -export async function getNamespace({ - log, - ctx, - override, - provider, - suffix, - skipCreate, -}: GetNamespaceParams): Promise { +export async function getNamespace({ log, ctx, override, provider, skipCreate }: GetNamespaceParams): Promise { const namespace = cloneDeep(override || provider.config.namespace)! - if (suffix) { - namespace.name = `${namespace.name}--${suffix}` - } - if (!skipCreate) { const api = await KubeApi.factory(log, ctx, provider) await ensureNamespace(api, namespace, log) @@ -155,15 +141,6 @@ export async function getAppNamespace(ctx: PluginContext, log: LogEntry, provide }) } -export function getMetadataNamespace(ctx: PluginContext, log: LogEntry, provider: KubernetesProvider) { - return getNamespace({ - log, - ctx, - provider, - suffix: "metadata", - }) -} - export async function getAllNamespaces(api: KubeApi): Promise { const allNamespaces = await api.core.listNamespace() return allNamespaces.items.map((n) => n.metadata.name) @@ -191,10 +168,13 @@ export async function prepareNamespaces({ ctx, log }: GetEnvironmentStatusParams ) } - return Bluebird.props({ - "app-namespace": getAppNamespace(k8sCtx, log, k8sCtx.provider), - "metadata-namespace": getMetadataNamespace(k8sCtx, log, k8sCtx.provider), - }) + const ns = await getAppNamespace(k8sCtx, log, k8sCtx.provider) + + // Including the metadata-namespace key for backwards-compatibility in provider outputs + return { + "app-namespace": ns, + "metadata-namespace": ns, + } } export async function deleteNamespaces(namespaces: string[], api: KubeApi, log?: LogEntry) { diff --git a/core/src/plugins/kubernetes/secrets.ts b/core/src/plugins/kubernetes/secrets.ts index 3f2bd5fe4e..c253fa8db2 100644 --- a/core/src/plugins/kubernetes/secrets.ts +++ b/core/src/plugins/kubernetes/secrets.ts @@ -9,7 +9,7 @@ import { KubeApi } from "./api" import { ProviderSecretRef, KubernetesPluginContext, KubernetesProvider } from "./config" import { ConfigurationError } from "../../exceptions" -import { getMetadataNamespace } from "./namespace" +import { getAppNamespace } from "./namespace" import { GetSecretParams } from "../../types/plugin/provider/getSecret" import { SetSecretParams } from "../../types/plugin/provider/setSecret" import { DeleteSecretParams } from "../../types/plugin/provider/deleteSecret" @@ -19,7 +19,7 @@ import { LogEntry } from "../../logger/log-entry" export async function getSecret({ ctx, log, key }: GetSecretParams) { const k8sCtx = ctx const api = await KubeApi.factory(log, ctx, k8sCtx.provider) - const ns = await getMetadataNamespace(k8sCtx, log, k8sCtx.provider) + const ns = await getAppNamespace(k8sCtx, log, k8sCtx.provider) try { const res = await api.core.readNamespacedSecret(key, ns) @@ -37,7 +37,7 @@ export async function setSecret({ ctx, log, key, value }: SetSecretParams) { // we store configuration in a separate metadata namespace, so that configs aren't cleared when wiping the namespace const k8sCtx = ctx const api = await KubeApi.factory(log, ctx, k8sCtx.provider) - const ns = await getMetadataNamespace(k8sCtx, log, k8sCtx.provider) + const ns = await getAppNamespace(k8sCtx, log, k8sCtx.provider) const body = { body: { apiVersion: "v1", @@ -69,7 +69,7 @@ export async function setSecret({ ctx, log, key, value }: SetSecretParams) { export async function deleteSecret({ ctx, log, key }: DeleteSecretParams) { const k8sCtx = ctx const api = await KubeApi.factory(log, ctx, k8sCtx.provider) - const ns = await getMetadataNamespace(k8sCtx, log, k8sCtx.provider) + const ns = await getAppNamespace(k8sCtx, log, k8sCtx.provider) try { await api.core.deleteNamespacedSecret(key, ns, {}) diff --git a/core/src/plugins/kubernetes/task-results.ts b/core/src/plugins/kubernetes/task-results.ts index f93a84609c..de78e83c64 100644 --- a/core/src/plugins/kubernetes/task-results.ts +++ b/core/src/plugins/kubernetes/task-results.ts @@ -13,7 +13,7 @@ import { KubernetesModule } from "./kubernetes-module/config" import { ModuleVersion } from "../../vcs/vcs" import { KubernetesPluginContext, KubernetesProvider } from "./config" import { KubeApi } from "./api" -import { getMetadataNamespace } from "./namespace" +import { getAppNamespace } from "./namespace" import { RunTaskResult } from "../../types/plugin/task/runTask" import { deserializeValues } from "../../util/util" import { PluginContext } from "../../plugin-context" @@ -35,7 +35,7 @@ export async function getTaskResult({ }: GetTaskResultParams): Promise { const k8sCtx = ctx const api = await KubeApi.factory(log, ctx, k8sCtx.provider) - const ns = await getMetadataNamespace(k8sCtx, log, k8sCtx.provider) + const ns = await getAppNamespace(k8sCtx, log, k8sCtx.provider) const resultKey = getTaskResultKey(ctx, module, task.name, taskVersion) try { @@ -95,7 +95,7 @@ export async function storeTaskResult({ }: StoreTaskResultParams): Promise { const provider = ctx.provider const api = await KubeApi.factory(log, ctx, provider) - const namespace = await getMetadataNamespace(ctx, log, provider) + const namespace = await getAppNamespace(ctx, log, provider) // FIXME: We should store the logs separately, because of the 1MB size limit on ConfigMaps. const data: RunTaskResult = trimRunOutput(result) @@ -136,7 +136,7 @@ export async function clearTaskResult({ }: GetTaskResultParams) { const provider = ctx.provider const api = await KubeApi.factory(log, ctx, provider) - const namespace = await getMetadataNamespace(ctx, log, provider) + const namespace = await getAppNamespace(ctx, log, provider) const key = getTaskResultKey(ctx, module, task.name, taskVersion) diff --git a/docs/reference/providers/kubernetes.md b/docs/reference/providers/kubernetes.md index a50b129b24..e1ca748ca3 100644 --- a/docs/reference/providers/kubernetes.md +++ b/docs/reference/providers/kubernetes.md @@ -1618,7 +1618,7 @@ The default hostname configured on the provider. ### `${providers..outputs.metadata-namespace}` -The namespace used for Garden metadata. +The namespace used for Garden metadata (currently always the same as app-namespace). | Type | | -------- |