Skip to content

Commit b76bbcf

Browse files
authored
[NPM] [Bug] Fixing the looping issue in backup and reconcile chains (#923)
* Fixing the looping issue in backup and reconcile chains * fixing a golint * addressing some comments
1 parent a1b487a commit b76bbcf

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

npm/npm.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,19 @@ func (npMgr *NetworkPolicyManager) restore() {
157157
}
158158

159159
// backup takes snapshots of iptables filter table and saves it periodically.
160-
func (npMgr *NetworkPolicyManager) backup() {
160+
func (npMgr *NetworkPolicyManager) backup(stopCh <-chan struct{}) {
161161
iptMgr := iptm.NewIptablesManager()
162-
var err error
163-
for {
164-
time.Sleep(backupWaitTimeInSeconds * time.Second)
162+
ticker := time.NewTicker(time.Second * time.Duration(backupWaitTimeInSeconds))
163+
defer ticker.Stop()
165164

166-
if err = iptMgr.Save(util.IptablesConfigFile); err != nil {
167-
metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to back up Azure-NPM states %s", err.Error())
165+
for {
166+
select {
167+
case <-stopCh:
168+
return
169+
case <-ticker.C:
170+
if err := iptMgr.Save(util.IptablesConfigFile); err != nil {
171+
metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to back up Azure-NPM states %s", err.Error())
172+
}
168173
}
169174
}
170175
}
@@ -194,8 +199,8 @@ func (npMgr *NetworkPolicyManager) Start(stopCh <-chan struct{}) error {
194199
go npMgr.podController.Run(threadness, stopCh)
195200
go npMgr.nameSpaceController.Run(threadness, stopCh)
196201
go npMgr.netPolController.Run(threadness, stopCh)
197-
go npMgr.reconcileChains()
198-
go npMgr.backup()
202+
go npMgr.reconcileChains(stopCh)
203+
go npMgr.backup(stopCh)
199204

200205
return nil
201206
}
@@ -280,13 +285,19 @@ func NewNetworkPolicyManager(clientset *kubernetes.Clientset, informerFactory in
280285
}
281286

282287
// reconcileChains checks for ordering of AZURE-NPM chain in FORWARD chain periodically.
283-
func (npMgr *NetworkPolicyManager) reconcileChains() error {
288+
func (npMgr *NetworkPolicyManager) reconcileChains(stopCh <-chan struct{}) {
284289
iptMgr := iptm.NewIptablesManager()
285-
select {
286-
case <-time.After(reconcileChainTimeInMinutes * time.Minute):
287-
if err := iptMgr.CheckAndAddForwardChain(); err != nil {
288-
return err
290+
ticker := time.NewTicker(time.Minute * time.Duration(reconcileChainTimeInMinutes))
291+
defer ticker.Stop()
292+
293+
for {
294+
select {
295+
case <-stopCh:
296+
return
297+
case <-ticker.C:
298+
if err := iptMgr.CheckAndAddForwardChain(); err != nil {
299+
metrics.SendErrorLogAndMetric(util.NpmID, "Error: failed to reconcileChains Azure-NPM due to %s", err.Error())
300+
}
289301
}
290302
}
291-
return nil
292303
}

0 commit comments

Comments
 (0)