@@ -197,29 +197,24 @@ func (t *Tagger) Shutdown(context.Context) error {
197
197
return nil
198
198
}
199
199
200
- // refreshLoop handles the refresh ticks and also responds to shutdown signal
201
- func (t * Tagger ) refreshLoop (refreshInterval time.Duration , stopAfterFirstSuccess bool ) {
200
+ // refreshLoopTags handles the refresh ticks for describe tags and also responds to shutdown signal
201
+ func (t * Tagger ) refreshLoopTags (refreshInterval time.Duration , stopAfterFirstSuccess bool ) {
202
202
refreshTicker := time .NewTicker (refreshInterval )
203
203
defer refreshTicker .Stop ()
204
204
for {
205
205
select {
206
206
case <- refreshTicker .C :
207
- t .logger .Debug ("ec2tagger refreshing" )
207
+ t .logger .Debug ("ec2tagger refreshing tags " )
208
208
allTagsRetrieved := t .ec2TagsRetrieved ()
209
- allVolumesRetrieved := t .ebsVolumesRetrieved ()
210
209
t .logger .Debug ("Retrieve status" ,
211
- zap .Bool ("Ec2AllTagsRetrieved" , allTagsRetrieved ),
212
- zap .Bool ("EbsAllVolumesRetrieved" , allVolumesRetrieved ))
210
+ zap .Bool ("Ec2AllTagsRetrieved" , allTagsRetrieved ))
213
211
refreshTags := len (t .EC2InstanceTagKeys ) > 0
214
- refreshVolumes := len (t .EBSDeviceKeys ) > 0
215
212
216
213
if stopAfterFirstSuccess {
217
214
// need refresh tags when it is configured and not all ec2 tags are retrieved
218
215
refreshTags = refreshTags && ! allTagsRetrieved
219
- // need refresh volumes when it is configured and not all volumes are retrieved
220
- refreshVolumes = refreshVolumes && ! allVolumesRetrieved
221
- if ! refreshTags && ! refreshVolumes {
222
- t .logger .Info ("ec2tagger: Refresh is no longer needed, stop refreshTicker." )
216
+ if ! refreshTags {
217
+ t .logger .Info ("ec2tagger: Refresh for tags is no longer needed, stop refreshTicker." )
223
218
return
224
219
}
225
220
}
@@ -230,6 +225,34 @@ func (t *Tagger) refreshLoop(refreshInterval time.Duration, stopAfterFirstSucces
230
225
}
231
226
}
232
227
228
+ case <- t .shutdownC :
229
+ return
230
+ }
231
+ }
232
+ }
233
+
234
+ // refreshLoopVolumes handles the refresh ticks for describe volumes and also responds to shutdown signal
235
+ func (t * Tagger ) refreshLoopVolumes (refreshInterval time.Duration , stopAfterFirstSuccess bool ) {
236
+ refreshTicker := time .NewTicker (refreshInterval )
237
+ defer refreshTicker .Stop ()
238
+ for {
239
+ select {
240
+ case <- refreshTicker .C :
241
+ t .logger .Debug ("ec2tagger refreshing volumes" )
242
+ allVolumesRetrieved := t .ebsVolumesRetrieved ()
243
+ t .logger .Debug ("Retrieve status" ,
244
+ zap .Bool ("EbsAllVolumesRetrieved" , allVolumesRetrieved ))
245
+ refreshVolumes := len (t .EBSDeviceKeys ) > 0
246
+
247
+ if stopAfterFirstSuccess {
248
+ // need refresh volumes when it is configured and not all volumes are retrieved
249
+ refreshVolumes = refreshVolumes && ! allVolumesRetrieved
250
+ if ! refreshVolumes {
251
+ t .logger .Info ("ec2tagger: Refresh for volumes is no longer needed, stop refreshTicker." )
252
+ return
253
+ }
254
+ }
255
+
233
256
if refreshVolumes {
234
257
if err := t .updateVolumes (); err != nil {
235
258
t .logger .Warn ("ec2tagger: Error refreshing EBS volumes, keeping old values" , zap .Error (err ))
@@ -333,7 +356,8 @@ func (t *Tagger) Start(ctx context.Context, host component.Host) error {
333
356
334
357
go func () { //Async start of initial retrieval to prevent block of agent start
335
358
t .initialRetrievalOfTagsAndVolumes ()
336
- t .refreshLoopToUpdateTagsAndVolumes ()
359
+ t .refreshLoopToUpdateTags ()
360
+ t .refreshLoopToUpdateVolumes ()
337
361
}()
338
362
t .logger .Info ("ec2tagger: EC2 tagger has started initialization." )
339
363
@@ -343,24 +367,49 @@ func (t *Tagger) Start(ctx context.Context, host component.Host) error {
343
367
return nil
344
368
}
345
369
346
- func (t * Tagger ) refreshLoopToUpdateTagsAndVolumes () {
370
+ func (t * Tagger ) refreshLoopToUpdateTags () {
347
371
needRefresh := false
348
372
stopAfterFirstSuccess := false
349
- refreshInterval := t .RefreshIntervalSeconds
350
373
351
- if t .RefreshIntervalSeconds .Seconds () == 0 {
374
+ refreshInterval := t .RefreshTagsInterval
375
+ if refreshInterval .Seconds () == 0 {
352
376
//when the refresh interval is 0, this means that customer don't want to
353
- //update tags/volumes values once they are retrieved successfully. In this case,
377
+ //update tags values once they are retrieved successfully. In this case,
354
378
//we still want to do refresh to make sure all the specified keys for tags/volumes
355
379
//are fetched successfully because initial retrieval might not get all of them.
356
380
//When the specified key is "*", there is no way for us to check if all
357
- //tags/volumes are fetched. So there is no need to do refresh in this case.
358
- needRefresh = ! (len (t .EC2InstanceTagKeys ) == 1 && t .EC2InstanceTagKeys [0 ] == "*" ) ||
359
- ! (len (t .EBSDeviceKeys ) == 1 && t .EBSDeviceKeys [0 ] == "*" )
381
+ //tags are fetched. So there is no need to do refresh in this case.
382
+ needRefresh = ! (len (t .EC2InstanceTagKeys ) == 1 && t .EC2InstanceTagKeys [0 ] == "*" )
383
+
384
+ stopAfterFirstSuccess = true
385
+ refreshInterval = defaultRefreshInterval
386
+ } else if refreshInterval .Seconds () > 0 {
387
+ //customer wants to update the tags with the given refresh interval
388
+ needRefresh = true
389
+ }
390
+
391
+ if needRefresh {
392
+ go func () {
393
+ // randomly stagger the time of the first refresh to mitigate throttling if a whole fleet is
394
+ // restarted at the same time
395
+ sleepUntilHostJitter (refreshInterval )
396
+ t .refreshLoopTags (refreshInterval , stopAfterFirstSuccess )
397
+ }()
398
+ }
399
+ }
400
+
401
+ func (t * Tagger ) refreshLoopToUpdateVolumes () {
402
+ needRefresh := false
403
+ stopAfterFirstSuccess := false
404
+
405
+ refreshInterval := t .RefreshVolumesInterval
406
+ if refreshInterval .Seconds () == 0 {
407
+ needRefresh = ! (len (t .EBSDeviceKeys ) == 1 && t .EBSDeviceKeys [0 ] == "*" )
408
+
360
409
stopAfterFirstSuccess = true
361
410
refreshInterval = defaultRefreshInterval
362
- } else if t . RefreshIntervalSeconds .Seconds () > 0 {
363
- //customer wants to update the tags/ volumes with the given refresh interval
411
+ } else if refreshInterval .Seconds () > 0 {
412
+ //customer wants to update the volumes with the given refresh interval
364
413
needRefresh = true
365
414
}
366
415
@@ -369,7 +418,7 @@ func (t *Tagger) refreshLoopToUpdateTagsAndVolumes() {
369
418
// randomly stagger the time of the first refresh to mitigate throttling if a whole fleet is
370
419
// restarted at the same time
371
420
sleepUntilHostJitter (refreshInterval )
372
- t .refreshLoop (refreshInterval , stopAfterFirstSuccess )
421
+ t .refreshLoopVolumes (refreshInterval , stopAfterFirstSuccess )
373
422
}()
374
423
}
375
424
}
0 commit comments