forked from flyteorg/flyteplugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launcher.go
163 lines (128 loc) · 5.35 KB
/
launcher.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
package k8s
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/lyft/flyteplugins/go/tasks/plugins/array/errorcollector"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
arrayCore "github.com/lyft/flyteplugins/go/tasks/plugins/array/core"
arraystatus2 "github.com/lyft/flyteplugins/go/tasks/plugins/array/arraystatus"
errors2 "github.com/lyft/flytestdlib/errors"
"github.com/lyft/flytestdlib/logger"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/utils"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/core"
)
const (
ErrBuildPodTemplate errors2.ErrorCode = "POD_TEMPLATE_FAILED"
ErrReplaceCmdTemplate errors2.ErrorCode = "CMD_TEMPLATE_FAILED"
ErrSubmitJob errors2.ErrorCode = "SUBMIT_JOB_FAILED"
JobIndexVarName string = "BATCH_JOB_ARRAY_INDEX_VAR_NAME"
FlyteK8sArrayIndexVarName string = "FLYTE_K8S_ARRAY_INDEX"
)
var arrayJobEnvVars = []corev1.EnvVar{
{
Name: JobIndexVarName,
Value: FlyteK8sArrayIndexVarName,
},
}
func formatSubTaskName(_ context.Context, parentName, suffix string) (subTaskName string) {
return fmt.Sprintf("%v-%v", parentName, suffix)
}
func ApplyPodPolicies(_ context.Context, cfg *Config, pod *corev1.Pod) *corev1.Pod {
if len(cfg.DefaultScheduler) > 0 {
pod.Spec.SchedulerName = cfg.DefaultScheduler
}
return pod
}
// Launches subtasks
func LaunchSubTasks(ctx context.Context, tCtx core.TaskExecutionContext, kubeClient core.KubeClient,
config *Config, currentState *arrayCore.State) (newState *arrayCore.State, err error) {
if int64(currentState.GetExecutionArraySize()) > config.MaxArrayJobSize {
ee := fmt.Errorf("array size > max allowed. Requested [%v]. Allowed [%v]", currentState.GetExecutionArraySize(), config.MaxArrayJobSize)
logger.Info(ctx, ee)
currentState = currentState.SetPhase(arrayCore.PhasePermanentFailure, 0).SetReason(ee.Error())
return currentState, nil
}
podTemplate, _, err := FlyteArrayJobToK8sPodTemplate(ctx, tCtx)
if err != nil {
return currentState, errors2.Wrapf(ErrBuildPodTemplate, err, "Failed to convert task template to a pod template for task")
}
var command []string
if len(podTemplate.Spec.Containers) > 0 {
command = append(podTemplate.Spec.Containers[0].Command, podTemplate.Spec.Containers[0].Args...)
podTemplate.Spec.Containers[0].Args = []string{}
}
size := currentState.GetExecutionArraySize()
// TODO: Respect parallelism param
for i := 0; i < size; i++ {
pod := podTemplate.DeepCopy()
indexStr := strconv.Itoa(i)
pod.Name = formatSubTaskName(ctx, tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName(), indexStr)
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, corev1.EnvVar{
Name: FlyteK8sArrayIndexVarName,
Value: indexStr,
})
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, arrayJobEnvVars...)
pod.Spec.Containers[0].Command, err = utils.ReplaceTemplateCommandArgs(ctx, command, arrayJobInputReader{tCtx.InputReader()}, tCtx.OutputWriter())
if err != nil {
return currentState, errors2.Wrapf(ErrReplaceCmdTemplate, err, "Failed to replace cmd args")
}
pod = ApplyPodPolicies(ctx, config, 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)
currentState = currentState.SetPhase(arrayCore.PhaseWaitingForResources, 0).SetReason("Not enough resources to launch job.")
} else {
currentState = currentState.SetPhase(arrayCore.PhaseRetryableFailure, 0).SetReason("Failed to launch job.")
}
currentState = currentState.SetReason(err.Error())
return currentState, nil
}
return currentState, errors2.Wrapf(ErrSubmitJob, err, "Failed to submit job")
}
}
logger.Infof(ctx, "Successfully submitted Job(s) with Prefix:[%v], Count:[%v]", tCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName(), size)
arrayStatus := arraystatus2.ArrayStatus{
Summary: arraystatus2.ArraySummary{},
Detailed: arrayCore.NewPhasesCompactArray(uint(size)),
}
currentState.SetPhase(arrayCore.PhaseCheckingSubTaskExecutions, 0).SetReason("Job launched.")
currentState.SetArrayStatus(arrayStatus)
return currentState, nil
}
func TerminateSubTasks(ctx context.Context, tMeta core.TaskExecutionMetadata, kubeClient core.KubeClient,
errsMaxLength int, currentState *arrayCore.State) error {
size := currentState.GetExecutionArraySize()
errs := errorcollector.NewErrorMessageCollector()
for i := 0; i < size; i++ {
indexStr := strconv.Itoa(i)
podName := formatSubTaskName(ctx, tMeta.GetTaskExecutionID().GetGeneratedName(), indexStr)
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: PodKind,
APIVersion: metav1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: tMeta.GetNamespace(),
},
}
err := kubeClient.GetClient().Delete(ctx, pod)
if err != nil {
if k8serrors.IsNotFound(err) {
continue
}
errs.Collect(i, err.Error())
}
}
if errs.Length() > 0 {
return fmt.Errorf(errs.Summary(errsMaxLength))
}
return nil
}