shim-client: add support for DoPut

While at it, make sure we check for nil in DoPost

Signed-off-by: Eric Ernst <eric_ernst@apple.com>
This commit is contained in:
Eric Ernst 2022-04-25 04:31:25 -07:00
parent 640173cfc2
commit 2a09378dd9
2 changed files with 29 additions and 4 deletions

View File

@ -163,5 +163,5 @@ func Resize(volumePath string, size uint64) error {
if err != nil { if err != nil {
return err return err
} }
return shimclient.DoPost(sandboxId, defaultTimeout, containerdshim.DirectVolumeResizeUrl, encoded) return shimclient.DoPost(sandboxId, defaultTimeout, containerdshim.DirectVolumeResizeUrl, "application/json", encoded)
} }

View File

@ -65,15 +65,40 @@ func DoGet(sandboxID string, timeoutInSeconds time.Duration, urlPath string) ([]
return body, nil 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) client, err := BuildShimClient(sandboxID, timeoutInSeconds)
if err != nil { if err != nil {
return err 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() { 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 return err
} }