Skip to content

Commit

Permalink
Fix ServiceAccount naming for target allocator (#2445)
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm authored Dec 18, 2023
1 parent 7e8e034 commit 0b43d4e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
16 changes: 16 additions & 0 deletions .chloggen/fix_ta-serviceaccount.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. operator, target allocator, github action)
component: target allocator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix ServiceAccount naming for target allocator

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

# (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:
6 changes: 5 additions & 1 deletion internal/manifests/targetallocator/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ import (
// ServiceAccountName returns the name of the existing or self-provisioned service account to use for the given instance.
func ServiceAccountName(instance v1alpha1.OpenTelemetryCollector) string {
if len(instance.Spec.TargetAllocator.ServiceAccount) == 0 {
return naming.ServiceAccount(instance.Name)
return naming.TargetAllocatorServiceAccount(instance.Name)
}

return instance.Spec.TargetAllocator.ServiceAccount
}

// ServiceAccount returns the service account for the given instance.
func ServiceAccount(params manifests.Params) *corev1.ServiceAccount {
if len(params.OtelCol.Spec.TargetAllocator.ServiceAccount) > 0 {
return nil
}

name := naming.TargetAllocatorServiceAccount(params.OtelCol.Name)
labels := Labels(params.OtelCol, name)

Expand Down
52 changes: 48 additions & 4 deletions internal/manifests/targetallocator/serviceaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/manifests"
)

func TestServiceAccountNewDefault(t *testing.T) {
func TestServiceAccountDefaultName(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -32,13 +34,13 @@ func TestServiceAccountNewDefault(t *testing.T) {
}

// test
sa := ServiceAccountName(otelcol)
saName := ServiceAccountName(otelcol)

// verify
assert.Equal(t, "my-instance-collector", sa)
assert.Equal(t, "my-instance-targetallocator", saName)
}

func TestServiceAccountOverride(t *testing.T) {
func TestServiceAccountOverrideName(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -57,3 +59,45 @@ func TestServiceAccountOverride(t *testing.T) {
// verify
assert.Equal(t, "my-special-sa", sa)
}

func TestServiceAccountDefault(t *testing.T) {
params := manifests.Params{
OtelCol: v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
},
}
expected := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-targetallocator",
Namespace: params.OtelCol.Namespace,
Labels: Labels(params.OtelCol, "my-instance-targetallocator"),
Annotations: params.OtelCol.Annotations,
},
}

saName := ServiceAccountName(params.OtelCol)
sa := ServiceAccount(params)

assert.Equal(t, sa.Name, saName)
assert.Equal(t, expected, sa)
}

func TestServiceAccountOverride(t *testing.T) {
params := manifests.Params{
OtelCol: v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
TargetAllocator: v1alpha1.OpenTelemetryTargetAllocator{
ServiceAccount: "my-special-sa",
},
},
},
}
sa := ServiceAccount(params)

assert.Nil(t, sa)
}

0 comments on commit 0b43d4e

Please sign in to comment.