Skip to content

Commit

Permalink
wip: start bringing DNS back
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Suraci <alex@dagger.io>
  • Loading branch information
vito committed Jul 18, 2023
1 parent 7e26a8d commit cd28433
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 353 deletions.
108 changes: 0 additions & 108 deletions cmd/shim/ip.go

This file was deleted.

98 changes: 41 additions & 57 deletions cmd/shim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -93,12 +94,7 @@ func check(args []string) error {
return fmt.Errorf("usage: check port/tcp [port/udp ...]")
}

host, err := ipExchange()
if err != nil {
return fmt.Errorf("exchange IPs: %w", err)
}

ports := args
host, ports := args[0], args[1:]

for _, port := range ports {
port, network, ok := strings.Cut(port, "/")
Expand Down Expand Up @@ -162,14 +158,6 @@ func shim() int {
return 1
}

checkerAddr, found := internalEnv("_DAGGER_CHECKER_ADDR")
if found {
if err := reportIP(checkerAddr); err != nil {
fmt.Fprintln(os.Stderr, "report container IP:", err)
return 1
}
}

name := os.Args[1]
args := []string{}
if len(os.Args) > 2 {
Expand Down Expand Up @@ -437,26 +425,45 @@ func setupBundle() int {
spec.Process.Args = append([]string{shimPath}, spec.Process.Args...)
}

// collect service IPs first so they can be used for service aliases below
var searchDomain string
for _, env := range spec.Process.Env {
if strings.HasPrefix(env, "_DAGGER_SEARCH_DOMAIN=") {
_, val, _ := strings.Cut(env, "=")
searchDomain = val
}
}

log.Println("!!! SEARCH DOMAIN", searchDomain)
if searchDomain == "" {
for _, env := range spec.Process.Env {
log.Println("!!! NO SEARCH DOMAIN", env)
}
}
log.Println("!!! BUNDLE DIR", bundleDir)

var hostsFilePath string
var resolvFilePath string
for _, mnt := range spec.Mounts {
if mnt.Destination == "/etc/hosts" {
hostsFilePath = mnt.Source
log.Println("!!! HOST FILE PATH", hostsFilePath)
}
if mnt.Destination == "/etc/resolv.conf" {
resolvFilePath = mnt.Source
}
}

// collect service IPs first so they can be used for service aliases below
serviceIPs := map[string]string{}
for _, env := range spec.Process.Env {
if strings.HasPrefix(env, "_DAGGER_SERVICES=") {
_, val, _ := strings.Cut(env, "=")
pairs := strings.Split(val, ";")
for _, pair := range pairs {
host, ip, ok := strings.Cut(pair, ":")
if ok {
serviceIPs[host] = ip
}
}
if resolvFilePath != "" {
log.Println("!!! RESOLV FILE PATH", resolvFilePath)
cat := exec.Command("cat", resolvFilePath)
cat.Stdout = os.Stdout
cat.Stderr = os.Stderr
if err := cat.Run(); err != nil {
panic(err)
}
} else {
log.Println("!!! NO RESOLV FILE PATH")
}

keepEnv := []string{}
Expand All @@ -473,19 +480,14 @@ func setupBundle() int {
Options: []string{"rbind"},
Source: "/run/buildkit/buildkitd.sock",
})
case strings.HasPrefix(env, "_DAGGER_SERVICES="):
case strings.HasPrefix(env, "_DAGGER_SEARCH_DOMAIN="):
// NB: don't keep this env var, it's only for the bundling step
// keepEnv = append(keepEnv, env)

if err := appendServiceHosts(hostsFilePath, env); err != nil {
fmt.Fprintln(os.Stderr, "append service hosts:", err)
return 1
}
case strings.HasPrefix(env, aliasPrefix):
// NB: don't keep this env var, it's only for the bundling step
// keepEnv = append(keepEnv, env)

if err := appendHostAlias(hostsFilePath, env, serviceIPs); err != nil {
if err := appendHostAlias(hostsFilePath, env, searchDomain); err != nil {
fmt.Fprintln(os.Stderr, "host alias:", err)
return 1
}
Expand Down Expand Up @@ -556,46 +558,28 @@ func setupBundle() int {

const aliasPrefix = "_DAGGER_HOSTNAME_ALIAS_"

func appendHostAlias(hostsFilePath string, env string, serviceIPs map[string]string) error {
func appendHostAlias(hostsFilePath string, env string, searchDomain string) error {
alias, target, ok := strings.Cut(strings.TrimPrefix(env, aliasPrefix), "=")
if !ok {
return fmt.Errorf("malformed host alias: %s", env)
}

ip, found := serviceIPs[target]
if !found {
return fmt.Errorf("service %s not found in _DAGGER_SERVICES: %v", target, serviceIPs)
if searchDomain != "" {
target += "." + searchDomain
}

hostsFile, err := os.OpenFile(hostsFilePath, os.O_APPEND|os.O_WRONLY, 0o777)
ip, err := net.LookupIP(target)
if err != nil {
return err
}

if _, err := fmt.Fprintf(hostsFile, "\n%s\t%s\n", ip, alias); err != nil {
return err
}

return hostsFile.Close()
}

func appendServiceHosts(hostsFilePath string, env string) error {
hostsFile, err := os.OpenFile(hostsFilePath, os.O_APPEND|os.O_WRONLY, 0o777)
if err != nil {
return err
}

_, val, _ := strings.Cut(env, "=")
pairs := strings.Split(val, ";")
for _, pair := range pairs {
host, ip, ok := strings.Cut(pair, ":")
if !ok {
return fmt.Errorf("malformed host:ip pair: %q", pair)
}

if _, err := fmt.Fprintf(hostsFile, "\n%s\t%s\n", ip, host); err != nil {
return err
}
if _, err := fmt.Fprintf(hostsFile, "\n%s\t%s\n", ip, alias); err != nil {
return err
}

return hostsFile.Close()
Expand Down
16 changes: 9 additions & 7 deletions core/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,13 +1096,10 @@ func (container *Container) WithExec(ctx context.Context, gw bkgw.Client, progSo
)
}

if len(container.Services) > 0 {
o, err := container.Services.RunOpt()
if err != nil {
return nil, err
}
runOpts = append(runOpts, o)
}
runOpts = append(runOpts, llb.AddSecret(
"_DAGGER_SEARCH_DOMAIN",
llb.SecretID(ServicesSearchDomainSecret),
llb.SecretAsEnv(true)))

metaSt, metaSourcePath := metaMount(opts.Stdin)

Expand Down Expand Up @@ -1841,9 +1838,14 @@ func hostHash(val digest.Digest) string {
if err != nil {
panic(err)
}

return strings.ToLower(b32(xxh3.Hash(b)))
}

func hostHashStr(val string) string {
return strings.ToLower(b32(xxh3.HashString(val)))
}

func b32(n uint64) string {
var sum [8]byte
binary.BigEndian.PutUint64(sum[:], n)
Expand Down
11 changes: 6 additions & 5 deletions core/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,12 @@ func wrapSolveError(inputErr *error, gw bkgw.Client) {
Args: execOp.Exec.Meta.Args,
// the magic env var is interpreted by the shim, telling it to just output
// the stdout/stderr contents rather than actually execute anything.
Env: append(execOp.Exec.Meta.Env, DebugFailedExecEnv+"=1"),
User: execOp.Exec.Meta.User,
Cwd: execOp.Exec.Meta.Cwd,
Stdout: &nopCloser{ctrOut},
Stderr: &nopCloser{ctrErr},
Env: append(execOp.Exec.Meta.Env, DebugFailedExecEnv+"=1"),
User: execOp.Exec.Meta.User,
Cwd: execOp.Exec.Meta.Cwd,
SecretEnv: execOp.Exec.Secretenv,
Stdout: &nopCloser{ctrOut},
Stderr: &nopCloser{ctrErr},
})
if err != nil {
return
Expand Down
25 changes: 0 additions & 25 deletions core/schema/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,6 @@ func (s *gitSchema) tree(ctx *router.Context, parent gitRef, args gitTreeArgs) (
opts = append(opts, llb.MountSSHSock(args.SSHAuthSocket.LLBID()))
}

hosts := llb.ExtraHosts{}
if parent.Repository.ServiceHost != nil {
svc, err := parent.Repository.ServiceHost.ToService()
if err != nil {
return nil, err
}

running, err := core.AllServices.Start(ctx, s.gw, svc)
if err != nil {
return nil, err
}

host, err := svc.Hostname()
if err != nil {
return nil, err
}

hosts = append(hosts, llb.HostIP{
Host: host,
IP: running.IP,
})

opts = append(opts, hosts)
}

var svcs core.ServiceBindings
if parent.Repository.ServiceHost != nil {
svcs = core.ServiceBindings{*parent.Repository.ServiceHost: nil}
Expand Down
25 changes: 1 addition & 24 deletions core/schema/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,12 @@ type httpArgs struct {
func (s *httpSchema) http(ctx *router.Context, parent *core.Query, args httpArgs) (*core.File, error) {
pipeline := parent.PipelinePath()

hosts := llb.ExtraHosts{}
if args.ExperimentalServiceHost != nil {
svc, err := args.ExperimentalServiceHost.ToService()
if err != nil {
return nil, err
}

running, err := core.AllServices.Start(ctx, s.gw, svc)
if err != nil {
return nil, err
}

host, err := svc.Hostname()
if err != nil {
return nil, err
}

hosts = append(hosts, llb.HostIP{
Host: host,
IP: running.IP,
})
}

// Use a filename that is set to the URL. Buildkit internally stores some cache metadata of etags
// and http checksums using an id based on this name, so setting it to the URL maximizes our chances
// of following more optimized cache codepaths.
// Do a hash encode to prevent conflicts with use of `/` in the URL while also not hitting max filename limits
filename := digest.FromString(args.URL).Encoded()
st := llb.HTTP(args.URL, llb.Filename(filename), hosts)
st := llb.HTTP(args.URL, llb.Filename(filename))

svcs := core.ServiceBindings{}
if args.ExperimentalServiceHost != nil {
Expand Down
Loading

0 comments on commit cd28433

Please sign in to comment.