-
Notifications
You must be signed in to change notification settings - Fork 85
/
remote_controller.go
228 lines (186 loc) · 6.95 KB
/
remote_controller.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
221
222
223
224
225
226
227
228
package worker
import (
"crypto/subtle"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/pborman/uuid"
"github.com/sirupsen/logrus"
"github.com/travis-ci/worker/context"
)
// RemoteController provides an HTTP API for controlling worker.
type RemoteController struct {
pool *ProcessorPool
auth string
workerInfo func() workerInfo
cancel func()
}
// Setup installs the HTTP routes that will handle requests to the HTTP API.
func (api *RemoteController) Setup() {
r := mux.NewRouter()
r.HandleFunc("/healthz", api.HealthCheck).Methods("GET")
r.HandleFunc("/ready", api.ReadyCheck).Methods("GET")
r.HandleFunc("/worker", api.GetWorkerInfo).Methods("GET")
r.HandleFunc("/worker", api.UpdateWorkerInfo).Methods("PATCH")
r.HandleFunc("/worker", api.ShutdownWorker).Methods("DELETE")
// It is preferable to use UpdateWorkerInfo to update the pool size,
// as it does not depend on the current state of worker.
r.HandleFunc("/pool/increment", api.IncrementPool).Methods("POST")
r.HandleFunc("/pool/decrement", api.DecrementPool).Methods("POST")
r.Use(api.SetContext)
r.Use(api.CheckAuth)
http.Handle("/", r)
}
// SetContext is a middleware function that loads some values into the request
// context. This allows these values to be shown in logging.
func (api *RemoteController) SetContext(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
ctx = context.FromComponent(ctx, "remote_controller")
ctx = context.FromUUID(ctx, uuid.NewRandom().String())
next.ServeHTTP(w, req.WithContext(ctx))
})
}
// CheckAuth is a middleware for all HTTP API methods that ensures that the
// configured basic auth credentials were passed in the request.
func (api *RemoteController) CheckAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
log := context.LoggerFromContext(req.Context())
// skip auth for the health and ready check endpoints
if strings.HasPrefix(req.URL.Path, "/healthz") || strings.HasPrefix(req.URL.Path, "/ready") {
next.ServeHTTP(w, req)
return
}
username, password, ok := req.BasicAuth()
if !ok {
log.Warn("no authentication credentials provided")
w.Header().Set("WWW-Authenticate", "Basic realm=\"travis-ci/worker\"")
w.WriteHeader(http.StatusUnauthorized)
return
}
authBytes := []byte(fmt.Sprintf("%s:%s", username, password))
if subtle.ConstantTimeCompare(authBytes, []byte(api.auth)) != 1 {
log.Warn("incorrect credentials provided")
w.WriteHeader(http.StatusForbidden)
return
}
// pass it on!
next.ServeHTTP(w, req)
})
}
// HealthCheck indicates whether worker is currently functioning in a healthy
// way. This can be used by a system like Kubernetes to determine whether to
// replace an instance of worker with a new one.
func (api *RemoteController) HealthCheck(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "OK")
}
// ReadyCheck indicates whether the worker is ready to receive requests.
// This is intended to be used as a readiness check in a system like Kubernetes.
// We should not attempt to interact with the remote controller until this returns
// a successful status.
func (api *RemoteController) ReadyCheck(w http.ResponseWriter, req *http.Request) {
if api.pool.Ready() {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "OK")
} else {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprint(w, "Not Ready")
}
}
// GetWorkerInfo writes a JSON payload with useful information about the current
// state of worker as a whole.
func (api *RemoteController) GetWorkerInfo(w http.ResponseWriter, req *http.Request) {
log := context.LoggerFromContext(req.Context()).WithField("method", "GetWorkerInfo")
info := api.workerInfo()
log.Info("got worker info")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(info)
}
// UpdateWorkerInfo allows reconfiguring some parts of worker on the fly.
//
// The main use of this is adjusting the size of the processor pool without
// interrupting existing running jobs.
func (api *RemoteController) UpdateWorkerInfo(w http.ResponseWriter, req *http.Request) {
log := context.LoggerFromContext(req.Context()).WithField("method", "UpdateWorkerInfo")
var info workerInfo
if err := json.NewDecoder(req.Body).Decode(&info); err != nil {
log.WithError(err).Error("could not decode json request body")
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(errorResponse{
Message: err.Error(),
})
return
}
if info.PoolSize > 0 {
api.pool.SetSize(info.PoolSize)
log.WithField("pool_size", info.PoolSize).Info("updated pool size")
}
w.WriteHeader(http.StatusNoContent)
}
// ShutdownWorker tells the worker to shutdown.
//
// Options can be passed in the body that determine whether the shutdown is
// done gracefully or not.
func (api *RemoteController) ShutdownWorker(w http.ResponseWriter, req *http.Request) {
log := context.LoggerFromContext(req.Context()).WithField("method", "ShutdownWorker")
var options shutdownOptions
if err := json.NewDecoder(req.Body).Decode(&options); err != nil {
log.WithError(err).Error("could not decode json request body")
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(errorResponse{
Message: err.Error(),
})
return
}
if options.Graceful {
api.pool.GracefulShutdown(options.Pause)
} else {
api.cancel()
}
log.WithFields(logrus.Fields{
"graceful": options.Graceful,
"pause": options.Pause,
}).Info("asked worker to shutdown")
w.WriteHeader(http.StatusNoContent)
}
// IncrementPool tells the worker to spin up another processor.
func (api *RemoteController) IncrementPool(w http.ResponseWriter, req *http.Request) {
log := context.LoggerFromContext(req.Context()).WithField("method", "IncrementPool")
api.pool.Incr()
log.Info("incremented pool")
w.WriteHeader(http.StatusNoContent)
}
// DecrementPool tells the worker to gracefully shutdown a processor.
func (api *RemoteController) DecrementPool(w http.ResponseWriter, req *http.Request) {
log := context.LoggerFromContext(req.Context()).WithField("method", "DecrementPool")
api.pool.Decr()
log.Info("decremented pool")
w.WriteHeader(http.StatusNoContent)
}
type workerInfo struct {
Version string `json:"version"`
Revision string `json:"revision"`
Generated string `json:"generated"`
Uptime string `json:"uptime"`
PoolSize int `json:"poolSize"`
ExpectedPoolSize int `json:"expectedPoolSize"`
TotalProcessed int `json:"totalProcessed"`
Processors []processorInfo `json:"processors"`
}
type processorInfo struct {
ID string `json:"id"`
Processed int `json:"processed"`
Status string `json:"status"`
LastJobID uint64 `json:"lastJobId"`
}
type shutdownOptions struct {
Graceful bool `json:"graceful"`
Pause bool `json:"pause"`
}
type errorResponse struct {
Message string `json:"error"`
}