Skip to content

Commit

Permalink
feat(api): respond with 200/201 with empty json for successful PUT (#…
Browse files Browse the repository at this point in the history
…12642)

## Motivation

In our OpenAPI spec we say that we're returning `application/json` for
put 200/201
https://github.com/kumahq/kuma/blob/a00b2e0f87c69b59e62b580a9f3cad1a36fa5762/tools/openapi/templates/endpoints.yaml#L63
and [we don't always do
that](https://github.com/kumahq/kuma/pull/12642/files#diff-a5eb0241e652f23066dcb33b652229602f43c52b565062af3c1cf5ac320f1249L427).
I can't find a way to describe this with OpenAPI schema (either you
don't set content or you set it, there is no `oneOf` on that level). I
tried this:

```
      responses:
        '200':
          description: Updated
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/MeshCreateOrUpdateSuccessResponse'
                  - type: 'null'
```

If we don't do this change terraform does not like the response not
having content type:

```
╷
│ Error: failure to invoke API
│
│   with konnect_mesh.default,
│   on main.tf line 24, in resource "konnect_mesh" "default":
│   24: resource "konnect_mesh" "default" {
│
│ unknown content-type received: : Status 200
╵
```

## Implementation information

Respond with 200/201 and empty `{}` when there are no errors.

## Supporting documentation

xrel Kong/kong-mesh#7201

---------

Signed-off-by: slonka <[email protected]>
  • Loading branch information
slonka authored Jan 23, 2025
1 parent 6152636 commit b7131bb
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 12 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ does not have any particular instructions.

## Upgrade to `2.10.x`

### API Server

A successful PUT request without any warnings will now set "content-type: application/json" header and return empty json.

### MeshLoadBalancingStrategy

#### Deprecation of `hashPolicies.type: SourceIP` and `maglev.type: SourceIP`
Expand Down
18 changes: 6 additions & 12 deletions pkg/api-server/resource_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,9 @@ func (r *resourceEndpoints) createResource(
return
}

if warnings := model.Deprecations(res); len(warnings) > 0 {
if err := response.WriteHeaderAndJson(201, api_server_types.CreateOrUpdateSuccessResponse{Warnings: warnings}, "application/json"); err != nil {
log.Error(err, "Could not write the response")
}
} else {
response.WriteHeader(201)
resp := api_server_types.CreateOrUpdateSuccessResponse{Warnings: model.Deprecations(res)}
if err := response.WriteHeaderAndJson(http.StatusCreated, resp, "application/json"); err != nil {
log.Error(err, "Could not write the response")
}
}

Expand Down Expand Up @@ -471,12 +468,9 @@ func (r *resourceEndpoints) updateResource(
return
}

if warnings := model.Deprecations(currentRes); len(warnings) > 0 {
if err := response.WriteHeaderAndJson(200, api_server_types.CreateOrUpdateSuccessResponse{Warnings: warnings}, "application/json"); err != nil {
log.Error(err, "Could not write the response")
}
} else {
response.WriteHeader(200)
resp := api_server_types.CreateOrUpdateSuccessResponse{Warnings: model.Deprecations(currentRes)}
if err := response.WriteHeaderAndJson(http.StatusOK, resp, "application/json"); err != nil {
log.Error(err, "Could not write the response")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit b7131bb

Please sign in to comment.