Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions client/client.container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ import (
"fmt"

"github.com/containerd/errdefs"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/client"
)

// ContainerCreate creates a new container.
func (c *sdkClient) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, name string) (container.CreateResponse, error) {
func (c *sdkClient) ContainerCreate(ctx context.Context, options client.ContainerCreateOptions) (client.ContainerCreateResult, error) {
// Add the labels that identify this as a container created by the SDK.
AddSDKLabels(config.Labels)
AddSDKLabels(options.Config.Labels)

return c.APIClient.ContainerCreate(ctx, config, hostConfig, networkingConfig, platform, name)
return c.APIClient.ContainerCreate(ctx, options)
}

// FindContainerByName finds a container by name. The name filter uses a regex to find the containers.
Expand All @@ -27,14 +24,16 @@ func (c *sdkClient) FindContainerByName(ctx context.Context, name string) (*cont
}

// Note that, 'name' filter will use regex to find the containers
filter := filters.NewArgs(filters.Arg("name", fmt.Sprintf("^%s$", name)))
containers, err := c.ContainerList(ctx, container.ListOptions{All: true, Filters: filter})
containers, err := c.ContainerList(ctx, client.ContainerListOptions{
All: true,
Filters: make(client.Filters).Add("name", "^"+name+"$"),
})
if err != nil {
return nil, fmt.Errorf("container list: %w", err)
}

if len(containers) > 0 {
return &containers[0], nil
if len(containers.Items) > 0 {
return &containers.Items[0], nil
}

return nil, errdefs.ErrNotFound.WithMessage(fmt.Sprintf("container %s not found", name))
Expand Down
10 changes: 5 additions & 5 deletions client/client.container_benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"sync"
"testing"

dockerclient "github.com/moby/moby/client"
"github.com/stretchr/testify/require"

"github.com/docker/docker/api/types/container"
"github.com/docker/go-sdk/client"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func BenchmarkContainerList(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := dockerClient.ContainerList(context.Background(), container.ListOptions{All: true})
_, err := dockerClient.ContainerList(context.Background(), dockerclient.ContainerListOptions{All: true})
require.NoError(b, err)
}
})
Expand Down Expand Up @@ -73,16 +73,16 @@ func BenchmarkContainerPause(b *testing.B) {
createContainer(b, dockerClient, img, containerName)

b.Run("container-pause-unpause", func(b *testing.B) {
err = dockerClient.ContainerStart(context.Background(), containerName, container.StartOptions{})
_, err = dockerClient.ContainerStart(context.Background(), containerName, dockerclient.ContainerStartOptions{})
require.NoError(b, err)

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := dockerClient.ContainerPause(context.Background(), containerName)
_, err := dockerClient.ContainerPause(context.Background(), containerName, dockerclient.ContainerPauseOptions{})
require.NoError(b, err)

err = dockerClient.ContainerUnpause(context.Background(), containerName)
_, err = dockerClient.ContainerUnpause(context.Background(), containerName, dockerclient.ContainerUnpauseOptions{})
require.NoError(b, err)
}
})
Expand Down
35 changes: 19 additions & 16 deletions client/client.container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"testing"

"github.com/containerd/errdefs"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/network"
dockerclient "github.com/moby/moby/client"
"github.com/stretchr/testify/require"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/go-connections/nat"
"github.com/docker/go-sdk/client"
)

Expand Down Expand Up @@ -40,10 +40,10 @@ func TestContainerList(t *testing.T) {

wg.Wait()

containers, err := dockerClient.ContainerList(context.Background(), container.ListOptions{All: true})
containers, err := dockerClient.ContainerList(context.Background(), dockerclient.ContainerListOptions{All: true})
require.NoError(t, err)
require.NotEmpty(t, containers)
require.Len(t, containers, max)
require.NotEmpty(t, containers.Items)
require.Len(t, containers.Items, max)
}

func TestFindContainerByName(t *testing.T) {
Expand Down Expand Up @@ -84,39 +84,42 @@ func TestContainerPause(t *testing.T) {
pullImage(t, dockerClient, img)
createContainer(t, dockerClient, img, "nginx-test-pause")

err = dockerClient.ContainerStart(context.Background(), "nginx-test-pause", container.StartOptions{})
_, err = dockerClient.ContainerStart(context.Background(), "nginx-test-pause", dockerclient.ContainerStartOptions{})
require.NoError(t, err)

err = dockerClient.ContainerPause(context.Background(), "nginx-test-pause")
_, err = dockerClient.ContainerPause(context.Background(), "nginx-test-pause", dockerclient.ContainerPauseOptions{})
require.NoError(t, err)

err = dockerClient.ContainerUnpause(context.Background(), "nginx-test-pause")
_, err = dockerClient.ContainerUnpause(context.Background(), "nginx-test-pause", dockerclient.ContainerUnpauseOptions{})
require.NoError(t, err)
}

func createContainer(tb testing.TB, dockerClient client.SDKClient, img string, name string) {
tb.Helper()

resp, err := dockerClient.ContainerCreate(context.Background(), &container.Config{
Image: img,
ExposedPorts: nat.PortSet{
"80/tcp": {},
resp, err := dockerClient.ContainerCreate(context.Background(), dockerclient.ContainerCreateOptions{
Config: &container.Config{
Image: img,
ExposedPorts: network.PortSet{
network.MustParsePort("80/tcp"): {},
},
},
}, nil, nil, nil, name)
Name: name,
})
require.NoError(tb, err)
require.NotNil(tb, resp)
require.NotEmpty(tb, resp.ID)

tb.Cleanup(func() {
err := dockerClient.ContainerRemove(context.Background(), resp.ID, container.RemoveOptions{Force: true})
_, err := dockerClient.ContainerRemove(context.Background(), resp.ID, dockerclient.ContainerRemoveOptions{Force: true})
require.NoError(tb, err)
})
}

func pullImage(tb testing.TB, client client.SDKClient, img string) {
tb.Helper()

r, err := client.ImagePull(context.Background(), img, image.PullOptions{})
r, err := client.ImagePull(context.Background(), img, dockerclient.ImagePullOptions{})
require.NoError(tb, err)
defer r.Close()

Expand Down
7 changes: 4 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"path/filepath"
"time"

"github.com/docker/docker/client"
"github.com/moby/moby/client"

dockercontext "github.com/docker/go-sdk/context"
)

Expand All @@ -34,7 +35,7 @@ var (
return func(c SDKClient) error {
var pingErr error
for i := range 3 {
if _, pingErr = c.Ping(ctx); pingErr == nil {
if _, pingErr = c.Ping(ctx, client.PingOptions{}); pingErr == nil {
return nil
}
select {
Expand Down Expand Up @@ -135,7 +136,7 @@ func (c *sdkClient) init() error {

opts = append(opts, client.WithHTTPHeaders(httpHeaders))

api, err := client.NewClientWithOpts(opts...)
api, err := client.New(opts...)
if err != nil {
return fmt.Errorf("new client: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions client/client.image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"io"

"github.com/docker/docker/api/types/build"
"github.com/moby/moby/client"
)

// ImageBuild builds an image from a build context and options.
func (c *sdkClient) ImageBuild(ctx context.Context, context io.Reader, options build.ImageBuildOptions) (build.ImageBuildResponse, error) {
func (c *sdkClient) ImageBuild(ctx context.Context, context io.Reader, options client.ImageBuildOptions) (client.ImageBuildResult, error) {
// Add client labels
AddSDKLabels(options.Labels)

Expand Down
4 changes: 2 additions & 2 deletions client/client.network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package client
import (
"context"

"github.com/docker/docker/api/types/network"
"github.com/moby/moby/client"
)

// NetworkCreate creates a new network
func (c *sdkClient) NetworkCreate(ctx context.Context, name string, options network.CreateOptions) (network.CreateResponse, error) {
func (c *sdkClient) NetworkCreate(ctx context.Context, name string, options client.NetworkCreateOptions) (client.NetworkCreateResult, error) {
// Add the labels that identify this as a network created by the SDK.
AddSDKLabels(options.Labels)

Expand Down
4 changes: 2 additions & 2 deletions client/client.volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package client
import (
"context"

"github.com/docker/docker/api/types/volume"
"github.com/moby/moby/client"
)

// VolumeCreate creates a new volume.
func (c *sdkClient) VolumeCreate(ctx context.Context, options volume.CreateOptions) (volume.Volume, error) {
func (c *sdkClient) VolumeCreate(ctx context.Context, options client.VolumeCreateOptions) (client.VolumeCreateResult, error) {
// Add the labels that identify this as a volume created by the SDK.
AddSDKLabels(options.Labels)

Expand Down
2 changes: 1 addition & 1 deletion client/client_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"testing"

dockerclient "github.com/moby/moby/client"
"github.com/stretchr/testify/require"

dockerclient "github.com/docker/docker/client"
"github.com/docker/go-sdk/client"
)

Expand Down
10 changes: 5 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"path/filepath"
"testing"

dockerclient "github.com/moby/moby/client"
"github.com/stretchr/testify/require"

dockerclient "github.com/docker/docker/client"
"github.com/docker/go-sdk/client"
dockercontext "github.com/docker/go-sdk/context"
)
Expand All @@ -24,7 +24,7 @@ func TestNew(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cli)

info, err := cli.Info(context.Background())
info, err := cli.Info(context.Background(), dockerclient.InfoOptions{})
require.NoError(t, err)
require.NotNil(t, info)
})
Expand All @@ -34,11 +34,11 @@ func TestNew(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cli)

info1, err := cli.Info(context.Background())
info1, err := cli.Info(context.Background(), dockerclient.InfoOptions{})
require.NoError(t, err)
require.NotNil(t, info1)

info2, err := cli.Info(context.Background())
info2, err := cli.Info(context.Background(), dockerclient.InfoOptions{})
require.NoError(t, err)
require.NotNil(t, info2)

Expand Down Expand Up @@ -99,7 +99,7 @@ func TestNew(t *testing.T) {

infoHealthCheck := func(ctx context.Context) func(c client.SDKClient) error {
return func(c client.SDKClient) error {
_, err := c.Info(ctx)
_, err := c.Info(ctx, dockerclient.InfoOptions{})
return err
}
}
Expand Down
26 changes: 14 additions & 12 deletions client/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package client
import (
"context"
"errors"
"net/netip"
"net/url"
"os"

"github.com/docker/docker/api/types/network"
"github.com/moby/moby/client"
)

// dockerEnvFile is the file that is created when running inside a container.
// It's a variable to allow testing.
var dockerEnvFile = "/.dockerenv"

// DaemonHost gets the host or ip of the Docker daemon where ports are exposed on
// DaemonHostWithContext gets the host or ip of the Docker daemon where ports are exposed on
// Warning: this is based on your Docker host setting. Will fail if using an SSH tunnel
func (c *sdkClient) DaemonHostWithContext(ctx context.Context) (string, error) {
c.mtx.Lock()
Expand All @@ -38,9 +39,10 @@ func (c *sdkClient) daemonHostLocked(ctx context.Context) (string, error) {
if inAContainer(dockerEnvFile) {
ip, err := c.getGatewayIP(ctx, "bridge")
if err != nil {
ip = "localhost"
host = "localhost"
} else {
host = ip.String()
}
host = ip
} else {
host = "localhost"
}
Expand All @@ -51,21 +53,21 @@ func (c *sdkClient) daemonHostLocked(ctx context.Context) (string, error) {
return host, nil
}

func (c *sdkClient) getGatewayIP(ctx context.Context, defaultNetwork string) (string, error) {
nw, err := c.NetworkInspect(ctx, defaultNetwork, network.InspectOptions{})
func (c *sdkClient) getGatewayIP(ctx context.Context, defaultNetwork string) (netip.Addr, error) {
nw, err := c.NetworkInspect(ctx, defaultNetwork, client.NetworkInspectOptions{})
if err != nil {
return "", err
return netip.Addr{}, err
}

var ip string
for _, cfg := range nw.IPAM.Config {
if cfg.Gateway != "" {
var ip netip.Addr
for _, cfg := range nw.Network.IPAM.Config {
if cfg.Gateway.IsValid() {
ip = cfg.Gateway
break
}
}
if ip == "" {
return "", errors.New("failed to get gateway IP from network settings")
if !ip.IsValid() {
return netip.Addr{}, errors.New("failed to get gateway IP from network settings")
}

return ip, nil
Expand Down
6 changes: 4 additions & 2 deletions client/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"

dockerclient "github.com/moby/moby/client"

"github.com/docker/go-sdk/client"
)

Expand All @@ -15,13 +17,13 @@ func ExampleNew() {
return
}

info, err := cli.Info(context.Background())
info, err := cli.Info(context.Background(), dockerclient.InfoOptions{})
if err != nil {
log.Printf("error getting info: %s", err)
return
}

fmt.Println(info.OperatingSystem != "")
fmt.Println(info.Info.OperatingSystem != "")

// Output:
// true
Expand Down
Loading
Loading