Commit c7a6819a authored by Yann Hodique's avatar Yann Hodique Committed by GitHub

fix(kurtosis-devnet): try harder to find a host ip (#13604)

On Mac and Windows, we should be relatively safe using
host.docker.internal (feature of Docker Desktop)

On Linux, try to find the docker bridge and infer the IP from
there.
parent 1609f070
...@@ -7,8 +7,10 @@ import ( ...@@ -7,8 +7,10 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"net"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"github.com/ethereum-optimism/optimism/kurtosis-devnet/pkg/build" "github.com/ethereum-optimism/optimism/kurtosis-devnet/pkg/build"
"github.com/ethereum-optimism/optimism/kurtosis-devnet/pkg/kurtosis" "github.com/ethereum-optimism/optimism/kurtosis-devnet/pkg/kurtosis"
...@@ -306,6 +308,40 @@ func mainAction(c *cli.Context) error { ...@@ -306,6 +308,40 @@ func mainAction(c *cli.Context) error {
return mainFunc(cfg) return mainFunc(cfg)
} }
func defaultDockerHost() string {
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
// Docker Desktop supports this.
return "host.docker.internal"
}
// On Linux, find docker0 bridge network
ifaces, err := net.Interfaces()
if err != nil {
// we're parsing main flags here, it's ok to panic.
panic(fmt.Errorf("error getting interfaces, consider setting --local-hostname manually: %w", err))
}
for _, iface := range ifaces {
// docker0 is the default bridge network
if iface.Name == "docker0" {
addrs, err := iface.Addrs()
if err != nil || len(addrs) == 0 {
continue
}
// Get the first IP address
ip, _, err := net.ParseCIDR(addrs[0].String())
if err != nil {
continue
}
return ip.String()
}
}
// If we didn't find docker0, we're desperate at this point.
// It might still work if we're on host network mode.
return "localhost"
}
func getFlags() []cli.Flag { func getFlags() []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
...@@ -338,7 +374,7 @@ func getFlags() []cli.Flag { ...@@ -338,7 +374,7 @@ func getFlags() []cli.Flag {
&cli.StringFlag{ &cli.StringFlag{
Name: "local-hostname", Name: "local-hostname",
Usage: "DNS for localhost from Kurtosis perspective (optional)", Usage: "DNS for localhost from Kurtosis perspective (optional)",
Value: "host.docker.internal", Value: defaultDockerHost(),
}, },
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment