forked from Qihoo360/wayne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
116 lines (108 loc) · 3.27 KB
/
job.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
package job
import (
"github.com/Qihoo360/wayne/src/backend/client"
"github.com/Qihoo360/wayne/src/backend/controllers/base"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/resources/job"
"github.com/Qihoo360/wayne/src/backend/util/logs"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/api/errors"
)
type KubeJobController struct {
base.APIController
}
type ClusterJob struct {
Job batchv1.Job `json:"kubeJob"`
Cluster string `json:"cluster"`
}
func (c *KubeJobController) URLMapping() {
c.Mapping("ListByCronjob", c.ListByCronjob)
c.Mapping("ListAllClusterByCronjob", c.ListAllClusterByCronjob)
c.Mapping("GetPodsEvent", c.GetPodsEvent)
}
func (c *KubeJobController) Prepare() {
// Check administration
c.APIController.Prepare()
}
// @Title Get
// @Description find Job by cluster
// @Success 200 {object} models.Job success
// @router /listByCronjob/:cronjob/namespaces/:namespace/clusters/:cluster [get]
func (c *KubeJobController) ListByCronjob() {
cluster := c.Ctx.Input.Param(":cluster")
namespace := c.Ctx.Input.Param(":namespace")
cronjobName := c.Ctx.Input.Param(":cronjob")
cli, err := client.Client(cluster)
if err == nil {
jobs, err := job.GetJobsByCronjobName(cli, namespace, cronjobName)
if err != nil {
logs.Error("get job by cronjob (%s) and cluster (%s) error.%v", cronjobName, cluster, err)
c.HandleError(err)
return
}
c.Success(jobs)
} else {
c.AbortBadRequestFormat("Cluster")
}
}
// @Title Get
// @Description find Job by cluster
// @Success 200 {object} models.Job success
// @router /listAllClusterByCronjob/:cronjob/namespaces/:namespace [get]
func (c *KubeJobController) ListAllClusterByCronjob() {
namespace := c.Ctx.Input.Param(":namespace")
cronjobName := c.Ctx.Input.Param(":cronjob")
clusters, err := models.ClusterModel.GetNames(false)
if err != nil {
logs.Error("get cluster error. %v", err)
c.HandleError(err)
return
}
var allJobs []ClusterJob
for _, cluster := range clusters {
cli, err := client.Client(cluster.Name)
if err == nil {
jobs, err := job.GetJobsByCronjobName(cli, namespace, cronjobName)
if err != nil {
if errors.IsNotFound(err) {
continue
}
logs.Error("get job by cronjob (%s) and cluster (%s) error.%v", cronjobName, cluster, err)
c.HandleError(err)
return
}
for _, job := range jobs {
oneJob := ClusterJob{
Job: job,
Cluster: cluster.Name,
}
allJobs = append(allJobs, oneJob)
}
} else {
c.AbortBadRequestFormat("Cluster")
}
}
c.Success(allJobs)
}
// @Title Get
// @Description find job by cluster
// @Success 200 {object} models.Job success
// @router /getPodsEvent/:job/:cronjob/namespaces/:namespace/clusters/:cluster [get]
func (c *KubeJobController) GetPodsEvent() {
cluster := c.Ctx.Input.Param(":cluster")
namespace := c.Ctx.Input.Param(":namespace")
name := c.Ctx.Input.Param(":job")
cronjobName := c.Ctx.Input.Param(":cronjob")
manager, err := client.Manager(cluster)
if err == nil {
result, err := job.GetPodsEvent(manager.Client, manager.Indexer, namespace, name, cronjobName)
if err != nil {
logs.Error("get kubernetes job pods event error.", cluster, namespace, name, err)
c.HandleError(err)
return
}
c.Success(result)
} else {
c.AbortBadRequestFormat("Cluster")
}
}