Skip to content

Commit

Permalink
Revert "finish swapping out DNS with static host config"
Browse files Browse the repository at this point in the history
Trying to bring back Git/HTTP service usage, since Projects uses it, and
it's nice to have a fully integrated API.

This reverts commit 9456f34.

Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
  • Loading branch information
vito committed Jul 18, 2023
1 parent c648629 commit 633e143
Show file tree
Hide file tree
Showing 19 changed files with 307 additions and 401 deletions.
55 changes: 0 additions & 55 deletions cmd/shim/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"fmt"
"io"
"net"
"strings"
)

func ip(args []string) error {
Expand Down Expand Up @@ -53,56 +51,3 @@ func containerIP() (net.IP, error) {

return nil, fmt.Errorf("could not determine container IP (must be in %s)", cidr)
}

func ipExchange() (string, error) {
ip, err := containerIP()
if err != nil {
return "", err
}

l, err := net.Listen("tcp", ip.String()+":0")
if err != nil {
return "", err
}

// print checker's IP so we can pass it to the service for collecting the
// service IP
fmt.Println(l.Addr())

conn, err := l.Accept()
if err != nil {
return "", err
}

svcIPPayload, err := io.ReadAll(conn)
if err != nil {
return "", err
}

svcIP := strings.TrimSpace(string(svcIPPayload))

// print service IP; this is read by the outer health check process and
// stored for passing to clients
fmt.Println(svcIP)

return svcIP, nil
}

func reportIP(addr string) error {
ip, err := containerIP()
if err != nil {
return fmt.Errorf("get container IP: %w", err)
}

conn, err := net.Dial("tcp", addr)
if err != nil {
return fmt.Errorf("dial: %w", err)
}

_, err = fmt.Fprintln(conn, ip.String())
if err != nil {
return fmt.Errorf("write: %w", err)
}

return conn.Close()
}
38 changes: 12 additions & 26 deletions cmd/shim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func internalCommand() int {
args := os.Args[2:]

switch cmd {
case "ip":
if err := ip(args); err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
case "check":
if err := check(args); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand All @@ -90,15 +96,10 @@ func internalCommand() int {

func check(args []string) error {
if len(args) == 0 {
return fmt.Errorf("usage: check port/tcp [port/udp ...]")
return fmt.Errorf("usage: check <host> 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 All @@ -108,14 +109,14 @@ func check(args []string) error {

pollAddr := net.JoinHostPort(host, port)

fmt.Fprintln(os.Stderr, "polling for port", pollAddr)
fmt.Println("polling for port", pollAddr)

reached, err := pollForPort(network, pollAddr)
if err != nil {
return fmt.Errorf("poll %s: %w", pollAddr, err)
}

fmt.Fprintln(os.Stderr, "port is up at", reached)
fmt.Println("port is up at", reached)
}

return nil
Expand Down Expand Up @@ -162,14 +163,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 @@ -399,19 +392,12 @@ func setupBundle() int {
break
}
}

// We're running an internal shim command, i.e. a service health check
for _, env := range spec.Process.Env {
// We're running an internal shim command, i.e. a service health check
if strings.HasPrefix(env, "_DAGGER_INTERNAL_COMMAND=") {
isDaggerExec = true
break
}
// We're running a service, which needs to report its IP to the health
// checker as part of the IP exchange dance
if strings.HasPrefix(env, "_DAGGER_CHECKER_ADDR=") {
isDaggerExec = true
break
}
}

if isDaggerExec {
Expand Down Expand Up @@ -564,7 +550,7 @@ func appendHostAlias(hostsFilePath string, env string, serviceIPs map[string]str

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

hostsFile, err := os.OpenFile(hostsFilePath, os.O_APPEND|os.O_WRONLY, 0o777)
Expand Down
25 changes: 13 additions & 12 deletions core/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,16 +764,12 @@ func (container *Container) WithSecretVariable(ctx context.Context, name string,
return container, nil
}

func (container *Container) Directory(ctx context.Context, gw bkgw.Client, dirPath string, lazy bool) (*Directory, error) {
func (container *Container) Directory(ctx context.Context, gw bkgw.Client, dirPath string) (*Directory, error) {
dir, _, err := locatePath(ctx, container, dirPath, NewDirectory)
if err != nil {
return nil, err
}

if lazy {
return dir, nil
}

// check that the directory actually exists so the user gets an error earlier
// rather than when the dir is used
info, err := dir.Stat(ctx, gw, ".")
Expand All @@ -788,16 +784,12 @@ func (container *Container) Directory(ctx context.Context, gw bkgw.Client, dirPa
return dir, nil
}

func (container *Container) File(ctx context.Context, gw bkgw.Client, filePath string, lazy bool) (*File, error) {
func (container *Container) File(ctx context.Context, gw bkgw.Client, filePath string) (*File, error) {
file, _, err := locatePath(ctx, container, filePath, NewFile)
if err != nil {
return nil, err
}

if lazy {
return file, nil
}

// check that the file actually exists so the user gets an error earlier
// rather than when the file is used
info, err := file.Stat(ctx, gw)
Expand Down Expand Up @@ -1128,11 +1120,20 @@ func (container *Container) WithExec(ctx context.Context, gw bkgw.Client, progSo
llb.SecretAsEnv(true)))
}

metaSt, metaSourcePath := metaMount(opts.Stdin)
// because the shim might run as non-root, we need to make a world-writable
// directory first and then make it the base of the /dagger mount point.
//
// TODO(vito): have the shim exec as the other user instead?
meta := llb.Mkdir(metaSourcePath, 0o777)
if opts.Stdin != "" {
meta = meta.Mkfile(path.Join(metaSourcePath, "stdin"), 0o600, []byte(opts.Stdin))
}

// create /dagger mount point for the shim to write to
runOpts = append(runOpts,
llb.AddMount(metaMountDestPath, metaSt, llb.SourcePath(metaSourcePath)))
llb.AddMount(metaMountDestPath,
llb.Scratch().File(meta, llb.WithCustomName("[internal] creating dagger metadata")),
llb.SourcePath(metaSourcePath)))

if opts.RedirectStdout != "" {
runOpts = append(runOpts, llb.AddEnv("_DAGGER_REDIRECT_STDOUT", opts.RedirectStdout))
Expand Down
5 changes: 1 addition & 4 deletions core/integration/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ func TestGit(t *testing.T) {
}

func TestGitSSHAuthSock(t *testing.T) {
t.Skip("this is hard to test now that Git can't reach services")

t.Parallel()
checkNotDisabled(t, engine.ServicesDNSEnvName)

Expand Down Expand Up @@ -164,8 +162,7 @@ sleep infinity
require.NoError(t, err)

repoURL := fmt.Sprintf("ssh://root@%s:%d/root/repo", sshHost, sshPort)
// XXX(vito):, dagger.GitOpts{ExperimentalServiceHost: sshSvc}).
entries, err := c.Git(repoURL).
entries, err := c.Git(repoURL, dagger.GitOpts{ExperimentalServiceHost: sshSvc}).
Branch("main").
Tree(dagger.GitRefTreeOpts{
SSHKnownHosts: fmt.Sprintf("[%s]:%d %s", sshHost, sshPort, strings.TrimSpace(hostPubKey)),
Expand Down
19 changes: 19 additions & 0 deletions core/integration/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package core
import (
"testing"

"dagger.io/dagger"
"github.com/dagger/dagger/internal/engine"
"github.com/stretchr/testify/require"
)

Expand All @@ -23,3 +25,20 @@ func TestHTTP(t *testing.T) {
require.NoError(t, err)
require.Contains(t, contents, "Dagger")
}

func TestHTTPService(t *testing.T) {
checkNotDisabled(t, engine.ServicesDNSEnvName)

t.Parallel()

c, ctx := connect(t)
defer c.Close()

svc, url := httpService(ctx, t, c, "Hello, world!")

contents, err := c.HTTP(url, dagger.HTTPOpts{
ExperimentalServiceHost: svc,
}).Contents(ctx)
require.NoError(t, err)
require.Equal(t, contents, "Hello, world!")
}
Loading

0 comments on commit 633e143

Please sign in to comment.