-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
check.go
173 lines (145 loc) · 4.8 KB
/
check.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
// 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 2022-present Datadog, Inc.
// Package containerimage implements the container image check.
package containerimage
import (
"errors"
"time"
"gopkg.in/yaml.v2"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
"github.com/DataDog/datadog-agent/comp/core/workloadmeta"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
"github.com/DataDog/datadog-agent/pkg/collector/check"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
ddConfig "github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/DataDog/datadog-agent/pkg/util/optional"
)
const (
// CheckName is the name of the check
CheckName = "container_image"
)
// Config holds the container_image check configuration
type Config struct {
ChunkSize int `yaml:"chunk_size"`
NewImagesMaxLatencySeconds int `yaml:"new_images_max_latency_seconds"`
PeriodicRefreshSeconds int `yaml:"periodic_refresh_seconds"`
}
type configValueRange struct {
min int
max int
defaultValue int
}
var /* const */ (
chunkSizeValueRange = &configValueRange{
min: 1,
max: 100,
defaultValue: 10,
}
newImagesMaxLatencySecondsValueRange = &configValueRange{
min: 1, // 1 s
max: 300, // 5 min
defaultValue: 30, // 30 s
}
periodicRefreshSecondsValueRange = &configValueRange{
min: 60, // 1 min
max: 86400, // 1 day
defaultValue: 300, // 5 min
}
)
func validateValue(val *int, valueRange *configValueRange) {
if *val == 0 {
*val = valueRange.defaultValue
} else if *val < valueRange.min {
*val = valueRange.min
} else if *val > valueRange.max {
*val = valueRange.max
}
}
// Parse parses the configuration
func (c *Config) Parse(data []byte) error {
if err := yaml.Unmarshal(data, c); err != nil {
return err
}
validateValue(&c.ChunkSize, chunkSizeValueRange)
validateValue(&c.NewImagesMaxLatencySeconds, newImagesMaxLatencySecondsValueRange)
validateValue(&c.PeriodicRefreshSeconds, periodicRefreshSecondsValueRange)
return nil
}
// Check reports container images
type Check struct {
core.CheckBase
workloadmetaStore workloadmeta.Component
instance *Config
processor *processor
stopCh chan struct{}
}
// Factory returns a new check factory
func Factory(store workloadmeta.Component) optional.Option[func() check.Check] {
return optional.NewOption(func() check.Check {
return core.NewLongRunningCheckWrapper(&Check{
CheckBase: core.NewCheckBase(CheckName),
workloadmetaStore: store,
instance: &Config{},
stopCh: make(chan struct{}),
})
})
}
// Configure parses the check configuration and initializes the container_image check
func (c *Check) Configure(senderManager sender.SenderManager, _ uint64, config, initConfig integration.Data, source string) error {
if !ddConfig.Datadog.GetBool("container_image.enabled") {
return errors.New("collection of container images is disabled")
}
if err := c.CommonConfigure(senderManager, initConfig, config, source); err != nil {
return err
}
if err := c.instance.Parse(config); err != nil {
return err
}
sender, err := c.GetSender()
if err != nil {
return err
}
c.processor = newProcessor(sender, c.instance.ChunkSize, time.Duration(c.instance.NewImagesMaxLatencySeconds)*time.Second)
return nil
}
// Run starts the container_image check
func (c *Check) Run() error {
log.Infof("Starting long-running check %q", c.ID())
defer log.Infof("Shutting down long-running check %q", c.ID())
filterParams := workloadmeta.FilterParams{
Kinds: []workloadmeta.Kind{workloadmeta.KindContainerImageMetadata},
Source: workloadmeta.SourceAll,
EventType: workloadmeta.EventTypeSet, // We don’t care about images removal because we just have to wait for them to expire on BE side once we stopped refreshing them periodically.
}
imgEventsCh := c.workloadmetaStore.Subscribe(
CheckName,
workloadmeta.NormalPriority,
workloadmeta.NewFilter(&filterParams),
)
imgRefreshTicker := time.NewTicker(time.Duration(c.instance.PeriodicRefreshSeconds) * time.Second)
defer c.processor.stop()
for {
select {
case eventBundle, ok := <-imgEventsCh:
if !ok {
return nil
}
c.processor.processEvents(eventBundle)
case <-imgRefreshTicker.C:
c.processor.processRefresh(c.workloadmetaStore.ListImages())
case <-c.stopCh:
return nil
}
}
}
// Stop stops the container_image check
func (c *Check) Stop() {
close(c.stopCh)
}
// Interval returns 0. It makes container_image a long-running check
func (c *Check) Interval() time.Duration {
return 0
}