-
Notifications
You must be signed in to change notification settings - Fork 267
/
run.ts
178 lines (162 loc) · 4.53 KB
/
run.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
/*
* 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 { HelmModule } from "./config"
import { PodRunner, runAndCopy } from "../run"
import { getChartResources, getBaseModule } from "./common"
import {
findServiceResource,
getResourceContainer,
getResourcePodSpec,
getServiceResourceSpec,
makePodName,
} from "../util"
import { ConfigurationError } from "../../../exceptions"
import { KubernetesPluginContext } from "../config"
import { storeTaskResult } from "../task-results"
import { RunModuleParams } from "../../../types/plugin/module/runModule"
import { RunResult } from "../../../types/plugin/base"
import { RunTaskParams, RunTaskResult } from "../../../types/plugin/task/runTask"
import { uniqByName } from "../../../util/util"
import { prepareEnvVars } from "../util"
import { KubeApi } from "../api"
import { getModuleNamespace } from "../namespace"
import { DEFAULT_TASK_TIMEOUT } from "../../../constants"
import { KubernetesPod } from "../types"
export async function runHelmModule({
ctx,
module,
args,
command,
interactive,
runtimeContext,
timeout,
log,
}: RunModuleParams<HelmModule>): Promise<RunResult> {
const k8sCtx = <KubernetesPluginContext>ctx
const provider = k8sCtx.provider
const namespace = await getModuleNamespace({
ctx: k8sCtx,
log,
module,
provider: k8sCtx.provider,
})
const baseModule = getBaseModule(module)
const resourceSpec = getServiceResourceSpec(module, baseModule)
if (!resourceSpec) {
throw new ConfigurationError(
`Helm module ${module.name} does not specify a \`serviceResource\`. ` +
`Please configure that in order to run the module ad-hoc.`,
{ moduleName: module.name }
)
}
const manifests = await getChartResources(k8sCtx, module, false, log)
const target = await findServiceResource({
ctx: k8sCtx,
log,
manifests,
module,
baseModule,
resourceSpec,
})
const container = getResourceContainer(target, resourceSpec.containerName)
// Apply overrides
const env = uniqByName([...prepareEnvVars(runtimeContext.envVars), ...(container.env || [])])
const api = await KubeApi.factory(log, ctx, provider)
const pod: KubernetesPod = {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: makePodName("run", module.name),
namespace,
},
spec: {
containers: [
{
...container,
...(command && { command }),
...(args && { args }),
env,
},
],
},
}
const runner = new PodRunner({
ctx,
api,
pod,
provider,
namespace,
})
const result = await runner.runAndWait({
log,
remove: true,
timeoutSec: timeout,
tty: !!interactive,
})
return {
...result,
moduleName: module.name,
version: module.version.versionString,
}
}
export async function runHelmTask(params: RunTaskParams<HelmModule>): Promise<RunTaskResult> {
const { ctx, log, module, task, taskVersion } = params
// TODO: deduplicate this from testHelmModule
const k8sCtx = <KubernetesPluginContext>ctx
const { command, args } = task.spec
const manifests = await getChartResources(k8sCtx, module, false, log)
const baseModule = getBaseModule(module)
const resourceSpec = task.spec.resource || getServiceResourceSpec(module, baseModule)
const target = await findServiceResource({
ctx: k8sCtx,
log,
manifests,
module,
baseModule,
resourceSpec,
})
const container = getResourceContainer(target, resourceSpec.containerName)
const namespace = await getModuleNamespace({
ctx: k8sCtx,
log,
module,
provider: k8sCtx.provider,
})
const res = await runAndCopy({
...params,
container,
podSpec: getResourcePodSpec(target),
command,
args,
artifacts: task.spec.artifacts,
envVars: task.spec.env,
image: container.image!,
namespace,
podName: makePodName("task", module.name, task.name),
description: `Task '${task.name}' in container module '${module.name}'`,
timeout: task.config.timeout || DEFAULT_TASK_TIMEOUT,
})
const result: RunTaskResult = {
...res,
taskName: task.name,
outputs: {
log: res.log || "",
},
}
if (task.config.cacheResult) {
await storeTaskResult({
ctx,
log,
module,
result,
taskVersion,
taskName: task.name,
})
}
return result
}