GeezerVison is showing github.com — Narrow docker interface (#3650) · knative/func@0073591
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pkg/buildpacks/builder.go +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -261,7 +261,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf | 261 | 261 | // (and update build opts as necessary) | 262 | 262 | if impl == nil { | 263 | 263 | var ( | 264 | | - cli client.APIClient | | 264 | + cli docker.DockerClient | 265 | 265 | dockerHost string | 266 | 266 | ) | 267 | 267 |
| @@ -296,7 +296,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf | 296 | 296 | return | 297 | 297 | } | 298 | 298 |
| 299 | | -func isPodmanV43(ctx context.Context, cli client.APIClient) (b bool, err error) { | | 299 | +func isPodmanV43(ctx context.Context, cli docker.DockerClient) (b bool, err error) { | 300 | 300 | version, err := cli.ServerVersion(ctx, client.ServerVersionOptions{}) | 301 | 301 | if err != nil { | 302 | 302 | return | |
Collapse file Copy file name to clipboardExpand all lines: pkg/docker/close_guarding_client.go +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,13 +2,12 @@ package docker | 2 | 2 |
| 3 | 3 | import ( | 4 | 4 | "sync" | 5 | | - | 6 | | - "github.com/moby/moby/client" | 7 | 5 | ) | 8 | 6 |
| 9 | | -// Client that panics when used after Close() | | 7 | +// closeGuardingClient wraps a DockerClient and panics if any method | | 8 | +// is called after Close(). | 10 | 9 | type closeGuardingClient struct { | 11 | | - pimpl client.APIClient | | 10 | + pimpl DockerClient | 12 | 11 | m sync.RWMutex | 13 | 12 | closed bool | 14 | 13 | } | |
Collapse file Copy file name to clipboardExpand all lines: pkg/docker/docker_client.go +44-9Lines changed: 44 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package docker | 2 | 2 |
| 3 | 3 | import ( | | 4 | + "context" | 4 | 5 | "crypto/tls" | 5 | 6 | "crypto/x509" | 6 | 7 | "encoding/json" | @@ -19,12 +20,45 @@ import ( | 19 | 20 |
| 20 | 21 | "github.com/docker/cli/cli/config" | 21 | 22 | "github.com/docker/go-connections/tlsconfig" | | 23 | + "github.com/google/go-containerregistry/pkg/v1/daemon" | 22 | 24 | "github.com/moby/moby/client" | 23 | 25 | "golang.org/x/crypto/ssh" | 24 | 26 |
| 25 | 27 | fnssh "knative.dev/func/pkg/ssh" | 26 | 28 | ) | 27 | 29 |
| | 30 | +// DockerClient is a subset of client.APIClient with only the methods | | 31 | +// needed by this project and its third-party integrations (pack, s2i). | | 32 | +type DockerClient interface { | | 33 | + daemon.Client // Ping, ImageSave, ImageLoad, ImageTag, ImageInspect, ImageHistory | | 34 | + | | 35 | + ContainerAttach(ctx context.Context, container string, options client.ContainerAttachOptions) (client.ContainerAttachResult, error) | | 36 | + ContainerCommit(ctx context.Context, container string, options client.ContainerCommitOptions) (client.ContainerCommitResult, error) | | 37 | + ContainerCreate(ctx context.Context, options client.ContainerCreateOptions) (client.ContainerCreateResult, error) | | 38 | + ContainerInspect(ctx context.Context, containerID string, options client.ContainerInspectOptions) (client.ContainerInspectResult, error) | | 39 | + ContainerKill(ctx context.Context, containerID string, options client.ContainerKillOptions) (client.ContainerKillResult, error) | | 40 | + ContainerRemove(ctx context.Context, container string, options client.ContainerRemoveOptions) (client.ContainerRemoveResult, error) | | 41 | + ContainerStart(ctx context.Context, container string, options client.ContainerStartOptions) (client.ContainerStartResult, error) | | 42 | + ContainerStop(ctx context.Context, containerID string, options client.ContainerStopOptions) (client.ContainerStopResult, error) | | 43 | + ContainerWait(ctx context.Context, containerID string, options client.ContainerWaitOptions) client.ContainerWaitResult | | 44 | + CopyFromContainer(ctx context.Context, containerID string, options client.CopyFromContainerOptions) (client.CopyFromContainerResult, error) | | 45 | + CopyToContainer(ctx context.Context, container string, options client.CopyToContainerOptions) (client.CopyToContainerResult, error) | | 46 | + | | 47 | + ImageBuild(ctx context.Context, buildContext io.Reader, options client.ImageBuildOptions) (client.ImageBuildResult, error) | | 48 | + ImagePull(ctx context.Context, ref string, options client.ImagePullOptions) (client.ImagePullResponse, error) | | 49 | + ImagePush(ctx context.Context, ref string, options client.ImagePushOptions) (client.ImagePushResponse, error) | | 50 | + ImageRemove(ctx context.Context, image string, options client.ImageRemoveOptions) (client.ImageRemoveResult, error) | | 51 | + | | 52 | + Info(ctx context.Context, options client.InfoOptions) (client.SystemInfoResult, error) | | 53 | + NetworkCreate(ctx context.Context, name string, options client.NetworkCreateOptions) (client.NetworkCreateResult, error) | | 54 | + NetworkRemove(ctx context.Context, networkID string, options client.NetworkRemoveOptions) (client.NetworkRemoveResult, error) | | 55 | + ServerVersion(ctx context.Context, options client.ServerVersionOptions) (client.ServerVersionResult, error) | | 56 | + VolumeList(ctx context.Context, options client.VolumeListOptions) (client.VolumeListResult, error) | | 57 | + VolumeRemove(ctx context.Context, volumeID string, options client.VolumeRemoveOptions) (client.VolumeRemoveResult, error) | | 58 | + | | 59 | + Close() error | | 60 | +} | | 61 | + | 28 | 62 | var ErrNoDocker = errors.New("docker/podman API not available") | 29 | 63 |
| 30 | 64 | // NewClient creates a new docker client. | @@ -35,10 +69,11 @@ var ErrNoDocker = errors.New("docker/podman API not available") | 35 | 69 | // - For TCP connections it returns "" so it defaults in the remote (note that | 36 | 70 | // one should not be use client.DefaultDockerHost in this situation). This is | 37 | 71 | // needed beaus of TCP+tls connections. | 38 | | -func NewClient(defaultHost string) (dockerClient client.APIClient, dockerHostInRemote string, err error) { | | 72 | +func NewClient(defaultHost string) (dc DockerClient, dockerHostInRemote string, err error) { | | 73 | + var rawClient client.APIClient | 39 | 74 | defer func() { | 40 | | - if dockerClient != nil && err == nil { | 41 | | - dockerClient = &closeGuardingClient{pimpl: dockerClient} | | 75 | + if rawClient != nil && err == nil { | | 76 | + dc = &closeGuardingClient{pimpl: rawClient} | 42 | 77 | } | 43 | 78 | }() | 44 | 79 |
| @@ -62,7 +97,7 @@ func NewClient(defaultHost string) (dockerClient client.APIClient, dockerHostInR | 62 | 97 | case os.IsNotExist(err) && podmanPresent(): | 63 | 98 | if runtime.GOOS == "linux" { | 64 | 99 | // on Linux: spawn temporary podman service | 65 | | - dockerClient, dockerHostInRemote, err = newClientWithPodmanService() | | 100 | + rawClient, dockerHostInRemote, err = newClientWithPodmanService() | 66 | 101 | return | 67 | 102 | } else { | 68 | 103 | // on non-Linux: try to use connection to podman machine | @@ -109,7 +144,7 @@ func NewClient(defaultHost string) (dockerClient client.APIClient, dockerHostInR | 109 | 144 | opts = append(opts, client.WithHTTPClient(httpClient)) | 110 | 145 | } | 111 | 146 | } | 112 | | - dockerClient, err = client.New(opts...) | | 147 | + rawClient, err = client.New(opts...) | 113 | 148 | return | 114 | 149 | } | 115 | 150 |
| @@ -133,19 +168,19 @@ func NewClient(defaultHost string) (dockerClient client.APIClient, dockerHostInR | 133 | 168 | }, | 134 | 169 | } | 135 | 170 |
| 136 | | - dockerClient, err = client.New( | | 171 | + rawClient, err = client.New( | 137 | 172 | client.WithHTTPClient(httpClient)) | 138 | 173 |
| 139 | 174 | if closer, ok := contextDialer.(io.Closer); ok { | 140 | | - dockerClient = clientWithAdditionalCleanup{ | 141 | | - APIClient: dockerClient, | | 175 | + rawClient = clientWithAdditionalCleanup{ | | 176 | + APIClient: rawClient, | 142 | 177 | cleanUp: func() { | 143 | 178 | closer.Close() | 144 | 179 | }, | 145 | 180 | } | 146 | 181 | } | 147 | 182 |
| 148 | | - return dockerClient, dockerHostInRemote, err | | 183 | + return dc, dockerHostInRemote, err | 149 | 184 | } | 150 | 185 |
| 151 | 186 | // If the DOCKER_TLS_VERIFY environment variable is set | |
Collapse file Copy file name to clipboardExpand all lines: pkg/docker/runner.go +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,9 +61,9 @@ func (n *Runner) Run(ctx context.Context, f fn.Function, address string, startTi | 61 | 61 | var ( | 62 | 62 | host = DefaultHost | 63 | 63 | port = DefaultPort | 64 | | - c client.APIClient // Docker client | 65 | | - id string // ID of running container | 66 | | - conn net.Conn // Connection to container's stdio | | 64 | + c DockerClient // Docker client | | 65 | + id string // ID of running container | | 66 | + conn net.Conn // Connection to container's stdio | 67 | 67 |
| 68 | 68 | // Channels for gathering runtime errors from the container instance | 69 | 69 | copyErrCh = make(chan error, 10) | @@ -254,7 +254,7 @@ func choosePort(host, preferredPort string, dialTimeout time.Duration) string { | 254 | 254 |
| 255 | 255 | } | 256 | 256 |
| 257 | | -func newContainer(ctx context.Context, c client.APIClient, f fn.Function, host, port string, verbose bool) (id string, err error) { | | 257 | +func newContainer(ctx context.Context, c DockerClient, f fn.Function, host, port string, verbose bool) (id string, err error) { | 258 | 258 | var ( | 259 | 259 | containerCfg container.Config | 260 | 260 | hostCfg container.HostConfig | @@ -322,7 +322,7 @@ func newHostConfig(host, port string) (c container.HostConfig, err error) { | 322 | 322 |
| 323 | 323 | // copy stdin and stdout from the container of the given ID. Errors encountered | 324 | 324 | // during copy are communicated via a provided errs channel. | 325 | | -func copyStdio(ctx context.Context, c client.APIClient, id string, errs chan error, out, errOut io.Writer) (conn net.Conn, err error) { | | 325 | +func copyStdio(ctx context.Context, c DockerClient, id string, errs chan error, out, errOut io.Writer) (conn net.Conn, err error) { | 326 | 326 | res, err := c.ContainerAttach(ctx, id, client.ContainerAttachOptions{ | 327 | 327 | Stdout: true, | 328 | 328 | Stderr: true, | |
|