Skip to content

Commit

Permalink
api/types: move image options to api/types/image
Browse files Browse the repository at this point in the history
To prevent a circular import between api/types and api/types image,
the RequestPrivilegeFunc reference was not moved, but defined as
part of the PullOptions / PushOptions.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Jan 23, 2024
1 parent 8906adc commit ac2a028
Show file tree
Hide file tree
Showing 41 changed files with 206 additions and 161 deletions.
2 changes: 1 addition & 1 deletion api/server/router/image/backend.go
Expand Up @@ -25,7 +25,7 @@ type Backend interface {
type imageBackend interface {
ImageDelete(ctx context.Context, imageRef string, force, prune bool) ([]image.DeleteResponse, error)
ImageHistory(ctx context.Context, imageName string) ([]*image.HistoryResponseItem, error)
Images(ctx context.Context, opts types.ImageListOptions) ([]*image.Summary, error)
Images(ctx context.Context, opts image.ListOptions) ([]*image.Summary, error)
GetImage(ctx context.Context, refOrID string, options backend.GetImageOpts) (*dockerimage.Image, error)
TagImage(ctx context.Context, id dockerimage.ID, newRef reference.Named) error
ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*types.ImagesPruneReport, error)
Expand Down
4 changes: 2 additions & 2 deletions api/server/router/image/image_routes.go
Expand Up @@ -190,7 +190,7 @@ func (ir *imageRouter) postImagesPush(ctx context.Context, w http.ResponseWriter

var ref reference.Named

// Tag is empty only in case ImagePushOptions.All is true.
// Tag is empty only in case PushOptions.All is true.
if tag != "" {
r, err := httputils.RepoTagReference(img, tag)
if err != nil {
Expand Down Expand Up @@ -396,7 +396,7 @@ func (ir *imageRouter) getImagesJSON(ctx context.Context, w http.ResponseWriter,
sharedSize = httputils.BoolValue(r, "shared-size")
}

images, err := ir.backend.Images(ctx, types.ImageListOptions{
images, err := ir.backend.Images(ctx, imagetypes.ListOptions{
All: httputils.BoolValue(r, "all"),
Filters: imageFilters,
SharedSize: sharedSize,
Expand Down
47 changes: 0 additions & 47 deletions api/types/client.go
Expand Up @@ -157,57 +157,19 @@ type ImageBuildResponse struct {
OSType string
}

// ImageCreateOptions holds information to create images.
type ImageCreateOptions struct {
RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry.
Platform string // Platform is the target platform of the image if it needs to be pulled from the registry.
}

// ImageImportSource holds source information for ImageImport
type ImageImportSource struct {
Source io.Reader // Source is the data to send to the server to create this image from. You must set SourceName to "-" to leverage this.
SourceName string // SourceName is the name of the image to pull. Set to "-" to leverage the Source attribute.
}

// ImageImportOptions holds information to import images from the client host.
type ImageImportOptions struct {
Tag string // Tag is the name to tag this image with. This attribute is deprecated.
Message string // Message is the message to tag the image with
Changes []string // Changes are the raw changes to apply to this image
Platform string // Platform is the target platform of the image
}

// ImageListOptions holds parameters to list images with.
type ImageListOptions struct {
// All controls whether all images in the graph are filtered, or just
// the heads.
All bool

// Filters is a JSON-encoded set of filter arguments.
Filters filters.Args

// SharedSize indicates whether the shared size of images should be computed.
SharedSize bool

// ContainerCount indicates whether container count should be computed.
ContainerCount bool
}

// ImageLoadResponse returns information to the client about a load process.
type ImageLoadResponse struct {
// Body must be closed to avoid a resource leak
Body io.ReadCloser
JSON bool
}

// ImagePullOptions holds information to pull images.
type ImagePullOptions struct {
All bool
RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry
PrivilegeFunc RequestPrivilegeFunc
Platform string
}

// RequestPrivilegeFunc is a function interface that
// clients can supply to retry operations after
// getting an authorization error.
Expand All @@ -216,15 +178,6 @@ type ImagePullOptions struct {
// if the privilege request fails.
type RequestPrivilegeFunc func() (string, error)

// ImagePushOptions holds information to push images.
type ImagePushOptions ImagePullOptions

// ImageRemoveOptions holds parameters to remove images.
type ImageRemoveOptions struct {
Force bool
PruneChildren bool
}

// ImageSearchOptions holds parameters to search images with.
type ImageSearchOptions struct {
RegistryAuth string
Expand Down
57 changes: 57 additions & 0 deletions api/types/image/opts.go
@@ -0,0 +1,57 @@
package image

import "github.com/docker/docker/api/types/filters"

// ImportOptions holds information to import images from the client host.
type ImportOptions struct {
Tag string // Tag is the name to tag this image with. This attribute is deprecated.
Message string // Message is the message to tag the image with
Changes []string // Changes are the raw changes to apply to this image
Platform string // Platform is the target platform of the image
}

// CreateOptions holds information to create images.
type CreateOptions struct {
RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry.
Platform string // Platform is the target platform of the image if it needs to be pulled from the registry.
}

// PullOptions holds information to pull images.
type PullOptions struct {
All bool
RegistryAuth string // RegistryAuth is the base64 encoded credentials for the registry

// PrivilegeFunc is a function that clients can supply to retry operations
// after getting an authorization error. This function returns the registry
// authentication header value in base64 encoded format, or an error if the
// privilege request fails.
//
// Also see [github.com/docker/docker/api/types.RequestPrivilegeFunc].
PrivilegeFunc func() (string, error)
Platform string
}

// PushOptions holds information to push images.
type PushOptions PullOptions

// ListOptions holds parameters to list images with.
type ListOptions struct {
// All controls whether all images in the graph are filtered, or just
// the heads.
All bool

// Filters is a JSON-encoded set of filter arguments.
Filters filters.Args

// SharedSize indicates whether the shared size of images should be computed.
SharedSize bool

// ContainerCount indicates whether container count should be computed.
ContainerCount bool
}

// RemoveOptions holds parameters to remove images.
type RemoveOptions struct {
Force bool
PruneChildren bool
}
30 changes: 30 additions & 0 deletions api/types/types_deprecated.go
Expand Up @@ -136,3 +136,33 @@ type ContainerRemoveOptions = container.RemoveOptions
func DecodeSecurityOptions(opts []string) ([]system.SecurityOpt, error) {
return system.DecodeSecurityOptions(opts)
}

// ImageImportOptions holds information to import images from the client host.
//
// Deprecated: use [image.ImportOptions].
type ImageImportOptions = image.ImportOptions

// ImageCreateOptions holds information to create images.
//
// Deprecated: use [image.CreateOptions].
type ImageCreateOptions = image.CreateOptions

// ImagePullOptions holds information to pull images.
//
// Deprecated: use [image.PullOptions].
type ImagePullOptions = image.PullOptions

// ImagePushOptions holds information to push images.
//
// Deprecated: use [image.PushOptions].
type ImagePushOptions = image.PushOptions

// ImageListOptions holds parameters to list images with.
//
// Deprecated: use [image.ListOptions].
type ImageListOptions = image.ListOptions

// ImageRemoveOptions holds parameters to remove images.
//
// Deprecated: use [image.RemoveOptions].
type ImageRemoveOptions = image.RemoveOptions
4 changes: 2 additions & 2 deletions client/image_create.go
Expand Up @@ -8,13 +8,13 @@ import (
"strings"

"github.com/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
)

// ImageCreate creates a new image based on the parent options.
// It returns the JSON content in the response body.
func (cli *Client) ImageCreate(ctx context.Context, parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
func (cli *Client) ImageCreate(ctx context.Context, parentReference string, options image.CreateOptions) (io.ReadCloser, error) {
ref, err := reference.ParseNormalizedNamed(parentReference)
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions client/image_create_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
Expand All @@ -20,7 +20,7 @@ func TestImageCreateError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ImageCreate(context.Background(), "reference", types.ImageCreateOptions{})
_, err := client.ImageCreate(context.Background(), "reference", image.CreateOptions{})
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
}

Expand Down Expand Up @@ -58,7 +58,7 @@ func TestImageCreate(t *testing.T) {
}),
}

createResponse, err := client.ImageCreate(context.Background(), expectedReference, types.ImageCreateOptions{
createResponse, err := client.ImageCreate(context.Background(), expectedReference, image.CreateOptions{
RegistryAuth: expectedRegistryAuth,
})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion client/image_import.go
Expand Up @@ -8,11 +8,12 @@ import (

"github.com/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
)

// ImageImport creates a new image based on the source options.
// It returns the JSON content in the response body.
func (cli *Client) ImageImport(ctx context.Context, source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) {
func (cli *Client) ImageImport(ctx context.Context, source types.ImageImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error) {
if ref != "" {
// Check if the given image name can be resolved
if _, err := reference.ParseNormalizedNamed(ref); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions client/image_import_test.go
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
Expand All @@ -20,7 +21,7 @@ func TestImageImportError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ImageImport(context.Background(), types.ImageImportSource{}, "image:tag", types.ImageImportOptions{})
_, err := client.ImageImport(context.Background(), types.ImageImportSource{}, "image:tag", image.ImportOptions{})
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
}

Expand Down Expand Up @@ -63,7 +64,7 @@ func TestImageImport(t *testing.T) {
importResponse, err := client.ImageImport(context.Background(), types.ImageImportSource{
Source: strings.NewReader("source"),
SourceName: "image_source",
}, "repository_name:imported", types.ImageImportOptions{
}, "repository_name:imported", image.ImportOptions{
Tag: "imported",
Message: "A message",
Changes: []string{"change1", "change2"},
Expand Down
3 changes: 1 addition & 2 deletions client/image_list.go
Expand Up @@ -5,14 +5,13 @@ import (
"encoding/json"
"net/url"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/versions"
)

// ImageList returns a list of images in the docker host.
func (cli *Client) ImageList(ctx context.Context, options types.ImageListOptions) ([]image.Summary, error) {
func (cli *Client) ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error) {
// Make sure we negotiated (if the client is configured to do so),
// as code below contains API-version specific handling of options.
//
Expand Down
19 changes: 9 additions & 10 deletions client/image_list_test.go
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/errdefs"
Expand All @@ -24,27 +23,27 @@ func TestImageListError(t *testing.T) {
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}

_, err := client.ImageList(context.Background(), types.ImageListOptions{})
_, err := client.ImageList(context.Background(), image.ListOptions{})
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
}

func TestImageList(t *testing.T) {
const expectedURL = "/images/json"

listCases := []struct {
options types.ImageListOptions
options image.ListOptions
expectedQueryParams map[string]string
}{
{
options: types.ImageListOptions{},
options: image.ListOptions{},
expectedQueryParams: map[string]string{
"all": "",
"filter": "",
"filters": "",
},
},
{
options: types.ImageListOptions{
options: image.ListOptions{
Filters: filters.NewArgs(
filters.Arg("label", "label1"),
filters.Arg("label", "label2"),
Expand All @@ -58,7 +57,7 @@ func TestImageList(t *testing.T) {
},
},
{
options: types.ImageListOptions{
options: image.ListOptions{
Filters: filters.NewArgs(filters.Arg("dangling", "false")),
},
expectedQueryParams: map[string]string{
Expand Down Expand Up @@ -141,7 +140,7 @@ func TestImageListApiBefore125(t *testing.T) {
version: "1.24",
}

options := types.ImageListOptions{
options := image.ListOptions{
Filters: filters.NewArgs(filters.Arg("reference", "image:tag")),
}

Expand All @@ -162,12 +161,12 @@ func TestImageListWithSharedSize(t *testing.T) {
for _, tc := range []struct {
name string
version string
options types.ImageListOptions
options image.ListOptions
sharedSize string // expected value for the shared-size query param, or empty if it should not be set.
}{
{name: "unset after 1.42, no options set", version: "1.42"},
{name: "set after 1.42, if requested", version: "1.42", options: types.ImageListOptions{SharedSize: true}, sharedSize: "1"},
{name: "unset before 1.42, even if requested", version: "1.41", options: types.ImageListOptions{SharedSize: true}},
{name: "set after 1.42, if requested", version: "1.42", options: image.ListOptions{SharedSize: true}, sharedSize: "1"},
{name: "unset before 1.42, even if requested", version: "1.41", options: image.ListOptions{SharedSize: true}},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions client/image_pull.go
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/errdefs"
)

Expand All @@ -19,7 +19,7 @@ import (
// FIXME(vdemeester): there is currently used in a few way in docker/docker
// - if not in trusted content, ref is used to pass the whole reference, and tag is empty
// - if in trusted content, ref is used to pass the reference name, and tag for the digest
func (cli *Client) ImagePull(ctx context.Context, refStr string, options types.ImagePullOptions) (io.ReadCloser, error) {
func (cli *Client) ImagePull(ctx context.Context, refStr string, options image.PullOptions) (io.ReadCloser, error) {
ref, err := reference.ParseNormalizedNamed(refStr)
if err != nil {
return nil, err
Expand Down

0 comments on commit ac2a028

Please sign in to comment.