-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
220 lines (174 loc) · 5.46 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
Copyright (c) 42Crunch Ltd. All rights reserved.
Licensed under the GNU Affero General Public License version 3. See LICENSE.txt in the project root for license information.
*/
package main
import (
"bytes"
"context"
"errors"
"flag"
"io"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes"
)
const defaultplatformService = "services.us.42crunch.cloud:8001"
const defaultScandImage = "42crunch/scand-agent:latest"
const defaultExpirationTime = 86400 // expire completed jobs after 24h
var namespace string
var platformService string
var scandImage string
var expirationTimeInt int64
var clientset *kubernetes.Clientset
var podconfig *v1.PodSpec
func main() {
podconfigFile := flag.String("podconfig", "", "pod configuration file")
flag.Parse()
readEnvConfig()
if *podconfigFile != "" {
readPodConfig(*podconfigFile)
}
connectk8sClient()
log.Printf("Starting scand manager: NAMESPACE: '%s', PLATFORM_HOST: '%s', SCAND_IMAGE: '%s', EXPIRATION_TIME: '%d'",
namespace, platformService, scandImage, expirationTimeInt)
if podconfig != nil {
log.Printf("Using pod configuration file: %s", *podconfigFile)
}
r := mux.NewRouter()
r.HandleFunc("/api/job/{name}", jobStatus).Methods("GET")
r.HandleFunc("/api/job", jobList).Methods("GET")
r.HandleFunc("/api/job", jobLaunch).Methods("POST")
r.HandleFunc("/api/job/{name}", jobDelete).Methods("DELETE")
r.HandleFunc("/api/logs/{name}", jobLogs).Methods("GET")
http.Handle("/", r)
srv := &http.Server{
Handler: r,
Addr: ":8090",
//timeouts
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}
func jobLaunch(w http.ResponseWriter, r *http.Request) {
job, err := readJobRequest(r)
if err != nil {
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
jobsClient := clientset.BatchV1().Jobs(namespace)
_, err = jobsClient.Create(context.TODO(), getk8sJob(*job), metav1.CreateOptions{})
if err != nil {
log.Printf("ERROR, failed to create to create the job '%s': %s", job.Name, err)
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
log.Println("Launched job:", job.Name)
sendResponse(w, map[string]interface{}{
"status": "started",
"name": job.Name,
})
}
func jobStatus(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
if !isValidJobName(name) {
log.Println("ERROR, failed get job status, invalid job name:", name)
writeErrorMsg(errors.New("invalid job name"), w, http.StatusBadRequest)
return
}
job, err := clientset.BatchV1().Jobs(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
log.Printf("ERROR, unable to retrieve job status %s: %s", name, err)
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
sendResponse(w, map[string]interface{}{
"status": getJobStatus(job),
"name": name,
})
}
func jobList(w http.ResponseWriter, r *http.Request) {
requirement, _ := labels.NewRequirement("jobgroup", selection.Equals, []string{"scand-job"})
selector := labels.NewSelector()
selector = selector.Add(*requirement)
jobs, err := clientset.BatchV1().Jobs(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: selector.String(),
})
if err != nil {
log.Printf("ERROR, unable to retrieve job list: %s", err)
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
var statuses = make([]map[string]string, 0)
for _, job := range jobs.Items {
statuses = append(statuses, map[string]string{
"name": job.Name,
"status": getJobStatus(&job),
})
}
sendResponse(w, map[string]interface{}{
"jobs": statuses,
})
}
func jobDelete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
if !isValidJobName(name) {
log.Println("ERROR, failed to delete a job, invalid job name:", name)
writeErrorMsg(errors.New("invalid job name"), w, http.StatusBadRequest)
return
}
background := metav1.DeletePropagationBackground
if err := clientset.BatchV1().Jobs(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{
PropagationPolicy: &background,
}); err != nil {
log.Printf("ERROR, unable to delete job %s: %s", name, err)
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
log.Println("Deleted job:", name)
sendResponse(w, map[string]interface{}{
"status": "deleted",
"name": name,
})
}
func jobLogs(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
if !isValidJobName(name) {
log.Println("ERROR, failed get job logs, invalid job name:", name)
writeErrorMsg(errors.New("invalid job name"), w, http.StatusBadRequest)
return
}
podName, err := getPod(name)
if err != nil {
log.Printf("ERROR, unable to find pod for the job %s: %s", name, err)
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
req := clientset.CoreV1().Pods(namespace).GetLogs(podName, &v1.PodLogOptions{})
podLogs, err := req.Stream(context.Background())
if err != nil {
log.Printf("ERROR, unable to retrieve the logs for the job %s: %s", name, err)
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
defer podLogs.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
log.Printf("ERROR, unable to retrieve the logs for the job %s: %s", name, err)
writeErrorMsg(err, w, http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/plain")
w.Write(buf.Bytes())
}