Skip to content

Commit

Permalink
api/types: move ContainerLogsOptions to api/types/container
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Oct 12, 2023
1 parent 9670d93 commit ebef4ef
Show file tree
Hide file tree
Showing 30 changed files with 101 additions and 91 deletions.
4 changes: 2 additions & 2 deletions api/server/httputils/write_log_stream.go
Expand Up @@ -7,16 +7,16 @@ import (
"net/url"
"sort"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
)

// WriteLogStream writes an encoded byte stream of log messages from the
// messages channel, multiplexing them with a stdcopy.Writer if mux is true
func WriteLogStream(_ context.Context, w io.Writer, msgs <-chan *backend.LogMessage, config *types.ContainerLogsOptions, mux bool) {
func WriteLogStream(_ context.Context, w io.Writer, msgs <-chan *backend.LogMessage, config *container.LogsOptions, mux bool) {
wf := ioutils.NewWriteFlusher(w)
defer wf.Close()

Expand Down
2 changes: 1 addition & 1 deletion api/server/router/container/backend.go
Expand Up @@ -50,7 +50,7 @@ type stateBackend interface {
type monitorBackend interface {
ContainerChanges(ctx context.Context, name string) ([]archive.Change, error)
ContainerInspect(ctx context.Context, name string, size bool, version string) (interface{}, error)
ContainerLogs(ctx context.Context, name string, config *types.ContainerLogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
ContainerLogs(ctx context.Context, name string, config *container.LogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
ContainerStats(ctx context.Context, name string, config *backend.ContainerStatsConfig) error
ContainerTop(name string, psArgs string) (*container.ContainerTopOKBody, error)
Containers(ctx context.Context, config *container.ListOptions) ([]*types.Container, error)
Expand Down
2 changes: 1 addition & 1 deletion api/server/router/container/container_routes.go
Expand Up @@ -142,7 +142,7 @@ func (s *containerRouter) getContainersLogs(ctx context.Context, w http.Response
}

containerName := vars["name"]
logsConfig := &types.ContainerLogsOptions{
logsConfig := &container.LogsOptions{
Follow: httputils.BoolValue(r, "follow"),
Timestamps: httputils.BoolValue(r, "timestamps"),
Since: r.Form.Get("since"),
Expand Down
3 changes: 2 additions & 1 deletion api/server/router/swarm/backend.go
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/swarm"
)

Expand All @@ -22,7 +23,7 @@ type Backend interface {
CreateService(swarm.ServiceSpec, string, bool) (*swarm.ServiceCreateResponse, error)
UpdateService(string, uint64, swarm.ServiceSpec, types.ServiceUpdateOptions, bool) (*swarm.ServiceUpdateResponse, error)
RemoveService(string) error
ServiceLogs(context.Context, *backend.LogSelector, *types.ContainerLogsOptions) (<-chan *backend.LogMessage, error)
ServiceLogs(context.Context, *backend.LogSelector, *container.LogsOptions) (<-chan *backend.LogMessage, error)
GetNodes(types.NodeListOptions) ([]swarm.Node, error)
GetNode(string) (swarm.Node, error)
UpdateNode(string, uint64, swarm.NodeSpec) error
Expand Down
5 changes: 3 additions & 2 deletions api/server/router/swarm/helpers.go
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/docker/api/server/httputils"
basictypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/versions"
)
Expand All @@ -25,9 +26,9 @@ func (sr *swarmRouter) swarmLogs(ctx context.Context, w http.ResponseWriter, r *
return fmt.Errorf("Bad parameters: you must choose at least one stream")
}

// there is probably a neater way to manufacture the ContainerLogsOptions
// there is probably a neater way to manufacture the LogsOptions
// struct, probably in the caller, to eliminate the dependency on net/http
logsConfig := &basictypes.ContainerLogsOptions{
logsConfig := &container.LogsOptions{
Follow: httputils.BoolValue(r, "follow"),
Timestamps: httputils.BoolValue(r, "timestamps"),
Since: r.Form.Get("since"),
Expand Down
12 changes: 0 additions & 12 deletions api/types/client.go
Expand Up @@ -20,18 +20,6 @@ type ContainerExecInspect struct {
Pid int
}

// ContainerLogsOptions holds parameters to filter logs with.
type ContainerLogsOptions struct {
ShowStdout bool
ShowStderr bool
Since string
Until string
Timestamps bool
Follow bool
Tail string
Details bool
}

// CopyToContainerOptions holds information
// about files to copy into a container
type CopyToContainerOptions struct {
Expand Down
12 changes: 12 additions & 0 deletions api/types/container/options.go
Expand Up @@ -53,3 +53,15 @@ type ListOptions struct {
Limit int
Filters filters.Args
}

// LogsOptions holds parameters to filter logs with.
type LogsOptions struct {
ShowStdout bool
ShowStderr bool
Since string
Until string
Timestamps bool
Follow bool
Tail string
Details bool
}
5 changes: 5 additions & 0 deletions api/types/types_deprecated.go
Expand Up @@ -119,6 +119,11 @@ type ContainerCommitOptions = container.CommitOptions
// Deprecated: use [container.ListOptions].
type ContainerListOptions = container.ListOptions

// ContainerLogsOptions holds parameters to filter logs with.
//
// Deprecated: use [container.LogsOptions].
type ContainerLogsOptions = container.LogsOptions

// ContainerRemoveOptions holds parameters to remove containers.
//
// Deprecated: use [container.RemoveOptions].
Expand Down
4 changes: 2 additions & 2 deletions client/container_logs.go
Expand Up @@ -6,7 +6,7 @@ import (
"net/url"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
timetypes "github.com/docker/docker/api/types/time"
"github.com/pkg/errors"
)
Expand All @@ -33,7 +33,7 @@ import (
//
// You can use github.com/docker/docker/pkg/stdcopy.StdCopy to demultiplex this
// stream.
func (cli *Client) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
func (cli *Client) ContainerLogs(ctx context.Context, container string, options container.LogsOptions) (io.ReadCloser, error) {
query := url.Values{}
if options.ShowStdout {
query.Set("stdout", "1")
Expand Down
26 changes: 13 additions & 13 deletions client/container_logs_test.go
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
Expand All @@ -22,22 +22,22 @@ func TestContainerLogsNotFoundError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusNotFound, "Not found")),
}
_, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
_, err := client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{})
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
}

func TestContainerLogsError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{})
_, err := client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{})
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))

_, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
_, err = client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{
Since: "2006-01-02TZ",
})
assert.Check(t, is.ErrorContains(err, `parsing time "2006-01-02TZ"`))
_, err = client.ContainerLogs(context.Background(), "container_id", types.ContainerLogsOptions{
_, err = client.ContainerLogs(context.Background(), "container_id", container.LogsOptions{
Until: "2006-01-02TZ",
})
assert.Check(t, is.ErrorContains(err, `parsing time "2006-01-02TZ"`))
Expand All @@ -46,7 +46,7 @@ func TestContainerLogsError(t *testing.T) {
func TestContainerLogs(t *testing.T) {
expectedURL := "/containers/container_id/logs"
cases := []struct {
options types.ContainerLogsOptions
options container.LogsOptions
expectedQueryParams map[string]string
expectedError string
}{
Expand All @@ -56,15 +56,15 @@ func TestContainerLogs(t *testing.T) {
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
Tail: "any",
},
expectedQueryParams: map[string]string{
"tail": "any",
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
Timestamps: true,
Expand All @@ -81,7 +81,7 @@ func TestContainerLogs(t *testing.T) {
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
// timestamp will be passed as is
Since: "1136073600.000000001",
},
Expand All @@ -91,7 +91,7 @@ func TestContainerLogs(t *testing.T) {
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
// timestamp will be passed as is
Until: "1136073600.000000001",
},
Expand All @@ -101,14 +101,14 @@ func TestContainerLogs(t *testing.T) {
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
// An complete invalid date will not be passed
Since: "invalid value",
},
expectedError: `invalid value for "since": failed to parse value as time or duration: "invalid value"`,
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
// An complete invalid date will not be passed
Until: "invalid value",
},
Expand Down Expand Up @@ -153,7 +153,7 @@ func ExampleClient_ContainerLogs_withTimeout() {
defer cancel()

client, _ := NewClientWithOpts(FromEnv)
reader, err := client.ContainerLogs(ctx, "container_id", types.ContainerLogsOptions{})
reader, err := client.ContainerLogs(ctx, "container_id", container.LogsOptions{})
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions client/interface.go
Expand Up @@ -60,7 +60,7 @@ type ContainerAPIClient interface {
ContainerInspectWithRaw(ctx context.Context, container string, getSize bool) (types.ContainerJSON, []byte, error)
ContainerKill(ctx context.Context, container, signal string) error
ContainerList(ctx context.Context, options container.ListOptions) ([]types.Container, error)
ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error)
ContainerLogs(ctx context.Context, container string, options container.LogsOptions) (io.ReadCloser, error)
ContainerPause(ctx context.Context, container string) error
ContainerRemove(ctx context.Context, container string, options container.RemoveOptions) error
ContainerRename(ctx context.Context, container, newContainerName string) error
Expand Down Expand Up @@ -146,8 +146,8 @@ type ServiceAPIClient interface {
ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error)
ServiceRemove(ctx context.Context, serviceID string) error
ServiceUpdate(ctx context.Context, serviceID string, version swarm.Version, service swarm.ServiceSpec, options types.ServiceUpdateOptions) (swarm.ServiceUpdateResponse, error)
ServiceLogs(ctx context.Context, serviceID string, options types.ContainerLogsOptions) (io.ReadCloser, error)
TaskLogs(ctx context.Context, taskID string, options types.ContainerLogsOptions) (io.ReadCloser, error)
ServiceLogs(ctx context.Context, serviceID string, options container.LogsOptions) (io.ReadCloser, error)
TaskLogs(ctx context.Context, taskID string, options container.LogsOptions) (io.ReadCloser, error)
TaskInspectWithRaw(ctx context.Context, taskID string) (swarm.Task, []byte, error)
TaskList(ctx context.Context, options types.TaskListOptions) ([]swarm.Task, error)
}
Expand Down
4 changes: 2 additions & 2 deletions client/service_logs.go
Expand Up @@ -6,14 +6,14 @@ import (
"net/url"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
timetypes "github.com/docker/docker/api/types/time"
"github.com/pkg/errors"
)

// ServiceLogs returns the logs generated by a service in an io.ReadCloser.
// It's up to the caller to close the stream.
func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
func (cli *Client) ServiceLogs(ctx context.Context, serviceID string, options container.LogsOptions) (io.ReadCloser, error) {
query := url.Values{}
if options.ShowStdout {
query.Set("stdout", "1")
Expand Down
18 changes: 9 additions & 9 deletions client/service_logs_test.go
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
Expand All @@ -22,10 +22,10 @@ func TestServiceLogsError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ServiceLogs(context.Background(), "service_id", types.ContainerLogsOptions{})
_, err := client.ServiceLogs(context.Background(), "service_id", container.LogsOptions{})
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))

_, err = client.ServiceLogs(context.Background(), "service_id", types.ContainerLogsOptions{
_, err = client.ServiceLogs(context.Background(), "service_id", container.LogsOptions{
Since: "2006-01-02TZ",
})
assert.Check(t, is.ErrorContains(err, `parsing time "2006-01-02TZ"`))
Expand All @@ -34,7 +34,7 @@ func TestServiceLogsError(t *testing.T) {
func TestServiceLogs(t *testing.T) {
expectedURL := "/services/service_id/logs"
cases := []struct {
options types.ContainerLogsOptions
options container.LogsOptions
expectedQueryParams map[string]string
expectedError string
}{
Expand All @@ -44,15 +44,15 @@ func TestServiceLogs(t *testing.T) {
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
Tail: "any",
},
expectedQueryParams: map[string]string{
"tail": "any",
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
Timestamps: true,
Expand All @@ -69,7 +69,7 @@ func TestServiceLogs(t *testing.T) {
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
// timestamp will be passed as is
Since: "1136073600.000000001",
},
Expand All @@ -79,7 +79,7 @@ func TestServiceLogs(t *testing.T) {
},
},
{
options: types.ContainerLogsOptions{
options: container.LogsOptions{
// An complete invalid date will not be passed
Since: "invalid value",
},
Expand Down Expand Up @@ -124,7 +124,7 @@ func ExampleClient_ServiceLogs_withTimeout() {
defer cancel()

client, _ := NewClientWithOpts(FromEnv)
reader, err := client.ServiceLogs(ctx, "service_id", types.ContainerLogsOptions{})
reader, err := client.ServiceLogs(ctx, "service_id", container.LogsOptions{})
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions client/task_logs.go
Expand Up @@ -6,13 +6,13 @@ import (
"net/url"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
timetypes "github.com/docker/docker/api/types/time"
)

// TaskLogs returns the logs generated by a task in an io.ReadCloser.
// It's up to the caller to close the stream.
func (cli *Client) TaskLogs(ctx context.Context, taskID string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
func (cli *Client) TaskLogs(ctx context.Context, taskID string, options container.LogsOptions) (io.ReadCloser, error) {
query := url.Values{}
if options.ShowStdout {
query.Set("stdout", "1")
Expand Down
2 changes: 1 addition & 1 deletion daemon/cluster/executor/backend.go
Expand Up @@ -41,7 +41,7 @@ type Backend interface {
CreateManagedContainer(ctx context.Context, config types.ContainerCreateConfig) (container.CreateResponse, error)
ContainerStart(ctx context.Context, name string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error
ContainerStop(ctx context.Context, name string, config container.StopOptions) error
ContainerLogs(ctx context.Context, name string, config *types.ContainerLogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
ContainerLogs(ctx context.Context, name string, config *container.LogsOptions) (msgs <-chan *backend.LogMessage, tty bool, err error)
ConnectContainerToNetwork(containerName, networkName string, endpointConfig *network.EndpointSettings) error
ActivateContainerServiceBinding(containerName string) error
DeactivateContainerServiceBinding(containerName string) error
Expand Down
2 changes: 1 addition & 1 deletion daemon/cluster/executor/container/adapter.go
Expand Up @@ -489,7 +489,7 @@ func (c *containerAdapter) deactivateServiceBinding() error {
}

func (c *containerAdapter) logs(ctx context.Context, options api.LogSubscriptionOptions) (<-chan *backend.LogMessage, error) {
apiOptions := &types.ContainerLogsOptions{
apiOptions := &containertypes.LogsOptions{
Follow: options.Follow,

// Always say yes to Timestamps and Details. we make the decision
Expand Down

0 comments on commit ebef4ef

Please sign in to comment.