From 3981155b20c378a9f766cb13577466bd14058f0d Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Tue, 26 Nov 2024 17:46:15 +0000 Subject: [PATCH] rename client --- cli/clitest/cli.go | 4 ++-- cli/clitest/fake.go | 2 +- cli/docker.go | 4 ++-- dockerutil/client.go | 10 +++++----- dockerutil/container.go | 4 ++-- dockerutil/daemon.go | 2 +- dockerutil/dockerfake/client.go | 2 +- dockerutil/exec.go | 2 +- dockerutil/image.go | 6 +++--- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/cli/clitest/cli.go b/cli/clitest/cli.go index d288d74..fb29dd4 100644 --- a/cli/clitest/cli.go +++ b/cli/clitest/cli.go @@ -35,7 +35,7 @@ func Mounter(ctx context.Context) *mount.FakeMounter { func DockerClient(t *testing.T, ctx context.Context) *dockerfake.MockClient { t.Helper() - client, err := dockerutil.Client(ctx) + client, err := dockerutil.ExtractClient(ctx) require.NoError(t, err) //nolint we should panic if this isn't the case. return client.(*dockerfake.MockClient) @@ -71,7 +71,7 @@ func New(t *testing.T, cmd string, args ...string) (context.Context, *cobra.Comm return ctx, root } -func ctx(t *testing.T, fs xunix.FS, ex xunix.Execer, mnt mount.Interface, client dockerutil.DockerClient) context.Context { +func ctx(t *testing.T, fs xunix.FS, ex xunix.Execer, mnt mount.Interface, client dockerutil.Client) context.Context { t.Helper() ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) diff --git a/cli/clitest/fake.go b/cli/clitest/fake.go index 048b804..61a31ef 100644 --- a/cli/clitest/fake.go +++ b/cli/clitest/fake.go @@ -30,7 +30,7 @@ func NewFakeExecer() *xunixfake.FakeExec { } } -func NewFakeDockerClient() dockerutil.DockerClient { +func NewFakeDockerClient() dockerutil.Client { client := &dockerfake.MockClient{} client.ContainerInspectFn = func(_ context.Context, _ string) (dockertypes.ContainerJSON, error) { diff --git a/cli/docker.go b/cli/docker.go index 9fd436d..f48ea64 100644 --- a/cli/docker.go +++ b/cli/docker.go @@ -243,7 +243,7 @@ func dockerCmd() *cobra.Command { return xerrors.Errorf("wait for sysbox-mgr: %w", err) } - client, err := dockerutil.Client(ctx) + client, err := dockerutil.ExtractClient(ctx) if err != nil { return xerrors.Errorf("new docker client: %w", err) } @@ -387,7 +387,7 @@ func dockerCmd() *cobra.Command { return cmd } -func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.DockerClient, blog buildlog.Logger, flags flags) error { +func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Client, blog buildlog.Logger, flags flags) error { fs := xunix.GetFS(ctx) // Set our OOM score to something really unfavorable to avoid getting killed diff --git a/dockerutil/client.go b/dockerutil/client.go index 6e5d600..4f719c3 100644 --- a/dockerutil/client.go +++ b/dockerutil/client.go @@ -13,7 +13,7 @@ import ( "golang.org/x/xerrors" ) -type DockerClient interface { +type Client interface { dockerclient.SystemAPIClient dockerclient.ContainerAPIClient dockerclient.ImageAPIClient @@ -23,13 +23,13 @@ type clientKey struct{} // WithClient sets the provided DockerClient on the context. // It should only be used for tests. -func WithClient(ctx context.Context, client DockerClient) context.Context { +func WithClient(ctx context.Context, client Client) context.Context { return context.WithValue(ctx, clientKey{}, client) } -// Client returns the DockerClient set on the context. If one can't be +// ExtractClient returns the DockerClient set on the context. If one can't be // found a default client is returned. -func Client(ctx context.Context) (DockerClient, error) { +func ExtractClient(ctx context.Context) (Client, error) { client := ctx.Value(clientKey{}) if client == nil { client, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv) @@ -41,7 +41,7 @@ func Client(ctx context.Context) (DockerClient, error) { } //nolint we should panic if this isn't the case. - return client.(DockerClient), nil + return client.(Client), nil } type AuthConfig registry.AuthConfig diff --git a/dockerutil/container.go b/dockerutil/container.go index 87cabe2..2731cbc 100644 --- a/dockerutil/container.go +++ b/dockerutil/container.go @@ -41,7 +41,7 @@ type ContainerConfig struct { } // CreateContainer creates a sysbox-runc container. -func CreateContainer(ctx context.Context, client DockerClient, conf *ContainerConfig) (string, error) { +func CreateContainer(ctx context.Context, client Client, conf *ContainerConfig) (string, error) { host := &container.HostConfig{ Runtime: runtime, AutoRemove: true, @@ -99,7 +99,7 @@ type BootstrapConfig struct { // BoostrapContainer runs a script inside the container as the provided user. // If conf.Script is empty then it is a noop. -func BootstrapContainer(ctx context.Context, client DockerClient, conf BootstrapConfig) error { +func BootstrapContainer(ctx context.Context, client Client, conf BootstrapConfig) error { if conf.Script == "" { return nil } diff --git a/dockerutil/daemon.go b/dockerutil/daemon.go index 476a866..45a602e 100644 --- a/dockerutil/daemon.go +++ b/dockerutil/daemon.go @@ -9,7 +9,7 @@ import ( // WaitForDaemon waits for a Docker daemon to startup. It waits a max // of 5m before giving up. -func WaitForDaemon(ctx context.Context, client DockerClient) error { +func WaitForDaemon(ctx context.Context, client Client) error { ticker := time.NewTicker(time.Millisecond * 250) defer ticker.Stop() diff --git a/dockerutil/dockerfake/client.go b/dockerutil/dockerfake/client.go index beedf94..cac310f 100644 --- a/dockerutil/dockerfake/client.go +++ b/dockerutil/dockerfake/client.go @@ -19,7 +19,7 @@ import ( "github.com/coder/envbox/dockerutil" ) -var _ dockerutil.DockerClient = MockClient{} +var _ dockerutil.Client = MockClient{} // MockClient provides overrides for functions that are called in envbox. type MockClient struct { diff --git a/dockerutil/exec.go b/dockerutil/exec.go index 6f78822..8f27f47 100644 --- a/dockerutil/exec.go +++ b/dockerutil/exec.go @@ -24,7 +24,7 @@ type ExecConfig struct { // ExecContainer runs a command in a container. It returns the output and any error. // If an error occurs during the execution of the command, the output is appended to the error. -func ExecContainer(ctx context.Context, client DockerClient, config ExecConfig) ([]byte, error) { +func ExecContainer(ctx context.Context, client Client, config ExecConfig) ([]byte, error) { exec, err := client.ContainerExecCreate(ctx, config.ContainerID, dockertypes.ExecConfig{ Detach: true, Cmd: append([]string{config.Cmd}, config.Args...), diff --git a/dockerutil/image.go b/dockerutil/image.go index dc941ad..dc1a0bb 100644 --- a/dockerutil/image.go +++ b/dockerutil/image.go @@ -23,7 +23,7 @@ import ( const diskFullStorageDriver = "vfs" type PullImageConfig struct { - Client DockerClient + Client Client Image string Auth AuthConfig ProgressFn ImagePullProgressFn @@ -107,7 +107,7 @@ func PullImage(ctx context.Context, config *PullImageConfig) error { } // PruneImage runs a simple 'docker prune'. -func PruneImages(ctx context.Context, client DockerClient) (dockertypes.ImagesPruneReport, error) { +func PruneImages(ctx context.Context, client Client) (dockertypes.ImagesPruneReport, error) { report, err := client.ImagesPrune(ctx, filters.NewArgs(filters.Arg("dangling", "false")), ) @@ -156,7 +156,7 @@ type ImageMetadata struct { // GetImageMetadata returns metadata about an image such as the UID/GID of the // provided username and whether it contains an /sbin/init that we should run. -func GetImageMetadata(ctx context.Context, client DockerClient, image, username string) (ImageMetadata, error) { +func GetImageMetadata(ctx context.Context, client Client, image, username string) (ImageMetadata, error) { // Creating a dummy container to inspect the filesystem. created, err := client.ContainerCreate(ctx, &container.Config{