Skip to content

Commit

Permalink
Consolidate name logic in one place (open-telemetry#1)
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Elliott <[email protected]>
  • Loading branch information
joe-elliott authored and jpkrohling committed Dec 11, 2019
1 parent e3c6e1e commit d54d5f4
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 10 deletions.
4 changes: 4 additions & 0 deletions pkg/controller/opentelemetrycollector/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@ func (r *ReconcileOpenTelemetryCollector) setControllerReference(ctx context.Con
instance := ctx.Value(opentelemetry.ContextInstance).(*v1alpha1.OpenTelemetryCollector)
return controllerutil.SetControllerReference(instance, object, r.scheme)
}

func resourceName(instanceName string) string {
return fmt.Sprintf("%s-collector", instanceName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package opentelemetrycollector

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -61,7 +60,7 @@ func TestUpdateConfigMap(t *testing.T) {
reconciler.Reconcile(req)

// sanity check
name := fmt.Sprintf("%s-collector", instance.Name)
name := resourceName(instance.Name)
persisted := &corev1.ConfigMap{}
err := clients.client.Get(ctx, types.NamespacedName{Name: name, Namespace: instance.Namespace}, persisted)
assert.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func daemonSets(ctx context.Context) []*appsv1.DaemonSet {
func daemonSet(ctx context.Context) *appsv1.DaemonSet {
instance := ctx.Value(opentelemetry.ContextInstance).(*v1alpha1.OpenTelemetryCollector)
logger := ctx.Value(opentelemetry.ContextLogger).(logr.Logger)
name := fmt.Sprintf("%s-collector", instance.Name)
name := resourceName(instance.Name)

image := instance.Spec.Image
if len(image) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package opentelemetrycollector

import (
"context"
"fmt"
"testing"

"github.com/spf13/viper"
Expand Down Expand Up @@ -152,7 +151,7 @@ func TestUpdateDaemonSet(t *testing.T) {
reconciler.Reconcile(req)

// sanity check
name := fmt.Sprintf("%s-collector", instance.Name)
name := resourceName(instance.Name)
persisted := &appsv1.DaemonSet{}
err := clients.client.Get(ctx, types.NamespacedName{Name: name, Namespace: instance.Namespace}, persisted)
assert.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func deployments(ctx context.Context) []*appsv1.Deployment {
func deployment(ctx context.Context) *appsv1.Deployment {
instance := ctx.Value(opentelemetry.ContextInstance).(*v1alpha1.OpenTelemetryCollector)
logger := ctx.Value(opentelemetry.ContextLogger).(logr.Logger)
name := fmt.Sprintf("%s-collector", instance.Name)
name := resourceName(instance.Name)

image := instance.Spec.Image
if len(image) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package opentelemetrycollector

import (
"context"
"fmt"
"testing"

"github.com/spf13/viper"
Expand Down Expand Up @@ -145,7 +144,7 @@ func TestUpdateDeployment(t *testing.T) {
reconciler.Reconcile(req)

// sanity check
name := fmt.Sprintf("%s-collector", instance.Name)
name := resourceName(instance.Name)
persisted := &appsv1.Deployment{}
err := clients.client.Get(ctx, types.NamespacedName{Name: name, Namespace: instance.Namespace}, persisted)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/opentelemetrycollector/reconcile_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r *ReconcileOpenTelemetryCollector) reconcileService(ctx context.Context)

func service(ctx context.Context) *corev1.Service {
instance := ctx.Value(opentelemetry.ContextInstance).(*v1alpha1.OpenTelemetryCollector)
name := fmt.Sprintf("%s-collector", instance.Name)
name := resourceName(instance.Name)

labels := commonLabels(ctx)
labels["app.kubernetes.io/name"] = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *ReconcileOpenTelemetryCollector) reconcileServiceMonitor(ctx context.Co

func serviceMonitor(ctx context.Context) *monitoringv1.ServiceMonitor {
instance := ctx.Value(opentelemetry.ContextInstance).(*v1alpha1.OpenTelemetryCollector)
name := fmt.Sprintf("%s-collector", instance.Name)
name := resourceName(instance.Name)

labels := commonLabels(ctx)
labels["app.kubernetes.io/name"] = name
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/opentelemetrycollector/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,10 @@ func TestSetControllerReference(t *testing.T) {
assert.Equal(t, instance.TypeMeta.Kind, d.OwnerReferences[0].Kind)

}

func TestNameGeneration(t *testing.T) {
instanceName := "test"
expectedResourceName := "test-collector"

assert.Equal(t, expectedResourceName, resourceName(instanceName))
}

0 comments on commit d54d5f4

Please sign in to comment.