Skip to content

Commit e166984

Browse files
authored
Fix ServiceAccount naming for target allocator (open-telemetry#2445)
1 parent f1421b5 commit e166984

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

.chloggen/fix_ta-serviceaccount.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: bug_fix
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: 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: Fix ServiceAccount naming for target allocator
9+
10+
# One or more tracking issues related to the change
11+
issues: [2443]
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/serviceaccount.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ import (
2727
// ServiceAccountName returns the name of the existing or self-provisioned service account to use for the given instance.
2828
func ServiceAccountName(instance v1alpha1.OpenTelemetryCollector) string {
2929
if len(instance.Spec.TargetAllocator.ServiceAccount) == 0 {
30-
return naming.ServiceAccount(instance.Name)
30+
return naming.TargetAllocatorServiceAccount(instance.Name)
3131
}
3232

3333
return instance.Spec.TargetAllocator.ServiceAccount
3434
}
3535

3636
// ServiceAccount returns the service account for the given instance.
3737
func ServiceAccount(params manifests.Params) *corev1.ServiceAccount {
38+
if len(params.OtelCol.Spec.TargetAllocator.ServiceAccount) > 0 {
39+
return nil
40+
}
41+
3842
name := naming.TargetAllocatorServiceAccount(params.OtelCol.Name)
3943
labels := Labels(params.OtelCol, name)
4044

internal/manifests/targetallocator/serviceaccount_test.go

+48-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import (
1818
"testing"
1919

2020
"github.com/stretchr/testify/assert"
21+
corev1 "k8s.io/api/core/v1"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223

2324
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
25+
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
2426
)
2527

26-
func TestServiceAccountNewDefault(t *testing.T) {
28+
func TestServiceAccountDefaultName(t *testing.T) {
2729
// prepare
2830
otelcol := v1alpha1.OpenTelemetryCollector{
2931
ObjectMeta: metav1.ObjectMeta{
@@ -32,13 +34,13 @@ func TestServiceAccountNewDefault(t *testing.T) {
3234
}
3335

3436
// test
35-
sa := ServiceAccountName(otelcol)
37+
saName := ServiceAccountName(otelcol)
3638

3739
// verify
38-
assert.Equal(t, "my-instance-collector", sa)
40+
assert.Equal(t, "my-instance-targetallocator", saName)
3941
}
4042

41-
func TestServiceAccountOverride(t *testing.T) {
43+
func TestServiceAccountOverrideName(t *testing.T) {
4244
// prepare
4345
otelcol := v1alpha1.OpenTelemetryCollector{
4446
ObjectMeta: metav1.ObjectMeta{
@@ -57,3 +59,45 @@ func TestServiceAccountOverride(t *testing.T) {
5759
// verify
5860
assert.Equal(t, "my-special-sa", sa)
5961
}
62+
63+
func TestServiceAccountDefault(t *testing.T) {
64+
params := manifests.Params{
65+
OtelCol: v1alpha1.OpenTelemetryCollector{
66+
ObjectMeta: metav1.ObjectMeta{
67+
Name: "my-instance",
68+
},
69+
},
70+
}
71+
expected := &corev1.ServiceAccount{
72+
ObjectMeta: metav1.ObjectMeta{
73+
Name: "my-instance-targetallocator",
74+
Namespace: params.OtelCol.Namespace,
75+
Labels: Labels(params.OtelCol, "my-instance-targetallocator"),
76+
Annotations: params.OtelCol.Annotations,
77+
},
78+
}
79+
80+
saName := ServiceAccountName(params.OtelCol)
81+
sa := ServiceAccount(params)
82+
83+
assert.Equal(t, sa.Name, saName)
84+
assert.Equal(t, expected, sa)
85+
}
86+
87+
func TestServiceAccountOverride(t *testing.T) {
88+
params := manifests.Params{
89+
OtelCol: v1alpha1.OpenTelemetryCollector{
90+
ObjectMeta: metav1.ObjectMeta{
91+
Name: "my-instance",
92+
},
93+
Spec: v1alpha1.OpenTelemetryCollectorSpec{
94+
TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{
95+
ServiceAccount: "my-special-sa",
96+
},
97+
},
98+
},
99+
}
100+
sa := ServiceAccount(params)
101+
102+
assert.Nil(t, sa)
103+
}

0 commit comments

Comments
 (0)