77 "context"
88 "strconv"
99 "strings"
10+ "sync"
1011 "sync/atomic"
1112 "time"
1213
@@ -18,48 +19,42 @@ import (
1819
1920// TODO: make this file a sub pacakge?
2021
21- // NetworkContainerSyncState manages the synchronization state for network container operations.
22- type NetworkContainerSyncState struct {
23- ncSynced chan struct {}
24- ncSyncLoop atomic.Bool
22+ // NetworkContainerSyncState manages waiting on conflist to ge
23+ // Basically a wait group that can only be added once and waits with a context
24+ // meant to be used uninitialized and then started once the sync loop begins.
25+ type networkContainerSyncState struct {
26+ wg sync.WaitGroup
27+ started atomic.Bool
2528}
2629
2730// Start attempts to start the sync loop. Returns an error if already started.
28- func (n * NetworkContainerSyncState ) Start () error {
29- if n == nil {
30- return errors .New ("NetworkContainerSyncState is nil" )
31- }
32- if ! n .ncSyncLoop .CompareAndSwap (false , true ) {
31+ func (n * networkContainerSyncState ) Start () error {
32+ if ! n .started .CompareAndSwap (false , true ) {
3333 return errors .New ("sync loop already started" )
3434 }
35- n .ncSynced = make ( chan struct {} )
35+ n .wg . Add ( 1 )
3636 return nil
3737}
3838
39- // NotifyReady closes the ncSynced channel to signal readiness.
40- func (n * NetworkContainerSyncState ) NotifyReady () {
41- if n == nil || ! n .ncSyncLoop .Load () {
39+ // NotifyReady called once
40+ func (n * networkContainerSyncState ) NotifyReady () {
41+ if ! n .started .Load () {
4242 return //nobody ever set this up just move on.
4343 }
44- close ( n . ncSynced )
44+ n . wg . Done ( )
4545}
4646
47- // WaitForConfList waits for the CNI conflist to be ready or for the context to be done.
48- func (n * NetworkContainerSyncState ) WaitForConfList (ctx context.Context ) {
49- if n == nil {
50- return //do nothing if we never got intiialized.
51- }
52-
53- // Sync loop never set up, get out of here.
54- if n .ncSyncLoop .Load () {
55- return
56- }
47+ // Wait waits for the CNI conflist to be ready or for the context to be done.
48+ func (n * networkContainerSyncState ) Wait (ctx context.Context ) {
49+ done := make (chan struct {})
50+ go func () {
51+ n .wg .Wait ()
52+ close (done )
53+ }()
5754
5855 select {
59- case <- n .ncSynced :
60- return
56+ case <- done :
6157 case <- ctx .Done ():
62- return
6358 }
6459}
6560
@@ -90,7 +85,7 @@ func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context,
9085 return nil
9186}
9287
93- // TODO: lowercase/unexport this function drive everything through
88+ // TODO: lowercase/unexport this function drive everything through StartSyncHostNCVersionLoop?
9489// SyncHostNCVersion will check NC version from NMAgent and save it as host NC version in container status.
9590// If NMAgent NC version got updated, CNS will refresh the pending programming IP status.
9691func (service * HTTPRestService ) SyncHostNCVersion (ctx context.Context , channelMode string ) {
0 commit comments