Skip to content

Commit f23a357

Browse files
authored
Merge pull request #109 from thin-edge/fix-filter-options
fix: respect filter options when triggered by engine events
2 parents ff52c13 + 9cf7551 commit f23a357

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

pkg/app/app.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ func (a *App) Monitor(ctx context.Context, filterOptions container.FilterOptions
280280
slog.Info("Stopping engine monitor")
281281
return ctx.Err()
282282
case evt := <-evtCh:
283+
slog.Info("Received event.", "value", evt)
283284
switch evt.Type {
284285
case events.ContainerEventType:
285286
// Note: health checks will run command inside the containers periodically, and this results
@@ -315,26 +316,33 @@ func (a *App) Monitor(ctx context.Context, filterOptions container.FilterOptions
315316

316317
switch evt.Action {
317318
case events.ActionExecDie, events.ActionCreate, events.ActionStart, events.ActionStop, events.ActionPause, events.ActionUnPause, events.ActionHealthStatusHealthy, events.ActionHealthStatusUnhealthy:
318-
go func() {
319+
go func(evt events.Message) {
319320
// Delay before trigger update to allow the service status to be updated
320321
time.Sleep(500 * time.Millisecond)
321322
if err := a.Update(container.FilterOptions{
322323
IDs: []string{evt.Actor.ID},
324+
325+
// Preserve default filter options
326+
Names: filterOptions.Names,
327+
Labels: filterOptions.Labels,
328+
Types: filterOptions.Types,
329+
ExcludeNames: filterOptions.ExcludeNames,
330+
ExcludeWithLabel: filterOptions.ExcludeWithLabel,
323331
}); err != nil {
324332
slog.Warn("Error updating container state.", "err", err)
325333
}
326-
}()
334+
}(evt)
327335
case events.ActionDestroy, events.ActionRemove, events.ActionDie:
328336
slog.Info("Container removed/destroyed", "container", evt.Actor.ID, "attributes", evt.Actor.Attributes)
329337
// TODO: Trigger a removal instead of checking the whole state
330338
// Lookup container name by container id (from the entity store) as lookup by name won't work for container-groups
331-
go func() {
339+
go func(evt events.Message) {
332340
// Delay before trigger update to allow the service status to be updated
333341
time.Sleep(500 * time.Millisecond)
334-
if err := a.Update(container.FilterOptions{}); err != nil {
342+
if err := a.Update(filterOptions); err != nil {
335343
slog.Warn("Error updating container state.", "err", err)
336344
}
337-
}()
345+
}(evt)
338346
}
339347

340348
if a.config.EnableEngineEvents {
@@ -345,8 +353,6 @@ func (a *App) Monitor(ctx context.Context, filterOptions container.FilterOptions
345353
}
346354
}
347355
}
348-
349-
slog.Info("Received event.", "value", evt)
350356
case err := <-errCh:
351357
if errors.Is(err, io.EOF) {
352358
slog.Info("No more events")

pkg/container/container.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,6 @@ func (c *ContainerClient) List(ctx context.Context, options FilterOptions) ([]Te
418418
})
419419
}
420420

421-
// filterValues = append(filterValues, filters.Arg("label", "com.docker.compose.project"))
422-
423421
// Match by label
424422
for _, label := range options.Labels {
425423
filterValues = append(filterValues, filters.KeyValuePair{

tests/operations-clone.robot

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ Clone Existing Container but Waiting For Exit
5959
${next_container_id}= DeviceLibrary.Execute Command cmd=tedge-container engine docker inspect app4 --format "{{.Id}}"
6060
Should Not Be Equal ${next_container_id} ${prev_container_id}
6161

62+
Ignore Containers With Given Label
63+
# ignore using label
64+
${prev_container_id}= DeviceLibrary.Execute Command cmd=sudo tedge-container engine docker run -d --label tedge.ignore=1 --name httpapp1 httpd:2.4.61-alpine
65+
Sleep 10s
66+
DeviceLibrary.Execute Command cmd=sudo tedge-container engine docker inspect httpapp1
67+
Cumulocity.Should Have Services service_type=container name=httpapp1 min_count=0 max_count=0 timeout=10
68+
69+
# don't ignore
70+
${prev_container_id}= DeviceLibrary.Execute Command cmd=sudo tedge-container engine docker run -d --name httpapp2 httpd:2.4.61-alpine
71+
Sleep 10s
72+
DeviceLibrary.Execute Command cmd=sudo tedge-container engine docker inspect httpapp2
73+
Cumulocity.Should Have Services service_type=container name=httpapp2 min_count=1 max_count=1 timeout=10
74+
6275
*** Keywords ***
6376

6477
Suite Setup

0 commit comments

Comments
 (0)