Make and use api.Protocol type

This commit is contained in:
Tim Hockin
2014-09-27 20:31:37 -07:00
parent 621df0da32
commit 04cdf286a4
8 changed files with 66 additions and 26 deletions

View File

@@ -100,6 +100,16 @@ type HostDirectory struct {
type EmptyDirectory struct{} type EmptyDirectory struct{}
// Protocol defines network protocols supported for things like conatiner ports.
type Protocol string
const (
// ProtocolTCP is the TCP protocol.
ProtocolTCP Protocol = "TCP"
// ProtocolUDP is the UDP protocol.
ProtocolUDP Protocol = "UDP"
)
// Port represents a network port in a single container. // Port represents a network port in a single container.
type Port struct { type Port struct {
// Optional: If specified, this must be a DNS_LABEL. Each named port // Optional: If specified, this must be a DNS_LABEL. Each named port
@@ -109,8 +119,8 @@ type Port struct {
HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"` HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
// Required: This must be a valid port number, 0 < x < 65536. // Required: This must be a valid port number, 0 < x < 65536.
ContainerPort int `yaml:"containerPort" json:"containerPort"` ContainerPort int `yaml:"containerPort" json:"containerPort"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"`
// Optional: What host IP to bind the external port to. // Optional: What host IP to bind the external port to.
HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"` HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
} }
@@ -381,8 +391,8 @@ type Service struct {
// Required. // Required.
Port int `json:"port" yaml:"port"` Port int `json:"port" yaml:"port"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"`
// This service's labels. // This service's labels.
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`

View File

@@ -100,6 +100,16 @@ type HostDirectory struct {
type EmptyDirectory struct{} type EmptyDirectory struct{}
// Protocol defines network protocols supported for things like conatiner ports.
type Protocol string
const (
// ProtocolTCP is the TCP protocol.
ProtocolTCP Protocol = "TCP"
// ProtocolUDP is the UDP protocol.
ProtocolUDP Protocol = "UDP"
)
// Port represents a network port in a single container. // Port represents a network port in a single container.
type Port struct { type Port struct {
// Optional: If specified, this must be a DNS_LABEL. Each named port // Optional: If specified, this must be a DNS_LABEL. Each named port
@@ -109,8 +119,8 @@ type Port struct {
HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"` HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
// Required: This must be a valid port number, 0 < x < 65536. // Required: This must be a valid port number, 0 < x < 65536.
ContainerPort int `yaml:"containerPort" json:"containerPort"` ContainerPort int `yaml:"containerPort" json:"containerPort"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"`
// Optional: What host IP to bind the external port to. // Optional: What host IP to bind the external port to.
HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"` HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
} }
@@ -394,8 +404,8 @@ type Service struct {
// Required. // Required.
Port int `json:"port" yaml:"port"` Port int `json:"port" yaml:"port"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"`
// This service's labels. // This service's labels.
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`

View File

@@ -100,6 +100,16 @@ type HostDirectory struct {
type EmptyDirectory struct{} type EmptyDirectory struct{}
// Protocol defines network protocols supported for things like conatiner ports.
type Protocol string
const (
// ProtocolTCP is the TCP protocol.
ProtocolTCP Protocol = "TCP"
// ProtocolUDP is the UDP protocol.
ProtocolUDP Protocol = "UDP"
)
// Port represents a network port in a single container. // Port represents a network port in a single container.
type Port struct { type Port struct {
// Optional: If specified, this must be a DNS_LABEL. Each named port // Optional: If specified, this must be a DNS_LABEL. Each named port
@@ -109,8 +119,8 @@ type Port struct {
HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"` HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
// Required: This must be a valid port number, 0 < x < 65536. // Required: This must be a valid port number, 0 < x < 65536.
ContainerPort int `yaml:"containerPort" json:"containerPort"` ContainerPort int `yaml:"containerPort" json:"containerPort"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"`
// Optional: What host IP to bind the external port to. // Optional: What host IP to bind the external port to.
HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"` HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
} }
@@ -391,8 +401,8 @@ type Service struct {
// Required. // Required.
Port int `json:"port" yaml:"port"` Port int `json:"port" yaml:"port"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"`
// This service's labels. // This service's labels.
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`

View File

@@ -100,6 +100,16 @@ type HostDirectory struct {
type EmptyDirectory struct{} type EmptyDirectory struct{}
// Protocol defines network protocols supported for things like conatiner ports.
type Protocol string
const (
// ProtocolTCP is the TCP protocol.
ProtocolTCP Protocol = "TCP"
// ProtocolUDP is the UDP protocol.
ProtocolUDP Protocol = "UDP"
)
// Port represents a network port in a single container. // Port represents a network port in a single container.
type Port struct { type Port struct {
// Optional: If specified, this must be a DNS_LABEL. Each named port // Optional: If specified, this must be a DNS_LABEL. Each named port
@@ -109,8 +119,8 @@ type Port struct {
HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"` HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
// Required: This must be a valid port number, 0 < x < 65536. // Required: This must be a valid port number, 0 < x < 65536.
ContainerPort int `yaml:"containerPort" json:"containerPort"` ContainerPort int `yaml:"containerPort" json:"containerPort"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"`
// Optional: What host IP to bind the external port to. // Optional: What host IP to bind the external port to.
HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"` HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
} }
@@ -390,8 +400,8 @@ type Service struct {
// Required. // Required.
Port int `json:"port" yaml:"port"` Port int `json:"port" yaml:"port"`
// Optional: Supports "TCP" and "UDP". Defaults to "TCP". // Optional: Defaults to "TCP".
Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"`
// This service's labels. // This service's labels.
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`

View File

@@ -78,7 +78,7 @@ func validateHostDir(hostDir *api.HostDirectory) errs.ErrorList {
return allErrs return allErrs
} }
var supportedPortProtocols = util.NewStringSet("TCP", "UDP") var supportedPortProtocols = util.NewStringSet(string(api.ProtocolTCP), string(api.ProtocolUDP))
func validatePorts(ports []api.Port) errs.ErrorList { func validatePorts(ports []api.Port) errs.ErrorList {
allErrs := errs.ErrorList{} allErrs := errs.ErrorList{}
@@ -106,7 +106,7 @@ func validatePorts(ports []api.Port) errs.ErrorList {
} }
if len(port.Protocol) == 0 { if len(port.Protocol) == 0 {
port.Protocol = "TCP" port.Protocol = "TCP"
} else if !supportedPortProtocols.Has(strings.ToUpper(port.Protocol)) { } else if !supportedPortProtocols.Has(strings.ToUpper(string(port.Protocol))) {
pErrs = append(pErrs, errs.NewFieldNotSupported("protocol", port.Protocol)) pErrs = append(pErrs, errs.NewFieldNotSupported("protocol", port.Protocol))
} }
allErrs = append(allErrs, pErrs.PrefixIndex(i)...) allErrs = append(allErrs, pErrs.PrefixIndex(i)...)
@@ -330,7 +330,7 @@ func ValidateService(service *api.Service) errs.ErrorList {
} }
if len(service.Protocol) == 0 { if len(service.Protocol) == 0 {
service.Protocol = "TCP" service.Protocol = "TCP"
} else if !supportedPortProtocols.Has(strings.ToUpper(service.Protocol)) { } else if !supportedPortProtocols.Has(strings.ToUpper(string(service.Protocol))) {
allErrs = append(allErrs, errs.NewFieldNotSupported("protocol", service.Protocol)) allErrs = append(allErrs, errs.NewFieldNotSupported("protocol", service.Protocol))
} }
if labels.Set(service.Selector).AsSelector().Empty() { if labels.Set(service.Selector).AsSelector().Empty() {

View File

@@ -222,7 +222,7 @@ func makePortsAndBindings(container *api.Container) (map[docker.Port]struct{}, m
// Some of this port stuff is under-documented voodoo. // Some of this port stuff is under-documented voodoo.
// See http://stackoverflow.com/questions/20428302/binding-a-port-to-a-host-interface-using-the-rest-api // See http://stackoverflow.com/questions/20428302/binding-a-port-to-a-host-interface-using-the-rest-api
var protocol string var protocol string
switch strings.ToUpper(port.Protocol) { switch strings.ToUpper(string(port.Protocol)) {
case "UDP": case "UDP":
protocol = "/udp" protocol = "/udp"
case "TCP": case "TCP":

View File

@@ -32,7 +32,7 @@ import (
type serviceInfo struct { type serviceInfo struct {
port int port int
protocol string protocol api.Protocol
socket proxySocket socket proxySocket
timeout time.Duration timeout time.Duration
mu sync.Mutex // protects active mu sync.Mutex // protects active
@@ -276,8 +276,8 @@ func logTimeout(err error) bool {
return false return false
} }
func newProxySocket(protocol string, host string, port int) (proxySocket, error) { func newProxySocket(protocol api.Protocol, host string, port int) (proxySocket, error) {
switch strings.ToUpper(protocol) { switch strings.ToUpper(string(protocol)) {
case "TCP": case "TCP":
listener, err := net.Listen("tcp", net.JoinHostPort(host, strconv.Itoa(port))) listener, err := net.Listen("tcp", net.JoinHostPort(host, strconv.Itoa(port)))
if err != nil { if err != nil {
@@ -350,7 +350,7 @@ func (proxier *Proxier) setServiceInfo(service string, info *serviceInfo) {
// addServiceOnUnusedPort starts listening for a new service, returning the // addServiceOnUnusedPort starts listening for a new service, returning the
// port it's using. For testing on a system with unknown ports used. The timeout only applies to UDP // port it's using. For testing on a system with unknown ports used. The timeout only applies to UDP
// connections, for now. // connections, for now.
func (proxier *Proxier) addServiceOnUnusedPort(service, protocol string, timeout time.Duration) (string, error) { func (proxier *Proxier) addServiceOnUnusedPort(service string, protocol api.Protocol, timeout time.Duration) (string, error) {
sock, err := newProxySocket(protocol, proxier.address, 0) sock, err := newProxySocket(protocol, proxier.address, 0)
if err != nil { if err != nil {
return "", err return "", err

View File

@@ -222,9 +222,9 @@ func makeEnvVariableName(str string) string {
func makeLinkVariables(service api.Service, machine string) []api.EnvVar { func makeLinkVariables(service api.Service, machine string) []api.EnvVar {
prefix := makeEnvVariableName(service.ID) prefix := makeEnvVariableName(service.ID)
protocol := "TCP" protocol := string(api.ProtocolTCP)
if service.Protocol != "" { if service.Protocol != "" {
protocol = service.Protocol protocol = string(service.Protocol)
} }
portPrefix := fmt.Sprintf("%s_PORT_%d_%s", prefix, service.Port, strings.ToUpper(protocol)) portPrefix := fmt.Sprintf("%s_PORT_%d_%s", prefix, service.Port, strings.ToUpper(protocol))
return []api.EnvVar{ return []api.EnvVar{