Skip to content

Commit

Permalink
feat: ssh dockerd connection
Browse files Browse the repository at this point in the history
this allows to connect to remote docker deamons via ssh

Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek committed Sep 10, 2021
1 parent bb2f306 commit 41c083e
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 31 deletions.
53 changes: 48 additions & 5 deletions buildpacks/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"io"
"net"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"time"

"github.com/matejvasek/sshdialer"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
Expand Down Expand Up @@ -90,11 +93,51 @@ func (builder *Builder) Build(ctx context.Context, f fn.Function) (err error) {
logWriter = &bytes.Buffer{}
}

var dockerClientOpts = []dockerClient.Opt{
dockerClient.WithAPIVersionNegotiation(),
}

// docker host from `func` binary POW
const defaultDockerHost = "unix:///var/run/docker.sock"

dockerHost := defaultDockerHost
if dh, ok := os.LookupEnv("DOCKER_HOST"); ok {
dockerHost = dh
}
// docker host from build container POW
dockerHostBuildContainer := dockerHost

if _url, err := url.Parse(dockerHost); err == nil && _url != nil && _url.Scheme == "ssh" {
dialer, err := sshdialer.CreateDialContext(_url, "", "")
if err != nil {
return fmt.Errorf("failed to dial ssh connection: %w", err)
}

if _url.Path != "" {
dockerHostBuildContainer = fmt.Sprintf("unix://%s", _url.Path)
} else {
dockerHostBuildContainer = defaultDockerHost
}

httpClient := &http.Client{
// No tls
// No proxy
Transport: &http.Transport{
DialContext: dialer,
},
}

dockerClientOpts = append(dockerClientOpts,
dockerClient.WithHTTPClient(httpClient),
dockerClient.WithHost("http://placeholder"),
dockerClient.WithDialContext(dialer),
)
} else {
dockerClientOpts = append(dockerClientOpts, dockerClient.FromEnv)
}

// Client with a logger which is enabled if in Verbose mode.
dockerClient, err := dockerClient.NewClientWithOpts(
dockerClient.FromEnv,
dockerClient.WithVersion("1.38"),
)
dockerClient, err := dockerClient.NewClientWithOpts(dockerClientOpts...)
if err != nil {
return err
}
Expand All @@ -119,7 +162,7 @@ func (builder *Builder) Build(ctx context.Context, f fn.Function) (err error) {
Builder: packBuilder,
Buildpacks: f.Buildpacks,
TrustBuilder: !deamonIsPodman && strings.HasPrefix(packBuilder, "quay.io/boson"),
DockerHost: os.Getenv("DOCKER_HOST"),
DockerHost: dockerHostBuildContainer,
ContainerConfig: struct {
Network string
Volumes []string
Expand Down
16 changes: 11 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/buildpacks/pack v0.19.0
github.com/cloudevents/sdk-go/v2 v2.4.1
github.com/containers/image/v5 v5.10.6
github.com/docker/docker v20.10.7+incompatible
github.com/docker/docker v20.10.8+incompatible
github.com/docker/docker-credential-helpers v0.6.4
github.com/docker/go-connections v0.4.0
github.com/go-git/go-billy/v5 v5.3.1
Expand All @@ -18,20 +18,26 @@ require (
github.com/google/uuid v1.2.0
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c
github.com/markbates/pkger v0.17.1
github.com/matejvasek/sshdialer v0.0.0-20210910025520-fddb287f1031
github.com/mitchellh/go-homedir v1.1.0
github.com/opencontainers/image-spec v1.0.2-0.20190823105129-775207bd45b6
github.com/ory/viper v1.7.5
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.3
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.19.7
k8s.io/apimachinery v0.19.7
k8s.io/client-go v0.19.7
k8s.io/api v0.20.6
k8s.io/apimachinery v0.20.6
k8s.io/client-go v0.20.6
knative.dev/client v0.23.2
knative.dev/eventing v0.23.3
knative.dev/pkg v0.0.0-20210510175900-4564797bf3b7
knative.dev/serving v0.23.1
)

// knative.dev/[email protected] and knative.dev/[email protected] require different versions of go-openapi/spec
replace github.com/go-openapi/spec => github.com/go-openapi/spec v0.19.6
replace (
github.com/go-openapi/spec => github.com/go-openapi/spec v0.19.6
k8s.io/api => k8s.io/api v0.19.7
k8s.io/apimachinery => k8s.io/apimachinery v0.19.7
k8s.io/client-go => k8s.io/client-go v0.19.7
)
Loading

0 comments on commit 41c083e

Please sign in to comment.