Skip to content

Commit

Permalink
fix: use http scheme for vector docker host
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Jul 23, 2024
1 parent 084d99b commit 65ffd18
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
2 changes: 2 additions & 0 deletions internal/functions/deploy/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func GetBindMounts(cwd, hostFuncDir, hostOutputDir, hostEntrypointDir, hostImpor
hostFuncDir += sep
}
dockerFuncDir := utils.ToDockerPath(hostFuncDir)
// TODO: bind ./supabase/functions:/home/deno/functions to hide PII?
binds := []string{
// Reuse deno cache directory, ie. DENO_DIR, between container restarts
// https://denolib.gitbook.io/guide/advanced/deno_dir-code-fetch-and-cache
Expand Down Expand Up @@ -128,6 +129,7 @@ func GetBindMounts(cwd, hostFuncDir, hostOutputDir, hostEntrypointDir, hostImpor
binds = append(binds, hostEntrypointDir+":"+dockerEntrypointDir+":ro")
}
}
// Imports outside of ./supabase/functions will be bound by absolute path
if len(hostImportMapPath) > 0 {
if !filepath.IsAbs(hostImportMapPath) {
hostImportMapPath = filepath.Join(cwd, hostImportMapPath)
Expand Down
1 change: 1 addition & 0 deletions internal/functions/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func populatePerFunctionConfigs(cwd, importMapPath string, noVerifyJWT *bool, fs
}
binds = append(binds, modules...)
fc.ImportMap = utils.ToDockerPath(fc.ImportMap)
fc.Entrypoint = utils.ToDockerPath(fc.Entrypoint)
functionsConfig[slug] = fc
}
functionsConfigBytes, err := json.Marshal(functionsConfig)
Expand Down
37 changes: 25 additions & 12 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
_ "embed"
"fmt"
"net"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -275,27 +277,38 @@ EOF
}); err != nil {
return errors.Errorf("failed to exec template: %w", err)
}
var binds []string
env := []string{
"VECTOR_CONFIG=/etc/vector/vector.yaml",
}
var binds, env []string
// Special case for GitLab pipeline
host := utils.Docker.DaemonHost()
if parsed, err := client.ParseHostURL(host); err == nil && parsed.Scheme == "tcp" {
parsed.Host = "host.docker.internal"
env = append(env, "DOCKER_HOST="+parsed.String())
} else if parsed, err := client.ParseHostURL(client.DefaultDockerHost); err == nil {
if host != client.DefaultDockerHost {
parsed, err := client.ParseHostURL(utils.Docker.DaemonHost())
if err != nil {
return errors.Errorf("failed to parse docker host: %w", err)
}
// Ref: https://vector.dev/docs/reference/configuration/sources/docker_logs/#docker_host
dindHost := url.URL{Scheme: "http", Host: net.JoinHostPort(utils.DinDHost, "2375")}
switch parsed.Scheme {
case "tcp":
if _, port, err := net.SplitHostPort(parsed.Host); err == nil {
dindHost.Host = net.JoinHostPort(utils.DinDHost, port)
}
env = append(env, "DOCKER_HOST="+dindHost.String())
case "npipe":
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "analytics requires docker daemon exposed on tcp://localhost:2375")
env = append(env, "DOCKER_HOST="+dindHost.String())
case "unix":
if parsed, err = client.ParseHostURL(client.DefaultDockerHost); err != nil {
return errors.Errorf("failed to parse default host: %w", err)
}
if utils.Docker.DaemonHost() != client.DefaultDockerHost {
fmt.Fprintln(os.Stderr, utils.Yellow("WARNING:"), "analytics requires mounting default docker socket:", parsed.Host)
}
binds = append(binds, parsed.Host+":/var/run/docker.sock:ro")
binds = append(binds, fmt.Sprintf("%[1]s:%[1]s:ro", parsed.Host))
}
if _, err := utils.DockerStart(
ctx,
container.Config{
Image: utils.Config.Analytics.VectorImage,
Env: env,
Entrypoint: []string{"sh", "-c", `cat <<'EOF' > /etc/vector/vector.yaml && vector
Entrypoint: []string{"sh", "-c", `cat <<'EOF' > /etc/vector/vector.yaml && vector --config /etc/vector/vector.yaml
` + vectorConfigBuf.String() + `
EOF
`},
Expand Down
1 change: 1 addition & 0 deletions internal/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func NewDocker() *client.Client {
}

const (
DinDHost = "host.docker.internal"
CliProjectLabel = "com.supabase.cli.project"
composeProjectLabel = "com.docker.compose.project"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/docker_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package utils
import "github.com/docker/docker/api/types/container"

// Allows containers to resolve host network: https://stackoverflow.com/a/62431165
var extraHosts = []string{"host.docker.internal:host-gateway"}
var extraHosts = []string{DinDHost + ":host-gateway"}

func isUserDefined(mode container.NetworkMode) bool {
return mode.IsUserDefined()
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ func TestSigningJWT(t *testing.T) {
})

t.Run("signs default service_role key", func(t *testing.T) {
anonToken := CustomClaims{Role: "service_role"}.NewToken()
signed, err := anonToken.SignedString([]byte(defaultJwtSecret))
serviceToken := CustomClaims{Role: "service_role"}.NewToken()
signed, err := serviceToken.SignedString([]byte(defaultJwtSecret))
assert.NoError(t, err)
assert.Equal(t, defaultServiceRoleKey, signed)
})
Expand Down

0 comments on commit 65ffd18

Please sign in to comment.