Skip to content

Commit 713c47c

Browse files
committed
guard our done channel
1 parent 3371a95 commit 713c47c

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

cns/restserver/restserver.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ type HTTPRestService struct {
102102
PnpIDByMacAddress map[string]string
103103
imdsClient imdsClient
104104
nodesubnetIPFetcher *nodesubnet.IPFetcher
105-
ncSynced chan struct{}
105+
//put in ncstate struct?
106+
ncSynced chan struct{}
107+
ncSyncLoop bool
106108
}
107109

108110
type CNIConflistGenerator interface {
@@ -253,6 +255,7 @@ func NewHTTPRestService(config *common.ServiceConfig, wscli interfaceGetter, wsp
253255
homeAzMonitor: homeAzMonitor,
254256
cniConflistGenerator: gen,
255257
imdsClient: imdsClient,
258+
ncSynced: make(chan struct{}),
256259
}, nil
257260
}
258261

cns/restserver/synchostnc.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ import (
1717

1818
//TODO make this file a sub pacakge?
1919

20-
func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context, cnsconfig configuration.CNSConfig) {
21-
//do we need a sync.once to protect this? should we error if this is called twice?
22-
service.ncSynced = make(chan struct{})
20+
func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context, cnsconfig configuration.CNSConfig) error {
21+
service.Lock() //could use a seperate lock or atomic bool.
22+
defer service.Unlock()
23+
if service.ncSyncLoop {
24+
return errors.New("SyncHostNCVersion loop already started")
25+
}
26+
service.ncSyncLoop = true
2327
go func() {
2428
logger.Printf("Starting SyncHostNCVersion loop.")
2529
// Periodically poll vfp programmed NC version from NMAgent
@@ -36,6 +40,7 @@ func (service *HTTPRestService) StartSyncHostNCVersionLoop(ctx context.Context,
3640
}
3741
}
3842
}()
43+
return nil
3944
}
4045

4146
// SyncHostNCVersion will check NC version from NMAgent and save it as host NC version in container status.
@@ -187,9 +192,11 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
187192
// a conflist generator. If not, this is a no-op.
188193
func (service *HTTPRestService) mustGenerateCNIConflistOnce() {
189194
service.generateCNIConflistOnce.Do(func() {
190-
if service.ncSynced != nil {
195+
service.Lock() //lock inside a do scary?
196+
if service.ncSyncLoop {
191197
close(service.ncSynced)
192198
}
199+
service.Unlock()
193200
if err := service.cniConflistGenerator.Generate(); err != nil {
194201
panic("unable to generate cni conflist with error: " + err.Error())
195202
}
@@ -202,9 +209,13 @@ func (service *HTTPRestService) mustGenerateCNIConflistOnce() {
202209

203210
func (service *HTTPRestService) WaitForConfList(ctx context.Context) {
204211
//sync loop never set up get out of here.
205-
if service.ncSynced == nil {
206-
return
207-
}
212+
func() {
213+
service.Lock()
214+
defer service.Unlock()
215+
if !service.ncSyncLoop {
216+
return
217+
}
218+
}()
208219

209220
select {
210221
case <-service.ncSynced:

cns/service/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,9 @@ func InitializeMultiTenantController(ctx context.Context, httpRestService cns.HT
12851285
time.Sleep(time.Millisecond * 500)
12861286
}
12871287

1288-
httpRestServiceImpl.StartSyncHostNCVersionLoop(ctx, cnsconfig)
1288+
if err := httpRestServiceImpl.StartSyncHostNCVersionLoop(ctx, cnsconfig); err != nil {
1289+
return err
1290+
}
12891291

12901292
return nil
12911293
}
@@ -1630,7 +1632,9 @@ func InitializeCRDState(ctx context.Context, z *zap.Logger, httpRestService cns.
16301632
break
16311633
}
16321634

1633-
httpRestServiceImplementation.StartSyncHostNCVersionLoop(ctx, *cnsconfig)
1635+
if err := httpRestServiceImplementation.StartSyncHostNCVersionLoop(ctx, *cnsconfig); err != nil {
1636+
return err
1637+
}
16341638
return nil
16351639
}
16361640

0 commit comments

Comments
 (0)