GeezerVison is showing github.com — Narrow docker interface by matejvasek · Pull Request #3650 · knative/func
Narrow docker interface
#3650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.go
(4)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
Narrowing docker interface
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit cf61ed592b2f87132a4e219a142996f1fd51290c
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -261,7 +261,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
// (and update build opts as necessary)
if impl == nil {
var (
cli client.APIClient
cli docker.DockerClient
dockerHost string
)
Expand Down
Expand Up
@@ -296,7 +296,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
return
}
func isPodmanV43(ctx context.Context, cli client.APIClient ) (b bool, err error) {
func isPodmanV43(ctx context.Context, cli docker.DockerClient ) (b bool, err error) {
version, err := cli.ServerVersion(ctx, client.ServerVersionOptions{})
if err != nil {
return
Expand Down
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
package docker
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
Expand All
@@ -19,12 +20,45 @@ import (
"github.com/docker/cli/cli/config"
"github.com/docker/go-connections/tlsconfig"
"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/moby/moby/client"
"golang.org/x/crypto/ssh"
fnssh "knative.dev/func/pkg/ssh"
)
// DockerClient is a subset of client.APIClient with only the methods
// needed by this project and its third-party integrations (pack, s2i).
type DockerClient interface {
daemon.Client // Ping, ImageSave, ImageLoad, ImageTag, ImageInspect, ImageHistory
ContainerAttach(ctx context.Context, container string, options client.ContainerAttachOptions) (client.ContainerAttachResult, error)
ContainerCommit(ctx context.Context, container string, options client.ContainerCommitOptions) (client.ContainerCommitResult, error)
ContainerCreate(ctx context.Context, options client.ContainerCreateOptions) (client.ContainerCreateResult, error)
ContainerInspect(ctx context.Context, containerID string, options client.ContainerInspectOptions) (client.ContainerInspectResult, error)
ContainerKill(ctx context.Context, containerID string, options client.ContainerKillOptions) (client.ContainerKillResult, error)
ContainerRemove(ctx context.Context, container string, options client.ContainerRemoveOptions) (client.ContainerRemoveResult, error)
ContainerStart(ctx context.Context, container string, options client.ContainerStartOptions) (client.ContainerStartResult, error)
ContainerStop(ctx context.Context, containerID string, options client.ContainerStopOptions) (client.ContainerStopResult, error)
ContainerWait(ctx context.Context, containerID string, options client.ContainerWaitOptions) client.ContainerWaitResult
CopyFromContainer(ctx context.Context, containerID string, options client.CopyFromContainerOptions) (client.CopyFromContainerResult, error)
CopyToContainer(ctx context.Context, container string, options client.CopyToContainerOptions) (client.CopyToContainerResult, error)
ImageBuild(ctx context.Context, buildContext io.Reader, options client.ImageBuildOptions) (client.ImageBuildResult, error)
ImagePull(ctx context.Context, ref string, options client.ImagePullOptions) (client.ImagePullResponse, error)
ImagePush(ctx context.Context, ref string, options client.ImagePushOptions) (client.ImagePushResponse, error)
ImageRemove(ctx context.Context, image string, options client.ImageRemoveOptions) (client.ImageRemoveResult, error)
Info(ctx context.Context, options client.InfoOptions) (client.SystemInfoResult, error)
NetworkCreate(ctx context.Context, name string, options client.NetworkCreateOptions) (client.NetworkCreateResult, error)
NetworkRemove(ctx context.Context, networkID string, options client.NetworkRemoveOptions) (client.NetworkRemoveResult, error)
ServerVersion(ctx context.Context, options client.ServerVersionOptions) (client.ServerVersionResult, error)
VolumeList(ctx context.Context, options client.VolumeListOptions) (client.VolumeListResult, error)
VolumeRemove(ctx context.Context, volumeID string, options client.VolumeRemoveOptions) (client.VolumeRemoveResult, error)
Close() error
}
var ErrNoDocker = errors.New("docker/podman API not available")
// NewClient creates a new docker client.
Expand All
@@ -35,10 +69,11 @@ var ErrNoDocker = errors.New("docker/podman API not available")
// - For TCP connections it returns "" so it defaults in the remote (note that
// one should not be use client.DefaultDockerHost in this situation). This is
// needed beaus of TCP+tls connections.
func NewClient(defaultHost string) (dockerClient client.APIClient, dockerHostInRemote string, err error) {
func NewClient(defaultHost string) (dc DockerClient, dockerHostInRemote string, err error) {
var rawClient client.APIClient
defer func() {
if dockerClient != nil && err == nil {
dockerClient = &closeGuardingClient{pimpl: dockerClient }
if rawClient != nil && err == nil {
dc = &closeGuardingClient{pimpl: rawClient }
}
}()
Expand All
@@ -62,7 +97,7 @@ func NewClient(defaultHost string) (dockerClient client.APIClient, dockerHostInR
case os.IsNotExist(err) && podmanPresent():
if runtime.GOOS == "linux" {
// on Linux: spawn temporary podman service
dockerClient , dockerHostInRemote, err = newClientWithPodmanService()
rawClient , dockerHostInRemote, err = newClientWithPodmanService()
return
} else {
// on non-Linux: try to use connection to podman machine
Expand Down
Expand Up
@@ -109,7 +144,7 @@ func NewClient(defaultHost string) (dockerClient client.APIClient, dockerHostInR
opts = append(opts, client.WithHTTPClient(httpClient))
}
}
dockerClient , err = client.New(opts...)
rawClient , err = client.New(opts...)
return
}
Expand All
@@ -133,19 +168,19 @@ func NewClient(defaultHost string) (dockerClient client.APIClient, dockerHostInR
},
}
dockerClient , err = client.New(
rawClient , err = client.New(
client.WithHTTPClient(httpClient))
if closer, ok := contextDialer.(io.Closer); ok {
dockerClient = clientWithAdditionalCleanup{
APIClient: dockerClient ,
rawClient = clientWithAdditionalCleanup{
APIClient: rawClient ,
cleanUp: func() {
closer.Close()
},
}
}
return dockerClient , dockerHostInRemote, err
return dc , dockerHostInRemote, err
}
// If the DOCKER_TLS_VERIFY environment variable is set
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -61,9 +61,9 @@ func (n *Runner) Run(ctx context.Context, f fn.Function, address string, startTi
var (
host = DefaultHost
port = DefaultPort
c client.APIClient // Docker client
id string // ID of running container
conn net.Conn // Connection to container's stdio
c DockerClient // Docker client
id string // ID of running container
conn net.Conn // Connection to container's stdio
// Channels for gathering runtime errors from the container instance
copyErrCh = make(chan error, 10)
Expand Down
Expand Up
@@ -254,7 +254,7 @@ func choosePort(host, preferredPort string, dialTimeout time.Duration) string {
}
func newContainer(ctx context.Context, c client.APIClient , f fn.Function, host, port string, verbose bool) (id string, err error) {
func newContainer(ctx context.Context, c DockerClient , f fn.Function, host, port string, verbose bool) (id string, err error) {
var (
containerCfg container.Config
hostCfg container.HostConfig
Expand Down
Expand Up
@@ -322,7 +322,7 @@ func newHostConfig(host, port string) (c container.HostConfig, err error) {
// copy stdin and stdout from the container of the given ID. Errors encountered
// during copy are communicated via a provided errs channel.
func copyStdio(ctx context.Context, c client.APIClient , id string, errs chan error, out, errOut io.Writer) (conn net.Conn, err error) {
func copyStdio(ctx context.Context, c DockerClient , id string, errs chan error, out, errOut io.Writer) (conn net.Conn, err error) {
res, err := c.ContainerAttach(ctx, id, client.ContainerAttachOptions{
Stdout: true,
Stderr: true,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -122,7 +122,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
var client = b.cli
if client == nil {
var c dockerClient.APIClient
var c docker.DockerClient
c, _, err = docker.NewClient(dockerClient.DefaultDockerHost)
if err != nil {
return fmt.Errorf("cannot create docker client: %w", err)
Expand Down
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.