-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.go
More file actions
288 lines (263 loc) · 8.38 KB
/
status.go
File metadata and controls
288 lines (263 loc) · 8.38 KB
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
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package http
import (
"fmt"
"net/http"
"reflect"
"strconv"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
)
// statusError is an object that can be converted into an metav1.Status
type statusError interface {
Status() metav1.Status
}
// ErrorToAPIStatus converts an error to an metav1.Status object.
func ErrorToAPIStatus(err error) *metav1.Status {
if isNil(err) {
return &metav1.Status{
TypeMeta: metav1.TypeMeta{
Kind: "Status",
APIVersion: "v1",
},
Status: metav1.StatusSuccess,
Code: http.StatusOK,
}
}
switch t := err.(type) {
case statusError:
status := t.Status()
if len(status.Status) == 0 {
status.Status = metav1.StatusFailure
}
switch status.Status {
case metav1.StatusSuccess:
if status.Code == 0 {
status.Code = http.StatusOK
}
case metav1.StatusFailure:
if status.Code == 0 {
status.Code = http.StatusInternalServerError
}
default:
runtime.HandleError(fmt.Errorf("apiserver received an error with wrong status field : %#+v", err))
if status.Code == 0 {
status.Code = http.StatusInternalServerError
}
}
status.Kind = "Status"
status.APIVersion = "v1"
// TODO: check for invalid responses
return &status
default:
status := http.StatusInternalServerError
// Log errors that were not converted to an error status
// by REST storage - these typically indicate programmer
// error by not using pkg/api/errors, or unexpected failure
// cases.
runtime.HandleError(fmt.Errorf("apiserver received an error that is not an metav1.Status: %#+v: %v", err, err))
return &metav1.Status{
TypeMeta: metav1.TypeMeta{
Kind: "Status",
APIVersion: "v1",
},
Status: metav1.StatusFailure,
Code: int32(status),
Reason: metav1.StatusReasonUnknown,
Message: err.Error(),
}
}
}
func isNil(i any) bool {
if i == nil {
return true
}
// WARNING: https://stackoverflow.com/a/46275411/244009
/*for error wrapper interfaces*/
v := reflect.ValueOf(i)
// Check if the kind is something that can be nil
switch v.Kind() {
case reflect.Ptr, reflect.Slice, reflect.Map, reflect.Chan, reflect.Interface:
return v.IsNil()
default:
return false
}
}
// ErrorNegotiated renders an error to the response. Returns the HTTP status code of the error.
// The context is optional and may be nil.
func (w *response) APIError(err error) int {
status := ErrorToAPIStatus(err)
code := int(status.Code)
// when writing an error, check to see if the status indicates a retry after period
if status.Details != nil && status.Details.RetryAfterSeconds > 0 {
delay := strconv.Itoa(int(status.Details.RetryAfterSeconds))
w.Header().Set("Retry-After", delay)
}
if code == http.StatusNoContent {
w.WriteHeader(code)
return code
}
if w.log.GetSink() != nil {
w.log.Error(err, string(status.Reason))
}
w.JSON(code, status)
return code
}
func toStatusReason(statusReason metav1.StatusReason, message ...string) metav1.StatusReason {
if len(message) > 0 {
statusReason = metav1.StatusReason(message[0])
}
return statusReason
}
// NewInternalError returns an error indicating the item is invalid and cannot be processed.
func NewInternalError(err error, message ...string) *kerr.StatusError {
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusInternalServerError,
Reason: toStatusReason(metav1.StatusReasonInternalError, message...),
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{{Message: err.Error()}},
},
Message: fmt.Sprintf("Internal error occurred: %v", err),
},
}
}
// NewNotFound returns a new error which indicates that the requested resource was not found.
func NewNotFound(err error, message ...string) *kerr.StatusError {
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusNotFound,
Reason: toStatusReason(metav1.StatusReasonNotFound, message...),
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{{Message: err.Error()}},
},
Message: fmt.Sprintf("Resource not found: %v", err),
},
}
}
// NewAlreadyExists returns an error indicating the item requested already exists.
func NewAlreadyExists(err error, message ...string) *kerr.StatusError {
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusConflict,
Reason: toStatusReason(metav1.StatusReasonAlreadyExists, message...),
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{{Message: err.Error()}},
},
Message: fmt.Sprintf("Resource already exists: %v", err),
},
}
}
// NewUnauthorized returns an error indicating the client is not authorized to perform the requested
// action.
func NewUnauthorized(reason string) *kerr.StatusError {
message := reason
if len(message) == 0 {
message = "not authorized"
}
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusUnauthorized,
Reason: metav1.StatusReasonUnauthorized,
Message: message,
},
}
}
// NewForbidden returns an error indicating the requested action was forbidden
func NewForbidden(reason string) *kerr.StatusError {
message := reason
if len(message) == 0 {
message = "not allowed"
}
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusForbidden,
Reason: metav1.StatusReasonForbidden,
Message: message,
},
}
}
// NewConflict returns an error indicating the item can't be updated as provided.
func NewConflict(err error, message ...string) *kerr.StatusError {
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusConflict,
Reason: toStatusReason(metav1.StatusReasonConflict, message...),
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{{Message: err.Error()}},
},
Message: fmt.Sprintf("Operation cannot be fulfilled: %v", err),
},
}
}
// NewResourceExpired creates an error that indicates that the requested resource content has expired
func NewResourceExpired(message string) *kerr.StatusError {
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusGone,
Reason: metav1.StatusReasonExpired,
Message: message,
},
}
}
// NewBadRequest creates an error that indicates that the request is invalid and can not be processed.
func NewBadRequest(err error, message ...string) *kerr.StatusError {
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusBadRequest,
Reason: toStatusReason(metav1.StatusReasonBadRequest, message...),
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{{Message: err.Error()}},
},
Message: fmt.Sprintf("Operation cannot be fulfilled: %v", err),
},
}
}
// NewMethodNotSupported returns an error indicating the requested action is not supported.
func NewMethodNotSupported(err error, message ...string) *kerr.StatusError {
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusMethodNotAllowed,
Reason: toStatusReason(metav1.StatusReasonMethodNotAllowed, message...),
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{{Message: err.Error()}},
},
Message: fmt.Sprintf("Method not supported: %v", err),
},
}
}
// NewStatusError returns a generic status type error
func NewStatusError(status int, err error, message ...string) *kerr.StatusError {
return &kerr.StatusError{
ErrStatus: metav1.Status{
Status: metav1.StatusFailure,
Code: int32(status),
Reason: toStatusReason(metav1.StatusReason(http.StatusText(status)), message...),
Details: &metav1.StatusDetails{
Causes: []metav1.StatusCause{{Message: err.Error()}},
},
Message: err.Error(),
},
}
}