/
job.go
142 lines (129 loc) · 2.7 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
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
package web
import (
"database/sql"
"net/http"
"github.com/gin-gonic/gin"
"github.com/konveyor/mig-controller/pkg/controller/discovery/model"
batchv1 "k8s.io/api/batch/v1"
)
const (
JobParam = "job"
JobsRoot = NamespaceRoot + "/jobs"
JobRoot = JobsRoot + "/:" + JobParam
)
// Job (route) handler.
type JobHandler struct {
// Base
ClusterScoped
}
// Add routes.
func (h JobHandler) AddRoutes(r *gin.Engine) {
r.GET(JobsRoot, h.List)
r.GET(JobsRoot+"/", h.List)
r.GET(JobRoot, h.Get)
}
// List all of the Jobs on a cluster.
func (h JobHandler) List(ctx *gin.Context) {
status := h.Prepare(ctx)
if status != http.StatusOK {
ctx.Status(status)
return
}
db := h.container.Db
collection := model.Job{
Base: model.Base{
Cluster: h.cluster.PK,
},
}
count, err := collection.Count(db, model.ListOptions{})
if err != nil {
ctx.Status(http.StatusInternalServerError)
return
}
list, err := collection.List(
db,
model.ListOptions{
Page: &h.page,
})
if err != nil {
Log.Trace(err)
ctx.Status(http.StatusInternalServerError)
return
}
content := JobList{
Count: count,
}
for _, m := range list {
r := Job{}
r.With(m)
r.SelfLink = h.Link(&h.cluster, m)
content.Items = append(content.Items, r)
}
ctx.JSON(http.StatusOK, content)
}
// Get a specific Job on a cluster.
func (h JobHandler) Get(ctx *gin.Context) {
status := h.Prepare(ctx)
if status != http.StatusOK {
ctx.Status(status)
return
}
m := model.Job{
Base: model.Base{
Cluster: h.cluster.PK,
Namespace: ctx.Param(Ns2Param),
Name: ctx.Param(JobParam),
},
}
err := m.Get(h.container.Db)
if err != nil {
if err != sql.ErrNoRows {
Log.Trace(err)
ctx.Status(http.StatusInternalServerError)
return
} else {
ctx.Status(http.StatusNotFound)
return
}
}
r := Job{}
r.With(&m)
r.SelfLink = h.Link(&h.cluster, &m)
content := r
ctx.JSON(http.StatusOK, content)
}
// Build self link.
func (h JobHandler) Link(c *model.Cluster, m *model.Job) string {
return h.BaseHandler.Link(
JobRoot,
Params{
NsParam: c.Namespace,
ClusterParam: c.Name,
Ns2Param: m.Namespace,
JobParam: m.Name,
})
}
// Job REST resource
type Job struct {
// The k8s namespace.
Namespace string `json:"namespace,omitempty"`
// The k8s name.
Name string `json:"name"`
// Self URI.
SelfLink string `json:"selfLink"`
// Raw k8s object.
Object *batchv1.Job `json:"object,omitempty"`
}
// Build the resource.
func (r *Job) With(m *model.Job) {
r.Namespace = m.Namespace
r.Name = m.Name
r.Object = m.DecodeObject()
}
// Job collection REST resource.
type JobList struct {
// Total number in the collection.
Count int64 `json:"count"`
// List of resources.
Items []Job `json:"resources"`
}