-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathapiserver.go
301 lines (254 loc) · 9.03 KB
/
apiserver.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package apiserver
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/pprof"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/zalando/postgres-operator/pkg/cluster"
"github.com/zalando/postgres-operator/pkg/spec"
"github.com/zalando/postgres-operator/pkg/util"
"github.com/zalando/postgres-operator/pkg/util/config"
)
const (
httpAPITimeout = time.Minute * 1
shutdownTimeout = time.Second * 10
httpReadTimeout = time.Millisecond * 100
)
// ControllerInformer describes stats methods of a controller
type controllerInformer interface {
GetConfig() *spec.ControllerConfig
GetOperatorConfig() *config.Config
GetStatus() *spec.ControllerStatus
TeamClusterList() map[string][]spec.NamespacedName
ClusterStatus(namespace, cluster string) (*cluster.ClusterStatus, error)
ClusterLogs(namespace, cluster string) ([]*spec.LogEntry, error)
ClusterHistory(namespace, cluster string) ([]*spec.Diff, error)
ClusterDatabasesMap() map[string][]string
WorkerLogs(workerID uint32) ([]*spec.LogEntry, error)
ListQueue(workerID uint32) (*spec.QueueDump, error)
GetWorkersCnt() uint32
WorkerStatus(workerID uint32) (*cluster.WorkerStatus, error)
}
// Server describes HTTP API server
type Server struct {
logger *logrus.Entry
http http.Server
controller controllerInformer
}
const (
teamRe = `(?P<team>[a-zA-Z][a-zA-Z0-9\-_]*)`
namespaceRe = `(?P<namespace>[a-z0-9]([-a-z0-9\-_]*[a-z0-9])?)`
clusterRe = `(?P<cluster>[a-zA-Z][a-zA-Z0-9\-_]*)`
)
var (
clusterStatusRe = fmt.Sprintf(`^/clusters/%s/%s/?$`, namespaceRe, clusterRe)
clusterLogsRe = fmt.Sprintf(`^/clusters/%s/%s/logs/?$`, namespaceRe, clusterRe)
clusterHistoryRe = fmt.Sprintf(`^/clusters/%s/%s/history/?$`, namespaceRe, clusterRe)
teamURLRe = fmt.Sprintf(`^/clusters/%s/?$`, teamRe)
clusterStatusURL = regexp.MustCompile(clusterStatusRe)
clusterLogsURL = regexp.MustCompile(clusterLogsRe)
clusterHistoryURL = regexp.MustCompile(clusterHistoryRe)
teamURL = regexp.MustCompile(teamURLRe)
workerLogsURL = regexp.MustCompile(`^/workers/(?P<id>\d+)/logs/?$`)
workerEventsQueueURL = regexp.MustCompile(`^/workers/(?P<id>\d+)/queue/?$`)
workerStatusURL = regexp.MustCompile(`^/workers/(?P<id>\d+)/status/?$`)
workerAllQueue = regexp.MustCompile(`^/workers/all/queue/?$`)
workerAllStatus = regexp.MustCompile(`^/workers/all/status/?$`)
clustersURL = "/clusters/"
)
// New creates new HTTP API server
func New(controller controllerInformer, address string, port int, logger *logrus.Logger) *Server {
s := &Server{
logger: logger.WithField("pkg", "apiserver"),
controller: controller,
}
mux := http.NewServeMux()
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
mux.Handle("/status/", http.HandlerFunc(s.controllerStatus))
mux.Handle("/readyz/", http.HandlerFunc(s.controllerReady))
mux.Handle("/config/", http.HandlerFunc(s.operatorConfig))
mux.HandleFunc("/clusters/", s.clusters)
mux.HandleFunc("/workers/", s.workers)
mux.HandleFunc("/databases/", s.databases)
s.http = http.Server{
Addr: net.JoinHostPort(strings.Trim(address, "[]"), strconv.Itoa(port)),
Handler: http.TimeoutHandler(mux, httpAPITimeout, ""),
ReadTimeout: httpReadTimeout,
}
return s
}
// Run starts the HTTP server
func (s *Server) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
var err error
defer wg.Done()
go func() {
if err2 := s.http.ListenAndServe(); err2 != http.ErrServerClosed {
s.logger.Fatalf("Could not start http server: %v", err2)
}
}()
s.logger.Infof("listening on %s", s.http.Addr)
<-stopCh
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err = s.http.Shutdown(ctx); err == nil {
s.logger.Infoln("Http server shut down")
return
}
if err == context.DeadlineExceeded {
s.logger.Warningf("Shutdown timeout exceeded. closing http server")
if err = s.http.Close(); err != nil {
s.logger.Errorf("could not close http connection: %v", err)
}
return
}
s.logger.Errorf("Could not shutdown http server: %v", err)
}
func (s *Server) respond(obj interface{}, err error, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
if err2 := json.NewEncoder(w).Encode(map[string]interface{}{"error": err.Error()}); err2 != nil {
s.logger.Errorf("could not encode error response %q: %v", err, err2)
}
return
}
err = json.NewEncoder(w).Encode(obj)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
s.logger.Errorf("Could not encode: %v", err)
}
}
func (s *Server) controllerStatus(w http.ResponseWriter, req *http.Request) {
s.respond(s.controller.GetStatus(), nil, w)
}
func (s *Server) controllerReady(w http.ResponseWriter, req *http.Request) {
s.respond("OK", nil, w)
}
func (s *Server) operatorConfig(w http.ResponseWriter, req *http.Request) {
s.respond(map[string]interface{}{
"controller": s.controller.GetConfig(),
"operator": s.controller.GetOperatorConfig(),
}, nil, w)
}
func (s *Server) clusters(w http.ResponseWriter, req *http.Request) {
var (
resp interface{}
err error
)
if matches := util.FindNamedStringSubmatch(clusterStatusURL, req.URL.Path); matches != nil {
namespace := matches["namespace"]
resp, err = s.controller.ClusterStatus(namespace, matches["cluster"])
} else if matches := util.FindNamedStringSubmatch(teamURL, req.URL.Path); matches != nil {
teamClusters := s.controller.TeamClusterList()
clusters, found := teamClusters[matches["team"]]
if !found {
s.respond(nil, fmt.Errorf("could not find clusters for the team"), w)
return
}
clusterNames := make([]string, 0)
for _, cluster := range clusters {
clusterNames = append(clusterNames, cluster.Name)
}
resp, err = clusterNames, nil
} else if matches := util.FindNamedStringSubmatch(clusterLogsURL, req.URL.Path); matches != nil {
namespace := matches["namespace"]
resp, err = s.controller.ClusterLogs(namespace, matches["cluster"])
} else if matches := util.FindNamedStringSubmatch(clusterHistoryURL, req.URL.Path); matches != nil {
namespace := matches["namespace"]
resp, err = s.controller.ClusterHistory(namespace, matches["cluster"])
} else if req.URL.Path == clustersURL {
clusterNamesPerTeam := make(map[string][]string)
for team, clusters := range s.controller.TeamClusterList() {
for _, cluster := range clusters {
clusterNamesPerTeam[team] = append(clusterNamesPerTeam[team], cluster.Name)
}
}
resp, err = clusterNamesPerTeam, nil
} else {
resp, err = nil, fmt.Errorf("page not found")
}
s.respond(resp, err, w)
}
func mustConvertToUint32(s string) uint32 {
result, err := strconv.Atoi(s)
if err != nil {
panic(fmt.Errorf("mustConvertToUint32 called for %s: %v", s, err))
}
return uint32(result)
}
func (s *Server) workers(w http.ResponseWriter, req *http.Request) {
var (
resp interface{}
err error
)
if workerAllQueue.MatchString(req.URL.Path) {
s.allQueues(w, req)
return
}
if workerAllStatus.MatchString(req.URL.Path) {
s.allWorkers(w, req)
return
}
err = fmt.Errorf("page not found")
if matches := util.FindNamedStringSubmatch(workerLogsURL, req.URL.Path); matches != nil {
workerID := mustConvertToUint32(matches["id"])
resp, err = s.controller.WorkerLogs(workerID)
} else if matches := util.FindNamedStringSubmatch(workerEventsQueueURL, req.URL.Path); matches != nil {
workerID := mustConvertToUint32(matches["id"])
resp, err = s.controller.ListQueue(workerID)
} else if matches := util.FindNamedStringSubmatch(workerStatusURL, req.URL.Path); matches != nil {
var workerStatus *cluster.WorkerStatus
workerID := mustConvertToUint32(matches["id"])
resp = "idle"
if workerStatus, err = s.controller.WorkerStatus(workerID); workerStatus != nil {
resp = workerStatus
}
}
s.respond(resp, err, w)
}
func (s *Server) databases(w http.ResponseWriter, req *http.Request) {
databaseNamesPerCluster := s.controller.ClusterDatabasesMap()
s.respond(databaseNamesPerCluster, nil, w)
}
func (s *Server) allQueues(w http.ResponseWriter, r *http.Request) {
workersCnt := s.controller.GetWorkersCnt()
resp := make(map[uint32]*spec.QueueDump, workersCnt)
for i := uint32(0); i < workersCnt; i++ {
queueDump, err := s.controller.ListQueue(i)
if err != nil {
s.respond(nil, err, w)
return
}
resp[i] = queueDump
}
s.respond(resp, nil, w)
}
func (s *Server) allWorkers(w http.ResponseWriter, r *http.Request) {
workersCnt := s.controller.GetWorkersCnt()
resp := make(map[uint32]interface{}, workersCnt)
for i := uint32(0); i < workersCnt; i++ {
status, err := s.controller.WorkerStatus(i)
if err != nil {
s.respond(nil, err, w)
continue
}
if status == nil {
resp[i] = "idle"
} else {
resp[i] = status
}
}
s.respond(resp, nil, w)
}