-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch_status.go
141 lines (123 loc) · 3.38 KB
/
batch_status.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
package azurebatch
import (
"encoding/json"
"fmt"
"github.com/Azure/azure-sdk-for-go/services/batch/2017-09-01.6.0/batch"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func getPodFromTask(task *batch.CloudTask) (pod *apiv1.Pod, err error) {
if task == nil || task.EnvironmentSettings == nil {
return nil, fmt.Errorf("invalid input task: %v", task)
}
ok := false
jsonData := ""
settings := *task.EnvironmentSettings
for _, s := range settings {
if *s.Name == podJSONKey && s.Value != nil {
ok = true
jsonData = *s.Value
}
}
if !ok {
return nil, fmt.Errorf("task doesn't have pod json stored in it: %v", task.EnvironmentSettings)
}
pod = &apiv1.Pod{}
err = json.Unmarshal([]byte(jsonData), pod)
if err != nil {
return nil, err
}
return
}
func convertTaskToPodStatus(task *batch.CloudTask) (status *apiv1.PodStatus, err error) {
pod, err := getPodFromTask(task)
if err != nil {
return
}
// Todo: Review indivudal container status response
status = &apiv1.PodStatus{
Phase: convertTaskStatusToPodPhase(task),
Conditions: []apiv1.PodCondition{},
Message: "",
Reason: "",
HostIP: "",
PodIP: "127.0.0.1",
StartTime: &pod.CreationTimestamp,
}
for _, container := range pod.Spec.Containers {
containerStatus := apiv1.ContainerStatus{
Name: container.Name,
State: convertTaskStatusToContainerState(task),
Ready: true,
RestartCount: 0,
Image: container.Image,
ImageID: "",
ContainerID: "",
}
status.ContainerStatuses = append(status.ContainerStatuses, containerStatus)
}
return
}
func convertTaskStatusToPodPhase(t *batch.CloudTask) (podPhase apiv1.PodPhase) {
switch t.State {
case batch.TaskStatePreparing:
podPhase = apiv1.PodPending
case batch.TaskStateActive:
podPhase = apiv1.PodPending
case batch.TaskStateRunning:
podPhase = apiv1.PodRunning
case batch.TaskStateCompleted:
podPhase = apiv1.PodFailed
if t.ExecutionInfo != nil && t.ExecutionInfo.ExitCode != nil && *t.ExecutionInfo.ExitCode == 0 {
podPhase = apiv1.PodSucceeded
}
}
return
}
func convertTaskStatusToContainerState(t *batch.CloudTask) (containerState apiv1.ContainerState) {
startTime := metav1.Time{}
if t.ExecutionInfo != nil {
if t.ExecutionInfo.StartTime != nil {
startTime.Time = t.ExecutionInfo.StartTime.Time
}
}
switch t.State {
case batch.TaskStatePreparing:
containerState = apiv1.ContainerState{
Waiting: &apiv1.ContainerStateWaiting{
Message: "Waiting for machine in AzureBatch",
Reason: "Preparing",
},
}
case batch.TaskStateActive:
containerState = apiv1.ContainerState{
Waiting: &apiv1.ContainerStateWaiting{
Message: "Waiting for machine in AzureBatch",
Reason: "Queued",
},
}
case batch.TaskStateRunning:
containerState = apiv1.ContainerState{
Running: &apiv1.ContainerStateRunning{
StartedAt: startTime,
},
}
case batch.TaskStateCompleted:
termStatus := apiv1.ContainerState{
Terminated: &apiv1.ContainerStateTerminated{
FinishedAt: metav1.Time{
Time: t.StateTransitionTime.Time,
},
StartedAt: startTime,
},
}
if t.ExecutionInfo != nil && t.ExecutionInfo.ExitCode != nil {
exitCode := *t.ExecutionInfo.ExitCode
termStatus.Terminated.ExitCode = exitCode
if exitCode != 0 {
termStatus.Terminated.Message = *t.ExecutionInfo.FailureInfo.Message
}
}
}
return
}