Merge pull request #51383 from thaJeztah/client_ImageLoad_interface

client: use interface for return types
This commit is contained in:
Austin Vazquez
2025-11-05 11:35:59 -06:00
committed by GitHub
20 changed files with 376 additions and 346 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"io"
"net/url"
"sync"
)
// ContainerExportOptions specifies options for container export operations.
@@ -13,9 +12,8 @@ type ContainerExportOptions struct {
}
// ContainerExportResult represents the result of a container export operation.
type ContainerExportResult struct {
rc io.ReadCloser
close func() error
type ContainerExportResult interface {
io.ReadCloser
}
// ContainerExport retrieves the raw contents of a container
@@ -24,39 +22,39 @@ type ContainerExportResult struct {
func (cli *Client) ContainerExport(ctx context.Context, containerID string, options ContainerExportOptions) (ContainerExportResult, error) {
containerID, err := trimID("container", containerID)
if err != nil {
return ContainerExportResult{}, err
return nil, err
}
resp, err := cli.get(ctx, "/containers/"+containerID+"/export", url.Values{}, nil)
if err != nil {
return ContainerExportResult{}, err
return nil, err
}
return newContainerExportResult(resp.Body), nil
return &containerExportResult{
body: resp.Body,
}, nil
}
func newContainerExportResult(rc io.ReadCloser) ContainerExportResult {
if rc == nil {
panic("nil io.ReadCloser")
}
return ContainerExportResult{
rc: rc,
close: sync.OnceValue(rc.Close),
}
type containerExportResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
// Read implements io.ReadCloser
func (r ContainerExportResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
var (
_ io.ReadCloser = (*containerExportResult)(nil)
_ ContainerExportResult = (*containerExportResult)(nil)
)
func (r *containerExportResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.rc.Read(p)
return r.body.Read(p)
}
// Close implements io.ReadCloser
func (r ContainerExportResult) Close() error {
if r.close == nil {
func (r *containerExportResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.close()
return r.body.Close()
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/url"
"sync"
"time"
"github.com/moby/moby/client/internal/timestamp"
@@ -24,9 +23,8 @@ type ContainerLogsOptions struct {
}
// ContainerLogsResult is the result of a container logs operation.
type ContainerLogsResult struct {
rc io.ReadCloser
close func() error
type ContainerLogsResult interface {
io.ReadCloser
}
// ContainerLogs returns the logs generated by a container in an [io.ReadCloser].
@@ -58,7 +56,7 @@ type ContainerLogsResult struct {
func (cli *Client) ContainerLogs(ctx context.Context, containerID string, options ContainerLogsOptions) (ContainerLogsResult, error) {
containerID, err := trimID("container", containerID)
if err != nil {
return ContainerLogsResult{}, err
return nil, err
}
query := url.Values{}
@@ -73,7 +71,7 @@ func (cli *Client) ContainerLogs(ctx context.Context, containerID string, option
if options.Since != "" {
ts, err := timestamp.GetTimestamp(options.Since, time.Now())
if err != nil {
return ContainerLogsResult{}, fmt.Errorf(`invalid value for "since": %w`, err)
return nil, fmt.Errorf(`invalid value for "since": %w`, err)
}
query.Set("since", ts)
}
@@ -81,7 +79,7 @@ func (cli *Client) ContainerLogs(ctx context.Context, containerID string, option
if options.Until != "" {
ts, err := timestamp.GetTimestamp(options.Until, time.Now())
if err != nil {
return ContainerLogsResult{}, fmt.Errorf(`invalid value for "until": %w`, err)
return nil, fmt.Errorf(`invalid value for "until": %w`, err)
}
query.Set("until", ts)
}
@@ -101,33 +99,33 @@ func (cli *Client) ContainerLogs(ctx context.Context, containerID string, option
resp, err := cli.get(ctx, "/containers/"+containerID+"/logs", query, nil)
if err != nil {
return ContainerLogsResult{}, err
return nil, err
}
return newContainerLogsResult(resp.Body), nil
return &containerLogsResult{
body: resp.Body,
}, nil
}
func newContainerLogsResult(rc io.ReadCloser) ContainerLogsResult {
if rc == nil {
panic("rc cannot be nil")
}
return ContainerLogsResult{
rc: rc,
close: sync.OnceValue(rc.Close),
}
type containerLogsResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
// Read implements the io.Reader interface.
func (r ContainerLogsResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
var (
_ io.ReadCloser = (*containerLogsResult)(nil)
_ ContainerLogsResult = (*containerLogsResult)(nil)
)
func (r *containerLogsResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.rc.Read(p)
return r.body.Read(p)
}
// Close closes the underlying reader.
func (r ContainerLogsResult) Close() error {
if r.close == nil {
func (r *containerLogsResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.close()
return r.body.Close()
}

View File

@@ -2,18 +2,24 @@ package client
import (
"context"
"io"
"net/url"
"github.com/distribution/reference"
)
// ImageImportResult holds the response body returned by the daemon for image import.
type ImageImportResult interface {
io.ReadCloser
}
// 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 ImageImportSource, ref string, options ImageImportOptions) (ImageImportResult, error) {
if ref != "" {
// Check if the given image name can be resolved
if _, err := reference.ParseNormalizedNamed(ref); err != nil {
return ImageImportResult{}, err
return nil, err
}
}
@@ -40,7 +46,32 @@ func (cli *Client) ImageImport(ctx context.Context, source ImageImportSource, re
resp, err := cli.postRaw(ctx, "/images/create", query, source.Source, nil)
if err != nil {
return ImageImportResult{}, err
return nil, err
}
return ImageImportResult{rc: resp.Body}, nil
return &imageImportResult{body: resp.Body}, nil
}
// ImageImportResult holds the response body returned by the daemon for image import.
type imageImportResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
var (
_ io.ReadCloser = (*imageImportResult)(nil)
_ ImageImportResult = (*imageImportResult)(nil)
)
func (r *imageImportResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.body.Read(p)
}
func (r *imageImportResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.body.Close()
}

View File

@@ -19,22 +19,3 @@ type ImageImportOptions struct {
Changes []string // Changes are the raw changes to apply to this image
Platform ocispec.Platform // Platform is the target platform of the image
}
// ImageImportResult holds the response body returned by the daemon for image import.
type ImageImportResult struct {
rc io.ReadCloser
}
func (r ImageImportResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
return 0, io.EOF
}
return r.rc.Read(p)
}
func (r ImageImportResult) Close() error {
if r.rc == nil {
return nil
}
return r.rc.Close()
}

View File

@@ -7,18 +7,19 @@ import (
"net/url"
)
// ImageLoad loads an image in the docker host from the client host.
// It's up to the caller to close the [io.ReadCloser] in the
// [ImageLoadResult] returned by this function.
//
// Platform is an optional parameter that specifies the platform to load from
// the provided multi-platform image. Passing a platform only has an effect
// if the input image is a multi-platform image.
// ImageLoadResult returns information to the client about a load process.
// It implements [io.ReadCloser] and must be closed to avoid a resource leak.
type ImageLoadResult interface {
io.ReadCloser
}
// ImageLoad loads an image in the docker host from the client host. It's up
// to the caller to close the [ImageLoadResult] returned by this function.
func (cli *Client) ImageLoad(ctx context.Context, input io.Reader, loadOpts ...ImageLoadOption) (ImageLoadResult, error) {
var opts imageLoadOpts
for _, opt := range loadOpts {
if err := opt.Apply(&opts); err != nil {
return ImageLoadResult{}, err
return nil, err
}
}
@@ -29,12 +30,12 @@ func (cli *Client) ImageLoad(ctx context.Context, input io.Reader, loadOpts ...I
}
if len(opts.apiOptions.Platforms) > 0 {
if err := cli.requiresVersion(ctx, "1.48", "platform"); err != nil {
return ImageLoadResult{}, err
return nil, err
}
p, err := encodePlatforms(opts.apiOptions.Platforms...)
if err != nil {
return ImageLoadResult{}, err
return nil, err
}
query["platform"] = p
}
@@ -43,25 +44,33 @@ func (cli *Client) ImageLoad(ctx context.Context, input io.Reader, loadOpts ...I
"Content-Type": {"application/x-tar"},
})
if err != nil {
return ImageLoadResult{}, err
return nil, err
}
return ImageLoadResult{
return &imageLoadResult{
body: resp.Body,
}, nil
}
// ImageLoadResult returns information to the client about a load process.
type ImageLoadResult struct {
// Body must be closed to avoid a resource leak
// imageLoadResult returns information to the client about a load process.
type imageLoadResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
func (r ImageLoadResult) Read(p []byte) (n int, err error) {
var (
_ io.ReadCloser = (*imageLoadResult)(nil)
_ ImageLoadResult = (*imageLoadResult)(nil)
)
func (r *imageLoadResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.body.Read(p)
}
func (r ImageLoadResult) Close() error {
if r.body == nil {
func (r *imageLoadResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.body.Close()

View File

@@ -38,6 +38,10 @@ func ImageLoadWithQuiet(quiet bool) ImageLoadOption {
}
// ImageLoadWithPlatforms sets the platforms to be loaded from the image.
//
// Platform is an optional parameter that specifies the platform to load from
// the provided multi-platform image. Passing a platform only has an effect
// if the input image is a multi-platform image.
func ImageLoadWithPlatforms(platforms ...ocispec.Platform) ImageLoadOption {
return imageLoadOptionFunc(func(opt *imageLoadOpts) error {
if opt.apiOptions.Platforms != nil {

View File

@@ -2,9 +2,14 @@ package client
import (
"context"
"io"
"net/url"
)
type ImageSaveResult interface {
io.ReadCloser
}
// ImageSave retrieves one or more images from the docker host as an
// [ImageSaveResult].
//
@@ -15,7 +20,7 @@ func (cli *Client) ImageSave(ctx context.Context, imageIDs []string, saveOpts ..
var opts imageSaveOpts
for _, opt := range saveOpts {
if err := opt.Apply(&opts); err != nil {
return ImageSaveResult{}, err
return nil, err
}
}
@@ -25,18 +30,44 @@ func (cli *Client) ImageSave(ctx context.Context, imageIDs []string, saveOpts ..
if len(opts.apiOptions.Platforms) > 0 {
if err := cli.requiresVersion(ctx, "1.48", "platform"); err != nil {
return ImageSaveResult{}, err
return nil, err
}
p, err := encodePlatforms(opts.apiOptions.Platforms...)
if err != nil {
return ImageSaveResult{}, err
return nil, err
}
query["platform"] = p
}
resp, err := cli.get(ctx, "/images/get", query, nil)
if err != nil {
return ImageSaveResult{}, err
return nil, err
}
return newImageSaveResult(resp.Body), nil
return &imageSaveResult{
body: resp.Body,
}, nil
}
type imageSaveResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
var (
_ io.ReadCloser = (*imageSaveResult)(nil)
_ ImageSaveResult = (*imageSaveResult)(nil)
)
func (r *imageSaveResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.body.Read(p)
}
func (r *imageSaveResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.body.Close()
}

View File

@@ -2,8 +2,6 @@ package client
import (
"fmt"
"io"
"sync"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
@@ -38,34 +36,3 @@ type imageSaveOptions struct {
// multi-platform image and has multiple variants.
Platforms []ocispec.Platform
}
func newImageSaveResult(rc io.ReadCloser) ImageSaveResult {
if rc == nil {
panic("nil io.ReadCloser")
}
return ImageSaveResult{
rc: rc,
close: sync.OnceValue(rc.Close),
}
}
type ImageSaveResult struct {
rc io.ReadCloser
close func() error
}
// Read implements io.ReadCloser
func (r ImageSaveResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
return 0, io.EOF
}
return r.rc.Read(p)
}
// Close implements io.ReadCloser
func (r ImageSaveResult) Close() error {
if r.close == nil {
return nil
}
return r.close()
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/url"
"sync"
"time"
"github.com/moby/moby/client/internal/timestamp"
@@ -26,16 +25,15 @@ type ServiceLogsOptions struct {
// ServiceLogsResult holds the result of a service logs operation.
// It implements [io.ReadCloser].
// It's up to the caller to close the stream.
type ServiceLogsResult struct {
rc io.ReadCloser
close func() error
type ServiceLogsResult interface {
io.ReadCloser
}
// ServiceLogs returns the logs generated by a service in an [ServiceLogsResult].
func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options ServiceLogsOptions) (ServiceLogsResult, error) {
serviceID, err := trimID("service", serviceID)
if err != nil {
return ServiceLogsResult{}, err
return nil, err
}
query := url.Values{}
@@ -50,7 +48,7 @@ func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options Se
if options.Since != "" {
ts, err := timestamp.GetTimestamp(options.Since, time.Now())
if err != nil {
return ServiceLogsResult{}, fmt.Errorf(`invalid value for "since": %w`, err)
return nil, fmt.Errorf(`invalid value for "since": %w`, err)
}
query.Set("since", ts)
}
@@ -70,33 +68,33 @@ func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options Se
resp, err := cli.get(ctx, "/services/"+serviceID+"/logs", query, nil)
if err != nil {
return ServiceLogsResult{}, err
return nil, err
}
return newServiceLogsResult(resp.Body), nil
return &serviceLogsResult{
body: resp.Body,
}, nil
}
func newServiceLogsResult(rc io.ReadCloser) ServiceLogsResult {
if rc == nil {
panic("nil io.ReadCloser")
}
return ServiceLogsResult{
rc: rc,
close: sync.OnceValue(rc.Close),
}
type serviceLogsResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
// Read implements [io.ReadCloser] for LogsResult.
func (r ServiceLogsResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
var (
_ io.ReadCloser = (*serviceLogsResult)(nil)
_ ServiceLogsResult = (*serviceLogsResult)(nil)
)
func (r *serviceLogsResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.rc.Read(p)
return r.body.Read(p)
}
// Close implements [io.ReadCloser] for LogsResult.
func (r ServiceLogsResult) Close() error {
if r.close == nil {
func (r *serviceLogsResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.close()
return r.body.Close()
}

View File

@@ -4,7 +4,6 @@ import (
"context"
"io"
"net/url"
"sync"
"time"
"github.com/moby/moby/client/internal/timestamp"
@@ -24,9 +23,8 @@ type TaskLogsOptions struct {
// TaskLogsResult holds the result of a task logs operation.
// It implements [io.ReadCloser].
type TaskLogsResult struct {
rc io.ReadCloser
close func() error
type TaskLogsResult interface {
io.ReadCloser
}
// TaskLogs returns the logs generated by a task.
@@ -44,7 +42,7 @@ func (cli *Client) TaskLogs(ctx context.Context, taskID string, options TaskLogs
if options.Since != "" {
ts, err := timestamp.GetTimestamp(options.Since, time.Now())
if err != nil {
return TaskLogsResult{}, err
return nil, err
}
query.Set("since", ts)
}
@@ -64,33 +62,33 @@ func (cli *Client) TaskLogs(ctx context.Context, taskID string, options TaskLogs
resp, err := cli.get(ctx, "/tasks/"+taskID+"/logs", query, nil)
if err != nil {
return TaskLogsResult{}, err
return nil, err
}
return newTaskLogsResult(resp.Body), nil
return &taskLogsResult{
body: resp.Body,
}, nil
}
func newTaskLogsResult(rc io.ReadCloser) TaskLogsResult {
if rc == nil {
panic("nil io.ReadCloser")
}
return TaskLogsResult{
rc: rc,
close: sync.OnceValue(rc.Close),
}
type taskLogsResult struct {
// body must be closed to avoid a resource leak
body io.ReadCloser
}
// Read implements [io.ReadCloser] for LogsResult.
func (r TaskLogsResult) Read(p []byte) (n int, err error) {
if r.rc == nil {
var (
_ io.ReadCloser = (*taskLogsResult)(nil)
_ ContainerLogsResult = (*taskLogsResult)(nil)
)
func (r *taskLogsResult) Read(p []byte) (int, error) {
if r == nil || r.body == nil {
return 0, io.EOF
}
return r.rc.Read(p)
return r.body.Read(p)
}
// Close implements [io.ReadCloser] for LogsResult.
func (r TaskLogsResult) Close() error {
if r.close == nil {
func (r *taskLogsResult) Close() error {
if r == nil || r.body == nil {
return nil
}
return r.close()
return r.body.Close()
}