Skip to content

Commit

Permalink
improvement(k8s): get rid of separate metadata namespace
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
edvald committed Feb 24, 2021
1 parent 0049530 commit 0703112
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 51 deletions.
13 changes: 3 additions & 10 deletions core/src/plugins/kubernetes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -314,11 +308,10 @@ export async function cleanupEnvironment({ ctx, log }: CleanupEnvironmentParams)
const k8sCtx = <KubernetesPluginContext>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
Expand All @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions core/src/plugins/kubernetes/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" })
Expand All @@ -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")
Expand Down
36 changes: 8 additions & 28 deletions core/src/plugins/kubernetes/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -100,29 +99,16 @@ interface GetNamespaceParams {
override?: NamespaceConfig
ctx: PluginContext
provider: KubernetesProvider
suffix?: string
skipCreate?: boolean
}

/**
* 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<string> {
export async function getNamespace({ log, ctx, override, provider, skipCreate }: GetNamespaceParams): Promise<string> {
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)
Expand Down Expand Up @@ -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<string[]> {
const allNamespaces = await api.core.listNamespace()
return allNamespaces.items.map((n) => n.metadata.name)
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions core/src/plugins/kubernetes/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,7 +19,7 @@ import { LogEntry } from "../../logger/log-entry"
export async function getSecret({ ctx, log, key }: GetSecretParams) {
const k8sCtx = <KubernetesPluginContext>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)
Expand All @@ -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 = <KubernetesPluginContext>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",
Expand Down Expand Up @@ -69,7 +69,7 @@ export async function setSecret({ ctx, log, key, value }: SetSecretParams) {
export async function deleteSecret({ ctx, log, key }: DeleteSecretParams) {
const k8sCtx = <KubernetesPluginContext>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, <any>{})
Expand Down
8 changes: 4 additions & 4 deletions core/src/plugins/kubernetes/task-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -35,7 +35,7 @@ export async function getTaskResult({
}: GetTaskResultParams<ContainerModule | HelmModule | KubernetesModule>): Promise<RunTaskResult | null> {
const k8sCtx = <KubernetesPluginContext>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 {
Expand Down Expand Up @@ -95,7 +95,7 @@ export async function storeTaskResult({
}: StoreTaskResultParams): Promise<RunTaskResult> {
const provider = <KubernetesProvider>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)
Expand Down Expand Up @@ -136,7 +136,7 @@ export async function clearTaskResult({
}: GetTaskResultParams<ContainerModule | HelmModule | KubernetesModule>) {
const provider = <KubernetesProvider>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)

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/providers/kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,7 @@ The default hostname configured on the provider.

### `${providers.<provider-name>.outputs.metadata-namespace}`

The namespace used for Garden metadata.
The namespace used for Garden metadata (currently always the same as app-namespace).

| Type |
| -------- |
Expand Down

0 comments on commit 0703112

Please sign in to comment.