Skip to content

Commit

Permalink
feat(k8s): add uninstall-garden-services command
Browse files Browse the repository at this point in the history
This cleans up everything in the garden-system namespace, as well as
related cluster-wide resources.
  • Loading branch information
edvald committed Jul 18, 2019
1 parent 857b165 commit 9352176
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
8 changes: 8 additions & 0 deletions docs/using-garden/remote-kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ To initialize or update your cluster-wide services, run:
garden --env=<environment-name> plugins kubernetes cluster-init
```

To later uninstall the installed services, you can run:

```sh
garden --env=<environment-name> plugins kubernetes uninstall-garden-services
```

This will remove all services from the `garden-system` namespace, as well as any installed cluster-scoped resources.

## Building and pushing images

Garden supports multiple methods for building images and making them available to the cluster. Below we detail how
Expand Down
27 changes: 27 additions & 0 deletions garden-service/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,33 @@ export class ActionHelper implements TypeGuard {
})
}

/**
* Deletes all services and cleans up the specified environment.
*/
async deleteEnvironment(log: LogEntry) {
const graph = await this.garden.getConfigGraph()

const servicesLog = log.info({ msg: chalk.white("Deleting services..."), status: "active" })

const services = await graph.getServices()
const serviceStatuses: { [key: string]: ServiceStatus } = {}

await Bluebird.map(services, async (service) => {
const runtimeContext = await getServiceRuntimeContext(this.garden, graph, service)
serviceStatuses[service.name] = await this.deleteService({ log: servicesLog, service, runtimeContext })
})

servicesLog.setSuccess()

log.info("")

const envLog = log.info({ msg: chalk.white("Cleaning up environments..."), status: "active" })
const environmentStatuses = await this.cleanupEnvironment({ log: envLog })
envLog.setSuccess()

return { serviceStatuses, environmentStatuses }
}

async getDebugInfo({ log }: { log: LogEntry }): Promise<DebugInfoMap> {
const handlers = this.getActionHandlers("getDebugInfo")
return Bluebird.props(mapValues(handlers, async (h) => h({ ...await this.commonParams(h, log) })))
Expand Down
23 changes: 2 additions & 21 deletions garden-service/src/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { ServiceStatus, getServiceRuntimeContext, ServiceStatusMap } from "../ty
import { printHeader } from "../logger/util"
import { DeleteSecretResult } from "../types/plugin/provider/deleteSecret"
import { EnvironmentStatusMap } from "../types/plugin/provider/getEnvironmentStatus"
import chalk from "chalk"

export class DeleteCommand extends Command {
name = "delete"
Expand Down Expand Up @@ -103,27 +102,9 @@ export class DeleteEnvironmentCommand extends Command {
printHeader(headerLog, `Deleting ${garden.environmentName} environment`, "skull_and_crossbones")

const actions = await garden.getActionHelper()
const graph = await garden.getConfigGraph()

const servicesLog = log.info({ msg: chalk.white("Deleting services..."), status: "active" })

const services = await graph.getServices()
const serviceStatuses: { [key: string]: ServiceStatus } = {}

await Bluebird.map(services, async (service) => {
const runtimeContext = await getServiceRuntimeContext(garden, graph, service)
serviceStatuses[service.name] = await actions.deleteService({ log: servicesLog, service, runtimeContext })
})

servicesLog.setSuccess()

log.info("")
const result = await actions.deleteEnvironment(log)

const envLog = log.info({ msg: chalk.white("Cleaning up environments..."), status: "active" })
const environmentStatuses = await actions.cleanupEnvironment({ log: envLog })
envLog.setSuccess()

return { result: { serviceStatuses, environmentStatuses } }
return { result }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import chalk from "chalk"
import { PluginCommand } from "../../../types/plugin/command"
import { getKubernetesSystemVariables } from "../init"
import { KubernetesPluginContext } from "../config"
import { getSystemGarden } from "../system"

export const uninstallGardenServices: PluginCommand = {
name: "uninstall-garden-services",
description: "Clean up all installed cluster-wide Garden services.",

handler: async ({ ctx, log }) => {
const entry = log.info({
msg: chalk.bold.magenta(
`Removing cluster-wide services for ${chalk.white(ctx.environmentName)} environment`,
),
})

const k8sCtx = <KubernetesPluginContext>ctx
const variables = getKubernetesSystemVariables(k8sCtx.provider.config)

const sysGarden = await getSystemGarden(k8sCtx, variables || {})
const actions = await sysGarden.getActionHelper()

const result = await actions.deleteEnvironment(entry)

log.info(chalk.green("Done!"))

return { result }
},
}
6 changes: 3 additions & 3 deletions garden-service/src/plugins/kubernetes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { combineStates, ServiceStatusMap } from "../../types/service"
*/
export async function getEnvironmentStatus({ ctx, log }: GetEnvironmentStatusParams): Promise<EnvironmentStatus> {
const k8sCtx = <KubernetesPluginContext>ctx
const variables = getVariables(k8sCtx.provider.config)
const variables = getKubernetesSystemVariables(k8sCtx.provider.config)

const sysGarden = await getSystemGarden(k8sCtx, variables || {})
const sysCtx = <KubernetesPluginContext>await sysGarden.getPluginContext(k8sCtx.provider.name)
Expand Down Expand Up @@ -131,7 +131,7 @@ export async function prepareSystem(
) {
const k8sCtx = <KubernetesPluginContext>ctx
const provider = k8sCtx.provider
const variables = getVariables(provider.config)
const variables = getKubernetesSystemVariables(provider.config)

const systemReady = status.detail && !!status.detail.systemReady && !force
const systemServiceNames = k8sCtx.provider.config._systemServices
Expand Down Expand Up @@ -206,7 +206,7 @@ export async function cleanupEnvironment({ ctx, log }: CleanupEnvironmentParams)
return {}
}

function getVariables(config: KubernetesConfig) {
export function getKubernetesSystemVariables(config: KubernetesConfig) {
return {
"namespace": systemNamespace,

Expand Down
2 changes: 2 additions & 0 deletions garden-service/src/plugins/kubernetes/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { configSchema } from "./config"
import { ConfigurationError } from "../../exceptions"
import { cleanupClusterRegistry } from "./commands/cleanup-cluster-registry"
import { clusterInit } from "./commands/cluster-init"
import { uninstallGardenServices } from "./commands/uninstall-garden-services"

export const name = "kubernetes"

Expand Down Expand Up @@ -92,6 +93,7 @@ export function gardenPlugin(): GardenPlugin {
commands: [
cleanupClusterRegistry,
clusterInit,
uninstallGardenServices,
],
actions: {
configureProvider,
Expand Down

0 comments on commit 9352176

Please sign in to comment.