From 2a09378dd90603d0021ed82ab08637a6885942ce Mon Sep 17 00:00:00 2001 From: Eric Ernst Date: Mon, 25 Apr 2022 04:31:25 -0700 Subject: [PATCH] shim-client: add support for DoPut While at it, make sure we check for nil in DoPost Signed-off-by: Eric Ernst --- src/runtime/cmd/kata-runtime/kata-volume.go | 2 +- .../shimclient/shim_management_client.go | 31 +++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/runtime/cmd/kata-runtime/kata-volume.go b/src/runtime/cmd/kata-runtime/kata-volume.go index 55274f7d8..b33febe19 100644 --- a/src/runtime/cmd/kata-runtime/kata-volume.go +++ b/src/runtime/cmd/kata-runtime/kata-volume.go @@ -163,5 +163,5 @@ func Resize(volumePath string, size uint64) error { if err != nil { return err } - return shimclient.DoPost(sandboxId, defaultTimeout, containerdshim.DirectVolumeResizeUrl, encoded) + return shimclient.DoPost(sandboxId, defaultTimeout, containerdshim.DirectVolumeResizeUrl, "application/json", encoded) } diff --git a/src/runtime/pkg/utils/shimclient/shim_management_client.go b/src/runtime/pkg/utils/shimclient/shim_management_client.go index c9ed3ad69..835b6f9b9 100644 --- a/src/runtime/pkg/utils/shimclient/shim_management_client.go +++ b/src/runtime/pkg/utils/shimclient/shim_management_client.go @@ -65,15 +65,40 @@ func DoGet(sandboxID string, timeoutInSeconds time.Duration, urlPath string) ([] return body, nil } -func DoPost(sandboxID string, timeoutInSeconds time.Duration, urlPath string, payload []byte) error { +// DoPut will make a PUT request to the shim endpoint that handles the given sandbox ID +func DoPut(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentType string, payload []byte) error { client, err := BuildShimClient(sandboxID, timeoutInSeconds) if err != nil { return err } - resp, err := client.Post(fmt.Sprintf("http://shim/%s", urlPath), "application/json", bytes.NewBuffer(payload)) + req, err := http.NewRequest(http.MethodPut, fmt.Sprintf("http://shim/%s", urlPath), bytes.NewBuffer(payload)) + if err != nil { + return err + } + req.Header.Set("Content-Type", contentType) + + resp, err := client.Do(req) defer func() { - resp.Body.Close() + if resp != nil { + resp.Body.Close() + } + }() + return err +} + +// DoPost will make a POST request to the shim endpoint that handles the given sandbox ID +func DoPost(sandboxID string, timeoutInSeconds time.Duration, urlPath, contentType string, payload []byte) error { + client, err := BuildShimClient(sandboxID, timeoutInSeconds) + if err != nil { + return err + } + + resp, err := client.Post(fmt.Sprintf("http://shim/%s", urlPath), contentType, bytes.NewBuffer(payload)) + defer func() { + if resp != nil { + resp.Body.Close() + } }() return err }