From 447b0f555b91d7b37011d2fe3b1233f985abbb06 Mon Sep 17 00:00:00 2001 From: swist Date: Thu, 18 Jun 2020 19:12:56 +0100 Subject: [PATCH] refactor(pod-utils): fold getDeploymentPodName to only one function There were multiple things doing the same job (see previous commit) This folds them down to one --- .../commands/cleanup-cluster-registry.ts | 17 +++------- .../src/plugins/kubernetes/container/build.ts | 32 +++---------------- garden-service/src/plugins/kubernetes/util.ts | 13 ++++++-- .../src/plugins/kubernetes/container/build.ts | 2 +- 4 files changed, 20 insertions(+), 44 deletions(-) diff --git a/garden-service/src/plugins/kubernetes/commands/cleanup-cluster-registry.ts b/garden-service/src/plugins/kubernetes/commands/cleanup-cluster-registry.ts index 4c7e184bbd..d6055ecf3e 100644 --- a/garden-service/src/plugins/kubernetes/commands/cleanup-cluster-registry.ts +++ b/garden-service/src/plugins/kubernetes/commands/cleanup-cluster-registry.ts @@ -30,8 +30,8 @@ import { apply, kubectl } from "../kubectl" import { waitForResources } from "../status/status" import { execInWorkload } from "../container/exec" import { dedent, deline } from "../../../util/string" -import { execInPod, getDeploymentPodName, BuilderExecParams, buildSyncDeploymentName } from "../container/build" -import { getRunningPodInDeployment } from "../util" +import { execInPod, BuilderExecParams, buildSyncDeploymentName } from "../container/build" +import { getDeploymentPodName } from "../util" import { getSystemNamespace } from "../namespace" const workspaceSyncDirTtl = 0.5 * 86400 // 2 days @@ -388,14 +388,7 @@ async function cleanupBuildSyncVolume(provider: KubernetesProvider, log: LogEntr status: "active", }) - const pod = await getRunningPodInDeployment(buildSyncDeploymentName, provider, log) - const systemNamespace = await getSystemNamespace(provider, log) - if (!pod) { - throw new PluginError(`Could not find running image builder`, { - builderDeploymentName: buildSyncDeploymentName, - systemNamespace, - }) - } + const podName = await getDeploymentPodName(buildSyncDeploymentName, provider, log) const statArgs = ["sh", "-c", 'stat /data/* -c "%n %X"'] const stat = await execInBuildSync({ @@ -403,7 +396,7 @@ async function cleanupBuildSyncVolume(provider: KubernetesProvider, log: LogEntr log, args: statArgs, timeout: 30, - podName: pod.metadata.name, + podName, containerName: dockerDaemonContainerName, }) @@ -430,7 +423,7 @@ async function cleanupBuildSyncVolume(provider: KubernetesProvider, log: LogEntr log, args: deleteArgs, timeout: 300, - podName: pod.metadata.name, + podName, containerName: dockerDaemonContainerName, }) diff --git a/garden-service/src/plugins/kubernetes/container/build.ts b/garden-service/src/plugins/kubernetes/container/build.ts index bb2c72d415..03bd2b6bc6 100644 --- a/garden-service/src/plugins/kubernetes/container/build.ts +++ b/garden-service/src/plugins/kubernetes/container/build.ts @@ -15,7 +15,7 @@ import { containerHelpers } from "../../container/helpers" import { buildContainerModule, getContainerBuildStatus, getDockerBuildFlags } from "../../container/build" import { GetBuildStatusParams, BuildStatus } from "../../../types/plugin/module/getBuildStatus" import { BuildModuleParams, BuildResult } from "../../../types/plugin/module/build" -import { millicpuToString, megabytesToString, getRunningPodInDeployment, makePodName } from "../util" +import { millicpuToString, megabytesToString, getDeploymentPodName, makePodName } from "../util" import { RSYNC_PORT, dockerAuthSecretName, @@ -31,7 +31,7 @@ import { kubectl } from "../kubectl" import { LogEntry } from "../../../logger/log-entry" import { getDockerAuthVolume } from "../util" import { KubernetesProvider, ContainerBuildMode, KubernetesPluginContext, DEFAULT_KANIKO_IMAGE } from "../config" -import { PluginError, InternalError, RuntimeError, BuildError, ConfigurationError } from "../../../exceptions" +import { InternalError, RuntimeError, BuildError, ConfigurationError } from "../../../exceptions" import { PodRunner } from "../run" import { getRegistryHostname, getKubernetesSystemVariables } from "../init" import { normalizeLocalRsyncPath } from "../../../util/fs" @@ -222,17 +222,7 @@ const remoteBuild: BuildHandler = async (params) => { return {} } - const buildSyncPod = await getRunningPodInDeployment(buildSyncDeploymentName, provider, log) - - // TODO: remove this after a few releases (from 0.10.15), since this is only necessary for environments initialized - // with 0.10.14 or earlier. - if (!buildSyncPod) { - throw new PluginError(`Could not find running build sync Pod`, { - deploymentName: buildSyncDeploymentName, - systemNamespace, - }) - } - + const buildSyncPod = await getDeploymentPodName(buildSyncDeploymentName, provider, log) // Sync the build context to the remote sync service // -> Get a tunnel to the service log.setState("Syncing sources to cluster...") @@ -240,7 +230,7 @@ const remoteBuild: BuildHandler = async (params) => { ctx, log, namespace: systemNamespace, - targetResource: `Pod/${buildSyncPod.metadata.name}`, + targetResource: `Pod/${buildSyncPod}`, port: RSYNC_PORT, }) @@ -438,20 +428,6 @@ export async function execInPod({ }) } -export async function getDeploymentPodName(deployment: string, provider: KubernetesProvider, log: LogEntry) { - const pod = await getRunningPodInDeployment(deployment, provider, log) - const systemNamespace = await getSystemNamespace(provider, log) - - if (!pod) { - throw new PluginError(`Could not find running image builder`, { - builderDeploymentName: deployment, - systemNamespace, - }) - } - - return pod.metadata.name -} - interface RunKanikoParams { provider: KubernetesProvider namespace: string diff --git a/garden-service/src/plugins/kubernetes/util.ts b/garden-service/src/plugins/kubernetes/util.ts index a6718bb883..ea02b9d648 100644 --- a/garden-service/src/plugins/kubernetes/util.ts +++ b/garden-service/src/plugins/kubernetes/util.ts @@ -20,7 +20,7 @@ import { KubeApi, KubernetesError } from "./api" import { gardenAnnotationKey, base64, deline } from "../../util/string" import { MAX_CONFIGMAP_DATA_SIZE, dockerAuthSecretName, dockerAuthSecretKey } from "./constants" import { ContainerEnvVars } from "../container/config" -import { ConfigurationError } from "../../exceptions" +import { ConfigurationError, PluginError } from "../../exceptions" import { KubernetesProvider, ServiceResourceSpec } from "./config" import { LogEntry } from "../../logger/log-entry" import { PluginContext } from "../../plugin-context" @@ -395,14 +395,21 @@ export function convertDeprecatedManifestVersion(manifest: KubernetesResource): return manifest } -export async function getRunningPodInDeployment(deploymentName: string, provider: KubernetesProvider, log: LogEntry) { +export async function getDeploymentPodName(deploymentName: string, provider: KubernetesProvider, log: LogEntry) { const api = await KubeApi.factory(log, provider) const systemNamespace = await getSystemNamespace(provider, log) const status = await api.apps.readNamespacedDeployment(deploymentName, systemNamespace) const pods = await getPods(api, systemNamespace, status.spec.selector.matchLabels) + const pod = sample(pods) + if (!pod) { + throw new PluginError(`Could not a running pod in a deployment: ${deploymentName}`, { + deploymentName, + systemNamespace, + }) + } - return sample(pods) + return pod.metadata.name } export function getStaticLabelsFromPod(pod: KubernetesPod): { [key: string]: string } { diff --git a/garden-service/test/integ/src/plugins/kubernetes/container/build.ts b/garden-service/test/integ/src/plugins/kubernetes/container/build.ts index 6f178928d7..cc046e7cbc 100644 --- a/garden-service/test/integ/src/plugins/kubernetes/container/build.ts +++ b/garden-service/test/integ/src/plugins/kubernetes/container/build.ts @@ -12,9 +12,9 @@ import { ConfigGraph } from "../../../../../../src/config-graph" import { k8sBuildContainer, k8sGetContainerBuildStatus, - getDeploymentPodName, execInPod, } from "../../../../../../src/plugins/kubernetes/container/build" +import { getDeploymentPodName } from "../../../../../../src/plugins/kubernetes/util" import { PluginContext } from "../../../../../../src/plugin-context" import { KubernetesProvider } from "../../../../../../src/plugins/kubernetes/config" import { expect } from "chai"