Skip to content

Commit 5baf4c8

Browse files
committed
Fixed labels and annotations filter
Signed-off-by: Yuri Sa <[email protected]>
1 parent 5e3b198 commit 5baf4c8

File tree

9 files changed

+31
-22
lines changed

9 files changed

+31
-22
lines changed

.chloggen/fix-filter-labels-annotations.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,4 @@ issues: [2770]
1313
# (Optional) One or more lines of additional information to render under the primary note.
1414
# These lines will be padded with 2 spaces and then inserted directly into the document.
1515
# Use pipe (|) for multiline entries.
16-
subtext: |
17-
Changed the approach from regexp.MatchString, which was representing a high resource cost
18-
and confusing the users to strings.Contains, which turned out to be simpler and more efficient.
16+
subtext:

.github/workflows/e2e.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- group: e2e-multi-instrumentation
3838
setup: "add-operator-arg OPERATOR_ARG=--enable-multi-instrumentation prepare-e2e"
3939
- group: e2e-metadata-filters
40-
setup: "add-operator-arg OPERATOR_ARG='--annotations-filter=filter.out --labels=filter.out' prepare-e2e"
40+
setup: "add-operator-arg OPERATOR_ARG='--annotations-.*filter=filter.out --labels=.*filter.out' prepare-e2e"
4141

4242
steps:
4343
- name: Check out code into the Go module directory

internal/manifests/collector/daemonset_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func TestDaemonsetFilterLabels(t *testing.T) {
227227
Spec: v1beta1.OpenTelemetryCollectorSpec{},
228228
}
229229

230-
cfg := config.New(config.WithLabelFilters([]string{"foo", ".bar"}))
230+
cfg := config.New(config.WithLabelFilters([]string{"foo*", ".*bar"}))
231231

232232
params := manifests.Params{
233233
Config: cfg,
@@ -258,7 +258,7 @@ func TestDaemonsetFilterAnnotations(t *testing.T) {
258258
Spec: v1beta1.OpenTelemetryCollectorSpec{},
259259
}
260260

261-
cfg := config.New(config.WithAnnotationFilters([]string{"foo", ".bar"}))
261+
cfg := config.New(config.WithAnnotationFilters([]string{"foo*", ".*bar"}))
262262

263263
params := manifests.Params{
264264
Config: cfg,

internal/manifests/collector/deployment_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func TestDeploymentFilterLabels(t *testing.T) {
309309
Spec: v1beta1.OpenTelemetryCollectorSpec{},
310310
}
311311

312-
cfg := config.New(config.WithLabelFilters([]string{"foo", ".bar"}))
312+
cfg := config.New(config.WithLabelFilters([]string{"foo*", "app.*.bar"}))
313313

314314
params := manifests.Params{
315315
Config: cfg,
@@ -340,7 +340,7 @@ func TestDeploymentFilterAnnotations(t *testing.T) {
340340
Spec: v1beta1.OpenTelemetryCollectorSpec{},
341341
}
342342

343-
cfg := config.New(config.WithAnnotationFilters([]string{"foo", ".bar"}))
343+
cfg := config.New(config.WithAnnotationFilters([]string{"foo*", "app.*.bar"}))
344344

345345
params := manifests.Params{
346346
Config: cfg,

internal/manifests/collector/statefulset_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func TestStatefulSetFilterLabels(t *testing.T) {
319319
Spec: v1beta1.OpenTelemetryCollectorSpec{},
320320
}
321321

322-
cfg := config.New(config.WithLabelFilters([]string{"foo", ".bar"}))
322+
cfg := config.New(config.WithLabelFilters([]string{"foo*", "app.*.bar"}))
323323

324324
params := manifests.Params{
325325
OtelCol: otelcol,
@@ -350,7 +350,7 @@ func TestStatefulSetFilterAnnotations(t *testing.T) {
350350
Spec: v1beta1.OpenTelemetryCollectorSpec{},
351351
}
352352

353-
cfg := config.New(config.WithAnnotationFilters([]string{"foo", ".bar"}))
353+
cfg := config.New(config.WithAnnotationFilters([]string{"foo*", "app.*.bar"}))
354354

355355
params := manifests.Params{
356356
OtelCol: otelcol,

internal/manifests/manifestutils/annotations_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestAnnotationsSingleFilter(t *testing.T) {
175175
}
176176

177177
// This requires the filter to be in regex match form and not the other simpler wildcard one.
178-
annotations, err := Annotations(otelcol, []string{".bar.io"})
178+
annotations, err := Annotations(otelcol, []string{".*bar.io"})
179179

180180
// verify
181181
require.NoError(t, err)
@@ -188,10 +188,10 @@ func TestAnnotationsFilter(t *testing.T) {
188188
otelcol := v1beta1.OpenTelemetryCollector{
189189
ObjectMeta: metav1.ObjectMeta{
190190
Annotations: map[string]string{
191-
"test.bar.io": "foo",
192-
"test.otel.io": "true",
193-
"test.io/port": "1234",
194-
"test.io/path": "/test",
191+
"test.bar.io": "foo",
192+
"test.io/port": "1234",
193+
"test.io/path": "/test",
194+
"config.otel.test/filter": "true",
195195
},
196196
},
197197
Spec: v1beta1.OpenTelemetryCollectorSpec{
@@ -200,12 +200,13 @@ func TestAnnotationsFilter(t *testing.T) {
200200
}
201201

202202
// This requires the filter to be in regex match form and not the other simpler wildcard one.
203-
annotations, err := Annotations(otelcol, []string{".bar.io", "otel.io"})
203+
annotations, err := Annotations(otelcol, []string{".*bar.io", ".*otel/io", "config.*otel.test.*"})
204204

205205
// verify
206206
require.NoError(t, err)
207207
assert.Len(t, annotations, 6)
208208
assert.NotContains(t, annotations, "test.bar.io")
209-
assert.NotContains(t, annotations, "test.otel.io")
209+
assert.NotContains(t, annotations, "test.otel/io")
210+
assert.NotContains(t, annotations, "config.otel.test/filter")
210211
assert.Equal(t, "1234", annotations["test.io/port"])
211212
}

internal/manifests/manifestutils/labels.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,27 @@
1515
package manifestutils
1616

1717
import (
18+
"regexp"
1819
"strings"
1920

21+
"github.com/go-logr/logr"
22+
2023
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
2124
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
2225

2326
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2427
)
2528

29+
var logger logr.Logger
30+
2631
func IsFilteredSet(sourceSet string, filterSet []string) bool {
27-
for _, pattern := range filterSet {
28-
if strings.Contains(sourceSet, pattern) {
32+
for _, basePattern := range filterSet {
33+
pattern, compileErr := regexp.Compile(basePattern)
34+
match := pattern.MatchString(sourceSet)
35+
if compileErr != nil {
36+
logger.Error(compileErr, "could not compile the regexp pattern")
37+
}
38+
if match {
2939
return true
3040
}
3141
}

internal/manifests/manifestutils/labels_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestLabelsFilter(t *testing.T) {
147147
}
148148

149149
// This requires the filter to be in regex match form and not the other simpler wildcard one.
150-
labels := Labels(otelcol.ObjectMeta, collectorName, otelcol.Spec.Image, "opentelemetry-collector", []string{".bar.io"})
150+
labels := Labels(otelcol.ObjectMeta, collectorName, otelcol.Spec.Image, "opentelemetry-collector", []string{".*bar.io"})
151151

152152
// verify
153153
assert.Len(t, labels, 7)

internal/manifests/opampbridge/deployment_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func TestDeploymentFilterLabels(t *testing.T) {
237237
Spec: v1alpha1.OpAMPBridgeSpec{},
238238
}
239239

240-
cfg := config.New(config.WithLabelFilters([]string{"foo", ".bar"}))
240+
cfg := config.New(config.WithLabelFilters([]string{"foo*", "app.*.bar"}))
241241

242242
params := manifests.Params{
243243
Config: cfg,
@@ -268,7 +268,7 @@ func TestDeploymentFilterAnnotations(t *testing.T) {
268268
Spec: v1alpha1.OpAMPBridgeSpec{},
269269
}
270270

271-
cfg := config.New(config.WithAnnotationFilters([]string{"foo", ".bar"}))
271+
cfg := config.New(config.WithAnnotationFilters([]string{"foo*", "app.*.bar"}))
272272

273273
params := manifests.Params{
274274
Config: cfg,

0 commit comments

Comments
 (0)