Skip to content

Commit aebf26c

Browse files
committed
refactor: apply general code cleanups and consistency improvements
Perform general refactoring to improve consistency, readability, and maintainability across the codebase.
1 parent a8ba485 commit aebf26c

File tree

8 files changed

+56
-90
lines changed

8 files changed

+56
-90
lines changed

pkg/manager/reference.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func GenerateResourceReference(client client.Client, resource client.Object) (st
5555

5656
gvr := mapping.Resource
5757

58-
// TODO: This is a legacy thing where the OG Kubernetes service is not corrcetly
58+
// TODO: This is a legacy thing where the OG Kubernetes service is not correctly
5959
// namespaced, much like Amazon regions :D We really need to work out a migration
6060
// strategy.
6161
if gvr.Group == "unikorn-cloud.org" {
@@ -68,7 +68,7 @@ func GenerateResourceReference(client client.Client, resource client.Object) (st
6868
// GetResourceReferences returns all resource references attached to a resource.
6969
// This is used primarily to poll a resource to see if it's in use, and thus its
7070
// deletion will have consequences. It may also be used to inhibit deletion in
71-
// certain cercumstances.
71+
// certain circumstances.
7272
func GetResourceReferences(object client.Object) []string {
7373
ignored := []string{
7474
// Our finalizer to inhibit deletion until we are finished.

pkg/messaging/consumer/cascadingdelete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (c *CascadingDelete) Consume(ctx context.Context, envelope *messaging.Envel
9898
}
9999

100100
if c.resourceLabel != "" {
101-
opts.LabelSelector = labels.SelectorFromSet(map[string]string{
101+
opts.LabelSelector = labels.SelectorFromSet(labels.Set{
102102
c.resourceLabel: envelope.ResourceID,
103103
})
104104
}

pkg/openapi/common.spec.yaml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,11 @@ components:
2020
description: Represents a structured error response returned by the API. It consolidates both OAuth 2.0-related and general API errors into a unified format.
2121
type: object
2222
required:
23-
- type
2423
- status
2524
- trace_id
2625
- error
2726
- error_description
2827
properties:
29-
type:
30-
description: Specifies whether the error originated from an OAuth 2.0 context or from the general API.
31-
type: string
32-
enum:
33-
- oauth2_error
34-
- api_error
3528
status:
3629
description: The HTTP status code returned with the response.
3730
type: integer
@@ -212,7 +205,6 @@ components:
212205
schema:
213206
$ref: '#/components/schemas/error'
214207
example:
215-
type: api_error
216208
status: 400
217209
trace_id: 77ce9d4485f746b6
218210
error: invalid_request
@@ -225,7 +217,6 @@ components:
225217
schema:
226218
$ref: '#/components/schemas/error'
227219
example:
228-
type: api_error
229220
status: 401
230221
trace_id: 77ce9d4485f746b6
231222
error: token_expired
@@ -240,7 +231,6 @@ components:
240231
schema:
241232
$ref: '#/components/schemas/error'
242233
example:
243-
type: api_error
244234
status: 403
245235
trace_id: 77ce9d4485f746b6
246236
error: access_denied
@@ -253,7 +243,6 @@ components:
253243
schema:
254244
$ref: '#/components/schemas/error'
255245
example:
256-
type: api_error
257246
status: 404
258247
trace_id: 77ce9d4485f746b6
259248
error: resource_missing
@@ -266,7 +255,6 @@ components:
266255
schema:
267256
$ref: '#/components/schemas/error'
268257
example:
269-
type: api_error
270258
status: 409
271259
trace_id: 77ce9d4485f746b6
272260
error: conflict
@@ -280,7 +268,6 @@ components:
280268
schema:
281269
$ref: '#/components/schemas/error'
282270
example:
283-
type: api_error
284271
status: 500
285272
trace_id: 77ce9d4485f746b6
286273
error: internal

pkg/openapi/schema.go

Lines changed: 40 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/openapi/types.go

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/server/conversion/conversion.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func OrganizationScopedResourceReadMetadata(in metav1.Object, tags unikornv1.Tag
166166
return out
167167
}
168168

169-
// ProjectScopedResourceReadMetadata extracts project scoped metdata from a resource for
169+
// ProjectScopedResourceReadMetadata extracts project scoped metadata from a resource for
170170
// GET APIs.
171171
//
172172
//nolint:errchkjson
@@ -304,12 +304,10 @@ func LogUpdate(ctx context.Context, current, required metav1.Object) error {
304304
}
305305

306306
func ConvertTag(in unikornv1.Tag) openapi.Tag {
307-
out := openapi.Tag{
307+
return openapi.Tag{
308308
Name: in.Name,
309309
Value: in.Value,
310310
}
311-
312-
return out
313311
}
314312

315313
func ConvertTags(in unikornv1.TagList) openapi.TagList {
@@ -327,12 +325,10 @@ func ConvertTags(in unikornv1.TagList) openapi.TagList {
327325
}
328326

329327
func GenerateTag(in openapi.Tag) unikornv1.Tag {
330-
out := unikornv1.Tag{
328+
return unikornv1.Tag{
331329
Name: in.Name,
332330
Value: in.Value,
333331
}
334-
335-
return out
336332
}
337333

338334
func GenerateTagList(in *openapi.TagList) unikornv1.TagList {

pkg/server/v2/errors/errors.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@ import (
2525

2626
const (
2727
HeaderWWWAuthenticate = "WWW-Authenticate"
28+
HeaderErrorVersion = "X-Error-Version"
2829
HeaderOAuth2Error = "X-Oauth2-Error"
2930
HeaderAPIError = "X-Api-Error"
3031
)
3132

32-
type ErrorType string
33-
34-
const (
35-
ErrorTypeOAuth2Error ErrorType = "oauth2_error"
36-
ErrorTypeAPIError ErrorType = "api_error"
37-
)
38-
3933
type OAuth2ErrorCode string
4034

4135
const (
@@ -93,11 +87,10 @@ type Error struct {
9387
OAuth2ErrorCode OAuth2ErrorCode `json:"-"`
9488
APIErrorCode APIErrorCode `json:"-"`
9589

96-
Type ErrorType `json:"type"`
97-
Status int `json:"status"`
98-
TraceID string `json:"trace_id"`
99-
ErrorCode string `json:"error"`
100-
ErrorDescription string `json:"error_description"`
90+
Status int `json:"status"`
91+
TraceID string `json:"trace_id"`
92+
ErrorCode string `json:"error"`
93+
ErrorDescription string `json:"error_description"`
10194
}
10295

10396
func (e *Error) Error() string {

pkg/server/v2/httputil/httputil.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func WriteErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
6666
}
6767

6868
func writeErrorCodeHeaders(w http.ResponseWriter, e *errorsv2.Error) {
69+
w.Header().Set(errorsv2.HeaderErrorVersion, "2")
6970
w.Header().Set(errorsv2.HeaderOAuth2Error, string(e.OAuth2ErrorCode))
7071
w.Header().Set(errorsv2.HeaderAPIError, string(e.APIErrorCode))
7172
}
@@ -86,7 +87,6 @@ func WriteOAuth2ErrorResponse(w http.ResponseWriter, r *http.Request, err error)
8687
response = errorsv2.NewInternalError().WithCause(err).Prefixed()
8788
}
8889

89-
response.Type = errorsv2.ErrorTypeOAuth2Error
9090
response.TraceID = trace.SpanContextFromContext(ctx).TraceID().String()
9191
response.ErrorCode = string(response.OAuth2ErrorCode)
9292

@@ -101,6 +101,10 @@ func writeWWWAuthenticateHeader(w http.ResponseWriter, e *errorsv2.Error) {
101101
return
102102
}
103103

104+
if e.OAuth2ErrorCode == "" {
105+
return
106+
}
107+
104108
var builder strings.Builder
105109

106110
builder.WriteString("Bearer error=")
@@ -130,7 +134,6 @@ func WriteAPIErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
130134
response = errorsv2.NewInternalError().WithCause(err).Prefixed()
131135
}
132136

133-
response.Type = errorsv2.ErrorTypeAPIError
134137
response.TraceID = trace.SpanContextFromContext(ctx).TraceID().String()
135138
response.ErrorCode = string(response.APIErrorCode)
136139

0 commit comments

Comments
 (0)