@@ -12,6 +12,7 @@ import (
1212 "net/http/httptest"
1313 "reflect"
1414 "strconv"
15+ "time"
1516
1617 "github.com/Azure/azure-container-networking/cns"
1718 "github.com/Azure/azure-container-networking/cns/logger"
@@ -152,6 +153,17 @@ func (service *HTTPRestService) SyncNodeStatus(
152153func (service * HTTPRestService ) SyncHostNCVersion (ctx context.Context , channelMode string ) {
153154 service .Lock ()
154155 defer service .Unlock ()
156+ start := time .Now ()
157+ err := service .syncHostNCVersion (ctx , channelMode )
158+ if err != nil {
159+ logger .Errorf ("sync host error %v" , err )
160+ }
161+ syncHostNcVersion .WithLabelValues (strconv .FormatBool (err == nil )).Observe (time .Since (start ).Seconds ())
162+ }
163+
164+ var errNonExistentContainerStatus = errors .New ("nonExistantContainerstatus" )
165+
166+ func (service * HTTPRestService ) syncHostNCVersion (ctx context.Context , channelMode string ) error {
155167 var hostVersionNeedsUpdateContainers []string
156168 for idx := range service .state .ContainerStatus {
157169 // Will open a separate PR to convert all the NC version related variable to int. Change from string to int is a pain.
@@ -173,12 +185,11 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
173185 }
174186 }
175187 if len (hostVersionNeedsUpdateContainers ) == 0 {
176- return
188+ return nil
177189 }
178190 ncList , err := service .nmagentClient .GetNCVersionList (ctx )
179191 if err != nil {
180- logger .Errorf ("%v" , err )
181- return
192+ return errors .Wrap (err , "failed to get nc version list from nmagent" )
182193 }
183194
184195 newHostNCVersionList := map [string ]string {}
@@ -192,15 +203,13 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
192203 }
193204 version , err := strconv .Atoi (versionStr )
194205 if err != nil {
195- logger .Errorf ("failed to parse %s to int" , versionStr )
196- continue
206+ return errors .Wrapf (err , "failed to parse container version of %s" , ncID )
197207 }
198208
199209 // Check whether it exist in service state and get the related nc info
200210 ncInfo , exist := service .state .ContainerStatus [ncID ]
201211 if ! exist {
202- logger .Errorf ("Can't find NC with ID %s in service state, stop updating this host NC version" , ncID )
203- continue
212+ return errors .Wrapf (errNonExistentContainerStatus , "can't find NC with ID %s in service state, stop updating this host NC version" , ncID )
204213 }
205214 if channelMode == cns .CRD {
206215 service .MarkIpsAsAvailableUntransacted (ncInfo .ID , version )
@@ -210,6 +219,7 @@ func (service *HTTPRestService) SyncHostNCVersion(ctx context.Context, channelMo
210219 service .state .ContainerStatus [ncID ] = ncInfo
211220 logger .Printf ("Updated NC %s host version from %s to %s" , ncID , oldHostNCVersion , ncInfo .HostVersion )
212221 }
222+ return nil
213223}
214224
215225// This API will be called by CNS RequestController on CRD update.
0 commit comments