/
process_handler.go
222 lines (182 loc) · 7.73 KB
/
process_handler.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
package apis
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/gorilla/schema"
"code.cloudfoundry.org/cf-k8s-controllers/api/apierrors"
"code.cloudfoundry.org/cf-k8s-controllers/api/authorization"
"code.cloudfoundry.org/cf-k8s-controllers/api/payloads"
"code.cloudfoundry.org/cf-k8s-controllers/api/presenter"
"code.cloudfoundry.org/cf-k8s-controllers/api/repositories"
"github.com/go-logr/logr"
"github.com/gorilla/mux"
)
const (
ProcessPath = "/v3/processes/{guid}"
ProcessSidecarsPath = "/v3/processes/{guid}/sidecars"
ProcessScalePath = "/v3/processes/{guid}/actions/scale"
ProcessStatsPath = "/v3/processes/{guid}/stats"
ProcessesPath = "/v3/processes"
)
//counterfeiter:generate -o fake -fake-name CFProcessRepository . CFProcessRepository
type CFProcessRepository interface {
GetProcess(context.Context, authorization.Info, string) (repositories.ProcessRecord, error)
ListProcesses(context.Context, authorization.Info, repositories.ListProcessesMessage) ([]repositories.ProcessRecord, error)
GetProcessByAppTypeAndSpace(context.Context, authorization.Info, string, string, string) (repositories.ProcessRecord, error)
PatchProcess(context.Context, authorization.Info, repositories.PatchProcessMessage) (repositories.ProcessRecord, error)
}
//counterfeiter:generate -o fake -fake-name ScaleProcess . ScaleProcess
type ScaleProcess func(ctx context.Context, authInfo authorization.Info, processGUID string, scale repositories.ProcessScaleValues) (repositories.ProcessRecord, error)
//counterfeiter:generate -o fake -fake-name FetchProcessStats . FetchProcessStats
type FetchProcessStats func(context.Context, authorization.Info, string) ([]repositories.PodStatsRecord, error)
type ProcessHandler struct {
logger logr.Logger
serverURL url.URL
processRepo CFProcessRepository
fetchProcessStats FetchProcessStats
scaleProcess ScaleProcess
decoderValidator *DecoderValidator
}
func NewProcessHandler(
logger logr.Logger,
serverURL url.URL,
processRepo CFProcessRepository,
fetchProcessStats FetchProcessStats,
scaleProcessFunc ScaleProcess,
decoderValidator *DecoderValidator,
) *ProcessHandler {
return &ProcessHandler{
logger: logger,
serverURL: serverURL,
processRepo: processRepo,
fetchProcessStats: fetchProcessStats,
scaleProcess: scaleProcessFunc,
decoderValidator: decoderValidator,
}
}
func (h *ProcessHandler) processGetHandler(authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) {
ctx := r.Context()
vars := mux.Vars(r)
processGUID := vars["guid"]
process, err := h.processRepo.GetProcess(ctx, authInfo, processGUID)
if err != nil {
h.logger.Error(err, "Failed to fetch process from Kubernetes", "ProcessGUID", processGUID)
return nil, apierrors.ForbiddenAsNotFound(err)
}
return NewHandlerResponse(http.StatusOK).WithBody(presenter.ForProcess(process, h.serverURL)), nil
}
func (h *ProcessHandler) processGetSidecarsHandler(authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) {
ctx := r.Context()
vars := mux.Vars(r)
processGUID := vars["guid"]
_, err := h.processRepo.GetProcess(ctx, authInfo, processGUID)
if err != nil {
h.logger.Error(err, "Failed to fetch process from Kubernetes", "ProcessGUID", processGUID)
return nil, apierrors.ForbiddenAsNotFound(err)
}
return NewHandlerResponse(http.StatusOK).WithBody(map[string]interface{}{
"pagination": map[string]interface{}{
"total_results": 0,
"total_pages": 1,
"first": map[string]interface{}{
"href": fmt.Sprintf("%s/v3/processes/%s/sidecars", h.serverURL.String(), processGUID),
},
"last": map[string]interface{}{
"href": fmt.Sprintf("%s/v3/processes/%s/sidecars", h.serverURL.String(), processGUID),
},
"next": nil,
"previous": nil,
},
"resources": []string{},
}), nil
}
func (h *ProcessHandler) processScaleHandler(authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) {
ctx := r.Context()
vars := mux.Vars(r)
processGUID := vars["guid"]
var payload payloads.ProcessScale
if err := h.decoderValidator.DecodeAndValidateJSONPayload(r, &payload); err != nil {
return nil, err
}
processRecord, err := h.scaleProcess(ctx, authInfo, processGUID, payload.ToRecord())
if err != nil {
h.logger.Error(err, "Failed due to error from Kubernetes", "processGUID", processGUID)
return nil, err
}
return NewHandlerResponse(http.StatusOK).WithBody(presenter.ForProcess(processRecord, h.serverURL)), nil
}
func (h *ProcessHandler) processGetStatsHandler(authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) {
ctx := r.Context()
vars := mux.Vars(r)
processGUID := vars["guid"]
records, err := h.fetchProcessStats(ctx, authInfo, processGUID)
if err != nil {
h.logger.Error(err, "Failed to get process stats from Kubernetes", "ProcessGUID", processGUID)
return nil, apierrors.ForbiddenAsNotFound(err)
}
return NewHandlerResponse(http.StatusOK).WithBody(presenter.ForProcessStats(records)), nil
}
func (h *ProcessHandler) processListHandler(authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) { //nolint:dupl
ctx := r.Context()
if err := r.ParseForm(); err != nil {
h.logger.Error(err, "Unable to parse request query parameters")
return nil, err
}
processListFilter := new(payloads.ProcessList)
err := schema.NewDecoder().Decode(processListFilter, r.Form)
if err != nil {
switch err.(type) {
case schema.MultiError:
multiError := err.(schema.MultiError)
for _, v := range multiError {
_, ok := v.(schema.UnknownKeyError)
if ok {
h.logger.Info("Unknown key used in Process filter")
return nil, apierrors.NewUnknownKeyError(err, processListFilter.SupportedFilterKeys())
}
}
h.logger.Error(err, "Unable to decode request query parameters")
return nil, err
default:
h.logger.Error(err, "Unable to decode request query parameters")
return nil, err
}
}
processList, err := h.processRepo.ListProcesses(ctx, authInfo, processListFilter.ToMessage())
if err != nil {
h.logger.Error(err, "Failed to fetch processes(s) from Kubernetes")
return nil, err
}
return NewHandlerResponse(http.StatusOK).WithBody(presenter.ForProcessList(processList, h.serverURL, *r.URL)), nil
}
func (h *ProcessHandler) processPatchHandler(authInfo authorization.Info, r *http.Request) (*HandlerResponse, error) {
ctx := r.Context()
vars := mux.Vars(r)
processGUID := vars["guid"]
var payload payloads.ProcessPatch
if err := h.decoderValidator.DecodeAndValidateJSONPayload(r, &payload); err != nil {
return nil, err
}
process, err := h.processRepo.GetProcess(ctx, authInfo, processGUID)
if err != nil {
h.logger.Error(err, "Failed to get process from Kubernetes", "ProcessGUID", processGUID)
return nil, apierrors.ForbiddenAsNotFound(err)
}
updatedProcess, err := h.processRepo.PatchProcess(ctx, authInfo, payload.ToProcessPatchMessage(processGUID, process.SpaceGUID))
if err != nil {
h.logger.Error(err, "Failed to patch process from Kubernetes", "ProcessGUID", processGUID)
return nil, err
}
return NewHandlerResponse(http.StatusOK).WithBody(presenter.ForProcess(updatedProcess, h.serverURL)), nil
}
func (h *ProcessHandler) RegisterRoutes(router *mux.Router) {
w := NewAuthAwareHandlerFuncWrapper(h.logger)
router.Path(ProcessPath).Methods("GET").HandlerFunc(w.Wrap(h.processGetHandler))
router.Path(ProcessSidecarsPath).Methods("GET").HandlerFunc(w.Wrap(h.processGetSidecarsHandler))
router.Path(ProcessScalePath).Methods("POST").HandlerFunc(w.Wrap(h.processScaleHandler))
router.Path(ProcessStatsPath).Methods("GET").HandlerFunc(w.Wrap(h.processGetStatsHandler))
router.Path(ProcessesPath).Methods("GET").HandlerFunc(w.Wrap(h.processListHandler))
router.Path(ProcessPath).Methods("PATCH").HandlerFunc(w.Wrap(h.processPatchHandler))
}