Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move & export ConstructPodPortMapping #43386

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion pkg/kubelet/network/hostport/hostport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
27 changes: 2 additions & 25 deletions pkg/kubelet/network/kubenet/kubenet_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down