-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
workloadmeta.go
135 lines (113 loc) · 3.91 KB
/
workloadmeta.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// Package workloadmeta implements the remote workloadmeta Collector.
package workloadmeta
import (
"context"
"fmt"
"go.uber.org/fx"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta/collectors/internal/remote"
"github.com/DataDog/datadog-agent/pkg/config"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core"
grpcutil "github.com/DataDog/datadog-agent/pkg/util/grpc"
protoutils "github.com/DataDog/datadog-agent/pkg/util/proto"
)
const (
collectorID = "remote-workloadmeta"
)
type client struct {
cl pb.AgentSecureClient
}
func (c *client) StreamEntities(ctx context.Context, _ ...grpc.CallOption) (remote.Stream, error) {
streamcl, err := c.cl.WorkloadmetaStreamEntities(
ctx,
&pb.WorkloadmetaStreamRequest{
Filter: nil, // Subscribes to all events
},
)
if err != nil {
return nil, err
}
return &stream{cl: streamcl}, nil
}
type stream struct {
cl pb.AgentSecure_WorkloadmetaStreamEntitiesClient
}
func (s *stream) Recv() (interface{}, error) {
return s.cl.Recv()
}
type streamHandler struct {
port int
config.Config
}
// NewCollector returns a CollectorProvider to build a remote workloadmeta collector, and an error if any.
func NewCollector() (workloadmeta.CollectorProvider, error) {
return workloadmeta.CollectorProvider{
Collector: &remote.GenericCollector{
CollectorID: collectorID,
StreamHandler: &streamHandler{Config: config.Datadog},
Catalog: workloadmeta.Remote,
},
}, nil
}
// GetFxOptions returns the FX framework options for the collector
func GetFxOptions() fx.Option {
return fx.Provide(NewCollector)
}
func init() {
// TODO(components): verify the grpclogin is initialized elsewhere and cleanup
grpclog.SetLoggerV2(grpcutil.NewLogger())
}
func (s *streamHandler) Port() int {
if s.port == 0 {
return s.Config.GetInt("cmd_port")
}
// for tests
return s.port
}
func (s *streamHandler) NewClient(cc grpc.ClientConnInterface) remote.GrpcClient {
return &client{cl: pb.NewAgentSecureClient(cc)}
}
// IsEnabled always return true for the remote workloadmeta because it uses the remote catalog
func (s *streamHandler) IsEnabled() bool {
return true
}
func (s *streamHandler) HandleResponse(resp interface{}) ([]workloadmeta.CollectorEvent, error) {
response, ok := resp.(*pb.WorkloadmetaStreamResponse)
if !ok {
return nil, fmt.Errorf("incorrect response type")
}
var collectorEvents []workloadmeta.CollectorEvent
for _, protoEvent := range response.Events {
workloadmetaEvent, err := protoutils.WorkloadmetaEventFromProtoEvent(protoEvent)
if err != nil {
return nil, err
}
collectorEvent := workloadmeta.CollectorEvent{
Type: workloadmetaEvent.Type,
Source: workloadmeta.SourceRemoteWorkloadmeta,
Entity: workloadmetaEvent.Entity,
}
collectorEvents = append(collectorEvents, collectorEvent)
}
return collectorEvents, nil
}
func (s *streamHandler) HandleResync(store workloadmeta.Component, events []workloadmeta.CollectorEvent) {
entities := make([]workloadmeta.Entity, 0, len(events))
for _, event := range events {
entities = append(entities, event.Entity)
}
// This should be the first response that we got from workloadmeta after
// we lost the connection and specified that a re-sync is needed. So, at
// this point we know that "entities" contains all the existing entities
// in the store, because when a client subscribes to workloadmeta, the
// first response is always a bundle of events with all the existing
// entities in the store that match the filters specified (see
// workloadmeta.Store#Subscribe).
store.Reset(entities, workloadmeta.SourceRemoteWorkloadmeta)
}