Merge pull request #49444 from thaJeztah/commit_and_exect_response

api/types/container: introduce CommitResponse, ExecCreateResponse types
This commit is contained in:
Sebastiaan van Stijn
2025-02-11 12:53:09 +01:00
committed by GitHub
13 changed files with 48 additions and 28 deletions

View File

@@ -71,7 +71,7 @@ func (c *containerRouter) postCommit(ctx context.Context, w http.ResponseWriter,
return err
}
return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{ID: imgID})
return httputils.WriteJSON(w, http.StatusCreated, &container.CommitResponse{ID: imgID})
}
func (c *containerRouter) getContainersJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {

View File

@@ -61,7 +61,7 @@ func (c *containerRouter) postContainerExecCreate(ctx context.Context, w http.Re
return err
}
return httputils.WriteJSON(w, http.StatusCreated, &types.IDResponse{
return httputils.WriteJSON(w, http.StatusCreated, &container.ExecCreateResponse{
ID: id,
})
}

View File

@@ -2908,9 +2908,10 @@ definitions:
example:
message: "Something went wrong."
IdResponse:
IDResponse:
description: "Response to an API call that returns just an Id"
type: "object"
x-go-name: "IDResponse"
required: ["Id"]
properties:
Id:
@@ -10233,7 +10234,7 @@ paths:
201:
description: "no error"
schema:
$ref: "#/definitions/IdResponse"
$ref: "#/definitions/IDResponse"
404:
description: "no such container"
schema:
@@ -10627,7 +10628,7 @@ paths:
201:
description: "no error"
schema:
$ref: "#/definitions/IdResponse"
$ref: "#/definitions/IDResponse"
404:
description: "no such container"
schema:
@@ -13107,7 +13108,7 @@ paths:
201:
description: "no error"
schema:
$ref: "#/definitions/IdResponse"
$ref: "#/definitions/IDResponse"
409:
description: "name conflicts with an existing object"
schema:
@@ -13314,7 +13315,7 @@ paths:
201:
description: "no error"
schema:
$ref: "#/definitions/IdResponse"
$ref: "#/definitions/IDResponse"
409:
description: "name conflicts with an existing object"
schema:

View File

@@ -1,10 +1,10 @@
package types
package common
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
// IDResponse Response to an API call that returns just an Id
// swagger:model IdResponse
// swagger:model IDResponse
type IDResponse struct {
// The id of the newly created object.

View File

@@ -0,0 +1,7 @@
package container
import "github.com/docker/docker/api/types/common"
// CommitResponse response for the commit API call, containing the ID of the
// image that was produced.
type CommitResponse = common.IDResponse

View File

@@ -1,5 +1,13 @@
package container
import "github.com/docker/docker/api/types/common"
// ExecCreateResponse is the response for a successful exec-create request.
// It holds the ID of the exec that was created.
//
// TODO(thaJeztah): make this a distinct type.
type ExecCreateResponse = common.IDResponse
// ExecOptions is a small subset of the Config struct that holds the configuration
// for the exec feature of docker.
type ExecOptions struct {

View File

@@ -3,11 +3,15 @@ package types
import (
"context"
"github.com/docker/docker/api/types/common"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/storage"
)
// IDResponse Response to an API call that returns just an Id
type IDResponse = common.IDResponse
// ContainerJSONBase contains response of Engine API GET "/containers/{name:.*}/json"
// for API version 1.18 and older.
//

View File

@@ -69,11 +69,11 @@ type HijackDialer interface {
// ContainerAPIClient defines API client methods for the containers
type ContainerAPIClient interface {
ContainerAttach(ctx context.Context, container string, options container.AttachOptions) (types.HijackedResponse, error)
ContainerCommit(ctx context.Context, container string, options container.CommitOptions) (types.IDResponse, error)
ContainerCommit(ctx context.Context, container string, options container.CommitOptions) (container.CommitResponse, error)
ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error)
ContainerDiff(ctx context.Context, container string) ([]container.FilesystemChange, error)
ContainerExecAttach(ctx context.Context, execID string, options container.ExecAttachOptions) (types.HijackedResponse, error)
ContainerExecCreate(ctx context.Context, container string, options container.ExecOptions) (types.IDResponse, error)
ContainerExecCreate(ctx context.Context, container string, options container.ExecOptions) (container.ExecCreateResponse, error)
ContainerExecInspect(ctx context.Context, execID string) (container.ExecInspect, error)
ContainerExecResize(ctx context.Context, execID string, options container.ResizeOptions) error
ContainerExecStart(ctx context.Context, execID string, options container.ExecStartOptions) error

View File

@@ -7,26 +7,25 @@ import (
"net/url"
"github.com/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
)
// ContainerCommit applies changes to a container and creates a new tagged image.
func (cli *Client) ContainerCommit(ctx context.Context, containerID string, options container.CommitOptions) (types.IDResponse, error) {
func (cli *Client) ContainerCommit(ctx context.Context, containerID string, options container.CommitOptions) (container.CommitResponse, error) {
containerID, err := trimID("container", containerID)
if err != nil {
return types.IDResponse{}, err
return container.CommitResponse{}, err
}
var repository, tag string
if options.Reference != "" {
ref, err := reference.ParseNormalizedNamed(options.Reference)
if err != nil {
return types.IDResponse{}, err
return container.CommitResponse{}, err
}
if _, isCanonical := ref.(reference.Canonical); isCanonical {
return types.IDResponse{}, errors.New("refusing to create a tag with a digest reference")
return container.CommitResponse{}, errors.New("refusing to create a tag with a digest reference")
}
ref = reference.TagNameOnly(ref)
@@ -49,7 +48,7 @@ func (cli *Client) ContainerCommit(ctx context.Context, containerID string, opti
query.Set("pause", "0")
}
var response types.IDResponse
var response container.CommitResponse
resp, err := cli.post(ctx, "/commit", query, options.Config, nil)
defer ensureReaderClosed(resp)
if err != nil {

View File

@@ -10,7 +10,6 @@ import (
"strings"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
@@ -77,7 +76,7 @@ func TestContainerCommit(t *testing.T) {
if len(changes) != len(expectedChanges) {
return nil, fmt.Errorf("expected container changes size to be '%d', got %d", len(expectedChanges), len(changes))
}
b, err := json.Marshal(types.IDResponse{
b, err := json.Marshal(container.CommitResponse{
ID: "new_container_id",
})
if err != nil {

View File

@@ -11,10 +11,10 @@ import (
)
// ContainerExecCreate creates a new exec configuration to run an exec process.
func (cli *Client) ContainerExecCreate(ctx context.Context, containerID string, options container.ExecOptions) (types.IDResponse, error) {
func (cli *Client) ContainerExecCreate(ctx context.Context, containerID string, options container.ExecOptions) (container.ExecCreateResponse, error) {
containerID, err := trimID("container", containerID)
if err != nil {
return types.IDResponse{}, err
return container.ExecCreateResponse{}, err
}
// Make sure we negotiated (if the client is configured to do so),
@@ -23,11 +23,11 @@ func (cli *Client) ContainerExecCreate(ctx context.Context, containerID string,
// Normally, version-negotiation (if enabled) would not happen until
// the API request is made.
if err := cli.checkVersion(ctx); err != nil {
return types.IDResponse{}, err
return container.ExecCreateResponse{}, err
}
if err := cli.NewVersionError(ctx, "1.25", "env"); len(options.Env) != 0 && err != nil {
return types.IDResponse{}, err
return container.ExecCreateResponse{}, err
}
if versions.LessThan(cli.ClientVersion(), "1.42") {
options.ConsoleSize = nil
@@ -36,10 +36,10 @@ func (cli *Client) ContainerExecCreate(ctx context.Context, containerID string,
resp, err := cli.post(ctx, "/containers/"+containerID+"/exec", nil, options, nil)
defer ensureReaderClosed(resp)
if err != nil {
return types.IDResponse{}, err
return container.ExecCreateResponse{}, err
}
var response types.IDResponse
var response container.ExecCreateResponse
err = json.NewDecoder(resp.body).Decode(&response)
return response, err
}

View File

@@ -10,7 +10,6 @@ import (
"strings"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
@@ -67,7 +66,7 @@ func TestContainerExecCreate(t *testing.T) {
if execConfig.User != "user" {
return nil, fmt.Errorf("expected an execConfig with User == 'user', got %v", execConfig)
}
b, err := json.Marshal(types.IDResponse{
b, err := json.Marshal(container.ExecCreateResponse{
ID: "exec_id",
})
if err != nil {

View File

@@ -4,13 +4,16 @@ set -eu
swagger generate model -f api/swagger.yaml \
-t api -m types --skip-validator -C api/swagger-gen.yaml \
-n ErrorResponse \
-n IdResponse \
-n Plugin \
-n PluginDevice \
-n PluginMount \
-n PluginEnv \
-n PluginInterfaceType
swagger generate model -f api/swagger.yaml \
-t api -m types/common --skip-validator -C api/swagger-gen.yaml \
-n IDResponse
swagger generate model -f api/swagger.yaml \
-t api -m types/storage --skip-validator -C api/swagger-gen.yaml \
-n DriverData