Skip to content

Commit 6ed42e9

Browse files
authored
Bind UDP ports (#107)
* UDP Port * Add udp port
1 parent 0b28efe commit 6ed42e9

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
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: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, map[string]string, er
386386
}
387387
}
388388

389+
resolvePort := func(name string, defaultPort int, protocol string) int {
390+
// For {{Port "name" "defaultPort"}}:
391+
// - Service runs on host: return the host port
392+
// - Service runs inside docker: return the docker port
393+
if d.isHostService(s.Name) {
394+
return s.MustGetPort(name).HostPort
395+
}
396+
return defaultPort
397+
}
398+
389399
funcs := template.FuncMap{
390400
"Service": func(name string, portLabel, protocol string) string {
391401
protocolPrefix := ""
@@ -418,13 +428,10 @@ func (d *LocalRunner) applyTemplate(s *service) ([]string, map[string]string, er
418428
}
419429
},
420430
"Port": func(name string, defaultPort int) int {
421-
// For {{Port "name" "defaultPort"}}:
422-
// - Service runs on host: return the host port
423-
// - Service runs inside docker: return the docker port
424-
if d.isHostService(s.Name) {
425-
return s.MustGetPort(name).HostPort
426-
}
427-
return defaultPort
431+
return resolvePort(name, defaultPort, ProtocolTCP)
432+
},
433+
"PortUDP": func(name string, defaultPort int) int {
434+
return resolvePort(name, defaultPort, ProtocolUDP)
428435
},
429436
}
430437

@@ -586,17 +593,18 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
586593
service["entrypoint"] = s.entrypoint
587594
}
588595

589-
fmt.Println("XXX")
590-
591596
if len(s.ports) > 0 {
592597
ports := []string{}
593-
fmt.Println("x", d.bindHostPortsLocally)
594-
595598
for _, p := range s.ports {
599+
protocol := ""
600+
if p.Protocol == ProtocolUDP {
601+
protocol = "/udp"
602+
}
603+
596604
if d.bindHostPortsLocally {
597-
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))
598606
} else {
599-
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))
600608
}
601609
}
602610
service["ports"] = ports

internal/manifest.go

Lines changed: 28 additions & 4 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
@@ -218,6 +223,9 @@ type Port struct {
218223
// Port is the port number
219224
Port int
220225

226+
// Protocol (tcp or udp)
227+
Protocol string
228+
221229
// HostPort is the port number assigned on the host machine for this
222230
// container port. It is populated by the local runner
223231
// TODO: We might want to move this to the runner itself.
@@ -356,18 +364,30 @@ func (s *service) WithTag(tag string) *service {
356364
return s
357365
}
358366

359-
func (s *service) WithPort(name string, portNumber int) *service {
367+
func (s *service) WithPort(name string, portNumber int, protocolVar ...string) *service {
368+
protocol := ProtocolTCP
369+
if len(protocol) > 0 {
370+
if protocolVar[0] != ProtocolTCP && protocolVar[0] != ProtocolUDP {
371+
panic(fmt.Sprintf("protocol %s not supported", protocolVar[0]))
372+
}
373+
protocol = protocolVar[0]
374+
}
375+
360376
// add the port if not already present with the same name.
361377
// if preset with the same name, they must have same port number
362378
for _, p := range s.ports {
363379
if p.Name == name {
364380
if p.Port != portNumber {
365381
panic(fmt.Sprintf("port %s already defined with different port number", name))
366382
}
383+
if p.Protocol != protocol {
384+
// If they have different protocols they are different ports
385+
continue
386+
}
367387
return s
368388
}
369389
}
370-
s.ports = append(s.ports, &Port{Name: name, Port: portNumber})
390+
s.ports = append(s.ports, &Port{Name: name, Port: portNumber, Protocol: protocol})
371391
return s
372392
}
373393

@@ -376,7 +396,7 @@ func (s *service) applyTemplate(arg string) {
376396
var nodeRef []NodeRef
377397
_, port, nodeRef = applyTemplate(arg)
378398
for _, p := range port {
379-
s.WithPort(p.Name, p.Port)
399+
s.WithPort(p.Name, p.Port, p.Protocol)
380400
}
381401
for _, n := range nodeRef {
382402
s.nodeRefs = append(s.nodeRefs, &n)
@@ -445,9 +465,13 @@ func applyTemplate(templateStr string) (string, []Port, []NodeRef) {
445465
return fmt.Sprintf(`{{Service "%s" "%s"}}`, name, portLabel)
446466
},
447467
"Port": func(name string, defaultPort int) string {
448-
portRef = append(portRef, Port{Name: name, Port: defaultPort})
468+
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolTCP})
449469
return fmt.Sprintf(`{{Port "%s" %d}}`, name, defaultPort)
450470
},
471+
"PortUDP": func(name string, defaultPort int) string {
472+
portRef = append(portRef, Port{Name: name, Port: defaultPort, Protocol: ProtocolUDP})
473+
return fmt.Sprintf(`{{PortUDP "%s" %d}}`, name, defaultPort)
474+
},
451475
}
452476

453477
tpl, err := template.New("").Funcs(funcs).Parse(templateStr)

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)