From 1b56ebe4def0000a7075db497f34e17a157c554a Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Thu, 26 Oct 2017 13:19:24 -0400 Subject: [PATCH] Fix the check for "pod has HostPorts" origin PR 16692 was previously ported to OSE 900 A problem in it was fixed by origin PR 17032 which is back ported here Origin PR16692 (OSE PR 900) didn't work because kubehostport.PodPortMapping.PortMappings includes all ports that a pod declares, not just hostports, so shouldSyncHostports will return true if any pod declares that it listens on any port. (ie, always) This (origin PR 17032) fixes that, which should make the code work in the way origin PR16692 (OSE PR 900) intended for it to work ("only call SyncHostPorts if there is at least one pod on the node that uses HostPorts"). It doesn't attempt to change it to "only call SyncHostPorts if the set of active HostPorts actually changed" because that would be larger and less suitable as a last-minute fix. Author: Dan Winship --- pkg/sdn/plugin/pod.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/sdn/plugin/pod.go b/pkg/sdn/plugin/pod.go index 3e7e97f77cd3..12715d5053bf 100644 --- a/pkg/sdn/plugin/pod.go +++ b/pkg/sdn/plugin/pod.go @@ -155,6 +155,15 @@ func (m *podManager) getPod(request *cniserver.PodRequest) *kubehostport.PodPort return nil } +func hasHostPorts(pod *kubehostport.PodPortMapping) bool { + for _, mapping := range pod.PortMappings { + if mapping.HostPort != 0 { + return true + } + } + return false +} + // Return a list of Kubernetes RunningPod objects for hostport operations func (m *podManager) shouldSyncHostports(newPod *kubehostport.PodPortMapping) []*kubehostport.PodPortMapping { if m.hostportSyncer == nil { @@ -165,11 +174,11 @@ func (m *podManager) shouldSyncHostports(newPod *kubehostport.PodPortMapping) [] mappings := make([]*kubehostport.PodPortMapping, 0) for _, runningPod := range m.runningPods { mappings = append(mappings, runningPod.podPortMapping) - if !newActiveHostports && len(runningPod.podPortMapping.PortMappings) > 0 { + if !newActiveHostports && hasHostPorts(runningPod.podPortMapping) { newActiveHostports = true } } - if newPod != nil && len(newPod.PortMappings) > 0 { + if newPod != nil && hasHostPorts(newPod) { newActiveHostports = true }