Skip to content

Commit 5044c81

Browse files
committed
try and encapsulate a little more
1 parent dcb45f8 commit 5044c81

File tree

5 files changed

+62
-29
lines changed

5 files changed

+62
-29
lines changed

cns/restserver/helper_for_nodesubnet_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func GetRestServiceObjectForNodeSubnetTest(t *testing.T, generator CNIConflistGe
7979
return interfaces, nil
8080
},
8181
},
82-
wscli: &fakes.WireserverClientFake{},
83-
ncSynced: make(chan struct{}),
82+
wscli: &fakes.WireserverClientFake{},
83+
ncSyncState: &NetworkContainerSyncState{},
8484
}
8585
}
8686

cns/restserver/nodesubnet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func (service *HTTPRestService) InitializeNodeSubnet(ctx context.Context, podInf
5959
// StartNodeSubnet starts the IP fetcher for NodeSubnet. This will cause secondary IPs to be fetched periodically.
6060
// After the first successful fetch, conflist will be generated to indicate CNS is ready.
6161
func (service *HTTPRestService) StartNodeSubnet(ctx context.Context) error {
62-
if !service.ncSyncLoop.CompareAndSwap(false, true) {
63-
return errors.New("SyncHostNCVersion loop already started")
62+
if err := service.ncSyncState.Start(); err != nil {
63+
return err
6464
}
6565
service.nodesubnetIPFetcher.Start(ctx)
6666
return nil

cns/restserver/restserver.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"net/http/pprof"
88
"sync"
9-
"sync/atomic"
109
"time"
1110

1211
"github.com/Azure/azure-container-networking/cns"
@@ -103,9 +102,7 @@ type HTTPRestService struct {
103102
PnpIDByMacAddress map[string]string
104103
imdsClient imdsClient
105104
nodesubnetIPFetcher *nodesubnet.IPFetcher
106-
//put in ncstate struct?
107-
ncSynced chan struct{}
108-
ncSyncLoop atomic.Bool
105+
ncSyncState *NetworkContainerSyncState
109106
}
110107

111108
type CNIConflistGenerator interface {
@@ -256,7 +253,7 @@ func NewHTTPRestService(config *common.ServiceConfig, wscli interfaceGetter, wsp
256253
homeAzMonitor: homeAzMonitor,
257254
cniConflistGenerator: gen,
258255
imdsClient: imdsClient,
259-
ncSynced: make(chan struct{}),
256+
ncSyncState: &NetworkContainerSyncState{},
260257
}, nil
261258
}
262259

@@ -390,3 +387,8 @@ func (service *HTTPRestService) Stop() {
390387
func (service *HTTPRestService) AttachIPConfigsHandlerMiddleware(middleware cns.IPConfigsHandlerMiddleware) {
391388
service.IPConfigsHandlerMiddleware = middleware
392389
}
390+
391+
// GetNetworkContainerSyncState returns the NetworkContainerSyncState for external use.
392+
func (service *HTTPRestService) Wait(ctx context.Context) {
393+
service.ncSyncState.WaitForConfList(ctx)
394+
}

cns/restserver/synchostnc.go

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"strconv"
99
"strings"
10+
"sync/atomic"
1011
"time"
1112

1213
"github.com/Azure/azure-container-networking/cns"
@@ -17,9 +18,54 @@ import (
1718

1819
// TODO: make this file a sub pacakge?
1920

21+
// NetworkContainerSyncState manages the synchronization state for network container operations.
22+
type NetworkContainerSyncState struct {
23+
ncSynced chan struct{}
24+
ncSyncLoop atomic.Bool
25+
}
26+
27+
// 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) {
33+
return errors.New("sync loop already started")
34+
}
35+
n.ncSynced = make(chan struct{})
36+
return nil
37+
}
38+
39+
// NotifyReady closes the ncSynced channel to signal readiness.
40+
func (n *NetworkContainerSyncState) NotifyReady() {
41+
if n == nil || !n.ncSyncLoop.Load() {
42+
return //nobody ever set this up just move on.
43+
}
44+
close(n.ncSynced)
45+
}
46+
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+
}
57+
58+
select {
59+
case <-n.ncSynced:
60+
return
61+
case <-ctx.Done():
62+
return
63+
}
64+
}
65+
2066
func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context, cnsconfig configuration.CNSConfig) error {
21-
if !service.ncSyncLoop.CompareAndSwap(false, true) {
22-
return errors.New("SyncHostNCVersion loop already started")
67+
if err := service.ncSyncState.Start(); err != nil {
68+
return err
2369
}
2470
go func() {
2571
logger.Printf("Starting SyncHostNCVersion loop.")
@@ -194,29 +240,14 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
194240
// a conflist generator. If not, this is a no-op.
195241
func (service *HTTPRestService) mustGenerateCNIConflistOnce() {
196242
service.generateCNIConflistOnce.Do(func() {
197-
if service.ncSyncLoop.Load() {
198-
close(service.ncSynced)
199-
}
243+
200244
if err := service.cniConflistGenerator.Generate(); err != nil {
201245
panic("unable to generate cni conflist with error: " + err.Error())
202246
}
203247

204248
if err := service.cniConflistGenerator.Close(); err != nil {
205249
panic("unable to close the cni conflist output stream: " + err.Error())
206250
}
251+
service.ncSyncState.NotifyReady()
207252
})
208253
}
209-
210-
func (service *HTTPRestService) WaitForConfList(ctx context.Context) {
211-
// Sync loop never set up, get out of here.
212-
if !service.ncSyncLoop.Load() {
213-
return
214-
}
215-
216-
select {
217-
case <-service.ncSynced:
218-
return
219-
case <-ctx.Done():
220-
return
221-
}
222-
}

cns/service/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ func main() {
11221122
}
11231123

11241124
// Wait for NC sync to complete before marking service as ready.
1125-
httpRemoteRestService.WaitForConfList(rootCtx)
1125+
httpRemoteRestService.Wait(rootCtx)
11261126
// mark the service as "ready"
11271127
close(readyCh)
11281128
// block until process exiting

0 commit comments

Comments
 (0)