This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
container_helper.go
executable file
·334 lines (275 loc) · 12.8 KB
/
container_helper.go
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
package flytek8s
import (
"context"
"github.com/flyteorg/flyteplugins/go/tasks/errors"
pluginscore "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core/template"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/flytek8s/config"
"github.com/flyteorg/flytestdlib/logger"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/validation"
)
const resourceGPU = "gpu"
// ResourceNvidiaGPU is the name of the Nvidia GPU resource.
// Copied from: k8s.io/autoscaler/cluster-autoscaler/utils/gpu/gpu.go
const ResourceNvidiaGPU = "nvidia.com/gpu"
// Specifies whether resource resolution should assign unset resource requests or limits from platform defaults
// or existing container values.
const assignIfUnset = true
func MergeResources(in v1.ResourceRequirements, out *v1.ResourceRequirements) {
if out.Limits == nil {
out.Limits = in.Limits
} else if in.Limits != nil {
for key, val := range in.Limits {
out.Limits[key] = val
}
}
if out.Requests == nil {
out.Requests = in.Requests
} else if in.Requests != nil {
for key, val := range in.Requests {
out.Requests[key] = val
}
}
}
type ResourceRequirement struct {
Request resource.Quantity
Limit resource.Quantity
}
func resolvePlatformDefaults(platformResources v1.ResourceRequirements, configCPU, configMemory resource.Quantity) v1.ResourceRequirements {
if len(platformResources.Requests) == 0 {
platformResources.Requests = make(v1.ResourceList)
}
if _, ok := platformResources.Requests[v1.ResourceCPU]; !ok {
platformResources.Requests[v1.ResourceCPU] = configCPU
}
if _, ok := platformResources.Requests[v1.ResourceMemory]; !ok {
platformResources.Requests[v1.ResourceMemory] = configMemory
}
if len(platformResources.Limits) == 0 {
platformResources.Limits = make(v1.ResourceList)
}
return platformResources
}
// AdjustOrDefaultResource validates resources conform to platform limits and assigns defaults for Request and Limit values by
// using the Request when the Limit is unset, and vice versa.
func AdjustOrDefaultResource(request, limit, platformDefault, platformLimit resource.Quantity) ResourceRequirement {
if request.IsZero() {
if !limit.IsZero() {
request = limit
} else {
request = platformDefault
}
}
if limit.IsZero() {
limit = request
}
return ensureResourceRange(request, limit, platformLimit)
}
func ensureResourceLimit(value, limit resource.Quantity) resource.Quantity {
if value.IsZero() || limit.IsZero() {
return value
}
if value.Cmp(limit) == 1 {
return limit
}
return value
}
// ensureResourceRange doesn't assign resources unless they need to be adjusted downwards
func ensureResourceRange(request, limit, platformLimit resource.Quantity) ResourceRequirement {
// Ensure request is < platformLimit
request = ensureResourceLimit(request, platformLimit)
// Ensure limit is < platformLimit
limit = ensureResourceLimit(limit, platformLimit)
// Ensure request is < limit
request = ensureResourceLimit(request, limit)
return ResourceRequirement{
Request: request,
Limit: limit,
}
}
func adjustResourceRequirement(resourceName v1.ResourceName, resourceRequirements,
platformResources v1.ResourceRequirements, assignIfUnset bool) {
var resourceValue ResourceRequirement
if assignIfUnset {
resourceValue = AdjustOrDefaultResource(resourceRequirements.Requests[resourceName],
resourceRequirements.Limits[resourceName], platformResources.Requests[resourceName],
platformResources.Limits[resourceName])
} else {
resourceValue = ensureResourceRange(resourceRequirements.Requests[resourceName],
resourceRequirements.Limits[resourceName], platformResources.Limits[resourceName])
}
resourceRequirements.Requests[resourceName] = resourceValue.Request
resourceRequirements.Limits[resourceName] = resourceValue.Limit
}
// ApplyResourceOverrides handles resource resolution, allocation and validation. Primarily, it ensures that container
// resources do not exceed defined platformResource limits and in the case of assignIfUnset, ensures that limits and
// requests are sensibly set for resources of all types.
// Furthermore, this function handles some clean-up such as converting GPU resources to the recognized Nvidia gpu
// resource name and deleting unsupported Storage-type resources.
func ApplyResourceOverrides(resources, platformResources v1.ResourceRequirements, assignIfUnset bool) v1.ResourceRequirements {
if len(resources.Requests) == 0 {
resources.Requests = make(v1.ResourceList)
}
if len(resources.Limits) == 0 {
resources.Limits = make(v1.ResourceList)
}
// As a fallback, in the case the Flyte workflow object does not have platformResource defaults set, the defaults
// come from the plugin config.
platformResources = resolvePlatformDefaults(platformResources, config.GetK8sPluginConfig().DefaultCPURequest,
config.GetK8sPluginConfig().DefaultMemoryRequest)
adjustResourceRequirement(v1.ResourceCPU, resources, platformResources, assignIfUnset)
adjustResourceRequirement(v1.ResourceMemory, resources, platformResources, assignIfUnset)
_, ephemeralStorageRequested := resources.Requests[v1.ResourceEphemeralStorage]
_, ephemeralStorageLimited := resources.Limits[v1.ResourceEphemeralStorage]
if ephemeralStorageRequested || ephemeralStorageLimited {
adjustResourceRequirement(v1.ResourceEphemeralStorage, resources, platformResources, assignIfUnset)
}
// TODO: Make configurable. 1/15/2019 Flyte Cluster doesn't support setting storage requests/limits.
// https://github.com/kubernetes/enhancements/issues/362
delete(resources.Requests, v1.ResourceStorage)
delete(resources.Limits, v1.ResourceStorage)
gpuResourceName := config.GetK8sPluginConfig().GpuResourceName
shouldAdjustGPU := false
_, gpuRequested := resources.Requests[gpuResourceName]
_, gpuLimited := resources.Limits[gpuResourceName]
if gpuRequested || gpuLimited {
shouldAdjustGPU = true
}
// Override GPU
if res, found := resources.Requests[resourceGPU]; found {
resources.Requests[gpuResourceName] = res
delete(resources.Requests, resourceGPU)
shouldAdjustGPU = true
}
if res, found := resources.Limits[resourceGPU]; found {
resources.Limits[gpuResourceName] = res
delete(resources.Limits, resourceGPU)
shouldAdjustGPU = true
}
if shouldAdjustGPU {
adjustResourceRequirement(gpuResourceName, resources, platformResources, assignIfUnset)
}
return resources
}
// BuildRawContainer constructs a Container based on the definition passed by the TaskExecutionContext.
func BuildRawContainer(ctx context.Context, tCtx pluginscore.TaskExecutionContext) (*v1.Container, error) {
taskTemplate, err := tCtx.TaskReader().Read(ctx)
if err != nil {
logger.Warnf(ctx, "failed to read task information when trying to construct container, err: %s", err.Error())
return nil, err
}
// validate arguments
taskContainer := taskTemplate.GetContainer()
if taskContainer == nil {
return nil, errors.Errorf(errors.BadTaskSpecification, "unable to create container with no definition in TaskTemplate")
}
if tCtx.TaskExecutionMetadata().GetOverrides() == nil || tCtx.TaskExecutionMetadata().GetOverrides().GetResources() == nil {
return nil, errors.Errorf(errors.BadTaskSpecification, "resource requirements not found for container task, required!")
}
// Make the container name the same as the pod name, unless it violates K8s naming conventions
// Container names are subject to the DNS-1123 standard
containerName := tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName()
if errs := validation.IsDNS1123Label(containerName); len(errs) > 0 {
containerName = rand.String(4)
}
res, err := ToK8sResourceRequirements(taskContainer.Resources)
if err != nil {
return nil, err
}
container := &v1.Container{
Name: containerName,
Image: taskContainer.GetImage(),
Args: taskContainer.GetArgs(),
Command: taskContainer.GetCommand(),
Env: ToK8sEnvVar(taskContainer.GetEnv()),
TerminationMessagePolicy: v1.TerminationMessageFallbackToLogsOnError,
Resources: *res,
}
return container, nil
}
// ToK8sContainer builds a Container based on the definition passed by the TaskExecutionContext. This involves applying
// all Flyte configuration including k8s plugins and resource requests.
func ToK8sContainer(ctx context.Context, tCtx pluginscore.TaskExecutionContext) (*v1.Container, error) {
// build raw container
container, err := BuildRawContainer(ctx, tCtx)
if err != nil {
return nil, err
}
if container.SecurityContext == nil && config.GetK8sPluginConfig().DefaultSecurityContext != nil {
container.SecurityContext = config.GetK8sPluginConfig().DefaultSecurityContext.DeepCopy()
}
// add flyte resource customizations to the container
templateParameters := template.Parameters{
TaskExecMetadata: tCtx.TaskExecutionMetadata(),
Inputs: tCtx.InputReader(),
OutputPath: tCtx.OutputWriter(),
Task: tCtx.TaskReader(),
}
if err := AddFlyteCustomizationsToContainer(ctx, templateParameters, ResourceCustomizationModeMergeExistingResources, container); err != nil {
return nil, err
}
return container, nil
}
//go:generate enumer -type=ResourceCustomizationMode -trimprefix=ResourceCustomizationMode
type ResourceCustomizationMode int
const (
// ResourceCustomizationModeAssignResources is used for container tasks where resources are validated and assigned if necessary.
ResourceCustomizationModeAssignResources ResourceCustomizationMode = iota
// ResourceCustomizationModeMergeExistingResources is used for primary containers in pod tasks where container requests and limits are
// merged, validated and assigned if necessary.
ResourceCustomizationModeMergeExistingResources
// ResourceCustomizationModeEnsureExistingResourcesInRange is used for secondary containers in pod tasks where requests and limits are only
// adjusted if needed (downwards).
ResourceCustomizationModeEnsureExistingResourcesInRange
)
// AddFlyteCustomizationsToContainer takes a container definition which specifies how to run a Flyte task and fills in
// templated command and argument values, updates resources and decorates environment variables with platform and
// task-specific customizations.
func AddFlyteCustomizationsToContainer(ctx context.Context, parameters template.Parameters,
mode ResourceCustomizationMode, container *v1.Container) error {
modifiedCommand, err := template.Render(ctx, container.Command, parameters)
if err != nil {
return err
}
container.Command = modifiedCommand
modifiedArgs, err := template.Render(ctx, container.Args, parameters)
if err != nil {
return err
}
container.Args = modifiedArgs
container.Env = DecorateEnvVars(ctx, container.Env, parameters.TaskExecMetadata.GetEnvironmentVariables(), parameters.TaskExecMetadata.GetTaskExecutionID())
// retrieve platformResources and overrideResources to use when aggregating container resources
platformResources := parameters.TaskExecMetadata.GetPlatformResources()
if platformResources == nil {
platformResources = &v1.ResourceRequirements{}
}
var overrideResources *v1.ResourceRequirements
if parameters.TaskExecMetadata.GetOverrides() != nil && parameters.TaskExecMetadata.GetOverrides().GetResources() != nil {
overrideResources = parameters.TaskExecMetadata.GetOverrides().GetResources()
}
if overrideResources == nil {
overrideResources = &v1.ResourceRequirements{}
}
logger.Infof(ctx, "ApplyResourceOverrides with Resources [%v], Platform Resources [%v] and Container"+
" Resources [%v] with mode [%v]", overrideResources, platformResources, container.Resources, mode)
switch mode {
case ResourceCustomizationModeAssignResources:
// this will use overrideResources to set container resources and fallback to the platformResource values.
// it is important to note that this ignores the existing container.Resources values.
container.Resources = ApplyResourceOverrides(*overrideResources, *platformResources, assignIfUnset)
case ResourceCustomizationModeMergeExistingResources:
// this merges the overrideResources on top of the existing container.Resources to apply the overrides, then it
// uses the platformResource values to set defaults for any missing resource.
MergeResources(*overrideResources, &container.Resources)
container.Resources = ApplyResourceOverrides(container.Resources, *platformResources, assignIfUnset)
case ResourceCustomizationModeEnsureExistingResourcesInRange:
// this use the platformResources defaults to ensure that the container.Resources values are within the
// platformResources limits. it will not override any existing container.Resources values.
container.Resources = ApplyResourceOverrides(container.Resources, *platformResources, !assignIfUnset)
}
logger.Infof(ctx, "Adjusted container resources [%v]", container.Resources)
return nil
}