Skip to content

Commit

Permalink
refactor(pod-utils): fold getDeploymentPodName to only one function
Browse files Browse the repository at this point in the history
There were multiple things doing the same job (see previous commit)
This folds them down to one
  • Loading branch information
swist authored and edvald committed Jun 21, 2020
1 parent be81679 commit 447b0f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -388,22 +388,15 @@ 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({
provider,
log,
args: statArgs,
timeout: 30,
podName: pod.metadata.name,
podName,
containerName: dockerDaemonContainerName,
})

Expand All @@ -430,7 +423,7 @@ async function cleanupBuildSyncVolume(provider: KubernetesProvider, log: LogEntr
log,
args: deleteArgs,
timeout: 300,
podName: pod.metadata.name,
podName,
containerName: dockerDaemonContainerName,
})

Expand Down
32 changes: 4 additions & 28 deletions garden-service/src/plugins/kubernetes/container/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -222,25 +222,15 @@ 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...")
const syncFwd = await getPortForward({
ctx,
log,
namespace: systemNamespace,
targetResource: `Pod/${buildSyncPod.metadata.name}`,
targetResource: `Pod/${buildSyncPod}`,
port: RSYNC_PORT,
})

Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions garden-service/src/plugins/kubernetes/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 } {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 447b0f5

Please sign in to comment.