-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscribe.go
59 lines (49 loc) · 1.38 KB
/
subscribe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/containerd/containerd"
"github.com/containerd/containerd/events"
"github.com/containerd/containerd/namespaces"
)
func main() {
// Create a new context
ctx := namespaces.WithNamespace(context.Background(), "default")
// Connect to the local containerd daemon
client, err := containerd.New("/run/containerd/containerd.sock")
if err != nil {
log.Fatalf("Failed to connect to containerd: %v", err)
}
defer client.Close()
// Subscribe to containerd events
eventCh, errCh := client.Subscribe(ctx)
// Create a signal channel to gracefully exit on interrupt
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
// Handle events and signals in a loop
for {
select {
case event := <-eventCh:
// Handle the received event
handleEvent(event)
case err := <-errCh:
// Handle errors from the event subscription
log.Printf("Error in event subscription: %v", err)
case <-signalCh:
// Handle interrupt signal for graceful exit
fmt.Println("Received interrupt signal. Exiting...")
return
}
}
}
// handleEvent prints information about the received event
func handleEvent(event *events.Envelope) {
fmt.Printf("Received event:\n")
fmt.Printf(" Topic: %s\n", event.Topic)
fmt.Printf(" Namespace: %s\n", event.Namespace)
fmt.Printf("\n")
}