From 4f8a2d6d20a5a635afc59a4e845b2a62418d593e Mon Sep 17 00:00:00 2001 From: Vladimir Vagaytsev Date: Wed, 10 Jul 2024 14:05:16 +0200 Subject: [PATCH] feat(kubernetes): plugin command to remove `garden-util` resources (#6278) * chore: remove unused constant * chore: use named constant as a deploy action name * feat(kubernetes): plugin command to remove `garden-util` resources * chore: print namespace in the log message --- .../commands/cleanup-garden-util.ts | 44 +++++++++++++++++++ .../kubernetes/container/build/common.ts | 3 +- core/src/plugins/kubernetes/kubernetes.ts | 2 + 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 core/src/plugins/kubernetes/commands/cleanup-garden-util.ts diff --git a/core/src/plugins/kubernetes/commands/cleanup-garden-util.ts b/core/src/plugins/kubernetes/commands/cleanup-garden-util.ts new file mode 100644 index 0000000000..211214c3c4 --- /dev/null +++ b/core/src/plugins/kubernetes/commands/cleanup-garden-util.ts @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2018-2024 Garden Technologies, Inc. + * + * 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 type { KubernetesPluginContext } from "../config.js" +import type { PluginCommand } from "../../../plugin/command.js" +import { utilDeploymentName } from "../container/build/common.js" +import { styles } from "../../../logger/styles.js" +import { deleteResources } from "../kubectl.js" +import type { KubernetesResource } from "../types.js" + +export const cleanupUtilDeployment: PluginCommand = { + name: "cleanup-util-deployment", + description: `Remove ${utilDeploymentName} utility deployment from the current namespace`, + title: `Cleanup ${utilDeploymentName} deployment`, + resolveGraph: true, + + handler: async ({ ctx, log }) => { + const result = {} + const k8sCtx = ctx as KubernetesPluginContext + const provider = k8sCtx.provider + + const namespace = provider.outputs["app-namespace"] + log.info({ msg: styles.highlight(`\nRemoving ${utilDeploymentName} deployment from namespace ${namespace}`) }) + + const targetKinds = ["Service", "Deployment"] + const resources: KubernetesResource[] = targetKinds.map((kind) => { + return { apiVersion: "v1", kind, metadata: { name: utilDeploymentName } } + }) + + for (const resource of resources) { + log.info(`Deleting ${resource.kind}/${resource.metadata.name}`) + await deleteResources({ ctx, log, provider, namespace, resources }) + } + + log.success("\nDone!") + + return { result } + }, +} diff --git a/core/src/plugins/kubernetes/container/build/common.ts b/core/src/plugins/kubernetes/container/build/common.ts index a44bfb98bd..c04b91cd91 100644 --- a/core/src/plugins/kubernetes/container/build/common.ts +++ b/core/src/plugins/kubernetes/container/build/common.ts @@ -36,7 +36,6 @@ import { LogLevel } from "../../../../logger/logger.js" import type { ActionRuntime } from "../../../../plugin/base.js" export const inClusterBuilderServiceAccount = "garden-in-cluster-builder" -export const sharedBuildSyncDeploymentName = "garden-build-sync" export const utilContainerName = "util" export const utilRsyncPort = 8730 export const utilDeploymentName = "garden-util" @@ -455,7 +454,7 @@ export async function ensureUtilDeployment({ namespace, ctx, provider, - actionName: "garden-util", + actionName: utilDeploymentName, resources: [deployment, service], log: buildUtilLog, timeoutSec: 600, diff --git a/core/src/plugins/kubernetes/kubernetes.ts b/core/src/plugins/kubernetes/kubernetes.ts index fcd3b40569..028ce85378 100644 --- a/core/src/plugins/kubernetes/kubernetes.ts +++ b/core/src/plugins/kubernetes/kubernetes.ts @@ -47,6 +47,7 @@ import { helmPodRunDefinition, helmPodTestDefinition } from "./helm/helm-pod.js" import { kubernetesPodRunDefinition, kubernetesPodTestDefinition } from "./kubernetes-type/kubernetes-pod.js" import { kubernetesExecRunDefinition, kubernetesExecTestDefinition } from "./kubernetes-type/kubernetes-exec.js" import { makeDocsLinkPlain, makeDocsLinkStyled } from "../../docs/common.js" +import { cleanupUtilDeployment } from "./commands/cleanup-garden-util.js" export const CONTAINER_BUILD_CONCURRENCY_LIMIT_REMOTE_KUBERNETES = 5 export const CONTAINER_STATUS_CONCURRENCY_LIMIT_REMOTE_KUBERNETES = 20 @@ -154,6 +155,7 @@ export const gardenPlugin = () => { outputsSchema, commands: [ cleanupClusterRegistry, + cleanupUtilDeployment, clusterInit, uninstallGardenServices, pullImage,