Skip to content

Commit 7b53851

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

File tree

10 files changed

+59
-16
lines changed

10 files changed

+59
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: 'bug_fix'
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: operator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Fix of Labels and Annotations filter
9+
10+
# One or more tracking issues related to the change
11+
issues: [2770]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext: Changed the approach from regexp.MatchString, which was representing a high resource cost and |
17+
confusing the users to strings.Contains, which turned out to be simpler and more efficient.

.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*", "app.*.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*", "app.*.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*", "app.*.bar"}))
312+
cfg := config.New(config.WithLabelFilters([]string{"foo", ".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*", "app.*.bar"}))
343+
cfg := config.New(config.WithAnnotationFilters([]string{"foo", ".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*", "app.*.bar"}))
322+
cfg := config.New(config.WithLabelFilters([]string{"foo", ".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*", "app.*.bar"}))
353+
cfg := config.New(config.WithAnnotationFilters([]string{"foo", ".bar"}))
354354

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

internal/manifests/manifestutils/annotations_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,36 @@ func TestAnnotationsPropagateDown(t *testing.T) {
160160
assert.Equal(t, "pod_annotation_value", podAnnotations["pod_annotation"])
161161
}
162162

163+
func TestAnnotationsSingleFilter(t *testing.T) {
164+
otelcol := v1beta1.OpenTelemetryCollector{
165+
ObjectMeta: metav1.ObjectMeta{
166+
Annotations: map[string]string{
167+
"test.bar.io": "foo",
168+
"test.io/port": "1234",
169+
"test.io/path": "/test",
170+
},
171+
},
172+
Spec: v1beta1.OpenTelemetryCollectorSpec{
173+
Mode: "deployment",
174+
},
175+
}
176+
177+
// 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"})
179+
180+
// verify
181+
require.NoError(t, err)
182+
assert.Len(t, annotations, 6)
183+
assert.NotContains(t, annotations, "test.bar.io")
184+
assert.Equal(t, "1234", annotations["test.io/port"])
185+
}
186+
163187
func TestAnnotationsFilter(t *testing.T) {
164188
otelcol := v1beta1.OpenTelemetryCollector{
165189
ObjectMeta: metav1.ObjectMeta{
166190
Annotations: map[string]string{
167191
"test.bar.io": "foo",
192+
"test.otel.io": "true",
168193
"test.io/port": "1234",
169194
"test.io/path": "/test",
170195
},
@@ -175,11 +200,12 @@ func TestAnnotationsFilter(t *testing.T) {
175200
}
176201

177202
// 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"})
203+
annotations, err := Annotations(otelcol, []string{".bar.io", "otel.io"})
179204

180205
// verify
181206
require.NoError(t, err)
182207
assert.Len(t, annotations, 6)
183208
assert.NotContains(t, annotations, "test.bar.io")
209+
assert.NotContains(t, annotations, "test.otel.io")
184210
assert.Equal(t, "1234", annotations["test.io/port"])
185211
}

internal/manifests/manifestutils/labels.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package manifestutils
1616

1717
import (
18-
"regexp"
1918
"strings"
2019

2120
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
@@ -26,8 +25,9 @@ import (
2625

2726
func IsFilteredSet(sourceSet string, filterSet []string) bool {
2827
for _, pattern := range filterSet {
29-
match, _ := regexp.MatchString(pattern, sourceSet)
30-
return match
28+
if strings.Contains(sourceSet, pattern) {
29+
return true
30+
}
3131
}
3232
return false
3333
}

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*", "app.*.bar"}))
240+
cfg := config.New(config.WithLabelFilters([]string{"foo", ".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*", "app.*.bar"}))
271+
cfg := config.New(config.WithAnnotationFilters([]string{"foo", ".bar"}))
272272

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

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ func main() {
150150
stringFlagOrEnv(&autoInstrumentationGo, "auto-instrumentation-go-image", "RELATED_IMAGE_AUTO_INSTRUMENTATION_GO", fmt.Sprintf("ghcr.io/open-telemetry/opentelemetry-go-instrumentation/autoinstrumentation-go:%s", v.AutoInstrumentationGo), "The default OpenTelemetry Go instrumentation image. This image is used when no image is specified in the CustomResource.")
151151
stringFlagOrEnv(&autoInstrumentationApacheHttpd, "auto-instrumentation-apache-httpd-image", "RELATED_IMAGE_AUTO_INSTRUMENTATION_APACHE_HTTPD", fmt.Sprintf("ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-apache-httpd:%s", v.AutoInstrumentationApacheHttpd), "The default OpenTelemetry Apache HTTPD instrumentation image. This image is used when no image is specified in the CustomResource.")
152152
stringFlagOrEnv(&autoInstrumentationNginx, "auto-instrumentation-nginx-image", "RELATED_IMAGE_AUTO_INSTRUMENTATION_NGINX", fmt.Sprintf("ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-apache-httpd:%s", v.AutoInstrumentationNginx), "The default OpenTelemetry Nginx instrumentation image. This image is used when no image is specified in the CustomResource.")
153-
pflag.StringArrayVar(&labelsFilter, "label", []string{}, "Labels to filter away from propagating onto deploys")
154-
pflag.StringArrayVar(&annotationsFilter, "annotations-filter", []string{}, "Annotations to filter away from propagating onto deploys. It should be a string array containing patterns, which are literal strings optionally containing a * wildcard character. Example: --annotations-filter=*filter.out will filter out annotations that looks like: annotation.filter.out: true")
153+
pflag.StringArrayVar(&labelsFilter, "label", []string{}, "Labels to filter away from propagating onto deploys. It should be a string array containing patterns, which are literal strings. Example: --labels-filter=filter.out will filter out labels that looks like: label.filter.out: true")
154+
pflag.StringArrayVar(&annotationsFilter, "annotations-filter", []string{}, "Annotations to filter away from propagating onto deploys. It should be a string array containing patterns, which are literal strings. Example: --annotations-filter=filter.out will filter out annotations that looks like: annotation.filter.out: true")
155155
pflag.IntVar(&webhookPort, "webhook-port", 9443, "The port the webhook endpoint binds to.")
156156
pflag.StringVar(&tlsOpt.minVersion, "tls-min-version", "VersionTLS12", "Minimum TLS version supported. Value must match version names from https://golang.org/pkg/crypto/tls/#pkg-constants.")
157157
pflag.StringSliceVar(&tlsOpt.cipherSuites, "tls-cipher-suites", nil, "Comma-separated list of cipher suites for the server. Values are from tls package constants (https://golang.org/pkg/crypto/tls/#pkg-constants). If omitted, the default Go cipher suites will be used")

0 commit comments

Comments
 (0)