@@ -72,6 +72,10 @@ type podManager struct {
72
72
ovs * ovsController
73
73
74
74
enableHostports bool
75
+ // true if hostports have been synced at least once
76
+ hostportsSynced bool
77
+ // true if at least one running pod has a hostport mapping
78
+ activeHostports bool
75
79
76
80
// Things only accessed through the processCNIRequests() goroutine
77
81
// and thus can be set from Start()
@@ -188,12 +192,33 @@ func (m *podManager) getPod(request *cniserver.PodRequest) *kubehostport.PodPort
188
192
}
189
193
190
194
// Return a list of Kubernetes RunningPod objects for hostport operations
191
- func (m * podManager ) getRunningPods () []* kubehostport.PodPortMapping {
192
- pods := make ([]* kubehostport.PodPortMapping , 0 )
195
+ func (m * podManager ) shouldSyncHostports (newPod * kubehostport.PodPortMapping ) []* kubehostport.PodPortMapping {
196
+ if m .hostportSyncer == nil {
197
+ return nil
198
+ }
199
+
200
+ newActiveHostports := false
201
+ mappings := make ([]* kubehostport.PodPortMapping , 0 )
193
202
for _ , runningPod := range m .runningPods {
194
- pods = append (pods , runningPod .podPortMapping )
203
+ mappings = append (mappings , runningPod .podPortMapping )
204
+ if ! newActiveHostports && len (runningPod .podPortMapping .PortMappings ) > 0 {
205
+ newActiveHostports = true
206
+ }
207
+ }
208
+ if newPod != nil && len (newPod .PortMappings ) > 0 {
209
+ newActiveHostports = true
195
210
}
196
- return pods
211
+
212
+ // Sync the first time a pod is started (to clear out stale mappings
213
+ // if kubelet crashed), or when there are any/will be active hostports.
214
+ // Otherwise don't bother.
215
+ if ! m .hostportsSynced || m .activeHostports || newActiveHostports {
216
+ m .hostportsSynced = true
217
+ m .activeHostports = newActiveHostports
218
+ return mappings
219
+ }
220
+
221
+ return nil
197
222
}
198
223
199
224
// Add a request to the podManager CNI request queue
@@ -513,8 +538,8 @@ func (m *podManager) setup(req *cniserver.PodRequest) (cnitypes.Result, *running
513
538
defer func () {
514
539
if ! success {
515
540
m .ipamDel (req .SandboxID )
516
- if m . hostportSyncer != nil {
517
- if err := m .hostportSyncer .SyncHostports (Tun0 , m . getRunningPods () ); err != nil {
541
+ if mappings := m . shouldSyncHostports ( nil ); mappings != nil {
542
+ if err := m .hostportSyncer .SyncHostports (Tun0 , mappings ); err != nil {
518
543
glog .Warningf ("failed syncing hostports: %v" , err )
519
544
}
520
545
}
@@ -527,8 +552,8 @@ func (m *podManager) setup(req *cniserver.PodRequest) (cnitypes.Result, *running
527
552
return nil , nil , err
528
553
}
529
554
podPortMapping := kubehostport .ConstructPodPortMapping (& v1Pod , podIP )
530
- if m . hostportSyncer != nil {
531
- if err := m .hostportSyncer .OpenPodHostportsAndSync (podPortMapping , Tun0 , m . getRunningPods () ); err != nil {
555
+ if mappings := m . shouldSyncHostports ( podPortMapping ); mappings != nil {
556
+ if err := m .hostportSyncer .OpenPodHostportsAndSync (podPortMapping , Tun0 , mappings ); err != nil {
532
557
return nil , nil , err
533
558
}
534
559
}
@@ -651,8 +676,8 @@ func (m *podManager) teardown(req *cniserver.PodRequest) error {
651
676
errList = append (errList , err )
652
677
}
653
678
654
- if m . hostportSyncer != nil {
655
- if err := m .hostportSyncer .SyncHostports (Tun0 , m . getRunningPods () ); err != nil {
679
+ if mappings := m . shouldSyncHostports ( nil ); mappings != nil {
680
+ if err := m .hostportSyncer .SyncHostports (Tun0 , mappings ); err != nil {
656
681
errList = append (errList , err )
657
682
}
658
683
}
0 commit comments