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

sdn: only sync HostPorts when we need to #16692

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions pkg/network/node/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ type podManager struct {
ovs *ovsController

enableHostports bool
// true if hostports have been synced at least once
hostportsSynced bool
// true if at least one running pod has a hostport mapping
activeHostports bool

// Things only accessed through the processCNIRequests() goroutine
// and thus can be set from Start()
Expand Down Expand Up @@ -188,12 +192,33 @@ func (m *podManager) getPod(request *cniserver.PodRequest) *kubehostport.PodPort
}

// Return a list of Kubernetes RunningPod objects for hostport operations
func (m *podManager) getRunningPods() []*kubehostport.PodPortMapping {
pods := make([]*kubehostport.PodPortMapping, 0)
func (m *podManager) shouldSyncHostports(newPod *kubehostport.PodPortMapping) []*kubehostport.PodPortMapping {
if m.hostportSyncer == nil {
return nil
}

newActiveHostports := false
mappings := make([]*kubehostport.PodPortMapping, 0)
for _, runningPod := range m.runningPods {
pods = append(pods, runningPod.podPortMapping)
mappings = append(mappings, runningPod.podPortMapping)
if !newActiveHostports && len(runningPod.podPortMapping.PortMappings) > 0 {
newActiveHostports = true
}
}
if newPod != nil && len(newPod.PortMappings) > 0 {
newActiveHostports = true
}
return pods

// Sync the first time a pod is started (to clear out stale mappings
// if kubelet crashed), or when there are any/will be active hostports.
// Otherwise don't bother.
if !m.hostportsSynced || m.activeHostports || newActiveHostports {
m.hostportsSynced = true
m.activeHostports = newActiveHostports
return mappings
}

return nil
}

// Add a request to the podManager CNI request queue
Expand Down Expand Up @@ -513,8 +538,8 @@ func (m *podManager) setup(req *cniserver.PodRequest) (cnitypes.Result, *running
defer func() {
if !success {
m.ipamDel(req.SandboxID)
if m.hostportSyncer != nil {
if err := m.hostportSyncer.SyncHostports(Tun0, m.getRunningPods()); err != nil {
if mappings := m.shouldSyncHostports(nil); mappings != nil {
if err := m.hostportSyncer.SyncHostports(Tun0, mappings); err != nil {
glog.Warningf("failed syncing hostports: %v", err)
}
}
Expand All @@ -527,8 +552,8 @@ func (m *podManager) setup(req *cniserver.PodRequest) (cnitypes.Result, *running
return nil, nil, err
}
podPortMapping := kubehostport.ConstructPodPortMapping(&v1Pod, podIP)
if m.hostportSyncer != nil {
if err := m.hostportSyncer.OpenPodHostportsAndSync(podPortMapping, Tun0, m.getRunningPods()); err != nil {
if mappings := m.shouldSyncHostports(podPortMapping); mappings != nil {
if err := m.hostportSyncer.OpenPodHostportsAndSync(podPortMapping, Tun0, mappings); err != nil {
return nil, nil, err
}
}
Expand Down Expand Up @@ -651,8 +676,8 @@ func (m *podManager) teardown(req *cniserver.PodRequest) error {
errList = append(errList, err)
}

if m.hostportSyncer != nil {
if err := m.hostportSyncer.SyncHostports(Tun0, m.getRunningPods()); err != nil {
if mappings := m.shouldSyncHostports(nil); mappings != nil {
if err := m.hostportSyncer.SyncHostports(Tun0, mappings); err != nil {
errList = append(errList, err)
}
}
Expand Down