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
/
plugin.go
184 lines (154 loc) · 6.35 KB
/
plugin.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
package pod
import (
"context"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteplugins/go/tasks/errors"
"github.com/flyteorg/flyteplugins/go/tasks/logs"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery"
pluginsCore "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/flytek8s"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/k8s"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/tasklog"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
podTaskType = "pod"
PrimaryContainerKey = "primary_container_name"
)
var (
DefaultPodPlugin = plugin{
defaultPodBuilder: containerPodBuilder{},
podBuilders: map[string]podBuilder{
SidecarTaskType: sidecarPodBuilder{},
},
}
)
type podBuilder interface {
buildPodSpec(ctx context.Context, task *core.TaskTemplate, taskCtx pluginsCore.TaskExecutionContext) (*v1.PodSpec, error)
getPrimaryContainerName(task *core.TaskTemplate, taskCtx pluginsCore.TaskExecutionContext) (string, error)
updatePodMetadata(ctx context.Context, pod *v1.Pod, task *core.TaskTemplate, taskCtx pluginsCore.TaskExecutionContext) error
}
type plugin struct {
defaultPodBuilder podBuilder
podBuilders map[string]podBuilder
}
func (plugin) BuildIdentityResource(_ context.Context, _ pluginsCore.TaskExecutionMetadata) (
client.Object, error) {
return flytek8s.BuildIdentityPod(), nil
}
func (p plugin) BuildResource(ctx context.Context, taskCtx pluginsCore.TaskExecutionContext) (client.Object, error) {
// read TaskTemplate
task, err := taskCtx.TaskReader().Read(ctx)
if err != nil {
return nil, errors.Errorf(errors.BadTaskSpecification,
"TaskSpecification cannot be read, Err: [%v]", err.Error())
}
// initialize PodBuilder
builder, exists := p.podBuilders[task.Type]
if !exists {
builder = p.defaultPodBuilder
}
podSpec, err := builder.buildPodSpec(ctx, task, taskCtx)
if err != nil {
return nil, err
}
podSpec.ServiceAccountName = flytek8s.GetServiceAccountNameFromTaskExecutionMetadata(taskCtx.TaskExecutionMetadata())
podTemplate := flytek8s.DefaultPodTemplateStore.LoadOrDefault(taskCtx.TaskExecutionMetadata().GetNamespace())
primaryContainerName, err := builder.getPrimaryContainerName(task, taskCtx)
if err != nil {
return nil, err
}
pod, err := flytek8s.BuildPodWithSpec(podTemplate, podSpec, primaryContainerName)
if err != nil {
return nil, err
}
// update pod metadata
if err = builder.updatePodMetadata(ctx, pod, task, taskCtx); err != nil {
return nil, err
}
return pod, nil
}
func (p plugin) GetTaskPhase(ctx context.Context, pluginContext k8s.PluginContext, r client.Object) (pluginsCore.PhaseInfo, error) {
logPlugin, err := logs.InitializeLogPlugins(logs.GetLogConfig())
if err != nil {
return pluginsCore.PhaseInfoUndefined, err
}
return p.GetTaskPhaseWithLogs(ctx, pluginContext, r, logPlugin, " (User)")
}
func (plugin) GetTaskPhaseWithLogs(ctx context.Context, pluginContext k8s.PluginContext, r client.Object, logPlugin tasklog.Plugin, logSuffix string) (pluginsCore.PhaseInfo, error) {
pod := r.(*v1.Pod)
transitionOccurredAt := flytek8s.GetLastTransitionOccurredAt(pod).Time
info := pluginsCore.TaskInfo{
OccurredAt: &transitionOccurredAt,
}
if pod.Status.Phase != v1.PodPending && pod.Status.Phase != v1.PodUnknown {
taskLogs, err := logs.GetLogsForContainerInPod(ctx, logPlugin, pod, 0, logSuffix)
if err != nil {
return pluginsCore.PhaseInfoUndefined, err
}
info.Logs = taskLogs
}
switch pod.Status.Phase {
case v1.PodSucceeded:
return flytek8s.DemystifySuccess(pod.Status, info)
case v1.PodFailed:
return flytek8s.DemystifyFailure(pod.Status, info)
case v1.PodPending:
return flytek8s.DemystifyPending(pod.Status)
case v1.PodReasonUnschedulable:
return pluginsCore.PhaseInfoQueued(transitionOccurredAt, pluginsCore.DefaultPhaseVersion, "pod unschedulable"), nil
case v1.PodUnknown:
return pluginsCore.PhaseInfoUndefined, nil
}
primaryContainerName, exists := r.GetAnnotations()[PrimaryContainerKey]
if !exists {
// if the primary container annotation dos not exist, then the task requires all containers
// to succeed to declare success. therefore, if the pod is not in one of the above states we
// fallback to declaring the task as 'running'.
if len(info.Logs) > 0 {
return pluginsCore.PhaseInfoRunning(pluginsCore.DefaultPhaseVersion+1, &info), nil
}
return pluginsCore.PhaseInfoRunning(pluginsCore.DefaultPhaseVersion, &info), nil
}
// if the primary container annotation exists, we use the status of the specified container
primaryContainerPhase := flytek8s.DeterminePrimaryContainerPhase(primaryContainerName, pod.Status.ContainerStatuses, &info)
if primaryContainerPhase.Phase() == pluginsCore.PhaseRunning && len(info.Logs) > 0 {
return pluginsCore.PhaseInfoRunning(pluginsCore.DefaultPhaseVersion+1, primaryContainerPhase.Info()), nil
}
return primaryContainerPhase, nil
}
func (plugin) GetProperties() k8s.PluginProperties {
return k8s.PluginProperties{}
}
func init() {
// Register ContainerTaskType and SidecarTaskType plugin entries. These separate task types
// still exist within the system, only now both are evaluated using the same internal pod plugin
// instance. This simplifies migration as users may keep the same configuration but are
// seamlessly transitioned from separate container and sidecar plugins to a single pod plugin.
pluginmachinery.PluginRegistry().RegisterK8sPlugin(
k8s.PluginEntry{
ID: ContainerTaskType,
RegisteredTaskTypes: []pluginsCore.TaskType{ContainerTaskType, PythonTaskType},
ResourceToWatch: &v1.Pod{},
Plugin: DefaultPodPlugin,
IsDefault: true,
})
pluginmachinery.PluginRegistry().RegisterK8sPlugin(
k8s.PluginEntry{
ID: SidecarTaskType,
RegisteredTaskTypes: []pluginsCore.TaskType{SidecarTaskType},
ResourceToWatch: &v1.Pod{},
Plugin: DefaultPodPlugin,
IsDefault: false,
})
// register podTaskType plugin entry
pluginmachinery.PluginRegistry().RegisterK8sPlugin(
k8s.PluginEntry{
ID: podTaskType,
RegisteredTaskTypes: []pluginsCore.TaskType{ContainerTaskType, PythonTaskType, SidecarTaskType},
ResourceToWatch: &v1.Pod{},
Plugin: DefaultPodPlugin,
IsDefault: true,
})
}