/
build.ts
640 lines (553 loc) · 21.2 KB
/
build.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
* Copyright (C) 2018-2020 Garden Technologies, Inc. <info@garden.io>
*
* 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 pRetry from "p-retry"
import split2 = require("split2")
import { differenceBy } from "lodash"
import { V1PodSpec } from "@kubernetes/client-node"
import { ContainerModule, ContainerRegistryConfig } from "../../container/config"
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 {
RSYNC_PORT,
dockerAuthSecretName,
inClusterRegistryHostname,
dockerDaemonDeploymentName,
gardenUtilDaemonDeploymentName,
dockerDaemonContainerName,
skopeoDaemonContainerName,
} from "../constants"
import { posix, resolve } from "path"
import { KubeApi } from "../api"
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 { PodRunner } from "../run"
import { getRegistryHostname, getKubernetesSystemVariables } from "../init"
import { normalizeLocalRsyncPath } from "../../../util/fs"
import { getPortForward } from "../port-forward"
import { Writable } from "stream"
import { LogLevel } from "../../../logger/log-node"
import { exec, renderOutputStream } from "../../../util/util"
import { loadImageToKind } from "../local/kind"
import { getSystemNamespace, getAppNamespace } from "../namespace"
import { dedent } from "../../../util/string"
import chalk = require("chalk")
import { loadImageToMicrok8s, getMicrok8sImageStatus } from "../local/microk8s"
import { RunResult } from "../../../types/plugin/base"
const registryPort = 5000
export const buildSyncDeploymentName = "garden-build-sync"
export async function k8sGetContainerBuildStatus(params: GetBuildStatusParams<ContainerModule>): Promise<BuildStatus> {
const { ctx, module } = params
const provider = <KubernetesProvider>ctx.provider
const hasDockerfile = await containerHelpers.hasDockerfile(module)
if (!hasDockerfile) {
// Nothing to build
return { ready: true }
}
const handler = buildStatusHandlers[provider.config.buildMode]
return handler(params)
}
export async function k8sBuildContainer(params: BuildModuleParams<ContainerModule>): Promise<BuildResult> {
const { ctx } = params
const provider = <KubernetesProvider>ctx.provider
const handler = buildHandlers[provider.config.buildMode]
return handler(params)
}
type BuildStatusHandler = (params: GetBuildStatusParams<ContainerModule>) => Promise<BuildStatus>
const buildStatusHandlers: { [mode in ContainerBuildMode]: BuildStatusHandler } = {
"local-docker": async (params) => {
const { ctx, module, log } = params
const k8sCtx = ctx as KubernetesPluginContext
const deploymentRegistry = k8sCtx.provider.config.deploymentRegistry
if (deploymentRegistry) {
const args = await getManifestInspectArgs(module, deploymentRegistry)
const res = await containerHelpers.dockerCli(module.buildPath, args, log, { ignoreError: true })
// Non-zero exit code can both mean the manifest is not found, and any other unexpected error
if (res.code !== 0 && !res.all.includes("no such manifest")) {
const detail = res.all || `docker manifest inspect exited with code ${res.code}`
log.warn(chalk.yellow(`Unable to query registry for image status: ${detail}`))
}
return { ready: res.code === 0 }
} else if (k8sCtx.provider.config.clusterType === "microk8s") {
const localId = await containerHelpers.getLocalImageId(module)
return getMicrok8sImageStatus(localId)
} else {
return getContainerBuildStatus(params)
}
},
// TODO: make these handlers faster by running a simple in-cluster service
// that wraps https://github.com/containers/image
"cluster-docker": async (params) => {
const { ctx, module, log } = params
const k8sCtx = ctx as KubernetesPluginContext
const provider = k8sCtx.provider
const deploymentRegistry = provider.config.deploymentRegistry
if (!deploymentRegistry) {
// This is validated in the provider configure handler, so this is an internal error if it happens
throw new InternalError(`Expected configured deploymentRegistry for remote build`, { config: provider.config })
}
const args = await getManifestInspectArgs(module, deploymentRegistry)
const pushArgs = ["/bin/sh", "-c", "DOCKER_CLI_EXPERIMENTAL=enabled docker " + args.join(" ")]
const podName = await getDeploymentPodName(dockerDaemonDeploymentName, provider, log)
const res = await execInPod({
provider,
log,
args: pushArgs,
timeout: 300,
podName,
containerName: dockerDaemonContainerName,
ignoreError: true,
})
// Non-zero exit code can both mean the manifest is not found, and any other unexpected error
if (res.exitCode !== 0 && !res.stderr.includes("no such manifest")) {
const detail = res.all || `docker manifest inspect exited with code ${res.exitCode}`
log.warn(chalk.yellow(`Unable to query registry for image status: ${detail}`))
}
return { ready: res.exitCode === 0 }
},
"kaniko": async (params) => {
const { ctx, module, log } = params
const k8sCtx = ctx as KubernetesPluginContext
const provider = k8sCtx.provider
const deploymentRegistry = provider.config.deploymentRegistry
if (!deploymentRegistry) {
// This is validated in the provider configure handler, so this is an internal error if it happens
throw new InternalError(`Expected configured deploymentRegistry for remote build`, { config: provider.config })
}
const remoteId = await containerHelpers.getDeploymentImageId(module, deploymentRegistry)
const inClusterRegistry = deploymentRegistry?.hostname === inClusterRegistryHostname
const skopeoCommand = ["skopeo", "--command-timeout=30s", "inspect", "--raw"]
if (inClusterRegistry) {
// The in-cluster registry is not exposed, so we don't configure TLS on it.
skopeoCommand.push("--tls-verify=false")
}
skopeoCommand.push(`docker://${remoteId}`)
const podCommand = ["sh", "-c", skopeoCommand.join(" ")]
const podName = await getDeploymentPodName(gardenUtilDaemonDeploymentName, provider, log)
const res = await execInPod({
provider,
log,
args: podCommand,
timeout: 300,
podName,
containerName: skopeoDaemonContainerName,
ignoreError: true,
})
// Non-zero exit code can both mean the manifest is not found, and any other unexpected error
if (res.exitCode !== 0 && !res.stderr.includes("manifest unknown")) {
throw new RuntimeError(`Unable to query registry for image status: ${res.all}`, {
command: skopeoCommand,
output: res.all,
})
}
return { ready: res.exitCode === 0 }
},
}
type BuildHandler = (params: BuildModuleParams<ContainerModule>) => Promise<BuildResult>
const localBuild: BuildHandler = async (params) => {
const { ctx, module, log } = params
const provider = ctx.provider as KubernetesProvider
const buildResult = await buildContainerModule(params)
if (!provider.config.deploymentRegistry) {
if (provider.config.clusterType === "kind") {
await loadImageToKind(buildResult, provider.config)
} else if (provider.config.clusterType === "microk8s") {
const imageId = await containerHelpers.getLocalImageId(module)
await loadImageToMicrok8s({ module, imageId, log })
}
return buildResult
}
if (!(await containerHelpers.hasDockerfile(module))) {
return buildResult
}
const localId = await containerHelpers.getLocalImageId(module)
const remoteId = await containerHelpers.getDeploymentImageId(module, ctx.provider.config.deploymentRegistry)
log.setState({ msg: `Pushing image ${remoteId} to cluster...` })
await containerHelpers.dockerCli(module.buildPath, ["tag", localId, remoteId], log)
await containerHelpers.dockerCli(module.buildPath, ["push", remoteId], log)
return buildResult
}
const remoteBuild: BuildHandler = async (params) => {
const { ctx, module, log } = params
const provider = <KubernetesProvider>ctx.provider
const namespace = await getAppNamespace(ctx, log, provider)
const systemNamespace = await getSystemNamespace(provider, log)
if (!(await containerHelpers.hasDockerfile(module))) {
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,
})
}
// 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}`,
port: RSYNC_PORT,
})
// -> Run rsync
const buildRoot = resolve(module.buildPath, "..")
// The '/./' trick is used to automatically create the correct target directory with rsync:
// https://stackoverflow.com/questions/1636889/rsync-how-can-i-configure-it-to-create-target-directory-on-server
let src = normalizeLocalRsyncPath(`${buildRoot}`) + `/./${module.name}/`
const destination = `rsync://localhost:${syncFwd.localPort}/volume/${ctx.workingCopyId}/`
const syncArgs = [
"--recursive",
"--relative",
// Copy symlinks (Note: These are sanitized while syncing to the build staging dir)
"--links",
// Preserve permissions
"--perms",
// Preserve modification times
"--times",
"--compress",
"--delete",
"--temp-dir",
"/tmp",
src,
destination,
]
log.debug(`Syncing from ${src} to ${destination}`)
// We retry a couple of times, because we may get intermittent connection issues or concurrency issues
await pRetry(() => exec("rsync", syncArgs), {
retries: 3,
minTimeout: 500,
})
const localId = await containerHelpers.getLocalImageId(module)
const deploymentImageId = await containerHelpers.getDeploymentImageId(module, provider.config.deploymentRegistry)
const dockerfile = module.spec.dockerfile || "Dockerfile"
// Because we're syncing to a shared volume, we need to scope by a unique ID
const contextPath = `/garden-build/${ctx.workingCopyId}/${module.name}/`
log.setState(`Building image ${localId}...`)
let buildLog = ""
// Stream debug log to a status line
const stdout = split2()
const statusLine = log.placeholder(LogLevel.verbose)
stdout.on("error", () => {})
stdout.on("data", (line: Buffer) => {
statusLine.setState(renderOutputStream(line.toString()))
})
if (provider.config.buildMode === "cluster-docker") {
// Prepare the build command
const dockerfilePath = posix.join(contextPath, dockerfile)
let args = [
"docker",
"build",
"-t",
deploymentImageId,
"-f",
dockerfilePath,
contextPath,
...getDockerBuildFlags(module),
]
// Execute the build
const podName = await getDeploymentPodName(dockerDaemonDeploymentName, provider, log)
const containerName = dockerDaemonContainerName
const buildTimeout = module.spec.build.timeout
if (provider.config.clusterDocker && provider.config.clusterDocker.enableBuildKit) {
args = ["/bin/sh", "-c", "DOCKER_BUILDKIT=1 " + args.join(" ")]
}
const buildRes = await execInPod({ provider, log, args, timeout: buildTimeout, podName, containerName, stdout })
buildLog = buildRes.stdout + buildRes.stderr
// Push the image to the registry
log.setState({ msg: `Pushing image ${localId} to registry...` })
const dockerCmd = ["docker", "push", deploymentImageId]
const pushArgs = ["/bin/sh", "-c", dockerCmd.join(" ")]
const pushRes = await execInPod({ provider, log, args: pushArgs, timeout: 300, podName, containerName, stdout })
buildLog += pushRes.stdout + pushRes.stderr
} else if (provider.config.buildMode === "kaniko") {
// build with Kaniko
const args = [
"--context",
"dir://" + contextPath,
"--dockerfile",
dockerfile,
"--destination",
deploymentImageId,
...getKanikoFlags(module.spec.extraFlags),
]
if (provider.config.deploymentRegistry?.hostname === inClusterRegistryHostname) {
// The in-cluster registry is not exposed, so we don't configure TLS on it.
args.push("--insecure")
}
args.push(...getDockerBuildFlags(module))
// Execute the build
const buildRes = await runKaniko({ provider, namespace, log, module, args, outputStream: stdout })
buildLog = buildRes.log
if (kanikoBuildFailed(buildRes)) {
throw new BuildError(`Failed building module ${chalk.bold(module.name)}:\n\n${buildLog}`, { buildLog })
}
} else {
throw new ConfigurationError("Uknown build mode", { buildMode: provider.config.buildMode })
}
log.silly(buildLog)
return {
buildLog,
fetched: false,
fresh: true,
version: module.version.versionString,
}
}
export interface BuilderExecParams {
provider: KubernetesProvider
log: LogEntry
args: string[]
env?: { [key: string]: string }
ignoreError?: boolean
timeout: number
podName: string
containerName: string
stdout?: Writable
stderr?: Writable
}
export const DEFAULT_KANIKO_FLAGS = ["--cache=true"]
export const getKanikoFlags = (flags?: string[]): string[] => {
if (!flags) {
return DEFAULT_KANIKO_FLAGS
}
const flagToKey = (flag) => {
const found = flag.match(/--([a-zA-Z]*)/)
if (found === null) {
throw new ConfigurationError(`Invalid format for a kaniko flag`, { flag })
}
return found[0]
}
const defaultsToKeep = differenceBy(DEFAULT_KANIKO_FLAGS, flags, flagToKey)
return [...flags, ...defaultsToKeep]
}
export function kanikoBuildFailed(buildRes: RunResult) {
return !buildRes.success && !buildRes.log.includes("cannot be overwritten because the repository is immutable.")
}
const buildHandlers: { [mode in ContainerBuildMode]: BuildHandler } = {
"local-docker": localBuild,
"cluster-docker": remoteBuild,
"kaniko": remoteBuild,
}
// TODO: we should make a simple service around this instead of execing into containers
export async function execInPod({
provider,
log,
args,
ignoreError,
timeout,
podName,
containerName,
stdout,
stderr,
}: BuilderExecParams) {
const execCmd = ["exec", "-i", podName, "-c", containerName, "--", ...args]
const systemNamespace = await getSystemNamespace(provider, log)
log.verbose(`Running: kubectl ${execCmd.join(" ")}`)
return kubectl.exec({
args: execCmd,
ignoreError,
provider,
log,
namespace: systemNamespace,
timeout,
stdout,
stderr,
})
}
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
log: LogEntry
module: ContainerModule
args: string[]
outputStream: Writable
}
async function runKaniko({ provider, namespace, log, module, args, outputStream }: RunKanikoParams) {
const api = await KubeApi.factory(log, provider)
const systemNamespace = await getSystemNamespace(provider, log)
const podName = makePodName("kaniko", namespace, module.name)
const registryHostname = getRegistryHostname(provider.config)
const k8sSystemVars = getKubernetesSystemVariables(provider.config)
const syncDataVolumeName = k8sSystemVars["sync-volume-name"]
const commsVolumeName = "comms"
const commsMountPath = "/.garden/comms"
// Escape the args so that we can safely interpolate them into the kaniko command
const argsStr = args.map((arg) => JSON.stringify(arg)).join(" ")
// This may seem kind of insane but we have to wait until the socat proxy is up (because Kaniko immediately tries to
// reach the registry we plan on pushing to). See the support container in the Pod spec below for more on this
// hackery.
const commandStr = dedent`
while true; do
if ls ${commsMountPath}/socatStarted 2> /dev/null; then
/kaniko/executor ${argsStr};
export exitcode=$?;
touch ${commsMountPath}/done;
exit $exitcode;
else
sleep 0.3;
fi
done
`
const kanikoImage = provider.config.kaniko?.image || DEFAULT_KANIKO_IMAGE
const spec: V1PodSpec = {
shareProcessNamespace: true,
volumes: [
// Mount the build sync volume, to get the build context from.
{
name: syncDataVolumeName,
persistentVolumeClaim: { claimName: syncDataVolumeName },
},
// Mount the docker auth secret, so Kaniko can pull from private registries.
getDockerAuthVolume(),
// Mount a volume to communicate between the containers in the Pod.
{
name: commsVolumeName,
emptyDir: {},
},
],
containers: [
{
name: "kaniko",
image: kanikoImage,
command: ["sh", "-c", commandStr],
volumeMounts: [
{
name: syncDataVolumeName,
mountPath: "/garden-build",
},
{
name: dockerAuthSecretName,
mountPath: "/kaniko/.docker",
readOnly: true,
},
{
name: commsVolumeName,
mountPath: commsMountPath,
},
],
resources: {
limits: {
cpu: millicpuToString(provider.config.resources.builder.limits.cpu),
memory: megabytesToString(provider.config.resources.builder.limits.memory),
},
requests: {
cpu: millicpuToString(provider.config.resources.builder.requests.cpu),
memory: megabytesToString(provider.config.resources.builder.requests.memory),
},
},
},
],
}
if (provider.config.deploymentRegistry?.hostname === inClusterRegistryHostname) {
spec.containers = spec.containers.concat([
getSocatContainer(registryHostname),
// This is a workaround so that the kaniko executor can wait until socat starts, and so that the socat proxy
// doesn't just keep running after the build finishes. Doing this in the kaniko Pod is currently not possible
// because of https://github.com/GoogleContainerTools/distroless/issues/225
{
name: "support",
image: "busybox:1.31.1",
command: [
"sh",
"-c",
dedent`
while true; do
if pidof socat 2> /dev/null; then
touch ${commsMountPath}/socatStarted;
break;
else
sleep 0.3;
fi
done
while true; do
if ls ${commsMountPath}/done 2> /dev/null; then
killall socat; exit 0;
else
sleep 0.3;
fi
done
`,
],
volumeMounts: [
{
name: commsVolumeName,
mountPath: commsMountPath,
},
],
},
])
}
const runner = new PodRunner({
api,
podName,
provider,
image: kanikoImage,
module,
namespace: systemNamespace,
spec,
})
try {
return runner.startAndWait({
ignoreError: true,
interactive: false,
log,
timeout: module.spec.build.timeout,
stdout: outputStream,
})
} finally {
await runner.stop()
}
}
async function getManifestInspectArgs(module: ContainerModule, deploymentRegistry: ContainerRegistryConfig) {
const remoteId = await containerHelpers.getDeploymentImageId(module, deploymentRegistry)
const dockerArgs = ["manifest", "inspect", remoteId]
if (isLocalHostname(deploymentRegistry.hostname)) {
dockerArgs.push("--insecure")
}
return dockerArgs
}
function isLocalHostname(hostname: string) {
return hostname === "localhost" || hostname.startsWith("127.")
}
function getSocatContainer(registryHostname: string) {
return {
name: "proxy",
image: "gardendev/socat:0.1.0",
command: ["/bin/sh", "-c", `socat TCP-LISTEN:5000,fork TCP:${registryHostname}:5000 || exit 0`],
ports: [
{
name: "proxy",
containerPort: registryPort,
protocol: "TCP",
},
],
readinessProbe: {
tcpSocket: { port: <any>registryPort },
},
}
}