Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit 435436b

Browse files
authored
Set PrimaryContainerKey annotation by default (#337)
* added pod plugin optimizations Signed-off-by: Daniel Rammer <daniel@union.ai> * fixed unit tests Signed-off-by: Daniel Rammer <daniel@union.ai> --------- Signed-off-by: Daniel Rammer <daniel@union.ai>
1 parent 4f02c2c commit 435436b

4 files changed

Lines changed: 65 additions & 4 deletions

File tree

go/tasks/plugins/array/k8s/integration_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,18 @@ func advancePodPhases(ctx context.Context, store *storage.DataStore, outputWrite
7777

7878
for _, pod := range podList.Items {
7979
newPhase := nextHappyPodPhase(pod.Status.Phase)
80+
primaryContainerName := pod.Annotations["primary_container_name"]
81+
if len(primaryContainerName) <= 0 {
82+
primaryContainerName = "foo"
83+
}
8084
pod.Status.ContainerStatuses = []v1.ContainerStatus{
81-
{ContainerID: "cont_123"},
85+
v1.ContainerStatus{
86+
Name: primaryContainerName,
87+
ContainerID: primaryContainerName,
88+
State: v1.ContainerState{
89+
Running: &v1.ContainerStateRunning{},
90+
},
91+
},
8292
}
8393

8494
if pod.Status.Phase != newPhase && newPhase == v1.PodSucceeded {
@@ -95,6 +105,10 @@ func advancePodPhases(ctx context.Context, store *storage.DataStore, outputWrite
95105
}
96106
}
97107

108+
pod.Status.ContainerStatuses[0].State = v1.ContainerState{
109+
Terminated: &v1.ContainerStateTerminated{},
110+
}
111+
98112
ref := outputWriter.GetOutputPath()
99113
if idx > -1 {
100114
ref, err = store.ConstructReference(ctx, outputWriter.GetOutputPrefixPath(), strconv.Itoa(idx), "outputs.pb")

go/tasks/plugins/array/k8s/management_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ func TestCheckSubTasksState(t *testing.T) {
164164
pod.Spec.Containers = append(pod.Spec.Containers, v1.Container{Name: "foo"})
165165

166166
pod.Status.Phase = v1.PodRunning
167+
pod.Status.ContainerStatuses = []v1.ContainerStatus{
168+
v1.ContainerStatus{
169+
State: v1.ContainerState{
170+
Running: &v1.ContainerStateRunning{},
171+
},
172+
},
173+
}
167174
_ = fakeKubeClient.Create(ctx, pod)
168175
_ = fakeKubeCache.Create(ctx, pod)
169176
}

go/tasks/plugins/k8s/pod/container_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ func TestContainerTaskExecutor_GetTaskStatus(t *testing.T) {
156156
ctx := context.TODO()
157157
t.Run("running", func(t *testing.T) {
158158
j.Status.Phase = v1.PodRunning
159+
j.Status.ContainerStatuses = []v1.ContainerStatus{
160+
{
161+
State: v1.ContainerState{
162+
Running: &v1.ContainerStateRunning{},
163+
},
164+
},
165+
}
166+
159167
phaseInfo, err := DefaultPodPlugin.GetTaskPhase(ctx, taskCtx, j)
160168
assert.NoError(t, err)
161169
assert.Equal(t, pluginsCore.PhaseRunning, phaseInfo.Phase())
@@ -193,6 +201,23 @@ func TestContainerTaskExecutor_GetTaskStatus(t *testing.T) {
193201
assert.Equal(t, "Unschedulable", ec)
194202
})
195203

204+
t.Run("successOptimized", func(t *testing.T) {
205+
j.Status.Phase = v1.PodRunning
206+
j.Status.ContainerStatuses = []v1.ContainerStatus{
207+
{
208+
State: v1.ContainerState{
209+
Terminated: &v1.ContainerStateTerminated{
210+
ExitCode: 0,
211+
},
212+
},
213+
},
214+
}
215+
216+
phaseInfo, err := DefaultPodPlugin.GetTaskPhase(ctx, taskCtx, j)
217+
assert.NoError(t, err)
218+
assert.Equal(t, pluginsCore.PhaseSuccess, phaseInfo.Phase())
219+
})
220+
196221
t.Run("success", func(t *testing.T) {
197222
j.Status.Phase = v1.PodSucceeded
198223
phaseInfo, err := DefaultPodPlugin.GetTaskPhase(ctx, taskCtx, j)

go/tasks/plugins/k8s/pod/plugin.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ func (p plugin) BuildResource(ctx context.Context, taskCtx pluginsCore.TaskExecu
121121
return nil, err
122122
}
123123

124-
// set primary container name if this is executed as a sidecar
125-
if taskTemplate.Type == SidecarTaskType {
124+
// set primaryContainerKey annotation if this is a Sidecar task or, as an optimization, if there is only a single
125+
// container. this plugin marks the task complete if the primary Container is complete, so if there is only one
126+
// container we can mark the task as complete before the Pod has been marked complete.
127+
if taskTemplate.Type == SidecarTaskType || len(podSpec.Containers) == 1 {
126128
objectMeta.Annotations[flytek8s.PrimaryContainerKey] = primaryContainerName
127129
}
128130

@@ -187,7 +189,20 @@ func (plugin) GetTaskPhaseWithLogs(ctx context.Context, pluginContext k8s.Plugin
187189
default:
188190
primaryContainerName, exists := r.GetAnnotations()[flytek8s.PrimaryContainerKey]
189191
if !exists {
190-
// if the primary container annotation dos not exist, then the task requires all containers
192+
// if all of the containers in the Pod are complete, as an optimization, we can declare the task as
193+
// succeeded rather than waiting for the Pod to be marked completed.
194+
allSuccessfullyTerminated := len(pod.Status.ContainerStatuses) > 0
195+
for _, s := range pod.Status.ContainerStatuses {
196+
if s.State.Waiting != nil || s.State.Running != nil || (s.State.Terminated != nil && s.State.Terminated.ExitCode != 0) {
197+
allSuccessfullyTerminated = false
198+
}
199+
}
200+
201+
if allSuccessfullyTerminated {
202+
return flytek8s.DemystifySuccess(pod.Status, info)
203+
}
204+
205+
// if the primary container annotation does not exist, then the task requires all containers
191206
// to succeed to declare success. therefore, if the pod is not in one of the above states we
192207
// fallback to declaring the task as 'running'.
193208
phaseInfo = pluginsCore.PhaseInfoRunning(pluginsCore.DefaultPhaseVersion, &info)

0 commit comments

Comments
 (0)