@@ -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
214219type 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 )
0 commit comments