diff --git a/pkg/api/types.go b/pkg/api/types.go index 221f73c7061..1d290b4d42c 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -99,8 +99,7 @@ type Port struct { // Optional: If specified, this must be a DNS_LABEL. Each named port // in a pod must have a unique name. Name string `yaml:"name,omitempty" json:"name,omitempty"` - // Optional: Defaults to ContainerPort. If specified, this must be a - // valid port number, 0 < x < 65536. + // Optional: If specified, this must be a valid port number, 0 < x < 65536. 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"` diff --git a/pkg/api/v1beta1/types.go b/pkg/api/v1beta1/types.go index 6b2a2dc4765..ef959597477 100644 --- a/pkg/api/v1beta1/types.go +++ b/pkg/api/v1beta1/types.go @@ -99,8 +99,7 @@ type Port struct { // Optional: If specified, this must be a DNS_LABEL. Each named port // in a pod must have a unique name. Name string `yaml:"name,omitempty" json:"name,omitempty"` - // Optional: Defaults to ContainerPort. If specified, this must be a - // valid port number, 0 < x < 65536. + // Optional: If specified, this must be a valid port number, 0 < x < 65536. 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"` diff --git a/pkg/api/validation.go b/pkg/api/validation.go index 4a7f08f9c94..cd54e00f7d1 100644 --- a/pkg/api/validation.go +++ b/pkg/api/validation.go @@ -95,9 +95,7 @@ func validatePorts(ports []Port) errs.ErrorList { if !util.IsValidPortNum(port.ContainerPort) { allErrs = append(allErrs, errs.NewInvalid("Port.ContainerPort", port.ContainerPort)) } - if port.HostPort == 0 { - port.HostPort = port.ContainerPort - } else if !util.IsValidPortNum(port.HostPort) { + if port.HostPort != 0 && !util.IsValidPortNum(port.HostPort) { allErrs = append(allErrs, errs.NewInvalid("Port.HostPort", port.HostPort)) } if len(port.Protocol) == 0 { @@ -160,6 +158,9 @@ func AccumulateUniquePorts(containers []Container, accumulator map[int]bool, ext ctr := &containers[ci] for pi := range ctr.Ports { port := extract(&ctr.Ports[pi]) + if port == 0 { + continue + } if accumulator[port] { allErrs = append(allErrs, errs.NewDuplicate("Port", port)) } else { diff --git a/pkg/api/validation_test.go b/pkg/api/validation_test.go index 8c1a8246df8..093adc14bbd 100644 --- a/pkg/api/validation_test.go +++ b/pkg/api/validation_test.go @@ -73,7 +73,7 @@ func TestValidatePorts(t *testing.T) { if errs := validatePorts(nonCanonicalCase); len(errs) != 0 { t.Errorf("expected success: %v", errs) } - if nonCanonicalCase[0].HostPort != 80 || nonCanonicalCase[0].Protocol != "TCP" { + if nonCanonicalCase[0].HostPort != 0 || nonCanonicalCase[0].Protocol != "TCP" { t.Errorf("expected default values: %+v", nonCanonicalCase[0]) } diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index f461124df5b..7548ca02145 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -227,8 +227,12 @@ func makePortsAndBindings(container *api.Container) (map[docker.Port]struct{}, m exposedPorts := map[docker.Port]struct{}{} portBindings := map[docker.Port][]docker.PortBinding{} for _, port := range container.Ports { - interiorPort := port.ContainerPort exteriorPort := port.HostPort + if exteriorPort == 0 { + // No need to do port binding when HostPort is not specified + continue + } + interiorPort := port.ContainerPort // 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 diff --git a/pkg/scheduler/randomfit.go b/pkg/scheduler/randomfit.go index b198f9c1c2b..ce32d741c6d 100644 --- a/pkg/scheduler/randomfit.go +++ b/pkg/scheduler/randomfit.go @@ -71,6 +71,9 @@ func (s *RandomFitScheduler) Schedule(pod api.Pod, minionLister MinionLister) (s for _, scheduledPod := range machineToPods[machine] { for _, container := range pod.DesiredState.Manifest.Containers { for _, port := range container.Ports { + if port.HostPort == 0 { + continue + } if s.containsPort(scheduledPod, port) { podFits = false }