Skip to content

Commit 523e15f

Browse files
authored
Fixes placement of pod annotations for opamp bridge (#4264)
* fix test * use pod annotations and normal annotations * fix test * fix tests
1 parent 6faf05e commit 523e15f

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed
Lines changed: 16 additions & 0 deletions
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: 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: opamp
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: fixes a bug where the bridge deployment wouldn't rollout on a config change.
9+
10+
# One or more tracking issues related to the change
11+
issues: [4020]
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:

internal/controllers/builder_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,9 +1021,7 @@ func TestBuildAll_OpAMPBridge(t *testing.T) {
10211021
"app.kubernetes.io/part-of": "opentelemetry",
10221022
"app.kubernetes.io/version": "latest",
10231023
},
1024-
Annotations: map[string]string{
1025-
"opentelemetry-opampbridge-config/hash": "05e1dc681267a9bc28fc2877ab464a98b9bd043843f14ffc0b4a394b5c86ba9f",
1026-
},
1024+
Annotations: map[string]string{},
10271025
},
10281026
Spec: appsv1.DeploymentSpec{
10291027
Replicas: &one,
@@ -1040,6 +1038,9 @@ func TestBuildAll_OpAMPBridge(t *testing.T) {
10401038
"app.kubernetes.io/part-of": "opentelemetry",
10411039
"app.kubernetes.io/version": "latest",
10421040
},
1041+
Annotations: map[string]string{
1042+
"opentelemetry-opampbridge-config/hash": "05e1dc681267a9bc28fc2877ab464a98b9bd043843f14ffc0b4a394b5c86ba9f",
1043+
},
10431044
},
10441045
Spec: corev1.PodSpec{
10451046
Volumes: []corev1.Volume{

internal/manifests/opampbridge/annotations.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package opampbridge
66
import (
77
"crypto/sha256"
88
"fmt"
9+
"maps"
910

1011
v1 "k8s.io/api/core/v1"
1112

@@ -15,13 +16,27 @@ import (
1516

1617
const configMapHashAnnotationKey = "opentelemetry-opampbridge-config/hash"
1718

18-
// Annotations returns the annotations for the OPAmpBridge Pod.
19-
func Annotations(instance v1alpha1.OpAMPBridge, configMap *v1.ConfigMap, filterAnnotations []string) map[string]string {
19+
// Annotations return the annotations for OpenTelemetryCollector resources.
20+
func Annotations(instance v1alpha1.OpAMPBridge, filterAnnotations []string) map[string]string {
21+
// new map every time, so that we don't touch the instance's annotations
22+
annotations := map[string]string{}
23+
24+
if instance.ObjectMeta.Annotations != nil {
25+
for k, v := range instance.ObjectMeta.Annotations {
26+
if !manifestutils.IsFilteredSet(k, filterAnnotations) {
27+
annotations[k] = v
28+
}
29+
}
30+
}
31+
32+
return annotations
33+
}
34+
35+
// PodAnnotations returns the annotations for the OPAmpBridge Pod.
36+
func PodAnnotations(instance v1alpha1.OpAMPBridge, configMap *v1.ConfigMap, filterAnnotations []string) map[string]string {
2037
// Make a copy of PodAnnotations to be safe
2138
annotations := make(map[string]string, len(instance.Spec.PodAnnotations))
22-
for key, value := range instance.Spec.PodAnnotations {
23-
annotations[key] = value
24-
}
39+
maps.Copy(annotations, instance.Spec.PodAnnotations)
2540
if instance.ObjectMeta.Annotations != nil {
2641
for k, v := range instance.ObjectMeta.Annotations {
2742
if !manifestutils.IsFilteredSet(k, filterAnnotations) {

internal/manifests/opampbridge/annotations_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestConfigMapHash(t *testing.T) {
4242
expectedConfig := expectedConfigMap.Data[OpAMPBridgeFilename]
4343
require.NotEmpty(t, expectedConfig)
4444
expectedHash := sha256.Sum256([]byte(expectedConfig))
45-
annotations := Annotations(opampBridge, expectedConfigMap, []string{".*\\.bar\\.io"})
45+
annotations := PodAnnotations(opampBridge, expectedConfigMap, []string{".*\\.bar\\.io"})
4646
require.Contains(t, annotations, configMapHashAnnotationKey)
4747
cmHash := annotations[configMapHashAnnotationKey]
4848
assert.Equal(t, fmt.Sprintf("%x", expectedHash), cmHash)

internal/manifests/opampbridge/deployment.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ func Deployment(params manifests.Params) *appsv1.Deployment {
2222
params.Log.Info("failed to construct OpAMPBridge ConfigMap for annotations")
2323
configMap = nil
2424
}
25-
annotations := Annotations(params.OpAMPBridge, configMap, params.Config.AnnotationsFilter)
25+
podAnnotations := PodAnnotations(params.OpAMPBridge, configMap, params.Config.AnnotationsFilter)
26+
annotations := Annotations(params.OpAMPBridge, params.Config.AnnotationsFilter)
2627
return &appsv1.Deployment{
2728
ObjectMeta: metav1.ObjectMeta{
2829
Name: name,
@@ -38,7 +39,7 @@ func Deployment(params manifests.Params) *appsv1.Deployment {
3839
Template: corev1.PodTemplateSpec{
3940
ObjectMeta: metav1.ObjectMeta{
4041
Labels: labels,
41-
Annotations: params.OpAMPBridge.Spec.PodAnnotations,
42+
Annotations: podAnnotations,
4243
},
4344
Spec: corev1.PodSpec{
4445
ServiceAccountName: ServiceAccountName(params.OpAMPBridge),

internal/manifests/opampbridge/deployment_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ func TestDeploymentNewDefault(t *testing.T) {
109109

110110
func TestDeploymentPodAnnotations(t *testing.T) {
111111
// prepare
112-
testPodAnnotationValues := map[string]string{"annotation-key": "annotation-value"}
112+
testPodAnnotationValues := map[string]string{
113+
"annotation-key": "annotation-value",
114+
"opentelemetry-opampbridge-config/hash": "ca3d163bab055381827226140568f3bef7eaac187cebd76878e0b63e9e442356",
115+
}
113116
opampBridge := v1alpha1.OpAMPBridge{
114117
ObjectMeta: metav1.ObjectMeta{
115118
Name: "my-instance",
@@ -130,7 +133,7 @@ func TestDeploymentPodAnnotations(t *testing.T) {
130133
d := Deployment(params)
131134

132135
// verify
133-
assert.Len(t, d.Spec.Template.Annotations, 1)
136+
assert.Len(t, d.Spec.Template.Annotations, 2)
134137
assert.Equal(t, "my-instance-opamp-bridge", d.Name)
135138
assert.Equal(t, testPodAnnotationValues, d.Spec.Template.Annotations)
136139
}
@@ -271,9 +274,12 @@ func TestDeploymentFilterAnnotations(t *testing.T) {
271274

272275
d := Deployment(params)
273276

274-
assert.Len(t, d.ObjectMeta.Annotations, 2)
277+
assert.Len(t, d.ObjectMeta.Annotations, 1)
275278
assert.NotContains(t, d.ObjectMeta.Annotations, "foo")
276279
assert.NotContains(t, d.ObjectMeta.Annotations, "app.foo.bar")
280+
assert.Len(t, d.Spec.Template.ObjectMeta.Annotations, 2)
281+
assert.NotContains(t, d.Spec.Template.ObjectMeta.Annotations, "foo")
282+
assert.NotContains(t, d.Spec.Template.ObjectMeta.Annotations, "app.foo.bar")
277283
}
278284

279285
func TestDeploymentNodeSelector(t *testing.T) {

0 commit comments

Comments
 (0)