-
Notifications
You must be signed in to change notification settings - Fork 1k
/
container_events.go
76 lines (64 loc) · 1.86 KB
/
container_events.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package server
import (
"sync"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
type containerEventConn struct {
wg sync.WaitGroup
err error
}
// GetContainerEvents sends the stream of container events to clients
func (s *Server) GetContainerEvents(_ *types.GetEventsRequest, ces types.RuntimeService_GetContainerEventsServer) error {
if !s.Config().EnablePodEvents {
return nil
}
s.containerEventStreamBroadcaster.Do(func() {
// note that this function will run indefinitely until ContainerEventsChan is closed
go s.broadcastEvents()
})
conn := &containerEventConn{
wg: sync.WaitGroup{},
}
s.containerEventClients.Store(ces, conn)
conn.wg.Add(1)
// wait here until we don't want to send events to this client anymore
conn.wg.Wait()
s.containerEventClients.Delete(ces)
return conn.err
}
func (s *Server) broadcastEvents() {
// notify all connections that ContainerEventsChan has been closed
defer s.containerEventClients.Range(func(_, value any) bool { // nolint: unparam
conn, ok := value.(*containerEventConn)
if !ok {
return true
}
conn.wg.Done()
return true
})
for containerEvent := range s.ContainerEventsChan {
s.containerEventClients.Range(func(key, value any) bool {
stream, ok := key.(types.RuntimeService_GetContainerEventsServer)
if !ok {
return true
}
conn, ok := value.(*containerEventConn)
if !ok {
return true
}
if err := stream.Send(&containerEvent); err != nil {
code, _ := status.FromError(err)
// when the client closes the connection this error is expected
// so only log non transport closing errors
if code.Code() != codes.Unavailable && code.Message() != "transport is closing" {
conn.err = err
}
// notify our waiting client connection that we are done
conn.wg.Done()
}
return true
})
}
}