Merge pull request #51326 from vvoland/client-containerupdate-structs

client/container_update: Wrap options and result
This commit is contained in:
Sebastiaan van Stijn
2025-10-29 15:17:28 +01:00
committed by GitHub
7 changed files with 68 additions and 28 deletions

View File

@@ -73,7 +73,7 @@ type ContainerAPIClient interface {
ContainerStop(ctx context.Context, container string, options ContainerStopOptions) (ContainerStopResult, error)
ContainerTop(ctx context.Context, container string, options ContainerTopOptions) (ContainerTopResult, error)
ContainerUnpause(ctx context.Context, container string, options ContainerUnpauseOptions) (ContainerUnpauseResult, error)
ContainerUpdate(ctx context.Context, container string, updateConfig container.UpdateConfig) (container.UpdateResponse, error)
ContainerUpdate(ctx context.Context, container string, updateConfig ContainerUpdateOptions) (ContainerUpdateResult, error)
ContainerWait(ctx context.Context, container string, options ContainerWaitOptions) ContainerWaitResult
CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, container.PathStat, error)
CopyToContainer(ctx context.Context, container, path string, content io.Reader, options CopyToContainerOptions) error

View File

@@ -7,20 +7,40 @@ import (
"github.com/moby/moby/api/types/container"
)
// ContainerUpdateOptions holds options for [Client.ContainerUpdate].
type ContainerUpdateOptions struct {
Resources *container.Resources
RestartPolicy *container.RestartPolicy
}
// ContainerUpdateResult is the result from updating a container.
type ContainerUpdateResult struct {
// Warnings encountered when updating the container.
Warnings []string
}
// ContainerUpdate updates the resources of a container.
func (cli *Client) ContainerUpdate(ctx context.Context, containerID string, updateConfig container.UpdateConfig) (container.UpdateResponse, error) {
func (cli *Client) ContainerUpdate(ctx context.Context, containerID string, options ContainerUpdateOptions) (ContainerUpdateResult, error) {
containerID, err := trimID("container", containerID)
if err != nil {
return container.UpdateResponse{}, err
return ContainerUpdateResult{}, err
}
updateConfig := container.UpdateConfig{}
if options.Resources != nil {
updateConfig.Resources = *options.Resources
}
if options.RestartPolicy != nil {
updateConfig.RestartPolicy = *options.RestartPolicy
}
resp, err := cli.post(ctx, "/containers/"+containerID+"/update", nil, updateConfig, nil)
defer ensureReaderClosed(resp)
if err != nil {
return container.UpdateResponse{}, err
return ContainerUpdateResult{}, err
}
var response container.UpdateResponse
err = json.NewDecoder(resp.Body).Decode(&response)
return response, err
return ContainerUpdateResult{Warnings: response.Warnings}, err
}

View File

@@ -14,14 +14,14 @@ import (
func TestContainerUpdateError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ContainerUpdate(context.Background(), "nothing", container.UpdateConfig{})
_, err = client.ContainerUpdate(context.Background(), "nothing", ContainerUpdateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerUpdate(context.Background(), "", container.UpdateConfig{})
_, err = client.ContainerUpdate(context.Background(), "", ContainerUpdateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
_, err = client.ContainerUpdate(context.Background(), " ", container.UpdateConfig{})
_, err = client.ContainerUpdate(context.Background(), " ", ContainerUpdateOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
@@ -37,11 +37,11 @@ func TestContainerUpdate(t *testing.T) {
}))
assert.NilError(t, err)
_, err = client.ContainerUpdate(context.Background(), "container_id", container.UpdateConfig{
Resources: container.Resources{
_, err = client.ContainerUpdate(context.Background(), "container_id", ContainerUpdateOptions{
Resources: &container.Resources{
CPUPeriod: 1,
},
RestartPolicy: container.RestartPolicy{
RestartPolicy: &container.RestartPolicy{
Name: "always",
},
})