Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed labels and annotations filter #2828

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/fix-filter-labels-annotations.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: operator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no component operator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is probably for the collector


# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix of Labels and Annotations filter

# One or more tracking issues related to the change
issues: [2770]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- group: e2e-multi-instrumentation
setup: "add-operator-arg OPERATOR_ARG=--enable-multi-instrumentation prepare-e2e"
- group: e2e-metadata-filters
setup: "add-operator-arg OPERATOR_ARG='--annotations-filter=*filter.out --labels=*filter.out' prepare-e2e"
setup: "add-operator-arg OPERATOR_ARG='--annotations-filter=.*filter.out --labels=.*filter.out' prepare-e2e"

steps:
- name: Check out code into the Go module directory
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/collector/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

func ConfigMap(params manifests.Params) (*corev1.ConfigMap, error) {
name := naming.ConfigMap(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})

replacedConf, err := ReplaceConfig(params.OtelCol)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/manifests/collector/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
// DaemonSet builds the deployment for the given instance.
func DaemonSet(params manifests.Params) (*appsv1.DaemonSet, error) {
name := naming.Collector(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())

annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
annotations, err := manifestutils.Annotations(params.Log, params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
return nil, err
}

podAnnotations, err := manifestutils.PodAnnotations(params.OtelCol, params.Config.AnnotationsFilter())
podAnnotations, err := manifestutils.PodAnnotations(params.Log, params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/manifests/collector/daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestDaemonsetFilterLabels(t *testing.T) {
Spec: v1beta1.OpenTelemetryCollectorSpec{},
}

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

params := manifests.Params{
Config: cfg,
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestDaemonsetFilterAnnotations(t *testing.T) {
Spec: v1beta1.OpenTelemetryCollectorSpec{},
}

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

params := manifests.Params{
Config: cfg,
Expand Down
6 changes: 3 additions & 3 deletions internal/manifests/collector/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import (
// Deployment builds the deployment for the given instance.
func Deployment(params manifests.Params) (*appsv1.Deployment, error) {
name := naming.Collector(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
annotations, err := manifestutils.Annotations(params.Log, params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
return nil, err
}

podAnnotations, err := manifestutils.PodAnnotations(params.OtelCol, params.Config.AnnotationsFilter())
podAnnotations, err := manifestutils.PodAnnotations(params.Log, params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/manifests/collector/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

func HorizontalPodAutoscaler(params manifests.Params) (*autoscalingv2.HorizontalPodAutoscaler, error) {
name := naming.Collector(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
annotations, err := manifestutils.Annotations(params.Log, params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/collector/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

func Ingress(params manifests.Params) (*networkingv1.Ingress, error) {
name := naming.Ingress(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
if params.OtelCol.Spec.Ingress.Type != v1beta1.IngressTypeNginx {
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/manifests/collector/poddisruptionbudget.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func PodDisruptionBudget(params manifests.Params) (*policyV1.PodDisruptionBudget
}

name := naming.Collector(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
annotations, err := manifestutils.Annotations(params.Log, params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/collector/podmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func PodMonitor(params manifests.Params) (*monitoringv1.PodMonitor, error) {
return nil, nil
}
name := naming.PodMonitor(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, nil)
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, nil)
selectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector)
pm = monitoringv1.PodMonitor{
ObjectMeta: metav1.ObjectMeta{
Expand Down
4 changes: 2 additions & 2 deletions internal/manifests/collector/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ClusterRole(params manifests.Params) (*rbacv1.ClusterRole, error) {
}

name := naming.ClusterRole(params.OtelCol.Name, params.OtelCol.Namespace)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())

return &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -71,7 +71,7 @@ func ClusterRoleBinding(params manifests.Params) (*rbacv1.ClusterRoleBinding, er
}

name := naming.ClusterRoleBinding(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())

return &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Expand Down
4 changes: 2 additions & 2 deletions internal/manifests/collector/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func HeadlessService(params manifests.Params) (*corev1.Service, error) {
func MonitoringService(params manifests.Params) (*corev1.Service, error) {

name := naming.MonitoringService(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels[monitoringLabel] = valueExists

out, err := params.OtelCol.Spec.Config.Yaml()
Expand Down Expand Up @@ -100,7 +100,7 @@ func MonitoringService(params manifests.Params) (*corev1.Service, error) {

func Service(params manifests.Params) (*corev1.Service, error) {
name := naming.Service(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})

out, err := params.OtelCol.Spec.Config.Yaml()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/collector/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func service(name string, ports []v1.ServicePort) v1.Service {

func serviceWithInternalTrafficPolicy(name string, ports []v1.ServicePort, internalTrafficPolicy v1.ServiceInternalTrafficPolicyType) v1.Service {
params := deploymentParams()
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})

return v1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/collector/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ServiceAccount(params manifests.Params) (*corev1.ServiceAccount, error) {
}

name := naming.ServiceAccount(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})

return &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion internal/manifests/collector/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ServiceMonitor(params manifests.Params) (*monitoringv1.ServiceMonitor, erro
return nil, nil
}
name := naming.ServiceMonitor(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, []string{})
selectorLabels := manifestutils.SelectorLabels(params.OtelCol.ObjectMeta, ComponentOpenTelemetryCollector)
selectorLabels[monitoringLabel] = valueExists

Expand Down
6 changes: 3 additions & 3 deletions internal/manifests/collector/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
// StatefulSet builds the statefulset for the given instance.
func StatefulSet(params manifests.Params) (*appsv1.StatefulSet, error) {
name := naming.Collector(params.OtelCol.Name)
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
labels := manifestutils.Labels(params.Log, params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())

annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
annotations, err := manifestutils.Annotations(params.Log, params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
return nil, err
}

podAnnotations, err := manifestutils.PodAnnotations(params.OtelCol, params.Config.AnnotationsFilter())
podAnnotations, err := manifestutils.PodAnnotations(params.Log, params.OtelCol, params.Config.AnnotationsFilter())
if err != nil {
return nil, err
}
Expand Down
15 changes: 9 additions & 6 deletions internal/manifests/manifestutils/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
package manifestutils

import (
"fmt"

"crypto/sha256"
"encoding/json"
"fmt"

"github.com/go-logr/logr"

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
)

// Annotations return the annotations for OpenTelemetryCollector pod.
func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
func Annotations(logger logr.Logger, instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
// new map every time, so that we don't touch the instance's annotations
annotations := map[string]string{}

Expand All @@ -38,7 +41,7 @@ func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []st
// allow override of prometheus annotations
if nil != instance.ObjectMeta.Annotations {
for k, v := range instance.ObjectMeta.Annotations {
if !IsFilteredSet(k, filterAnnotations) {
if !IsFilteredSet(logger, k, filterAnnotations) {
annotations[k] = v
}
}
Expand All @@ -56,18 +59,18 @@ func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []st
}

// PodAnnotations return the spec annotations for OpenTelemetryCollector pod.
func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
func PodAnnotations(logger logr.Logger, instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
// new map every time, so that we don't touch the instance's annotations
podAnnotations := map[string]string{}
if nil != instance.Spec.PodAnnotations {
for k, v := range instance.Spec.PodAnnotations {
if !IsFilteredSet(k, filterAnnotations) {
if !IsFilteredSet(logger, k, filterAnnotations) {
podAnnotations[k] = v
}
}
}

annotations, err := Annotations(instance, filterAnnotations)
annotations, err := Annotations(logger, instance, filterAnnotations)
if err != nil {
return nil, err
}
Expand Down
50 changes: 40 additions & 10 deletions internal/manifests/manifestutils/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
logf "sigs.k8s.io/controller-runtime/pkg/log"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
)

var logger = logf.Log.WithName("unit-tests")

func TestDefaultAnnotations(t *testing.T) {
// prepare
otelcol := v1beta1.OpenTelemetryCollector{
Expand All @@ -45,9 +48,9 @@ func TestDefaultAnnotations(t *testing.T) {
}

// test
annotations, err := Annotations(otelcol, []string{})
annotations, err := Annotations(logger, otelcol, []string{})
require.NoError(t, err)
podAnnotations, err := PodAnnotations(otelcol, []string{})
podAnnotations, err := PodAnnotations(logger, otelcol, []string{})
require.NoError(t, err)

//verify
Expand Down Expand Up @@ -79,9 +82,9 @@ func TestNonDefaultPodAnnotation(t *testing.T) {
}

// test
annotations, err := Annotations(otelcol, []string{})
annotations, err := Annotations(logger, otelcol, []string{})
require.NoError(t, err)
podAnnotations, err := PodAnnotations(otelcol, []string{})
podAnnotations, err := PodAnnotations(logger, otelcol, []string{})
require.NoError(t, err)

//verify
Expand Down Expand Up @@ -121,9 +124,9 @@ func TestUserAnnotations(t *testing.T) {
}

// test
annotations, err := Annotations(otelcol, []string{})
annotations, err := Annotations(logger, otelcol, []string{})
require.NoError(t, err)
podAnnotations, err := PodAnnotations(otelcol, []string{})
podAnnotations, err := PodAnnotations(logger, otelcol, []string{})
require.NoError(t, err)

//verify
Expand All @@ -148,9 +151,9 @@ func TestAnnotationsPropagateDown(t *testing.T) {
}

// test
annotations, err := Annotations(otelcol, []string{})
annotations, err := Annotations(logger, otelcol, []string{})
require.NoError(t, err)
podAnnotations, err := PodAnnotations(otelcol, []string{})
podAnnotations, err := PodAnnotations(logger, otelcol, []string{})
require.NoError(t, err)

// verify
Expand All @@ -160,7 +163,7 @@ func TestAnnotationsPropagateDown(t *testing.T) {
assert.Equal(t, "pod_annotation_value", podAnnotations["pod_annotation"])
}

func TestAnnotationsFilter(t *testing.T) {
func TestAnnotationsSingleFilter(t *testing.T) {
otelcol := v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
Expand All @@ -175,11 +178,38 @@ func TestAnnotationsFilter(t *testing.T) {
}

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

// verify
require.NoError(t, err)
assert.Len(t, annotations, 6)
assert.NotContains(t, annotations, "test.bar.io")
assert.Equal(t, "1234", annotations["test.io/port"])
}

func TestAnnotationsFilter(t *testing.T) {
otelcol := v1beta1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"test.bar.io": "foo",
"test.io/port": "1234",
"test.io/path": "/test",
"config.otel.test/filter": "true",
},
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
Mode: "deployment",
},
}

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

// verify
require.NoError(t, err)
assert.Len(t, annotations, 6)
assert.NotContains(t, annotations, "test.bar.io")
assert.NotContains(t, annotations, "test.otel/io")
assert.NotContains(t, annotations, "config.otel.test/filter")
assert.Equal(t, "1234", annotations["test.io/port"])
}
Loading
Loading