diff --git a/pkg/kubelet/network/hostport/hostport.go b/pkg/kubelet/network/hostport/hostport.go index 374df8eb6c8..f62da6fa186 100644 --- a/pkg/kubelet/network/hostport/hostport.go +++ b/pkg/kubelet/network/hostport/hostport.go @@ -18,10 +18,11 @@ package hostport import ( "fmt" - "github.com/golang/glog" "net" "strings" + "github.com/golang/glog" + "k8s.io/kubernetes/pkg/api/v1" utiliptables "k8s.io/kubernetes/pkg/util/iptables" ) @@ -51,6 +52,31 @@ type PodPortMapping struct { IP net.IP } +// ConstructPodPortMapping creates a PodPortMapping from the ports specified in the pod's +// containers. +func ConstructPodPortMapping(pod *v1.Pod, podIP net.IP) *PodPortMapping { + portMappings := make([]*PortMapping, 0) + for _, c := range pod.Spec.Containers { + for _, port := range c.Ports { + portMappings = append(portMappings, &PortMapping{ + Name: port.Name, + HostPort: port.HostPort, + ContainerPort: port.ContainerPort, + Protocol: port.Protocol, + HostIP: port.HostIP, + }) + } + } + + return &PodPortMapping{ + Namespace: pod.Namespace, + Name: pod.Name, + PortMappings: portMappings, + HostNetwork: pod.Spec.HostNetwork, + IP: podIP, + } +} + type hostport struct { port int32 protocol string diff --git a/pkg/kubelet/network/kubenet/kubenet_linux.go b/pkg/kubelet/network/kubenet/kubenet_linux.go index 166dcabc8ad..ec51133ee45 100644 --- a/pkg/kubelet/network/kubenet/kubenet_linux.go +++ b/pkg/kubelet/network/kubenet/kubenet_linux.go @@ -381,7 +381,7 @@ func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kube return err } - newPodPortMapping := constructPodPortMapping(pod, ip4) + newPodPortMapping := hostport.ConstructPodPortMapping(pod, ip4) if err := plugin.hostportSyncer.OpenPodHostportsAndSync(newPodPortMapping, BridgeName, activePodPortMappings); err != nil { return err } @@ -638,35 +638,12 @@ func (plugin *kubenetNetworkPlugin) getPodPortMappings() ([]*hostport.PodPortMap continue } if pod, ok := plugin.host.GetPodByName(p.Namespace, p.Name); ok { - activePodPortMappings = append(activePodPortMappings, constructPodPortMapping(pod, podIP)) + activePodPortMappings = append(activePodPortMappings, hostport.ConstructPodPortMapping(pod, podIP)) } } return activePodPortMappings, nil } -func constructPodPortMapping(pod *v1.Pod, podIP net.IP) *hostport.PodPortMapping { - portMappings := make([]*hostport.PortMapping, 0) - for _, c := range pod.Spec.Containers { - for _, port := range c.Ports { - portMappings = append(portMappings, &hostport.PortMapping{ - Name: port.Name, - HostPort: port.HostPort, - ContainerPort: port.ContainerPort, - Protocol: port.Protocol, - HostIP: port.HostIP, - }) - } - } - - return &hostport.PodPortMapping{ - Namespace: pod.Namespace, - Name: pod.Name, - PortMappings: portMappings, - HostNetwork: pod.Spec.HostNetwork, - IP: podIP, - } -} - // ipamGarbageCollection will release unused IP. // kubenet uses the CNI bridge plugin, which stores allocated ips on file. Each // file created under defaultIPAMDir has the format: ip/container-hash. So this