Skip to content

Commit 2fd3c2f

Browse files
committed
Use 'defer' to simplify handling of AppNetworkStatus pending flags
Currently, we have to remember to set PendingAdd/PendingModify inside AppNetworkStatus to false just before the corresponding handler returns. However, there are few error branches where we forgot to clear the pending flag. This causes zedmanager to just wait and not report the error published inside the AppNetworkStatus. Instead of fixing this case-by-case, let's take advantage of 'defer' to make sure that we will never forget to clear the Pending flag, even if a new branch with return statement is added in the future. Signed-off-by: Milan Lenco <[email protected]> (cherry picked from commit fbacc17)
1 parent d22e49b commit 2fd3c2f

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

pkg/pillar/cmd/zedrouter/appnetwork.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ func (z *zedrouter) doActivateAppNetwork(config types.AppNetworkConfig,
140140

141141
// Update AppNetwork and NetworkInstance status.
142142
status.Activated = true
143-
status.PendingAdd = false
144-
status.PendingModify = false
145143
z.publishAppNetworkStatus(status)
146144
z.updateNIStatusAfterAppNetworkActivate(status)
147145

@@ -307,7 +305,6 @@ func (z *zedrouter) doUpdateActivatedAppNetwork(oldConfig, newConfig types.AppNe
307305
// Update app network status as well as status of connected network instances.
308306
z.processAppConnReconcileStatus(appConnRecStatus, status)
309307
z.reloadStatusOfAssignedIPs(status)
310-
status.PendingModify = false
311308
z.publishAppNetworkStatus(status)
312309
z.updateNIStatusAfterAppNetworkActivate(status)
313310
}
@@ -335,8 +332,6 @@ func (z *zedrouter) doInactivateAppNetwork(config types.AppNetworkConfig,
335332

336333
// Update AppNetwork and NetworkInstance status.
337334
status.Activated = false
338-
status.PendingModify = false
339-
status.PendingDelete = false
340335
z.updateNIStatusAfterAppNetworkInactivate(status)
341336
z.removeAssignedIPsFromAppNetStatus(status)
342337
z.publishAppNetworkStatus(status)

pkg/pillar/cmd/zedrouter/pubsubhandlers.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -439,15 +439,19 @@ func (z *zedrouter) handleAppNetworkCreate(ctxArg interface{}, key string,
439439
// Start by marking with PendingAdd
440440
status := types.AppNetworkStatus{
441441
UUIDandVersion: config.UUIDandVersion,
442-
PendingAdd: true,
443442
DisplayName: config.DisplayName,
444443
}
445444
z.doCopyAppNetworkConfigToStatus(config, &status)
445+
status.PendingAdd = true
446+
z.publishAppNetworkStatus(&status)
447+
defer func() {
448+
status.PendingAdd = false
449+
z.publishAppNetworkStatus(&status)
450+
}()
446451

447452
if err := z.validateAppNetworkConfig(config); err != nil {
448453
z.log.Errorf("handleAppNetworkCreate(%v): validation failed: %v",
449454
config.UUIDandVersion.UUID, err)
450-
status.PendingAdd = false
451455
z.addAppNetworkError(&status, "handleAppNetworkCreate", err)
452456
return
453457
}
@@ -460,7 +464,6 @@ func (z *zedrouter) handleAppNetworkCreate(ctxArg interface{}, key string,
460464
err = fmt.Errorf("failed to allocate appNum for %s/%s: %v",
461465
config.UUIDandVersion.UUID, config.DisplayName, err)
462466
z.log.Errorf("handleAppNetworkCreate(%v): %v", config.UUIDandVersion.UUID, err)
463-
status.PendingAdd = false
464467
z.addAppNetworkError(&status, "handleAppNetworkCreate", err)
465468
return
466469
}
@@ -474,7 +477,6 @@ func (z *zedrouter) handleAppNetworkCreate(ctxArg interface{}, key string,
474477
err = fmt.Errorf("failed to allocate numbers for VIFs of the app %s/%s: %v",
475478
config.UUIDandVersion.UUID, config.DisplayName, err)
476479
z.log.Errorf("handleAppNetworkCreate(%v): %v", config.UUIDandVersion.UUID, err)
477-
status.PendingAdd = false
478480
z.addAppNetworkError(&status, "handleAppNetworkCreate", err)
479481
return
480482
}
@@ -485,20 +487,14 @@ func (z *zedrouter) handleAppNetworkCreate(ctxArg interface{}, key string,
485487
if err != nil {
486488
z.log.Errorf("handleAppNetworkCreate(%v): %v", config.UUIDandVersion.UUID, err)
487489
status.AwaitNetworkInstance = true
488-
status.PendingAdd = false
489490
if netInErrState {
490491
z.addAppNetworkError(&status, "handleAppNetworkCreate", err)
491-
} else {
492-
z.publishAppNetworkStatus(&status)
493492
}
494493
return
495494
}
496495

497496
if config.Activate {
498497
z.doActivateAppNetwork(config, &status)
499-
} else {
500-
status.PendingAdd = false
501-
z.publishAppNetworkStatus(&status)
502498
}
503499

504500
z.maybeScheduleRetry()
@@ -519,12 +515,15 @@ func (z *zedrouter) handleAppNetworkModify(ctxArg interface{}, key string,
519515
status.ClearError()
520516
status.PendingModify = true
521517
z.publishAppNetworkStatus(status)
518+
defer func() {
519+
status.PendingModify = false
520+
z.publishAppNetworkStatus(status)
521+
}()
522522

523523
// Check for unsupported/invalid changes.
524524
if err := z.validateAppNetworkConfigForModify(newConfig, oldConfig); err != nil {
525525
z.log.Errorf("handleAppNetworkModify(%v): validation failed: %v",
526526
newConfig.UUIDandVersion.UUID, err)
527-
status.PendingModify = false
528527
z.addAppNetworkError(status, "handleAppNetworkModify", err)
529528
return
530529
}
@@ -538,27 +537,21 @@ func (z *zedrouter) handleAppNetworkModify(ctxArg interface{}, key string,
538537
if err != nil {
539538
z.log.Errorf("handleAppNetworkModify(%v): %v", newConfig.UUIDandVersion.UUID, err)
540539
status.AwaitNetworkInstance = true
541-
status.PendingModify = false
542540
if netInErrState {
543541
z.addAppNetworkError(status, "handleAppNetworkModify", err)
544-
} else {
545-
z.publishAppNetworkStatus(status)
546542
}
547543
return
548544
}
549545

550546
if !newConfig.Activate && status.Activated {
551547
z.doInactivateAppNetwork(newConfig, status)
552548
z.doCopyAppNetworkConfigToStatus(newConfig, status)
553-
z.publishAppNetworkStatus(status)
554549
} else if newConfig.Activate && !status.Activated {
555550
z.doCopyAppNetworkConfigToStatus(newConfig, status)
556551
z.doActivateAppNetwork(newConfig, status)
557552
} else if !status.Activated {
558553
// Just copy in newConfig
559554
z.doCopyAppNetworkConfigToStatus(newConfig, status)
560-
status.PendingModify = false
561-
z.publishAppNetworkStatus(status)
562555
} else { // Config change while application network is active.
563556
z.doUpdateActivatedAppNetwork(oldConfig, newConfig, status)
564557
}
@@ -584,6 +577,8 @@ func (z *zedrouter) handleAppNetworkDelete(ctxArg interface{}, key string,
584577

585578
// Deactivate app network if it is currently activated.
586579
if status.Activated {
580+
// No need to clear PendingDelete later. Instead, we un-publish
581+
// the status completely few lines below.
587582
status.PendingDelete = true
588583
z.publishAppNetworkStatus(status)
589584
z.doInactivateAppNetwork(config, status)

0 commit comments

Comments
 (0)