Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add creation of ServiceAccount to the Target Allocator #836

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apis/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ type OpenTelemetryTargetAllocator struct {
// +optional
PrometheusCR OpenTelemetryTargetAllocatorPrometheusCR `json:"prometheusCR,omitempty"`

// ServiceAccount indicates the name of an existing service account to use with this instance.
// +optional
ServiceAccount string `json:"serviceAccount,omitempty"`

// Image indicates the container image to use for the OpenTelemetry TargetAllocator.
// +optional
Image string `json:"image,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ metadata:
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
repository: github.com/open-telemetry/opentelemetry-operator
support: OpenTelemetry Community
name: opentelemetry-operator.v0.49.0
name: opentelemetry-operator.v0.49.0-3-g665d4ad
namespace: placeholder
spec:
apiservicedefinitions: {}
Expand Down Expand Up @@ -297,7 +297,7 @@ spec:
- args:
- --metrics-addr=127.0.0.1:8080
- --enable-leader-election
image: ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator:0.49.0
image: jaronoff/opentelemetry-operator:latest
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved
livenessProbe:
httpGet:
path: /healthz
Expand Down Expand Up @@ -389,7 +389,7 @@ spec:
maturity: alpha
provider:
name: OpenTelemetry Community
version: 0.49.0
version: 0.49.0-3-g665d4ad
webhookdefinitions:
- admissionReviewVersions:
- v1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,10 @@ spec:
custom resources as targets or not.
type: boolean
type: object
serviceAccount:
description: ServiceAccount indicates the name of an existing
service account to use with this instance.
type: string
type: object
tolerations:
description: Toleration to schedule OpenTelemetry Collector pods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,10 @@ spec:
custom resources as targets or not.
type: boolean
type: object
serviceAccount:
description: ServiceAccount indicates the name of an existing
service account to use with this instance.
type: string
type: object
tolerations:
description: Toleration to schedule OpenTelemetry Collector pods.
Expand Down
8 changes: 7 additions & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
resources:
- manager.yaml
- manager.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: local/opentelemetry-operator
newTag: e2e
7 changes: 7 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,13 @@ TargetAllocator indicates a value which determines whether to spawn a target all
PrometheusCR defines the configuration for the retrieval of PrometheusOperator CRDs ( servicemonitor.monitoring.coreos.com/v1 and podmonitor.monitoring.coreos.com/v1 ) retrieval. All CR instances which the ServiceAccount has access to will be retrieved. This includes other namespaces.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>serviceAccount</b></td>
<td>string</td>
<td>
ServiceAccount indicates the name of an existing service account to use with this instance.<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down
5 changes: 5 additions & 0 deletions pkg/collector/reconcile/serviceaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package reconcile
import (
"context"
"fmt"
"github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator"
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved

corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -36,6 +37,10 @@ func ServiceAccounts(ctx context.Context, params Params) error {
if params.Instance.Spec.Mode != v1alpha1.ModeSidecar {
desired = append(desired, collector.ServiceAccount(params.Instance))
}
// QUESTION: Should we create the service account if someone supplies an existing name?
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved
if params.Instance.Spec.TargetAllocator.Enabled {
desired = append(desired, targetallocator.ServiceAccount(params.Instance))
}

// first, handle the create/update parts
if err := expectedServiceAccounts(ctx, params, desired); err != nil {
Expand Down
10 changes: 8 additions & 2 deletions pkg/collector/reconcile/serviceaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package reconcile

import (
"context"
"github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator"
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -27,15 +28,20 @@ import (
)

func TestExpectedServiceAccounts(t *testing.T) {
t.Run("should create service account", func(t *testing.T) {
t.Run("should create multiple service accounts", func(t *testing.T) {
desired := collector.ServiceAccount(params().Instance)
err := expectedServiceAccounts(context.Background(), params(), []v1.ServiceAccount{desired})
allocatorDesired := targetallocator.ServiceAccount(params().Instance)
err := expectedServiceAccounts(context.Background(), params(), []v1.ServiceAccount{desired, allocatorDesired})
assert.NoError(t, err)

exists, err := populateObjectIfExists(t, &v1.ServiceAccount{}, types.NamespacedName{Namespace: "default", Name: "test-collector"})
assert.NoError(t, err)
assert.True(t, exists)

allocatorExists, err := populateObjectIfExists(t, &v1.ServiceAccount{}, types.NamespacedName{Namespace: "default", Name: "test-targetallocator"})
assert.NoError(t, err)
assert.True(t, allocatorExists)

})

t.Run("should update existing service account", func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/naming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ func TAService(otelcol v1alpha1.OpenTelemetryCollector) string {
func ServiceAccount(otelcol v1alpha1.OpenTelemetryCollector) string {
return DNSName(Truncate("%s-collector", 63, otelcol.Name))
}

// TargetAllocatorServiceAccount returns the TargetAllocator service account resource name.
func TargetAllocatorServiceAccount(otelcol v1alpha1.OpenTelemetryCollector) string {
return DNSName(Truncate("%s-targetallocator", 63, otelcol.Name))
}
5 changes: 3 additions & 2 deletions pkg/targetallocator/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele
Annotations: otelcol.Spec.PodAnnotations,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{Container(cfg, logger, otelcol)},
Volumes: Volumes(cfg, otelcol),
ServiceAccountName: ServiceAccountName(otelcol),
Containers: []corev1.Container{Container(cfg, logger, otelcol)},
Volumes: Volumes(cfg, otelcol),
},
},
},
Expand Down
47 changes: 47 additions & 0 deletions pkg/targetallocator/serviceaccount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package targetallocator

import (
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/pkg/naming"
)

// 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)
}

return instance.Spec.TargetAllocator.ServiceAccount
}
Comment on lines +26 to +32
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches what is done for the collector


//ServiceAccount returns the service account for the given instance.
func ServiceAccount(otelcol v1alpha1.OpenTelemetryCollector) corev1.ServiceAccount {
labels := Labels(otelcol)
labels["app.kubernetes.io/name"] = naming.TargetAllocatorServiceAccount(otelcol)

return corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: naming.TargetAllocatorServiceAccount(otelcol),
Namespace: otelcol.Namespace,
Labels: labels,
Annotations: otelcol.Annotations,
},
}
}
58 changes: 58 additions & 0 deletions pkg/targetallocator/serviceaccount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package targetallocator_test
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"

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

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
. "github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator"
jaronoff97 marked this conversation as resolved.
Show resolved Hide resolved
)

func TestServiceAccountNewDefault(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
}

// test
sa := ServiceAccountName(otelcol)

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

func TestServiceAccountOverride(t *testing.T) {
// prepare
otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
ServiceAccount: "my-special-sa",
},
}

// test
sa := ServiceAccountName(otelcol)

// verify
assert.Equal(t, "my-special-sa", sa)
}
6 changes: 5 additions & 1 deletion tests/e2e/smoke-targetallocator/02-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ apiVersion: v1
kind: ConfigMap
metadata:
name: stateful-targetallocator

---
apiVersion: v1
kind: ServiceAccount
metadata:
name: stateful-targetallocator