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
/
task.go
328 lines (277 loc) · 10.9 KB
/
task.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
package k8s
import (
"context"
"strconv"
"strings"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
idlCore "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/tasklog"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyteplugins/go/tasks/plugins/array"
"github.com/flyteorg/flyteplugins/go/tasks/plugins/array/arraystatus"
arrayCore "github.com/flyteorg/flyteplugins/go/tasks/plugins/array/core"
"github.com/flyteorg/flyteplugins/go/tasks/plugins/array/errorcollector"
"github.com/flyteorg/flytestdlib/bitarray"
errors2 "github.com/flyteorg/flytestdlib/errors"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/storage"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sTypes "k8s.io/apimachinery/pkg/types"
)
type Task struct {
State *arrayCore.State
NewArrayStatus *arraystatus.ArrayStatus
Config *Config
ChildIdx int
MessageCollector *errorcollector.ErrorMessageCollector
SubTaskIDs []*string
}
type LaunchResult int8
type MonitorResult int8
const (
LaunchSuccess LaunchResult = iota
LaunchError
LaunchWaiting
LaunchReturnState
)
const finalizer = "flyte/array"
func addPodFinalizer(pod *corev1.Pod) *corev1.Pod {
pod.Finalizers = append(pod.Finalizers, finalizer)
return pod
}
func removeString(list []string, target string) []string {
ret := make([]string, 0)
for _, s := range list {
if s != target {
ret = append(ret, s)
}
}
return ret
}
func clearFinalizer(pod *corev1.Pod) *corev1.Pod {
pod.Finalizers = removeString(pod.Finalizers, finalizer)
return pod
}
const (
MonitorSuccess MonitorResult = iota
MonitorError
)
func getTaskContainerIndex(pod *v1.Pod) (int, error) {
primaryContainerName, ok := pod.Annotations[primaryContainerKey]
// For tasks with a Container target, we only ever build one container as part of the pod
if !ok {
if len(pod.Spec.Containers) == 1 {
return 0, nil
}
// For tasks with a K8sPod task target, they may produce multiple containers but at least one must be the designated primary.
return -1, errors2.Errorf(ErrBuildPodTemplate, "Expected a specified primary container key when building an array job with a K8sPod spec target")
}
for idx, container := range pod.Spec.Containers {
if container.Name == primaryContainerName {
return idx, nil
}
}
return -1, errors2.Errorf(ErrBuildPodTemplate, "Couldn't find any container matching the primary container key when building an array job with a K8sPod spec target")
}
func (t Task) Launch(ctx context.Context, tCtx core.TaskExecutionContext, kubeClient core.KubeClient) (LaunchResult, error) {
podTemplate, _, err := FlyteArrayJobToK8sPodTemplate(ctx, tCtx, t.Config.NamespaceTemplate)
if err != nil {
return LaunchError, errors2.Wrapf(ErrBuildPodTemplate, err, "Failed to convert task template to a pod template for a task")
}
// Remove owner references for remote cluster execution
if t.Config.RemoteClusterConfig.Enabled {
podTemplate.OwnerReferences = nil
}
if len(podTemplate.Spec.Containers) == 0 {
return LaunchError, errors2.Wrapf(ErrReplaceCmdTemplate, err, "No containers found in podSpec.")
}
containerIndex, err := getTaskContainerIndex(&podTemplate)
if err != nil {
return LaunchError, err
}
indexStr := strconv.Itoa(t.ChildIdx)
podName := formatSubTaskName(ctx, tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName(), indexStr)
pod := podTemplate.DeepCopy()
pod.Name = podName
pod.Spec.Containers[containerIndex].Env = append(pod.Spec.Containers[containerIndex].Env, corev1.EnvVar{
Name: FlyteK8sArrayIndexVarName,
Value: indexStr,
})
pod.Spec.Containers[containerIndex].Env = append(pod.Spec.Containers[containerIndex].Env, arrayJobEnvVars...)
taskTemplate, err := tCtx.TaskReader().Read(ctx)
if err != nil {
return LaunchError, errors2.Wrapf(ErrGetTaskTypeVersion, err, "Unable to read task template")
} else if taskTemplate == nil {
return LaunchError, errors2.Wrapf(ErrGetTaskTypeVersion, err, "Missing task template")
}
pod = ApplyPodPolicies(ctx, t.Config, pod)
pod = applyNodeSelectorLabels(ctx, t.Config, pod)
pod = applyPodTolerations(ctx, t.Config, pod)
pod = addPodFinalizer(pod)
allocationStatus, err := allocateResource(ctx, tCtx, t.Config, podName)
if err != nil {
return LaunchError, err
}
if allocationStatus != core.AllocationStatusGranted {
t.NewArrayStatus.Detailed.SetItem(t.ChildIdx, bitarray.Item(core.PhaseWaitingForResources))
t.NewArrayStatus.Summary.Inc(core.PhaseWaitingForResources)
return LaunchWaiting, nil
}
// Check for existing pods to prevent unnecessary Resource-Quota usage: https://github.com/kubernetes/kubernetes/issues/76787
existingPod := &corev1.Pod{}
err = kubeClient.GetCache().Get(ctx, client.ObjectKey{
Namespace: pod.GetNamespace(),
Name: pod.GetName(),
}, existingPod)
if err != nil && k8serrors.IsNotFound(err) {
// Attempt creating non-existing pod.
err = kubeClient.GetClient().Create(ctx, pod)
if err != nil && !k8serrors.IsAlreadyExists(err) {
if k8serrors.IsForbidden(err) {
if strings.Contains(err.Error(), "exceeded quota") {
// TODO: Quota errors are retried forever, it would be good to have support for backoff strategy.
logger.Infof(ctx, "Failed to launch job, resource quota exceeded. Err: %v", err)
t.State = t.State.SetPhase(arrayCore.PhaseWaitingForResources, 0).SetReason("Not enough resources to launch job")
} else {
t.State = t.State.SetPhase(arrayCore.PhaseRetryableFailure, 0).SetReason("Failed to launch job.")
}
t.State = t.State.SetReason(err.Error())
return LaunchReturnState, nil
}
return LaunchError, errors2.Wrapf(ErrSubmitJob, err, "Failed to submit job.")
}
} else if err != nil {
// Another error returned.
logger.Error(ctx, err)
return LaunchError, errors2.Wrapf(ErrSubmitJob, err, "Failed to submit job.")
}
return LaunchSuccess, nil
}
func (t *Task) Monitor(ctx context.Context, tCtx core.TaskExecutionContext, kubeClient core.KubeClient, dataStore *storage.DataStore, outputPrefix, baseOutputDataSandbox storage.DataReference,
logPlugin tasklog.Plugin) (MonitorResult, []*idlCore.TaskLog, error) {
indexStr := strconv.Itoa(t.ChildIdx)
podName := formatSubTaskName(ctx, tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName(), indexStr)
t.SubTaskIDs = append(t.SubTaskIDs, &podName)
var loglinks []*idlCore.TaskLog
// Use original-index for log-name/links
originalIdx := arrayCore.CalculateOriginalIndex(t.ChildIdx, t.State.GetIndexesToCache())
phaseInfo, err := FetchPodStatusAndLogs(ctx, kubeClient,
k8sTypes.NamespacedName{
Name: podName,
Namespace: GetNamespaceForExecution(tCtx, t.Config.NamespaceTemplate),
},
originalIdx,
tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetID().RetryAttempt,
logPlugin)
if err != nil {
return MonitorError, loglinks, errors2.Wrapf(ErrCheckPodStatus, err, "Failed to check pod status.")
}
if phaseInfo.Info() != nil {
loglinks = phaseInfo.Info().Logs
}
if phaseInfo.Err() != nil {
t.MessageCollector.Collect(t.ChildIdx, phaseInfo.Err().String())
}
actualPhase := phaseInfo.Phase()
if phaseInfo.Phase().IsSuccess() {
actualPhase, err = array.CheckTaskOutput(ctx, dataStore, outputPrefix, baseOutputDataSandbox, t.ChildIdx, originalIdx)
if err != nil {
return MonitorError, loglinks, err
}
}
t.NewArrayStatus.Detailed.SetItem(t.ChildIdx, bitarray.Item(actualPhase))
t.NewArrayStatus.Summary.Inc(actualPhase)
return MonitorSuccess, loglinks, nil
}
func (t Task) Abort(ctx context.Context, tCtx core.TaskExecutionContext, kubeClient core.KubeClient) error {
indexStr := strconv.Itoa(t.ChildIdx)
podName := formatSubTaskName(ctx, tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName(), indexStr)
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: PodKind,
APIVersion: metav1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: GetNamespaceForExecution(tCtx, t.Config.NamespaceTemplate),
},
}
err := kubeClient.GetClient().Delete(ctx, pod)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
return nil
}
func (t Task) Finalize(ctx context.Context, tCtx core.TaskExecutionContext, kubeClient core.KubeClient) error {
indexStr := strconv.Itoa(t.ChildIdx)
podName := formatSubTaskName(ctx, tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName(), indexStr)
pod := &v1.Pod{
TypeMeta: metaV1.TypeMeta{
Kind: PodKind,
APIVersion: v1.SchemeGroupVersion.String(),
},
}
err := kubeClient.GetClient().Get(ctx, k8sTypes.NamespacedName{
Name: podName,
Namespace: GetNamespaceForExecution(tCtx, t.Config.NamespaceTemplate),
}, pod)
if err != nil {
if !k8serrors.IsNotFound(err) {
logger.Errorf(ctx, "Error fetching pod [%s] in Finalize [%s]", podName, err)
return err
}
} else {
pod = clearFinalizer(pod)
err := kubeClient.GetClient().Update(ctx, pod)
if err != nil {
logger.Errorf(ctx, "Error updating pod finalizer [%s] in Finalize [%s]", podName, err)
return err
}
}
// Deallocate Resource
err = deallocateResource(ctx, tCtx, t.Config, t.ChildIdx)
if err != nil {
logger.Errorf(ctx, "Error releasing allocation token [%s] in Finalize [%s]", podName, err)
return err
}
return nil
}
func allocateResource(ctx context.Context, tCtx core.TaskExecutionContext, config *Config, podName string) (core.AllocationStatus, error) {
if !IsResourceConfigSet(config.ResourceConfig) {
return core.AllocationStatusGranted, nil
}
resourceNamespace := core.ResourceNamespace(config.ResourceConfig.PrimaryLabel)
resourceConstraintSpec := core.ResourceConstraintsSpec{
ProjectScopeResourceConstraint: nil,
NamespaceScopeResourceConstraint: nil,
}
allocationStatus, err := tCtx.ResourceManager().AllocateResource(ctx, resourceNamespace, podName, resourceConstraintSpec)
if err != nil {
logger.Errorf(ctx, "Resource manager failed for TaskExecId [%s] token [%s]. error %s",
tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetID(), podName, err)
return core.AllocationUndefined, err
}
logger.Infof(ctx, "Allocation result for [%s] is [%s]", podName, allocationStatus)
return allocationStatus, nil
}
func deallocateResource(ctx context.Context, tCtx core.TaskExecutionContext, config *Config, childIdx int) error {
if !IsResourceConfigSet(config.ResourceConfig) {
return nil
}
indexStr := strconv.Itoa((childIdx))
podName := formatSubTaskName(ctx, tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName(), indexStr)
resourceNamespace := core.ResourceNamespace(config.ResourceConfig.PrimaryLabel)
err := tCtx.ResourceManager().ReleaseResource(ctx, resourceNamespace, podName)
if err != nil {
logger.Errorf(ctx, "Error releasing token [%s]. error %s", podName, err)
return err
}
return nil
}