Skip to content

Use custom protocol and user for Connections #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,12 @@ func getPrivKey(privStr string) (*ecdsa.PrivateKey, error) {
return priv, nil
}

func ConnectRaw(service, port, protocol string) string {
return fmt.Sprintf(`{{Service "%s" "%s" "%s"}}`, service, port, protocol)
func ConnectRaw(service, port, protocol, user string) string {
return fmt.Sprintf(`{{Service "%s" "%s" "%s" "%s"}}`, service, port, protocol, user)
}

func Connect(service, port string) string {
return ConnectRaw(service, port, "http")
return ConnectRaw(service, port, "http", "")
}

type output struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (b *BuilderHub) Run(service *Service, ctx *ExContext) {
WithImage("docker.io/flashbots/builder-hub").
WithTag("latest").
WithEntrypoint("/app/builder-hub").
WithEnv("POSTGRES_DSN", "postgres://postgres:postgres@"+ConnectRaw(b.postgres, "postgres", "")+"/postgres?sslmode=disable").
WithEnv("POSTGRES_DSN", ConnectRaw(b.postgres, "postgres", "postgres", "postgres:postgres")+"/postgres?sslmode=disable").
WithEnv("LISTEN_ADDR", "0.0.0.0:"+`{{Port "http" 8080}}`).
WithEnv("ADMIN_ADDR", "0.0.0.0:"+`{{Port "admin" 8081}}`).
WithEnv("INTERNAL_ADDR", "0.0.0.0:"+`{{Port "internal" 8082}}`).
Expand Down
28 changes: 18 additions & 10 deletions internal/local_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,7 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er
}

funcs := template.FuncMap{
"Service": func(name string, portLabel, protocol string) string {
protocolPrefix := ""
if protocol == "http" {
protocolPrefix = "http://"
}

"Service": func(name string, portLabel, protocol, user string) string {
// For {{Service "name" "portLabel"}}:
// - Service runs on host:
// A: target is inside docker: access with localhost:hostPort
Expand All @@ -497,14 +492,14 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er

if d.isHostService(s.Name) {
// A and B
return fmt.Sprintf("%slocalhost:%d", protocolPrefix, port.HostPort)
return printAddr(protocol, "localhost", port.HostPort, user)
} else {
if d.isHostService(svc.Name) {
// D
return fmt.Sprintf("%shost.docker.internal:%d", protocolPrefix, port.HostPort)
return printAddr(protocol, "host.docker.internal", port.HostPort, user)
}
// C
return fmt.Sprintf("%s%s:%d", protocolPrefix, svc.Name, port.Port)
return printAddr(protocol, svc.Name, port.Port, user)
}
},
"Port": func(name string, defaultPort int) int {
Expand Down Expand Up @@ -552,6 +547,19 @@ func (d *LocalRunner) applyTemplate(s *Service) ([]string, map[string]string, er
return argsResult, envs, nil
}

func printAddr(protocol, serviceName string, port int, user string) string {
var protocolPrefix string
if protocol != "" {
protocolPrefix = protocol + "://"
}

if user != "" {
return fmt.Sprintf("%s%s@%s:%s", protocolPrefix, user, serviceName, serviceName)
}

return fmt.Sprintf("%s%s:%d", protocolPrefix, serviceName, port)
}

func (d *LocalRunner) validateImageExists(image string) error {
// check locally
_, err := d.client.ImageInspect(context.Background(), image)
Expand All @@ -570,7 +578,7 @@ func (d *LocalRunner) validateImageExists(image string) error {
return err
}

return fmt.Errorf("image %s not found: %w", image)
return fmt.Errorf("image %s not found", image)
}

func (d *LocalRunner) toDockerComposeService(s *Service) (map[string]interface{}, error) {
Expand Down
8 changes: 5 additions & 3 deletions internal/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ type Port struct {
type NodeRef struct {
Service string
PortLabel string
Protocol string
User string
}

// serviceLogs is a service to access the logs of the running service
Expand Down Expand Up @@ -443,7 +445,7 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) {
// ther can be multiple port and nodere because in the case of op-geth we pass a whole string as nested command args

funcs := template.FuncMap{
"Service": func(name string, portLabel, protocol string) string {
"Service": func(name string, portLabel, protocol, user string) string {
if name == "" {
panic("BUG: service name cannot be empty")
}
Expand All @@ -455,8 +457,8 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) {
// here we only keep the references to the services to be checked if they are valid and an be resolved
// later on for the runtime we will do the resolve stage.
// TODO: this will get easier when we move away from templates and use interface and structs.
nodeRef = append(nodeRef, NodeRef{Service: name, PortLabel: portLabel})
return fmt.Sprintf(`{{Service "%s" "%s"}}`, name, portLabel)
nodeRef = append(nodeRef, NodeRef{Service: name, PortLabel: portLabel, Protocol: protocol, User: user})
return fmt.Sprintf(`{{Service "%s" "%s" "%s" "%s"}}`, name, portLabel, protocol, user)
},
"Port": func(name string, defaultPort int) string {
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolTCP})
Expand Down
58 changes: 58 additions & 0 deletions internal/manifest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package internal

import (
"testing"
)

func TestNodeRefString(t *testing.T) {
var testCases = []struct {
protocol string
service string
port int
user string
expected string
}{
{
protocol: "",
service: "test",
port: 80,
user: "",
expected: "test:80",
},
{
protocol: "",
service: "test",
port: 80,
user: "test",
expected: "test@test:test",
},
{
protocol: "http",
service: "test",
port: 80,
user: "",
expected: "http://test:80",
},
{
protocol: "http",
service: "test",
port: 80,
user: "test",
expected: "http://test@test:test",
},
{
protocol: "enode",
service: "test",
port: 80,
user: "",
expected: "enode://test:80",
},
}

for _, testCase := range testCases {
result := printAddr(testCase.protocol, testCase.service, testCase.port, testCase.user)
if result != testCase.expected {
t.Errorf("expected %s, got %s", testCase.expected, result)
}
}
}