This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_execution.go
82 lines (70 loc) · 1.98 KB
/
task_execution.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
package api
import (
"fmt"
"net/http"
"sort"
"strconv"
"time"
"github.com/Attsun1031/jobnetes/dao"
"github.com/Attsun1031/jobnetes/dao/db"
"github.com/Attsun1031/jobnetes/utils/config"
"github.com/Attsun1031/jobnetes/utils/log"
"github.com/gin-gonic/gin"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type TaskExecutionApi struct {
TaskExecutionDao dao.TaskExecutionDao
Client kubernetes.Interface
}
type PodResponse struct {
PodName string `json:"podName"`
StartTime time.Time `json:"startTime"`
Containers []string `json:"containers"`
}
func (api TaskExecutionApi) GetPods(c *gin.Context) {
d := db.Connect(config.JobnetesConfig.DbConfig, log.Logger)
defer d.Close()
id64, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
log.Logger.Warnf("Failed to parse request. error=%v", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
id := uint(id64)
te, err := api.TaskExecutionDao.FindById(id, d)
if err != nil {
panic(err)
}
if te == nil {
log.Logger.Warnf("Task not found. id=%v", id)
c.JSON(http.StatusNotFound, gin.H{"error": "task not found"})
return
}
ns := config.JobnetesConfig.KubernetesConfig.JobNamespace
podListResult, err := api.Client.
CoreV1().
Pods(ns).
List(v1meta.ListOptions{LabelSelector: fmt.Sprintf("job-name=%v", te.ExecutionName)})
if err != nil {
panic(err)
}
pods := podListResult.Items
// sort by start time desc
sort.SliceStable(pods, func(i, j int) bool {
return pods[i].Status.StartTime.After(pods[j].Status.StartTime.Time)
})
podResponses := make([]*PodResponse, len(pods))
for i, p := range pods {
resultContainers := make([]string, len(p.Spec.Containers))
for i, c := range p.Spec.Containers {
resultContainers[i] = c.Name
}
podResponses[i] = &PodResponse{
PodName: p.Name,
StartTime: p.Status.StartTime.Time,
Containers: resultContainers,
}
}
c.JSON(http.StatusOK, gin.H{"items": podResponses})
}