Skip to content

Commit 08b33a5

Browse files
authored
Created annotations filter (#2692)
* Created annotations filter Signed-off-by: Yuri Sa <[email protected]> * Created annotations filters test Signed-off-by: Yuri Sa <[email protected]> * Adjusted linters Signed-off-by: Yuri Sa <[email protected]> * Added Annotations Filter for OpAMP and TA Signed-off-by: Yuri Sa <[email protected]> * Added e2e tests for Labels and Annotations Signed-off-by: Yuri Sa <[email protected]> * Added e2e tests for Labels and Annotations Signed-off-by: Yuri Sa <[email protected]> * Added e2e tests for Labels and Annotations Signed-off-by: Yuri Sa <[email protected]> * Added e2e tests for Labels and Annotations Signed-off-by: Yuri Sa <[email protected]> * Fixed tests Signed-off-by: Yuri Sa <[email protected]> * Reverting kustomization Signed-off-by: Yuri Sa <[email protected]> * Reverting kustomization Signed-off-by: Yuri Sa <[email protected]> * Improved annotations Signed-off-by: Yuri Sa <[email protected]> * Changed description Signed-off-by: Yuri Sa <[email protected]> --------- Signed-off-by: Yuri Sa <[email protected]>
1 parent c6734e2 commit 08b33a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+675
-205
lines changed

.chloggen/annotations-filter.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: 'enhancement'
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, 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: Created ability to filter out Annotations
9+
10+
# One or more tracking issues related to the change
11+
issues: [2627]
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:

.github/workflows/e2e.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ jobs:
3232
- e2e-targetallocator
3333
- e2e-upgrade
3434
- e2e-multi-instrumentation
35+
- e2e-metadata-filters
3536
include:
3637
- group: e2e-prometheuscr
3738
setup: "prepare-e2e-with-featuregates FEATUREGATES=+operator.observability.prometheus"
3839
- group: e2e-multi-instrumentation
3940
setup: "add-operator-arg OPERATOR_ARG=--enable-multi-instrumentation prepare-e2e"
41+
- group: e2e-metadata-filters
42+
setup: "add-operator-arg OPERATOR_ARG='--annotations-filter=*filter.out --labels=*filter.out' prepare-e2e"
4043

4144
steps:
4245
- name: Check out code into the Go module directory

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ e2e-prometheuscr: chainsaw
240240
e2e-targetallocator: chainsaw
241241
$(CHAINSAW) test --test-dir ./tests/e2e-targetallocator
242242

243+
# end-to-end-test for Annotations/Labels Filters
244+
.PHONY: e2e-metadata-filters
245+
e2e-metadata-filters: chainsaw
246+
$(CHAINSAW) test --test-dir ./tests/e2e-metadata-filters
247+
243248
# end-to-end-test for testing upgrading
244249
.PHONY: e2e-upgrade
245250
e2e-upgrade: undeploy chainsaw

controllers/builder_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,9 @@ func TestBuildAll_OpAMPBridge(t *testing.T) {
931931
"app.kubernetes.io/part-of": "opentelemetry",
932932
"app.kubernetes.io/version": "latest",
933933
},
934+
Annotations: map[string]string{
935+
"opentelemetry-opampbridge-config/hash": "bd5cfc0df684966e25597a2847d5a3bae2c2b037d8bf10e7ea402ebe4d41c9f0",
936+
},
934937
},
935938
Spec: appsv1.DeploymentSpec{
936939
Replicas: &one,

internal/config/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Config struct {
5656
autoInstrumentationJavaImage string
5757
openshiftRoutesAvailability openshift.RoutesAvailability
5858
labelsFilter []string
59+
annotationsFilter []string
5960
}
6061

6162
// New constructs a new configuration based on the given options.
@@ -95,6 +96,7 @@ func New(opts ...Option) Config {
9596
autoInstrumentationApacheHttpdImage: o.autoInstrumentationApacheHttpdImage,
9697
autoInstrumentationNginxImage: o.autoInstrumentationNginxImage,
9798
labelsFilter: o.labelsFilter,
99+
annotationsFilter: o.annotationsFilter,
98100
}
99101
}
100102

@@ -204,3 +206,8 @@ func (c *Config) AutoInstrumentationNginxImage() string {
204206
func (c *Config) LabelsFilter() []string {
205207
return c.labelsFilter
206208
}
209+
210+
// AnnotationsFilter Returns the filters converted to regex strings used to filter out unwanted labels from propagations.
211+
func (c *Config) AnnotationsFilter() []string {
212+
return c.annotationsFilter
213+
}

internal/config/options.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type options struct {
5151
operatorOpAMPBridgeImage string
5252
openshiftRoutesAvailability openshift.RoutesAvailability
5353
labelsFilter []string
54+
annotationsFilter []string
5455
}
5556

5657
func WithAutoDetect(a autodetect.AutoDetect) Option {
@@ -180,14 +181,35 @@ func WithLabelFilters(labelFilters []string) Option {
180181
if i > 0 {
181182
result.WriteString(".*")
182183
}
183-
184184
// Quote any regular expression meta characters in the
185185
// literal text.
186186
result.WriteString(regexp.QuoteMeta(literal))
187187
}
188188
filters = append(filters, result.String())
189189
}
190-
191190
o.labelsFilter = filters
192191
}
193192
}
193+
194+
func WithAnnotationFilters(annotationFilters []string) Option {
195+
return func(o *options) {
196+
197+
filters := []string{}
198+
for _, pattern := range annotationFilters {
199+
var result strings.Builder
200+
201+
for i, literal := range strings.Split(pattern, "*") {
202+
203+
// Replace * with .*
204+
if i > 0 {
205+
result.WriteString(".*")
206+
}
207+
// Quote any regular expression meta characters in the
208+
// literal text.
209+
result.WriteString(regexp.QuoteMeta(literal))
210+
}
211+
filters = append(filters, result.String())
212+
}
213+
o.annotationsFilter = filters
214+
}
215+
}

internal/manifests/collector/daemonset.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ func DaemonSet(params manifests.Params) (*appsv1.DaemonSet, error) {
2929
name := naming.Collector(params.OtelCol.Name)
3030
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
3131

32-
annotations, err := Annotations(params.OtelCol)
32+
annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
3333
if err != nil {
3434
return nil, err
3535
}
36-
podAnnotations, err := PodAnnotations(params.OtelCol)
36+
37+
podAnnotations, err := manifestutils.PodAnnotations(params.OtelCol, params.Config.AnnotationsFilter())
3738
if err != nil {
3839
return nil, err
3940
}

internal/manifests/collector/daemonset_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,37 @@ func TestDaemonsetFilterLabels(t *testing.T) {
244244
}
245245
}
246246

247+
func TestDaemonsetFilterAnnotations(t *testing.T) {
248+
excludedAnnotations := map[string]string{
249+
"foo": "1",
250+
"app.foo.bar": "1",
251+
}
252+
253+
otelcol := v1beta1.OpenTelemetryCollector{
254+
ObjectMeta: metav1.ObjectMeta{
255+
Name: "my-instance",
256+
Annotations: excludedAnnotations,
257+
},
258+
Spec: v1beta1.OpenTelemetryCollectorSpec{},
259+
}
260+
261+
cfg := config.New(config.WithAnnotationFilters([]string{"foo*", "app.*.bar"}))
262+
263+
params := manifests.Params{
264+
Config: cfg,
265+
OtelCol: otelcol,
266+
Log: logger,
267+
}
268+
269+
d, err := DaemonSet(params)
270+
require.NoError(t, err)
271+
272+
assert.Len(t, d.ObjectMeta.Annotations, 4)
273+
for k := range excludedAnnotations {
274+
assert.NotContains(t, d.ObjectMeta.Annotations, k)
275+
}
276+
}
277+
247278
func TestDaemonSetNodeSelector(t *testing.T) {
248279
// Test default
249280
otelcol1 := v1beta1.OpenTelemetryCollector{

internal/manifests/collector/deployment.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ import (
2828
func Deployment(params manifests.Params) (*appsv1.Deployment, error) {
2929
name := naming.Collector(params.OtelCol.Name)
3030
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
31-
32-
annotations, err := Annotations(params.OtelCol)
31+
annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
3332
if err != nil {
3433
return nil, err
3534
}
3635

37-
podAnnotations, err := PodAnnotations(params.OtelCol)
36+
podAnnotations, err := manifestutils.PodAnnotations(params.OtelCol, params.Config.AnnotationsFilter())
3837
if err != nil {
3938
return nil, err
4039
}

internal/manifests/collector/deployment_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,37 @@ func TestDeploymentFilterLabels(t *testing.T) {
326326
}
327327
}
328328

329+
func TestDeploymentFilterAnnotations(t *testing.T) {
330+
excludedAnnotations := map[string]string{
331+
"foo": "1",
332+
"app.foo.bar": "1",
333+
}
334+
335+
otelcol := v1beta1.OpenTelemetryCollector{
336+
ObjectMeta: metav1.ObjectMeta{
337+
Name: "my-instance",
338+
Annotations: excludedAnnotations,
339+
},
340+
Spec: v1beta1.OpenTelemetryCollectorSpec{},
341+
}
342+
343+
cfg := config.New(config.WithAnnotationFilters([]string{"foo*", "app.*.bar"}))
344+
345+
params := manifests.Params{
346+
Config: cfg,
347+
OtelCol: otelcol,
348+
Log: logger,
349+
}
350+
351+
d, err := Deployment(params)
352+
require.NoError(t, err)
353+
354+
assert.Len(t, d.ObjectMeta.Annotations, 4)
355+
for k := range excludedAnnotations {
356+
assert.NotContains(t, d.ObjectMeta.Annotations, k)
357+
}
358+
}
359+
329360
func TestDeploymentNodeSelector(t *testing.T) {
330361
// Test default
331362
otelcol1 := v1beta1.OpenTelemetryCollector{

internal/manifests/collector/horizontalpodautoscaler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
func HorizontalPodAutoscaler(params manifests.Params) (*autoscalingv2.HorizontalPodAutoscaler, error) {
2929
name := naming.Collector(params.OtelCol.Name)
3030
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
31-
annotations, err := Annotations(params.OtelCol)
31+
annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
3232
if err != nil {
3333
return nil, err
3434
}

internal/manifests/collector/poddisruptionbudget.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func PodDisruptionBudget(params manifests.Params) (*policyV1.PodDisruptionBudget
3232

3333
name := naming.Collector(params.OtelCol.Name)
3434
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
35-
annotations, err := Annotations(params.OtelCol)
35+
annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
3636
if err != nil {
3737
return nil, err
3838
}

internal/manifests/collector/statefulset.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ func StatefulSet(params manifests.Params) (*appsv1.StatefulSet, error) {
2929
name := naming.Collector(params.OtelCol.Name)
3030
labels := manifestutils.Labels(params.OtelCol.ObjectMeta, name, params.OtelCol.Spec.Image, ComponentOpenTelemetryCollector, params.Config.LabelsFilter())
3131

32-
annotations, err := Annotations(params.OtelCol)
32+
annotations, err := manifestutils.Annotations(params.OtelCol, params.Config.AnnotationsFilter())
3333
if err != nil {
3434
return nil, err
3535
}
3636

37-
podAnnotations, err := PodAnnotations(params.OtelCol)
37+
podAnnotations, err := manifestutils.PodAnnotations(params.OtelCol, params.Config.AnnotationsFilter())
3838
if err != nil {
3939
return nil, err
4040
}

internal/manifests/collector/statefulset_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,37 @@ func TestStatefulSetFilterLabels(t *testing.T) {
336336
}
337337
}
338338

339+
func TestStatefulSetFilterAnnotations(t *testing.T) {
340+
excludedAnnotations := map[string]string{
341+
"foo": "1",
342+
"app.foo.bar": "1",
343+
}
344+
345+
otelcol := v1beta1.OpenTelemetryCollector{
346+
ObjectMeta: metav1.ObjectMeta{
347+
Name: "my-instance",
348+
Annotations: excludedAnnotations,
349+
},
350+
Spec: v1beta1.OpenTelemetryCollectorSpec{},
351+
}
352+
353+
cfg := config.New(config.WithAnnotationFilters([]string{"foo*", "app.*.bar"}))
354+
355+
params := manifests.Params{
356+
OtelCol: otelcol,
357+
Config: cfg,
358+
Log: logger,
359+
}
360+
361+
d, err := StatefulSet(params)
362+
require.NoError(t, err)
363+
364+
assert.Len(t, d.ObjectMeta.Annotations, 4)
365+
for k := range excludedAnnotations {
366+
assert.NotContains(t, d.ObjectMeta.Annotations, k)
367+
}
368+
}
369+
339370
func TestStatefulSetNodeSelector(t *testing.T) {
340371
// Test default
341372
otelcol1 := v1beta1.OpenTelemetryCollector{

internal/manifests/collector/annotations.go internal/manifests/manifestutils/annotations.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package collector
15+
package manifestutils
1616

1717
import (
1818
"crypto/sha256"
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
// Annotations return the annotations for OpenTelemetryCollector pod.
26-
func Annotations(instance v1beta1.OpenTelemetryCollector) (map[string]string, error) {
26+
func Annotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
2727
// new map every time, so that we don't touch the instance's annotations
2828
annotations := map[string]string{}
2929

@@ -36,9 +36,11 @@ func Annotations(instance v1beta1.OpenTelemetryCollector) (map[string]string, er
3636
}
3737

3838
// allow override of prometheus annotations
39-
if nil != instance.Annotations {
40-
for k, v := range instance.Annotations {
41-
annotations[k] = v
39+
if nil != instance.ObjectMeta.Annotations {
40+
for k, v := range instance.ObjectMeta.Annotations {
41+
if !IsFilteredSet(k, filterAnnotations) {
42+
annotations[k] = v
43+
}
4244
}
4345
}
4446

@@ -54,16 +56,18 @@ func Annotations(instance v1beta1.OpenTelemetryCollector) (map[string]string, er
5456
}
5557

5658
// PodAnnotations return the spec annotations for OpenTelemetryCollector pod.
57-
func PodAnnotations(instance v1beta1.OpenTelemetryCollector) (map[string]string, error) {
59+
func PodAnnotations(instance v1beta1.OpenTelemetryCollector, filterAnnotations []string) (map[string]string, error) {
5860
// new map every time, so that we don't touch the instance's annotations
5961
podAnnotations := map[string]string{}
60-
61-
// allow override of pod annotations
62-
for k, v := range instance.Spec.PodAnnotations {
63-
podAnnotations[k] = v
62+
if nil != instance.Spec.PodAnnotations {
63+
for k, v := range instance.Spec.PodAnnotations {
64+
if !IsFilteredSet(k, filterAnnotations) {
65+
podAnnotations[k] = v
66+
}
67+
}
6468
}
6569

66-
annotations, err := Annotations(instance)
70+
annotations, err := Annotations(instance, filterAnnotations)
6771
if err != nil {
6872
return nil, err
6973
}

0 commit comments

Comments
 (0)