-
Notifications
You must be signed in to change notification settings - Fork 10
/
filter.go
244 lines (221 loc) · 7.54 KB
/
filter.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
// 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 containers
import (
"fmt"
"regexp"
"strings"
"github.com/n9e/n9e-agentd/pkg/config"
"k8s.io/klog/v2"
)
const (
// Pause container image names that should be filtered out.
// Where appropriate, each constant is loosely structured as
// image:domain.*/pause.*
pauseContainerKubernetes = "image:kubernetes/pause"
pauseContainerECS = "image:amazon/amazon-ecs-pause"
pauseContainerOpenshift = "image:openshift/origin-pod"
pauseContainerOpenshift3 = "image:.*rhel7/pod-infrastructure"
// - asia.gcr.io/google-containers/pause-amd64
// - gcr.io/google-containers/pause
// - *.gcr.io/google_containers/pause
// - *.jfrog.io/google_containers/pause
pauseContainerGoogle = "image:google(_|-)containers/pause.*"
// - k8s.gcr.io/pause-amd64:3.1
// - asia.gcr.io/google_containers/pause-amd64:3.0
// - gcr.io/google_containers/pause-amd64:3.0
// - gcr.io/gke-release/pause-win:1.1.0
// - eu.gcr.io/k8s-artifacts-prod/pause:3.3
// - k8s.gcr.io/pause
pauseContainerGCR = `image:.*gcr\.io(.*)/pause.*`
// - k8s-gcrio.azureedge.net/pause-amd64
// - gcrio.azureedge.net/google_containers/pause-amd64
pauseContainerAzure = `image:.*azureedge\.net(.*)/pause.*`
// amazonaws.com/eks/pause-windows:latest
// eks/pause-amd64
pauseContainerEKS = `image:(amazonaws\.com/)?eks/pause.*`
// rancher/pause-amd64:3.0
pauseContainerRancher = `image:rancher/pause.*`
// - mcr.microsoft.com/k8s/core/pause-amd64
pauseContainerMCR = `image:mcr\.microsoft\.com(.*)/pause.*`
// - aksrepos.azurecr.io/mirror/pause-amd64
pauseContainerAKS = `image:aksrepos\.azurecr\.io(.*)/pause.*`
// - kubeletwin/pause:latest
pauseContainerWin = `image:kubeletwin/pause.*`
// - ecr.us-east-1.amazonaws.com/pause
pauseContainerECR = `image:ecr(.*)amazonaws\.com/pause.*`
// - *.ecr.us-east-1.amazonaws.com/upstream/pause
pauseContainerUpstream = `image:upstream/pause.*`
// - cdk/pause-amd64
pauseContainerCDK = `image:cdk/pause.*`
)
// Filter holds the state for the container filtering logic
type Filter struct {
Enabled bool
ImageIncludeList []*regexp.Regexp
NameIncludeList []*regexp.Regexp
NamespaceIncludeList []*regexp.Regexp
ImageExcludeList []*regexp.Regexp
NameExcludeList []*regexp.Regexp
NamespaceExcludeList []*regexp.Regexp
}
var sharedFilter *Filter
func parseFilters(filters []string) (imageFilters, nameFilters, namespaceFilters []*regexp.Regexp, err error) {
for _, filter := range filters {
switch {
case strings.HasPrefix(filter, "image:"):
pat := strings.TrimPrefix(filter, "image:")
r, err := regexp.Compile(strings.TrimPrefix(pat, "image:"))
if err != nil {
return nil, nil, nil, fmt.Errorf("invalid regex '%s': %s", pat, err)
}
imageFilters = append(imageFilters, r)
case strings.HasPrefix(filter, "name:"):
pat := strings.TrimPrefix(filter, "name:")
r, err := regexp.Compile(pat)
if err != nil {
return nil, nil, nil, fmt.Errorf("invalid regex '%s': %s", pat, err)
}
nameFilters = append(nameFilters, r)
case strings.HasPrefix(filter, "kube_namespace:"):
pat := strings.TrimPrefix(filter, "kube_namespace:")
r, err := regexp.Compile(pat)
if err != nil {
return nil, nil, nil, fmt.Errorf("invalid regex '%s': %s", pat, err)
}
namespaceFilters = append(namespaceFilters, r)
default:
klog.Warningf("Container filter %q is unknown, Ignoring it. The supported filters are 'image', 'name' and 'kube_namespace'", filter)
}
}
return imageFilters, nameFilters, namespaceFilters, nil
}
// GetSharedMetricFilter allows to share the result of NewFilterFromConfig
// for several user classes
func GetSharedMetricFilter() (*Filter, error) {
if sharedFilter != nil {
return sharedFilter, nil
}
f, err := newMetricFilterFromConfig()
if err != nil {
return nil, err
}
sharedFilter = f
return f, nil
}
// ResetSharedFilter is only to be used in unit tests: it resets the global
// filter instance to force re-parsing of the configuration.
func ResetSharedFilter() {
sharedFilter = nil
}
// NewFilter creates a new container filter from a two slices of
// regexp patterns for a include list and exclude list. Each pattern should have
// the following format: "field:pattern" where field can be: [image, name].
// An error is returned if any of the expression don't compile.
func NewFilter(includeList, excludeList []string) (*Filter, error) {
imgIncl, nameIncl, nsIncl, err := parseFilters(includeList)
if err != nil {
return nil, err
}
imgExcl, nameExcl, nsExcl, err := parseFilters(excludeList)
if err != nil {
return nil, err
}
return &Filter{
Enabled: len(includeList) > 0 || len(excludeList) > 0,
ImageIncludeList: imgIncl,
NameIncludeList: nameIncl,
NamespaceIncludeList: nsIncl,
ImageExcludeList: imgExcl,
NameExcludeList: nameExcl,
NamespaceExcludeList: nsExcl,
}, nil
}
// newMetricFilterFromConfig creates a new container filter, sourcing patterns
// from the pkg/config options, to be used only for metrics
func newMetricFilterFromConfig() (*Filter, error) {
cf := config.C
// We merge `container_include` and `container_include_metrics` as this filter
// is used by all core and python checks (so components sending metrics).
includeList := cf.Container.IncludeMetrics
excludeList := cf.Container.ExcludeMetrics
if cf.Container.ExcludePauseContainer {
excludeList = append(excludeList,
pauseContainerGCR,
pauseContainerOpenshift,
pauseContainerOpenshift3,
pauseContainerKubernetes,
pauseContainerGoogle,
pauseContainerAzure,
pauseContainerECS,
pauseContainerEKS,
pauseContainerRancher,
pauseContainerMCR,
pauseContainerWin,
pauseContainerAKS,
pauseContainerECR,
pauseContainerUpstream,
pauseContainerCDK,
)
}
return NewFilter(includeList, excludeList)
}
// NewAutodiscoveryFilter creates a new container filter for Autodiscovery
// It sources patterns from the pkg/config options but ignores the exclude_pause_container options
// It allows to filter metrics and logs separately
// For use in autodiscovery.
func NewAutodiscoveryFilter(filter FilterType) (*Filter, error) {
includeList := []string{}
excludeList := []string{}
switch filter {
case GlobalFilter, MetricsFilter:
includeList = config.C.Container.IncludeMetrics
excludeList = config.C.Container.ExcludeMetrics
case LogsFilter:
includeList = config.C.Container.IncludeLogs
excludeList = config.C.Container.ExcludeLogs
}
return NewFilter(includeList, excludeList)
}
// IsExcluded returns a bool indicating if the container should be excluded
// based on the filters in the containerFilter instance.
func (cf Filter) IsExcluded(containerName, containerImage, podNamespace string) bool {
if !cf.Enabled {
return false
}
// Any includeListed take precedence on excluded
for _, r := range cf.ImageIncludeList {
if r.MatchString(containerImage) {
return false
}
}
for _, r := range cf.NameIncludeList {
if r.MatchString(containerName) {
return false
}
}
for _, r := range cf.NamespaceIncludeList {
if r.MatchString(podNamespace) {
return false
}
}
// Check if excludeListed
for _, r := range cf.ImageExcludeList {
if r.MatchString(containerImage) {
return true
}
}
for _, r := range cf.NameExcludeList {
if r.MatchString(containerName) {
return true
}
}
for _, r := range cf.NamespaceExcludeList {
if r.MatchString(podNamespace) {
return true
}
}
return false
}