Skip to content

Commit 0f9c881

Browse files
committed
feat(targetallocator): propagate cr annotation to deployment
1 parent 27a401b commit 0f9c881

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
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: enhancement
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: target allocator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Annotations of the `OpenTelemetryCollector` and `TargetAllocator` resources are now propagated to the target allocator deployment resource.
9+
10+
# One or more tracking issues related to the change
11+
issues: [4393]
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/manifests/targetallocator/annotations.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ import (
1515

1616
const configMapHashAnnotationKey = "opentelemetry-targetallocator-config/hash"
1717

18+
// Annotations returns the annotations for the TargetAllocator resources.
19+
func Annotations(instance v1alpha1.TargetAllocator, configMap *v1.ConfigMap, filterAnnotations []string) map[string]string {
20+
annotations := make(map[string]string, len(instance.ObjectMeta.Annotations))
21+
if instance.ObjectMeta.Annotations != nil {
22+
for k, v := range instance.ObjectMeta.Annotations {
23+
if !manifestutils.IsFilteredSet(k, filterAnnotations) {
24+
annotations[k] = v
25+
}
26+
}
27+
}
28+
if configMap != nil {
29+
cmHash := getConfigMapSHA(configMap)
30+
if cmHash != "" {
31+
annotations[configMapHashAnnotationKey] = getConfigMapSHA(configMap)
32+
}
33+
}
34+
35+
return annotations
36+
}
37+
1838
// PodAnnotations returns the annotations for the TargetAllocator Pod.
1939
func PodAnnotations(instance v1alpha1.TargetAllocator, configMap *v1.ConfigMap, filterAnnotations []string) map[string]string {
2040
// Make a copy of PodAnnotations to be safe

internal/manifests/targetallocator/deployment.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ func Deployment(params Params) (*appsv1.Deployment, error) {
2222
params.Log.Info("failed to construct target allocator config map for annotations")
2323
configMap = nil
2424
}
25+
annotations := Annotations(params.TargetAllocator, configMap, params.Config.AnnotationsFilter)
2526
podAnnotations := PodAnnotations(params.TargetAllocator, configMap, params.Config.AnnotationsFilter)
2627

2728
return &appsv1.Deployment{
2829
ObjectMeta: metav1.ObjectMeta{
29-
Name: name,
30-
Namespace: params.TargetAllocator.Namespace,
31-
Labels: labels,
30+
Name: name,
31+
Namespace: params.TargetAllocator.Namespace,
32+
Labels: labels,
33+
Annotations: annotations,
3234
},
3335
Spec: appsv1.DeploymentSpec{
3436
Replicas: params.TargetAllocator.Spec.Replicas,

internal/manifests/targetallocator/deployment_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,37 @@ func TestDeploymentNewDefault(t *testing.T) {
145145
assert.Contains(t, d.Spec.Template.Annotations, configMapHashAnnotationKey)
146146
assert.Len(t, d.Spec.Template.Annotations, 1)
147147

148+
// should only have the ConfigMap hash annotation
149+
assert.Contains(t, d.ObjectMeta.Annotations, configMapHashAnnotationKey)
150+
assert.Len(t, d.ObjectMeta.Annotations, 1)
151+
148152
// the pod selector should match the pod spec's labels
149153
assert.Subset(t, d.Spec.Template.Labels, d.Spec.Selector.MatchLabels)
150154
}
151155

156+
func TestDeploymentAnnotations(t *testing.T) {
157+
// prepare
158+
testAnnotationValues := map[string]string{"annotation-key": "annotation-value"}
159+
otelcol := collectorInstance()
160+
targetAllocator := targetAllocatorInstance()
161+
targetAllocator.ObjectMeta.Annotations = testAnnotationValues
162+
cfg := config.New()
163+
164+
params := Params{
165+
Collector: otelcol,
166+
TargetAllocator: targetAllocator,
167+
Config: cfg,
168+
Log: logger,
169+
}
170+
171+
// test
172+
ds, err := Deployment(params)
173+
assert.NoError(t, err)
174+
// verify
175+
assert.Equal(t, "my-instance-targetallocator", ds.Name)
176+
assert.Subset(t, ds.ObjectMeta.Annotations, testAnnotationValues)
177+
}
178+
152179
func TestDeploymentPodAnnotations(t *testing.T) {
153180
// prepare
154181
testPodAnnotationValues := map[string]string{"annotation-key": "annotation-value"}

0 commit comments

Comments
 (0)