Skip to content

Commit

Permalink
fix(k8s): only cleanup generated namespaces
Browse files Browse the repository at this point in the history
After this fix, only namespaces generated by Garden (as identified by the
"garden.io/generated": "true" annotation) are deleted by the garden
delete env command.
  • Loading branch information
thsig authored and edvald committed Dec 17, 2019
1 parent 0b2eae5 commit c94b2ca
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions garden-service/src/plugins/kubernetes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { PrepareEnvironmentParams, PrepareEnvironmentResult } from "../../types/
import { CleanupEnvironmentParams } from "../../types/plugin/provider/cleanupEnvironment"
import { millicpuToString, megabytesToString } from "./util"
import chalk from "chalk"
import { deline, dedent } from "../../util/string"
import { deline, dedent, gardenAnnotationKey } from "../../util/string"
import { combineStates, ServiceStatusMap } from "../../types/service"
import {
setupCertManager,
Expand Down Expand Up @@ -330,13 +330,41 @@ export async function cleanupEnvironment({ ctx, log }: CleanupEnvironmentParams)
const api = await KubeApi.factory(log, 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) => {
try {
const annotations = (await api.core.readNamespace(ns)).metadata.annotations || {}
return annotations[gardenAnnotationKey("generated")] === "true" ? ns : null
} catch (err) {
if (err.code === 404) {
return null
} else {
throw err
}
}
})
).filter(Boolean)

if (namespacesToDelete.length === 0) {
return {}
}

let nsDescription
if (namespacesToDelete.length === 1) {
nsDescription = `namespace ${namespacesToDelete[0]}`
} else {
nsDescription = `namespaces ${namespacesToDelete[0]} and ${namespacesToDelete[1]}`
}

const entry = log.info({
section: "kubernetes",
msg: `Deleting namespace ${namespace} (this may take a while)`,
msg: `Deleting ${nsDescription} (this may take a while)`,
status: "active",
})

await deleteNamespaces([namespace, metadataNamespace], api, entry)
await deleteNamespaces(<string[]>namespacesToDelete, api, entry)

return {}
}
Expand Down

0 comments on commit c94b2ca

Please sign in to comment.