Skip to content
Permalink
Browse files

Merge pull request #661 from posox/pf-protocol

Add support udp protocol in port forwarding
  • Loading branch information...
k8s-ci-robot committed Jul 12, 2019
2 parents a132fa8 + a74c578 commit 9205bac4a8377fb7d301a8c14650592b85a34f58
@@ -19,6 +19,7 @@ package cri
import (
"encoding/json"
"fmt"
"strings"
)

/*
@@ -65,3 +66,43 @@ func (m *Mount) UnmarshalJSON(data []byte) error {
}
return nil
}

// MarshalJSON implements custom encoding for JSON and Yaml
// https://golang.org/pkg/encoding/json/
func (p *PortMapping) MarshalJSON() ([]byte, error) {
type Alias PortMapping
name, ok := PortMappingProtocolValueToName[p.Protocol]
if !ok {
return nil, fmt.Errorf("unknown protocol value: %v", p.Protocol)
}
return json.Marshal(&struct {
Protocol string `json:"protocol"`
*Alias
}{
Protocol: name,
Alias: (*Alias)(p),
})
}

// UnmarshalJSON implements custom decoding for JSON and Yaml
// https://golang.org/pkg/encoding/json/
func (p *PortMapping) UnmarshalJSON(data []byte) error {
type Alias PortMapping
aux := &struct {
Protocol string `json:"protocol"`
*Alias
}{
Alias: (*Alias)(p),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
if aux.Protocol != "" {
val, ok := PortMappingProtocolNameToValue[strings.ToUpper(aux.Protocol)]
if !ok {
return fmt.Errorf("unknown protocol value: %s", aux.Protocol)
}
p.Protocol = PortMappingProtocol(val)
}
return nil
}
@@ -54,13 +54,16 @@ type Mount struct {
// containerPort: 80
// hostPort: 8000
// listenAddress: 127.0.0.1
// protocol: TCP
type PortMapping struct {
// Port within the container.
ContainerPort int32 `protobuf:"varint,1,opt,name=container_port,json=containerPort,proto3" json:"containerPort,omitempty"`
// Port on the host.
HostPort int32 `protobuf:"varint,2,opt,name=host_path,json=hostPort,proto3" json:"hostPort,omitempty"`
// TODO: add protocol (tcp/udp) and port-ranges
ListenAddress string `protobuf:"bytes,3,opt,name=listenAddress,json=hostPort,proto3" json:"listenAddress,omitempty"`
ListenAddress string `protobuf:"bytes,3,opt,name=listenAddress,json=listenAddress,proto3" json:"listenAddress,omitempty"`
// Protocol (TCP/UDP)
Protocol PortMappingProtocol `protobuf:"varint,4,opt,name=protocol,proto3,enum=runtime.v1alpha2.PortMappingProtocol" json:"protocol,omitempty"`
}

// MountPropagation represents an "enum" for mount propagation options,
@@ -95,3 +98,32 @@ var MountPropagationNameToValue = map[string]MountPropagation{
"HostToContainer": MountPropagationHostToContainer,
"Bidirectional": MountPropagationBidirectional,
}

// PortMappingProtocol represents an "enum" for port mapping protocol options,
// see also PortMapping.
type PortMappingProtocol int32

const (
// PortMappingProtocolTCP specifies TCP protocol
PortMappingProtocolTCP PortMappingProtocol = 0
// PortMappingProtocolUDP specifies UDP protocol
PortMappingProtocolUDP PortMappingProtocol = 1
// PortMappingProtocolSCTP specifies SCTP protocol
PortMappingProtocolSCTP PortMappingProtocol = 2
)

// PortMappingProtocolValueToName is a map of valid PortMappingProtocol values to
// their string names
var PortMappingProtocolValueToName = map[PortMappingProtocol]string{
PortMappingProtocolTCP: "TCP",
PortMappingProtocolUDP: "UDP",
PortMappingProtocolSCTP: "SCTP",
}

// PortMappingProtocolNameToValue is a map of valid PortMappingProtocol names to
// their values
var PortMappingProtocolNameToValue = map[string]PortMappingProtocol{
"TCP": PortMappingProtocolTCP,
"UDP": PortMappingProtocolUDP,
"SCTP": PortMappingProtocolSCTP,
}
@@ -82,7 +82,18 @@ func generatePortMappings(portMappings ...cri.PortMapping) []string {
} else {
hostPortBinding = fmt.Sprintf("%d", pm.HostPort)
}
publish := fmt.Sprintf("--publish=%s:%d", hostPortBinding, pm.ContainerPort)
var protocol string
switch pm.Protocol {
case cri.PortMappingProtocolTCP:
protocol = "TCP"
case cri.PortMappingProtocolUDP:
protocol = "UDP"
case cri.PortMappingProtocolSCTP:
protocol = "SCTP"
default:
protocol = "TCP"
}
publish := fmt.Sprintf("--publish=%s:%d/%s", hostPortBinding, pm.ContainerPort, protocol)
result = append(result, publish)
}
return result
@@ -280,6 +280,7 @@ nodes:
- containerPort: 80
hostPort: 80
listenAddress: "127.0.0.1" # Optional, defaults to "0.0.0.0"
protocol: udp # Optional, defaults to tcp
```
This can be useful if using `NodePort` services or daemonsets exposing host ports.

0 comments on commit 9205bac

Please sign in to comment.
You can’t perform that action at this time.