Skip to content

Commit

Permalink
fix: incl. ingresses & services in delete command.
Browse files Browse the repository at this point in the history
When deleting a container service via the delete command, also delete
its k8s service and ingress.

This fixes problems previously caused by dangling k8s
ingresses/services.
  • Loading branch information
thsig committed Sep 19, 2018
1 parent 90da248 commit 8d3f366
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion garden-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion garden-cli/src/plugins/kubernetes/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ export async function cleanupEnvironment({ ctx, logEntry }: CleanupEnvironmentPa
export async function deleteService(params: DeleteServiceParams): Promise<ServiceStatus> {
const { ctx, logEntry, service } = params
const namespace = await getAppNamespace(ctx, ctx.provider)
const provider = ctx.provider

await deleteContainerService({ provider: ctx.provider, logEntry, namespace, serviceName: service.name })
await deleteContainerService(
{ provider, namespace, serviceName: service.name, deploymentOnly: false, logEntry })

return getContainerServiceStatus(params)
}
Expand Down
20 changes: 10 additions & 10 deletions garden-cli/src/plugins/kubernetes/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ import { RuntimeContext, ServiceStatus } from "../../types/service"
import { createIngresses, getIngresses } from "./ingress"
import { createServices } from "./service"
import { waitForObjects, compareDeployedObjects } from "./status"
import { applyMany } from "./kubectl"
import { applyMany, deleteObjectsByLabel } from "./kubectl"
import { getAppNamespace } from "./namespace"
import { KubernetesObject } from "./helm"
import { PluginContext } from "../../plugin-context"
import { KubernetesProvider } from "./kubernetes"
import { GARDEN_ANNOTATION_KEYS_VERSION } from "../../constants"
import { KubeApi } from "./api"
import { LogEntry } from "../../logger/log-entry"

export const DEFAULT_CPU_REQUEST = "10m"
export const DEFAULT_CPU_LIMIT = "500m"
Expand Down Expand Up @@ -324,17 +322,19 @@ export async function createDeployment(
return deployment
}

export async function deleteContainerService({ namespace, provider, serviceName, logEntry }: {
namespace: string,
provider: KubernetesProvider,
serviceName: string,
logEntry?: LogEntry,
}) {
const api = new KubeApi(provider)
export async function deleteContainerService(
{ namespace, provider, serviceName, deploymentOnly, logEntry },
) {

let found = true
const context = provider.config.context
const api = new KubeApi(provider)

try {
await api.extensions.deleteNamespacedDeployment(serviceName, namespace, <any>{})
if (!deploymentOnly) {
await deleteObjectsByLabel(context, namespace, "service", serviceName)
}
} catch (err) {
if (err.code === 404) {
found = false
Expand Down
30 changes: 30 additions & 0 deletions garden-cli/src/plugins/kubernetes/kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export interface ApplyOptions {
namespace?: string,
}

export interface DeleteOptions {
includeUninitialized?: boolean,
objectTypes?: string[]
}

export const KUBECTL_DEFAULT_TIMEOUT = 300

export class Kubectl {
Expand Down Expand Up @@ -193,3 +198,28 @@ export async function applyMany(
return result.output
}
}

const defaultObjectTypesForDelete = ["deployment", "service", "ingress"]

export async function deleteObjectsByLabel(
context: string, namespace: string, labelKey: string, labelValue: string,
{ includeUninitialized = false, objectTypes = defaultObjectTypesForDelete }: DeleteOptions = {},
) {

let args = [
"delete",
objectTypes.join(","),
"-l",
`${labelKey}=${labelValue}`,
]

includeUninitialized && args.push("--include-uninitialized")

const result = await kubectl(context, namespace).call(args)

try {
return JSON.parse(result.output)
} catch (_) {
return result.output
}
}
5 changes: 4 additions & 1 deletion garden-cli/src/plugins/openfaas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
const provider = getK8sProvider(ctx)
const namespace = await getAppNamespace(ctx, provider)

await deleteContainerService({ provider, logEntry, namespace, serviceName: service.name })
await deleteContainerService({
namespace, provider, serviceName: service.name,
deploymentOnly: true, logEntry,
})

return await getServiceStatus(params)
},
Expand Down

0 comments on commit 8d3f366

Please sign in to comment.