Skip to content

feat: watch network connect / disconnect events #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"os/signal"
"slices"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -281,8 +282,20 @@ func (g *generator) generateFromEvents() {
time.Sleep(10 * time.Second)
break
}
if event.Status == "start" || event.Status == "stop" || event.Status == "die" || strings.Index(event.Status, "health_status:") != -1 {
log.Printf("Received event %s for container %s", event.Status, event.ID[:12])

watchedEvent := false

switch event.Type {
case "container":
watchedContainerActions := []string{"start", "stop", "die", "health_status"}
watchedEvent = slices.Contains(watchedContainerActions, event.Action)
case "network":
watchedNetworkActions := []string{"connect", "disconnect"}
watchedEvent = slices.Contains(watchedNetworkActions, event.Action)
}

if watchedEvent {
log.Printf("Received event %s for %s %s", event.Action, event.Type, event.Actor.ID[:12])
// fanout event to all watchers
for _, watcher := range watchers {
watcher <- event
Expand Down
10 changes: 5 additions & 5 deletions internal/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TestGenerateFromEvents(t *testing.T) {
var counter atomic.Int32

eventsResponse := `
{"status":"start","id":"8dfafdbc3a40","from":"base:latest","time":1374067924}
{"status":"stop","id":"8dfafdbc3a40","from":"base:latest","time":1374067966}
{"status":"start","id":"8dfafdbc3a40","from":"base:latest","time":1374067970}
{"status":"destroy","id":"8dfafdbc3a40","from":"base:latest","time":1374067990}`
{"Type":"container","Action":"start","Actor": {"ID":"8dfafdbc3a40"},"Time":1374067924}
{"Type":"container","Action":"stop","Actor": {"ID":"8dfafdbc3a40"},"Time":1374067966}
{"Type":"container","Action":"start","Actor": {"ID":"8dfafdbc3a40"},"Time":1374067970}
{"Type":"container","Action":"destroy","Actor": {"ID":"8dfafdbc3a40"},"Time":1374067990}`
infoResponse := `{"Containers":1,"Images":1,"Debug":0,"NFd":11,"NGoroutines":21,"MemoryLimit":1,"SwapLimit":0}`
versionResponse := `{"Version":"1.8.0","Os":"Linux","KernelVersion":"3.18.5-tinycore64","GoVersion":"go1.4.1","GitCommit":"a8a31ef","Arch":"amd64","ApiVersion":"1.19"}`
versionResponse := `{"Version":"19.03.12","Os":"Linux","KernelVersion":"4.19.76-linuxkit","GoVersion":"go1.13.14","GitCommit":"48a66213fe","Arch":"amd64","ApiVersion":"1.40"}`

server, _ := dockertest.NewServer("127.0.0.1:0", nil, nil)
server.CustomHandler("/events", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down