diff --git a/pkg/api/types.go b/pkg/api/types.go index 1d9a41e8051..6916cc24a86 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -100,6 +100,16 @@ type HostDirectory 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. type Port struct { // 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"` // Required: This must be a valid port number, 0 < x < 65536. ContainerPort int `yaml:"containerPort" json:"containerPort"` - // Optional: Supports "TCP" and "UDP". Defaults to "TCP". - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` + // Optional: Defaults to "TCP". + Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"` // Optional: What host IP to bind the external port to. HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"` } @@ -381,8 +391,8 @@ type Service struct { // Required. Port int `json:"port" yaml:"port"` - // Optional: Supports "TCP" and "UDP". Defaults to "TCP". - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` + // Optional: Defaults to "TCP". + Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"` // This service's labels. Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` diff --git a/pkg/api/v1beta1/types.go b/pkg/api/v1beta1/types.go index 61277c3e801..9225b592e98 100644 --- a/pkg/api/v1beta1/types.go +++ b/pkg/api/v1beta1/types.go @@ -100,6 +100,16 @@ type HostDirectory 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. type Port struct { // 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"` // Required: This must be a valid port number, 0 < x < 65536. ContainerPort int `yaml:"containerPort" json:"containerPort"` - // Optional: Supports "TCP" and "UDP". Defaults to "TCP". - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` + // Optional: Defaults to "TCP". + Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"` // Optional: What host IP to bind the external port to. HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"` } @@ -394,8 +404,8 @@ type Service struct { // Required. Port int `json:"port" yaml:"port"` - // Optional: Supports "TCP" and "UDP". Defaults to "TCP". - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` + // Optional: Defaults to "TCP". + Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"` // This service's labels. Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` diff --git a/pkg/api/v1beta2/types.go b/pkg/api/v1beta2/types.go index 61d066dd4f7..44cdfb4be21 100644 --- a/pkg/api/v1beta2/types.go +++ b/pkg/api/v1beta2/types.go @@ -100,6 +100,16 @@ type HostDirectory 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. type Port struct { // 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"` // Required: This must be a valid port number, 0 < x < 65536. ContainerPort int `yaml:"containerPort" json:"containerPort"` - // Optional: Supports "TCP" and "UDP". Defaults to "TCP". - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` + // Optional: Defaults to "TCP". + Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"` // Optional: What host IP to bind the external port to. HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"` } @@ -391,8 +401,8 @@ type Service struct { // Required. Port int `json:"port" yaml:"port"` - // Optional: Supports "TCP" and "UDP". Defaults to "TCP". - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` + // Optional: Defaults to "TCP". + Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"` // This service's labels. Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` diff --git a/pkg/api/v1beta3/types.go b/pkg/api/v1beta3/types.go index 661bc855749..382bb1932ea 100644 --- a/pkg/api/v1beta3/types.go +++ b/pkg/api/v1beta3/types.go @@ -100,6 +100,16 @@ type HostDirectory 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. type Port struct { // 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"` // Required: This must be a valid port number, 0 < x < 65536. ContainerPort int `yaml:"containerPort" json:"containerPort"` - // Optional: Supports "TCP" and "UDP". Defaults to "TCP". - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` + // Optional: Defaults to "TCP". + Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"` // Optional: What host IP to bind the external port to. HostIP string `yaml:"hostIP,omitempty" json:"hostIP,omitempty"` } @@ -390,8 +400,8 @@ type Service struct { // Required. Port int `json:"port" yaml:"port"` - // Optional: Supports "TCP" and "UDP". Defaults to "TCP". - Protocol string `yaml:"protocol,omitempty" json:"protocol,omitempty"` + // Optional: Defaults to "TCP". + Protocol Protocol `yaml:"protocol,omitempty" json:"protocol,omitempty"` // This service's labels. Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index 01d96b4c463..d209e3c8790 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -78,7 +78,7 @@ func validateHostDir(hostDir *api.HostDirectory) errs.ErrorList { 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 { allErrs := errs.ErrorList{} @@ -106,7 +106,7 @@ func validatePorts(ports []api.Port) errs.ErrorList { } if len(port.Protocol) == 0 { 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)) } allErrs = append(allErrs, pErrs.PrefixIndex(i)...) @@ -330,7 +330,7 @@ func ValidateService(service *api.Service) errs.ErrorList { } if len(service.Protocol) == 0 { 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)) } if labels.Set(service.Selector).AsSelector().Empty() { diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index bee489f3d55..5ad944dc245 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -222,7 +222,7 @@ func makePortsAndBindings(container *api.Container) (map[docker.Port]struct{}, m // 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 var protocol string - switch strings.ToUpper(port.Protocol) { + switch strings.ToUpper(string(port.Protocol)) { case "UDP": protocol = "/udp" case "TCP": diff --git a/pkg/proxy/proxier.go b/pkg/proxy/proxier.go index ecdf32cf425..339423676fa 100644 --- a/pkg/proxy/proxier.go +++ b/pkg/proxy/proxier.go @@ -32,7 +32,7 @@ import ( type serviceInfo struct { port int - protocol string + protocol api.Protocol socket proxySocket timeout time.Duration mu sync.Mutex // protects active @@ -276,8 +276,8 @@ func logTimeout(err error) bool { return false } -func newProxySocket(protocol string, host string, port int) (proxySocket, error) { - switch strings.ToUpper(protocol) { +func newProxySocket(protocol api.Protocol, host string, port int) (proxySocket, error) { + switch strings.ToUpper(string(protocol)) { case "TCP": listener, err := net.Listen("tcp", net.JoinHostPort(host, strconv.Itoa(port))) if err != nil { @@ -350,7 +350,7 @@ func (proxier *Proxier) setServiceInfo(service string, info *serviceInfo) { // 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 // 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) if err != nil { return "", err diff --git a/pkg/registry/service/rest.go b/pkg/registry/service/rest.go index 635533ff66e..0394ece8cc5 100644 --- a/pkg/registry/service/rest.go +++ b/pkg/registry/service/rest.go @@ -222,9 +222,9 @@ func makeEnvVariableName(str string) string { func makeLinkVariables(service api.Service, machine string) []api.EnvVar { prefix := makeEnvVariableName(service.ID) - protocol := "TCP" + protocol := string(api.ProtocolTCP) if service.Protocol != "" { - protocol = service.Protocol + protocol = string(service.Protocol) } portPrefix := fmt.Sprintf("%s_PORT_%d_%s", prefix, service.Port, strings.ToUpper(protocol)) return []api.EnvVar{