From 9ced463f8489c02c0c63c36141a4a7a68269f795 Mon Sep 17 00:00:00 2001 From: slonka Date: Wed, 22 Jan 2025 16:57:13 +0100 Subject: [PATCH 1/3] feat(api): set content type on response without warnings Signed-off-by: slonka --- UPGRADE.md | 4 ++++ pkg/api-server/resource_endpoints.go | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 671b3354b620..7be4d3f45e3a 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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. + ### MeshLoadBalancingStrategy #### Deprecation of `hashPolicies.type: SourceIP` and `maglev.type: SourceIP` diff --git a/pkg/api-server/resource_endpoints.go b/pkg/api-server/resource_endpoints.go index 3989d8c19940..4c2a8c75c17c 100644 --- a/pkg/api-server/resource_endpoints.go +++ b/pkg/api-server/resource_endpoints.go @@ -424,7 +424,9 @@ func (r *resourceEndpoints) createResource( log.Error(err, "Could not write the response") } } else { - response.WriteHeader(201) + if err := response.WriteHeaderAndJson(201, nil, "application/json"); err != nil { + log.Error(err, "Could not write the response") + } } } @@ -476,7 +478,9 @@ func (r *resourceEndpoints) updateResource( log.Error(err, "Could not write the response") } } else { - response.WriteHeader(200) + if err := response.WriteHeaderAndJson(200, nil, "application/json"); err != nil { + log.Error(err, "Could not write the response") + } } } From eb53d57ae385b1aa808a9d4430cdf0e00f2f92da Mon Sep 17 00:00:00 2001 From: slonka Date: Thu, 23 Jan 2025 08:34:41 +0100 Subject: [PATCH 2/3] feat(api): fix returning empty json Signed-off-by: slonka --- UPGRADE.md | 2 +- pkg/api-server/resource_endpoints.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 7be4d3f45e3a..47fa21d8da6c 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -10,7 +10,7 @@ does not have any particular instructions. ### API Server -A successful PUT request without any warnings will now set "content-type: application/json" header. +A successful PUT request without any warnings will now set "content-type: application/json" header and return empty json. ### MeshLoadBalancingStrategy diff --git a/pkg/api-server/resource_endpoints.go b/pkg/api-server/resource_endpoints.go index 4c2a8c75c17c..530fe2bf4721 100644 --- a/pkg/api-server/resource_endpoints.go +++ b/pkg/api-server/resource_endpoints.go @@ -424,7 +424,7 @@ func (r *resourceEndpoints) createResource( log.Error(err, "Could not write the response") } } else { - if err := response.WriteHeaderAndJson(201, nil, "application/json"); err != nil { + if err := response.WriteHeaderAndJson(201, struct{}{}, "application/json"); err != nil { log.Error(err, "Could not write the response") } } @@ -478,7 +478,7 @@ func (r *resourceEndpoints) updateResource( log.Error(err, "Could not write the response") } } else { - if err := response.WriteHeaderAndJson(200, nil, "application/json"); err != nil { + if err := response.WriteHeaderAndJson(200, struct{}{}, "application/json"); err != nil { log.Error(err, "Could not write the response") } } From 44a8b828a9a918908a3ebcb9e5061fa098539296 Mon Sep 17 00:00:00 2001 From: slonka Date: Thu, 23 Jan 2025 09:49:48 +0100 Subject: [PATCH 3/3] feat(api): post review changes Signed-off-by: slonka --- pkg/api-server/resource_endpoints.go | 22 +++++-------------- .../crud/put_circuitbreaker_00.golden.json | 1 + .../crud/put_dataplanes_00.golden.json | 1 + .../crud/put_faultinjection_00.golden.json | 1 + .../crud/put_globalsecret_00.golden.json | 1 + .../crud/put_healthcheck_00.golden.json | 1 + .../resources/crud/put_meshes_02.golden.json | 1 + .../resources/crud/put_meshes_03.golden.json | 1 + .../crud/put_meshservice_00.golden.json | 1 + .../resources/crud/put_secret_00.golden.json | 1 + .../crud/put_trafficroute_00.golden.json | 1 + .../crud/put_traffictrace_00.golden.json | 1 + .../crud/put_update_dataplanes_00.golden.json | 1 + 13 files changed, 18 insertions(+), 16 deletions(-) create mode 100644 pkg/api-server/testdata/resources/crud/put_circuitbreaker_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_dataplanes_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_faultinjection_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_globalsecret_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_healthcheck_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_meshes_02.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_meshes_03.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_meshservice_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_secret_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_trafficroute_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_traffictrace_00.golden.json create mode 100644 pkg/api-server/testdata/resources/crud/put_update_dataplanes_00.golden.json diff --git a/pkg/api-server/resource_endpoints.go b/pkg/api-server/resource_endpoints.go index 530fe2bf4721..fa57ff126b30 100644 --- a/pkg/api-server/resource_endpoints.go +++ b/pkg/api-server/resource_endpoints.go @@ -419,14 +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 { - if err := response.WriteHeaderAndJson(201, struct{}{}, "application/json"); err != nil { - log.Error(err, "Could not write the response") - } + 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") } } @@ -473,14 +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 { - if err := response.WriteHeaderAndJson(200, struct{}{}, "application/json"); err != nil { - log.Error(err, "Could not write the response") - } + 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") } } diff --git a/pkg/api-server/testdata/resources/crud/put_circuitbreaker_00.golden.json b/pkg/api-server/testdata/resources/crud/put_circuitbreaker_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_circuitbreaker_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_dataplanes_00.golden.json b/pkg/api-server/testdata/resources/crud/put_dataplanes_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_dataplanes_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_faultinjection_00.golden.json b/pkg/api-server/testdata/resources/crud/put_faultinjection_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_faultinjection_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_globalsecret_00.golden.json b/pkg/api-server/testdata/resources/crud/put_globalsecret_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_globalsecret_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_healthcheck_00.golden.json b/pkg/api-server/testdata/resources/crud/put_healthcheck_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_healthcheck_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_meshes_02.golden.json b/pkg/api-server/testdata/resources/crud/put_meshes_02.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_meshes_02.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_meshes_03.golden.json b/pkg/api-server/testdata/resources/crud/put_meshes_03.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_meshes_03.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_meshservice_00.golden.json b/pkg/api-server/testdata/resources/crud/put_meshservice_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_meshservice_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_secret_00.golden.json b/pkg/api-server/testdata/resources/crud/put_secret_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_secret_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_trafficroute_00.golden.json b/pkg/api-server/testdata/resources/crud/put_trafficroute_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_trafficroute_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_traffictrace_00.golden.json b/pkg/api-server/testdata/resources/crud/put_traffictrace_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_traffictrace_00.golden.json @@ -0,0 +1 @@ +{} diff --git a/pkg/api-server/testdata/resources/crud/put_update_dataplanes_00.golden.json b/pkg/api-server/testdata/resources/crud/put_update_dataplanes_00.golden.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/pkg/api-server/testdata/resources/crud/put_update_dataplanes_00.golden.json @@ -0,0 +1 @@ +{}