Skip to content

Commit 42b1f50

Browse files
committed
Add udp port
1 parent a8d3445 commit 42b1f50

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

internal/components.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (o *OpNode) Run(service *service, ctx *ExContext) {
8787
"--rpc.port", `{{Port "http" 8549}}`,
8888
"--p2p.listen.ip", "0.0.0.0",
8989
"--p2p.listen.tcp", `{{Port "p2p" 9003}}`,
90-
"--p2p.listen.udp", `{{Port "p2p" 9003}}`,
90+
"--p2p.listen.udp", `{{PortUDP "p2p" 9003}}`,
9191
"--p2p.scoring.peers", "light",
9292
"--p2p.ban.peers", "true",
9393
"--metrics.enabled",
@@ -302,7 +302,7 @@ func (l *LighthouseBeaconNode) Run(svc *service, ctx *ExContext) {
302302
"--disable-peer-scoring",
303303
"--staking",
304304
"--enr-address", "127.0.0.1",
305-
"--enr-udp-port", `{{Port "p2p" 9000}}`,
305+
"--enr-udp-port", `{{PortUDP "p2p" 9000}}`,
306306
"--enr-tcp-port", `{{Port "p2p" 9000}}`,
307307
"--enr-quic-port", `{{Port "quic-p2p" 9100}}`,
308308
"--port", `{{Port "p2p" 9000}}`,

internal/local_runner.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, map[string]string, er
386386
}
387387
}
388388

389-
resolvePort := func(name string, defaultPort int) int {
389+
resolvePort := func(name string, defaultPort int, protocol string) int {
390390
// For {{Port "name" "defaultPort"}}:
391391
// - Service runs on host: return the host port
392392
// - Service runs inside docker: return the docker port
@@ -428,10 +428,10 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, map[string]string, er
428428
}
429429
},
430430
"Port": func(name string, defaultPort int) int {
431-
return resolvePort(name, defaultPort)
431+
return resolvePort(name, defaultPort, ProtocolTCP)
432432
},
433433
"PortUDP": func(name string, defaultPort int) int {
434-
return resolvePort(name, defaultPort)
434+
return resolvePort(name, defaultPort, ProtocolUDP)
435435
},
436436
}
437437

@@ -593,17 +593,18 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
593593
service["entrypoint"] = s.entrypoint
594594
}
595595

596-
fmt.Println("XXX")
597-
598596
if len(s.ports) > 0 {
599597
ports := []string{}
600-
fmt.Println("x", d.bindHostPortsLocally)
601-
602598
for _, p := range s.ports {
599+
protocol := ""
600+
if p.Protocol == ProtocolUDP {
601+
protocol = "/udp"
602+
}
603+
603604
if d.bindHostPortsLocally {
604-
ports = append(ports, fmt.Sprintf("127.0.0.1:%d:%d", p.HostPort, p.Port))
605+
ports = append(ports, fmt.Sprintf("127.0.0.1:%d:%d%s", p.HostPort, p.Port, protocol))
605606
} else {
606-
ports = append(ports, fmt.Sprintf("%d:%d", p.HostPort, p.Port))
607+
ports = append(ports, fmt.Sprintf("%d:%d%s", p.HostPort, p.Port, protocol))
607608
}
608609
}
609610
service["ports"] = ports

internal/manifest.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ func (s *Manifest) Validate() error {
210210
return nil
211211
}
212212

213+
const (
214+
ProtocolUDP = "udp"
215+
ProtocolTCP = "tcp"
216+
)
217+
213218
// Port describes a port that a service exposes
214219
type Port struct {
215220
// Name is the name of the port
@@ -360,9 +365,9 @@ func (s *service) WithTag(tag string) *service {
360365
}
361366

362367
func (s *service) WithPort(name string, portNumber int, protocolVar ...string) *service {
363-
protocol := "tcp"
368+
protocol := ProtocolTCP
364369
if len(protocol) > 0 {
365-
if protocolVar[0] != "tcp" && protocolVar[0] != "udp" {
370+
if protocolVar[0] != ProtocolTCP && protocolVar[0] != ProtocolUDP {
366371
panic(fmt.Sprintf("protocol %s not supported", protocolVar[0]))
367372
}
368373
protocol = protocolVar[0]
@@ -375,10 +380,14 @@ func (s *service) WithPort(name string, portNumber int, protocolVar ...string) *
375380
if p.Port != portNumber {
376381
panic(fmt.Sprintf("port %s already defined with different port number", name))
377382
}
383+
if p.Protocol != protocol {
384+
// If they have different protocols they are different ports
385+
continue
386+
}
378387
return s
379388
}
380389
}
381-
s.ports = append(s.ports, &Port{Name: name, Port: portNumber, Protocol: "tcp"})
390+
s.ports = append(s.ports, &Port{Name: name, Port: portNumber, Protocol: protocol})
382391
return s
383392
}
384393

@@ -456,11 +465,11 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) {
456465
return fmt.Sprintf(`{{Service "%s" "%s"}}`, name, portLabel)
457466
},
458467
"Port": func(name string, defaultPort int) string {
459-
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: "tcp"})
468+
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolTCP})
460469
return fmt.Sprintf(`{{Port "%s" %d}}`, name, defaultPort)
461470
},
462471
"PortUDP": func(name string, defaultPort int) string {
463-
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: "udp"})
472+
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolUDP})
464473
return fmt.Sprintf(`{{PortUDP "%s" %d}}`, name, defaultPort)
465474
},
466475
}

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ func runIt(recipe internal.Recipe) error {
266266

267267
portsStr := []string{}
268268
for _, p := range ports {
269-
portsStr = append(portsStr, fmt.Sprintf("%s: %d/%d", p.Name, p.Port, p.HostPort))
269+
protocol := ""
270+
if p.Protocol == internal.ProtocolUDP {
271+
protocol = "/udp"
272+
}
273+
portsStr = append(portsStr, fmt.Sprintf("%s: %d/%d%s", p.Name, p.Port, p.HostPort, protocol))
270274
}
271275
fmt.Printf("- %s (%s)\n", ss.Name, strings.Join(portsStr, ", "))
272276
}

0 commit comments

Comments
 (0)