-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtypes.go
663 lines (546 loc) · 20.2 KB
/
types.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// 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
import (
"context"
"fmt"
"strings"
"time"
"github.com/mohae/deepcopy"
"github.com/DataDog/datadog-agent/pkg/util/containers"
)
// Store is a central storage of metadata about workloads. A workload is any
// unit of work being done by a piece of software, like a process, a container,
// a kubernetes pod, or a task in any cloud provider.
//
// Typically there is only one instance, accessed via GetGlobalStore.
type Store interface {
// Start starts the store, asynchronously initializing collectors and
// beginning to gather workload data. This is typically called during
// agent startup.
Start(ctx context.Context)
// Subscribe subscribes the caller to events representing changes to the
// store, limited to events matching the filter. The name is used for
// telemetry and debugging.
//
// The first message on the channel is special: it contains an EventTypeSet
// event for each entity currently in the store. If the Subscribe call
// occurs at agent startup, then the first message approximates entities
// that were running before the agent started. This is an inherently racy
// distinction, but may be useful for decisions such as whether to begin
// logging at the head or tail of an entity's logs.
//
// Multiple EventTypeSet messages may be sent, either as the entity's state
// evolves or as information about the entity is reported from multiple
// sources (such as a container runtime and an orchestrator).
//
// See the documentation for EventBundle regarding appropropriate handling
// for messages on this channel.
Subscribe(name string, priority SubscriberPriority, filter *Filter) chan EventBundle
// Unsubscribe reverses the effect of Subscribe.
Unsubscribe(ch chan EventBundle)
// GetContainer returns metadata about a container. It fetches the entity
// with kind KindContainer and the given ID.
GetContainer(id string) (*Container, error)
// ListContainers returns metadata about all known containers, equivalent
// to all entities with kind KindContainer.
ListContainers() []*Container
// ListContainersWithFilter returns all the containers for which the passed
// filter evaluates to true.
ListContainersWithFilter(filter ContainerFilterFunc) []*Container
// GetKubernetesPod returns metadata about a Kubernetes pod. It fetches
// the entity with kind KindKubernetesPod and the given ID.
GetKubernetesPod(id string) (*KubernetesPod, error)
// GetKubernetesPodForContainer searches all known KubernetesPod entities
// for one containing the given container.
GetKubernetesPodForContainer(containerID string) (*KubernetesPod, error)
// GetECSTask returns metadata about an ECS task. It fetches the entity with
// kind KindECSTask and the given ID.
GetECSTask(id string) (*ECSTask, error)
// Notify notifies the store with a slice of events. It should only be
// used by workloadmeta collectors.
Notify(events []CollectorEvent)
// Dump lists the content of the store, for debugging purposes.
Dump(verbose bool) WorkloadDumpResponse
}
// Kind is the kind of an entity.
type Kind string
// Defined Kinds
const (
KindContainer Kind = "container"
KindKubernetesPod Kind = "kubernetes_pod"
KindECSTask Kind = "ecs_task"
)
// Source is the source name of an entity.
type Source string
// Defined Sources
const (
// SourceAll matches any source. Should not be returned by collectors,
// as its only meant to be used in filters.
SourceAll Source = ""
// SourceRuntime represents entities detected by the container runtime
// running on the node, collecting lower level information about
// containers. `docker`, `containerd`, `podman` and `ecs_fargate` use
// this source.
SourceRuntime Source = "runtime"
// SourceNodeOrchestrator represents entities detected by the node
// agent from an orchestrator. `kubelet` and `ecs` use this.
SourceNodeOrchestrator Source = "node_orchestrator"
// SourceClusterOrchestrator represents entities detected by calling
// the central component of an orchestrator, or the Datadog Cluster
// Agent. `kube_metadata` and `cloudfoundry` use this.
SourceClusterOrchestrator Source = "cluster_orchestrator"
)
// ContainerRuntime is the container runtime used by a container.
type ContainerRuntime string
// Defined ContainerRuntimes
const (
ContainerRuntimeDocker ContainerRuntime = "docker"
ContainerRuntimeContainerd ContainerRuntime = "containerd"
ContainerRuntimePodman ContainerRuntime = "podman"
ContainerRuntimeCRIO ContainerRuntime = "cri-o"
ContainerRuntimeGarden ContainerRuntime = "garden"
// ECS Fargate can be considered as a runtime in the sense that we don't
// know the actual runtime but we need to identify it's Fargate
ContainerRuntimeECSFargate ContainerRuntime = "ecsfargate"
)
// ContainerStatus is the status of the container
type ContainerStatus string
// Defined ContainerStatus
const (
ContainerStatusUnknown ContainerStatus = "unknown"
ContainerStatusCreated ContainerStatus = "created"
ContainerStatusRunning ContainerStatus = "running"
ContainerStatusRestarting ContainerStatus = "restarting"
ContainerStatusPaused ContainerStatus = "paused"
ContainerStatusStopped ContainerStatus = "stopped"
)
// ContainerHealth is the health of the container
type ContainerHealth string
// Defined ContainerHealth
const (
ContainerHealthUnknown ContainerHealth = "unknown"
ContainerHealthHealthy ContainerHealth = "healthy"
ContainerHealthUnhealthy ContainerHealth = "unhealthy"
)
// ECSLaunchType is the launch type of an ECS task.
type ECSLaunchType string
// Defined ECSLaunchTypes
const (
ECSLaunchTypeEC2 ECSLaunchType = "ec2"
ECSLaunchTypeFargate ECSLaunchType = "fargate"
)
// EventType is the type of an event (set or unset).
type EventType int
const (
// EventTypeAll matches any event type. Should not be returned by
// collectors, as it is only meant to be used in filters.
EventTypeAll EventType = iota
// EventTypeSet indicates that an entity has been added or updated.
EventTypeSet
// EventTypeUnset indicates that an entity has been removed. If multiple
// sources provide data for an entity, this message is only sent when the
// last source stops providing that data.
EventTypeUnset
)
// Entity represents a single unit of work being done that is of interest to
// the agent.
//
// This interface is implemented by several concrete types, and is typically
// cast to that concrete type to get detailed information. The concrete type
// corresponds to the entity's type (GetID().Kind), and it is safe to make an
// unchecked cast.
type Entity interface {
// GetID gets the EntityID for this entity.
GetID() EntityID
// Merge merges this entity with another of the same kind. This is used
// to generate a composite entity representing data from several sources.
Merge(Entity) error
// DeepCopy copies an entity such that modifications of the copy will not
// affect the original.
DeepCopy() Entity
// String provides a summary of the entity. The string may span several lines,
// especially if verbose.
String(verbose bool) string
}
// EntityID represents the ID of an Entity. Note that entities from different sources
// may have the same EntityID.
type EntityID struct {
// Kind identifies the kind of entity. This typically corresponds to the concrete
// type of the Entity, but this is not always the case; see Entity for details.
Kind Kind
// ID is the ID for this entity, in a format specific to the entity Kind.
ID string
}
// String implements Entity#String.
func (i EntityID) String(_ bool) string {
return fmt.Sprintln("Kind:", i.Kind, "ID:", i.ID)
}
// EntityMeta represents generic metadata about an Entity.
type EntityMeta struct {
Name string
Namespace string
Annotations map[string]string
Labels map[string]string
}
// String returns a string representation of EntityMeta.
func (e EntityMeta) String(verbose bool) string {
var sb strings.Builder
_, _ = fmt.Fprintln(&sb, "Name:", e.Name)
_, _ = fmt.Fprintln(&sb, "Namespace:", e.Namespace)
if verbose {
_, _ = fmt.Fprintln(&sb, "Annotations:", mapToString(e.Annotations))
_, _ = fmt.Fprintln(&sb, "Labels:", mapToString(e.Labels))
}
return sb.String()
}
// ContainerImage is the an image used by a container.
type ContainerImage struct {
ID string
RawName string
Name string
ShortName string
Tag string
}
// NewContainerImage builds a ContainerImage from an image name
func NewContainerImage(imageName string) (ContainerImage, error) {
image := ContainerImage{
RawName: imageName,
Name: imageName,
}
name, shortName, tag, err := containers.SplitImageName(imageName)
if err != nil {
return image, err
}
if tag == "" {
tag = "latest"
}
image.Name = name
image.ShortName = shortName
image.Tag = tag
return image, nil
}
// String returns a string representation of ContainerImage.
func (c ContainerImage) String(verbose bool) string {
var sb strings.Builder
_, _ = fmt.Fprintln(&sb, "Name:", c.Name)
_, _ = fmt.Fprintln(&sb, "Tag:", c.Tag)
if verbose {
_, _ = fmt.Fprintln(&sb, "ID:", c.ID)
_, _ = fmt.Fprintln(&sb, "Raw Name:", c.RawName)
_, _ = fmt.Fprintln(&sb, "Short Name:", c.ShortName)
}
return sb.String()
}
// ContainerState is the state of a container.
type ContainerState struct {
Running bool
Status ContainerStatus
Health ContainerHealth
CreatedAt time.Time
StartedAt time.Time
FinishedAt time.Time
ExitCode *uint32
}
// String returns a string representation of ContainerState.
func (c ContainerState) String(verbose bool) string {
var sb strings.Builder
_, _ = fmt.Fprintln(&sb, "Running:", c.Running)
if verbose {
_, _ = fmt.Fprintln(&sb, "Status:", c.Status)
_, _ = fmt.Fprintln(&sb, "Health:", c.Health)
_, _ = fmt.Fprintln(&sb, "Created At:", c.CreatedAt)
_, _ = fmt.Fprintln(&sb, "Started At:", c.StartedAt)
_, _ = fmt.Fprintln(&sb, "Finished At:", c.FinishedAt)
if c.ExitCode != nil {
_, _ = fmt.Fprintln(&sb, "Exit Code:", *c.ExitCode)
}
}
return sb.String()
}
// ContainerPort is a port open in the container.
type ContainerPort struct {
Name string
Port int
Protocol string
}
// String returns a string representation of ContainerPort.
func (c ContainerPort) String(verbose bool) string {
var sb strings.Builder
_, _ = fmt.Fprintln(&sb, "Port:", c.Port)
if verbose {
_, _ = fmt.Fprintln(&sb, "Name:", c.Name)
_, _ = fmt.Fprintln(&sb, "Protocol:", c.Protocol)
}
return sb.String()
}
// OrchestratorContainer is a reference to a Container with
// orchestrator-specific data attached to it.
type OrchestratorContainer struct {
ID string
Name string
Image ContainerImage
}
// String returns a string representation of OrchestratorContainer.
func (o OrchestratorContainer) String(_ bool) string {
return fmt.Sprintln("Name:", o.Name, "ID:", o.ID)
}
// Container is an Entity representing a containerized workload.
type Container struct {
EntityID
EntityMeta
EnvVars map[string]string
Hostname string
Image ContainerImage
NetworkIPs map[string]string
PID int
Ports []ContainerPort
Runtime ContainerRuntime
State ContainerState
// CollectorTags represent tags coming from the collector itself
// and that it would impossible to compute later on
CollectorTags []string
}
// GetID implements Entity#GetID.
func (c Container) GetID() EntityID {
return c.EntityID
}
// Merge implements Entity#Merge.
func (c *Container) Merge(e Entity) error {
cc, ok := e.(*Container)
if !ok {
return fmt.Errorf("cannot merge Container with different kind %T", e)
}
return merge(c, cc)
}
// DeepCopy implements Entity#DeepCopy.
func (c Container) DeepCopy() Entity {
cp := deepcopy.Copy(c).(Container)
return &cp
}
// String implements Entity#String.
func (c Container) String(verbose bool) string {
var sb strings.Builder
_, _ = fmt.Fprintln(&sb, "----------- Entity ID -----------")
_, _ = fmt.Fprint(&sb, c.EntityID.String(verbose))
_, _ = fmt.Fprintln(&sb, "----------- Entity Meta -----------")
_, _ = fmt.Fprint(&sb, c.EntityMeta.String(verbose))
_, _ = fmt.Fprintln(&sb, "----------- Image -----------")
_, _ = fmt.Fprint(&sb, c.Image.String(verbose))
_, _ = fmt.Fprintln(&sb, "----------- Container Info -----------")
_, _ = fmt.Fprintln(&sb, "Runtime:", c.Runtime)
_, _ = fmt.Fprint(&sb, c.State.String(verbose))
if verbose {
_, _ = fmt.Fprintln(&sb, "Allowed env variables:", filterAndFormatEnvVars(c.EnvVars))
_, _ = fmt.Fprintln(&sb, "Hostname:", c.Hostname)
_, _ = fmt.Fprintln(&sb, "Network IPs:", mapToString(c.NetworkIPs))
_, _ = fmt.Fprintln(&sb, "PID:", c.PID)
}
if len(c.Ports) > 0 && verbose {
_, _ = fmt.Fprintln(&sb, "----------- Ports -----------")
for _, p := range c.Ports {
_, _ = fmt.Fprint(&sb, p.String(verbose))
}
}
return sb.String()
}
var _ Entity = &Container{}
// ContainerFilterFunc is a function used to filter containers.
type ContainerFilterFunc func(container *Container) bool
// GetRunningContainers is a function that evaluates to true for running containers.
var GetRunningContainers ContainerFilterFunc = func(container *Container) bool { return container.State.Running }
// KubernetesPod is an Entity representing a Kubernetes Pod.
type KubernetesPod struct {
EntityID
EntityMeta
Owners []KubernetesPodOwner
PersistentVolumeClaimNames []string
Containers []OrchestratorContainer
Ready bool
Phase string
IP string
PriorityClass string
QOSClass string
KubeServices []string
NamespaceLabels map[string]string
}
// GetID implements Entity#GetID.
func (p KubernetesPod) GetID() EntityID {
return p.EntityID
}
// Merge implements Entity#Merge.
func (p *KubernetesPod) Merge(e Entity) error {
pp, ok := e.(*KubernetesPod)
if !ok {
return fmt.Errorf("cannot merge KubernetesPod with different kind %T", e)
}
return merge(p, pp)
}
// DeepCopy implements Entity#DeepCopy.
func (p KubernetesPod) DeepCopy() Entity {
cp := deepcopy.Copy(p).(KubernetesPod)
return &cp
}
// String implements Entity#String.
func (p KubernetesPod) String(verbose bool) string {
var sb strings.Builder
_, _ = fmt.Fprintln(&sb, "----------- Entity ID -----------")
_, _ = fmt.Fprintln(&sb, p.EntityID.String(verbose))
_, _ = fmt.Fprintln(&sb, "----------- Entity Meta -----------")
_, _ = fmt.Fprint(&sb, p.EntityMeta.String(verbose))
if len(p.Owners) > 0 {
_, _ = fmt.Fprintln(&sb, "----------- Owners -----------")
for _, o := range p.Owners {
_, _ = fmt.Fprint(&sb, o.String(verbose))
}
}
if len(p.Containers) > 0 {
_, _ = fmt.Fprintln(&sb, "----------- Containers -----------")
for _, c := range p.Containers {
_, _ = fmt.Fprint(&sb, c.String(verbose))
}
}
_, _ = fmt.Fprintln(&sb, "----------- Pod Info -----------")
_, _ = fmt.Fprintln(&sb, "Ready:", p.Ready)
_, _ = fmt.Fprintln(&sb, "Phase:", p.Phase)
_, _ = fmt.Fprintln(&sb, "IP:", p.IP)
if verbose {
_, _ = fmt.Fprintln(&sb, "Priority Class:", p.PriorityClass)
_, _ = fmt.Fprintln(&sb, "QOS Class:", p.QOSClass)
_, _ = fmt.Fprintln(&sb, "PVCs:", sliceToString(p.PersistentVolumeClaimNames))
_, _ = fmt.Fprintln(&sb, "Kube Services:", sliceToString(p.KubeServices))
_, _ = fmt.Fprintln(&sb, "Namespace Labels:", mapToString(p.NamespaceLabels))
}
return sb.String()
}
var _ Entity = &KubernetesPod{}
// KubernetesPodOwner is extracted from a pod's owner references.
type KubernetesPodOwner struct {
Kind string
Name string
ID string
}
// String returns a string representation of KubernetesPodOwner.
func (o KubernetesPodOwner) String(verbose bool) string {
var sb strings.Builder
_, _ = fmt.Fprintln(&sb, "Kind:", o.Kind, "Name:", o.Name)
if verbose {
_, _ = fmt.Fprintln(&sb, "ID:", o.ID)
}
return sb.String()
}
// ECSTask is an Entity representing an ECS Task.
type ECSTask struct {
EntityID
EntityMeta
Tags map[string]string
ContainerInstanceTags map[string]string
ClusterName string
Region string
AvailabilityZone string
Family string
Version string
LaunchType ECSLaunchType
Containers []OrchestratorContainer
}
// GetID implements Entity#GetID.
func (t ECSTask) GetID() EntityID {
return t.EntityID
}
// Merge implements Entity#Merge.
func (t *ECSTask) Merge(e Entity) error {
tt, ok := e.(*ECSTask)
if !ok {
return fmt.Errorf("cannot merge ECSTask with different kind %T", e)
}
return merge(t, tt)
}
// DeepCopy implements Entity#DeepCopy.
func (t ECSTask) DeepCopy() Entity {
cp := deepcopy.Copy(t).(ECSTask)
return &cp
}
// String implements Entity#String.
func (t ECSTask) String(verbose bool) string {
var sb strings.Builder
_, _ = fmt.Fprintln(&sb, "----------- Entity ID -----------")
_, _ = fmt.Fprint(&sb, t.EntityID.String(verbose))
_, _ = fmt.Fprintln(&sb, "----------- Entity Meta -----------")
_, _ = fmt.Fprint(&sb, t.EntityMeta.String(verbose))
_, _ = fmt.Fprintln(&sb, "----------- Containers -----------")
for _, c := range t.Containers {
_, _ = fmt.Fprint(&sb, c.String(verbose))
}
if verbose {
_, _ = fmt.Fprintln(&sb, "----------- Task Info -----------")
_, _ = fmt.Fprintln(&sb, "Tags:", mapToString(t.Tags))
_, _ = fmt.Fprintln(&sb, "Container Instance Tags:", mapToString(t.ContainerInstanceTags))
_, _ = fmt.Fprintln(&sb, "Cluster Name:", t.ClusterName)
_, _ = fmt.Fprintln(&sb, "Region:", t.Region)
_, _ = fmt.Fprintln(&sb, "Availability Zone:", t.AvailabilityZone)
_, _ = fmt.Fprintln(&sb, "Family:", t.Family)
_, _ = fmt.Fprintln(&sb, "Version:", t.Version)
_, _ = fmt.Fprintln(&sb, "Launch Type:", t.LaunchType)
}
return sb.String()
}
var _ Entity = &ECSTask{}
// CollectorEvent is an event generated by a metadata collector, to be handled
// by the metadata store.
type CollectorEvent struct {
Type EventType
Source Source
Entity Entity
}
// Event represents a change to an entity.
type Event struct {
// Type gives the type of this event.
//
// When Type is EventTypeSet, this represents an added or updated entity.
// Multiple set events may be sent for a single entity.
//
// When Type is EventTypeUnset, this represents a removed entity.
Type EventType
// Entity is the entity involved in this event. For an EventTypeSet event,
// this may contain information "merged" from multiple sources. For an
// unset event it contains only an EntityID.
//
// For Type == EventTypeSet, this field can be cast unconditionally to the
// concrete type corresponding to its kind (Entity.GetID().Kind). For Type
// == EventTypeUnset, only the Entity ID is available and such a cast will
// fail.
Entity Entity
}
// SubscriberPriority is a priority for subscribers to the store. Subscribers
// are notified in order by their priority, with each notification blocking the
// next, so this allows control of which compoents are informed of changes in
// the store first.
type SubscriberPriority int
const (
// TaggerPriority is the priority for the Tagger. The Tagger must always
// come first.
TaggerPriority SubscriberPriority = iota
// ConfigProviderPriority is the priority for the AD Config Provider.
// This should come before other subscribers so that config provided by
// entities is available to those other subscribers.
ConfigProviderPriority SubscriberPriority = iota
// NormalPriority should be used by subscribers on which other components
// do not depend.
NormalPriority SubscriberPriority = iota
)
// EventBundle is a collection of events sent to Store subscribers.
//
// Subscribers are expected to respond to EventBundles quickly. The Store will
// not move on to notify the next subscriber until the included channel Ch is
// closed. Subscribers which need to update their state before other
// subscribers are notified should close this channel once those updates are
// complete. Other subscribers should close the channel immediately.
// See the example for Store#Subscribe for details.
type EventBundle struct {
// Events gives the events in this bundle.
Events []Event
// Ch should be closed once the subscriber has handled the event.
Ch chan struct{}
}