Skip to content

Commit

Permalink
Updated folder structure to reduce code duplication between collector…
Browse files Browse the repository at this point in the history
… and targetallocator & Minor changes
  • Loading branch information
rsvarma95 committed Aug 4, 2021
1 parent 6f39c47 commit 367c623
Show file tree
Hide file tree
Showing 37 changed files with 894 additions and 1,453 deletions.
5 changes: 5 additions & 0 deletions api/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,10 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
return fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the attribute 'tolerations'", r.Spec.Mode)
}

// validate target allocation
if r.Spec.TargetAllocator.Enabled && !(r.Spec.Mode == ModeStatefulSet) {
return fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support the target allocation deployment", r.Spec.Mode)
}

return nil
}
8 changes: 4 additions & 4 deletions controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

"github.com/open-telemetry/opentelemetry-operator/api/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/pkg/collector/reconcile"
"github.com/open-telemetry/opentelemetry-operator/pkg/reconcile"
)

// OpenTelemetryCollectorReconciler reconciles a OpenTelemetryCollector object.
Expand Down Expand Up @@ -66,7 +66,7 @@ func NewReconciler(p Params) *OpenTelemetryCollectorReconciler {
p.Tasks = []Task{
{
"config maps",
reconcile.ConfigMaps,
reconcile.CollectorConfigMaps,
true,
},
{
Expand All @@ -76,12 +76,12 @@ func NewReconciler(p Params) *OpenTelemetryCollectorReconciler {
},
{
"services",
reconcile.Services,
reconcile.CollectorServices,
true,
},
{
"deployments",
reconcile.Deployments,
reconcile.CollectorDeployments,
true,
},
{
Expand Down
2 changes: 1 addition & 1 deletion controllers/opentelemetrycollector_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/open-telemetry/opentelemetry-operator/api/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/controllers"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/pkg/collector/reconcile"
"github.com/open-telemetry/opentelemetry-operator/pkg/reconcile"
)

var logger = logf.Log.WithName("unit-tests")
Expand Down
30 changes: 7 additions & 23 deletions controllers/targetallocator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

"github.com/open-telemetry/opentelemetry-operator/api/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator/reconcile"
"github.com/open-telemetry/opentelemetry-operator/pkg/reconcile"
)

// OpenTelemetryTargetAllocatorReconciler reconciles a OpenTelemetryTargetAllocator object.
Expand All @@ -38,42 +38,26 @@ type OpenTelemetryTargetAllocatorReconciler struct {
log logr.Logger
scheme *runtime.Scheme
config config.Config
tasks []TgAlTask
}

// TgAlTask represents a reconciliation task to be executed by the reconciler.
type TgAlTask struct {
Name string
Do func(context.Context, reconcile.Params) error
BailOnError bool
}

// TgAlParams is the set of options to build a new OpenTelemetryTargetAllocatorReconciler.
type TgAlParams struct {
client.Client
Log logr.Logger
Scheme *runtime.Scheme
Config config.Config
Tasks []TgAlTask
tasks []Task
}

// NewTargetAlllocatorReconciler creates a new reconciler for OpenTelemetryTargetAllocator objects.
func NewTargetAllocatorReconciler(p TgAlParams) *OpenTelemetryTargetAllocatorReconciler {
func NewTargetAllocatorReconciler(p Params) *OpenTelemetryTargetAllocatorReconciler {
if len(p.Tasks) == 0 {
p.Tasks = []TgAlTask{
p.Tasks = []Task{
{
"config maps",
reconcile.ConfigMaps,
reconcile.TAConfigMaps,
true,
},
{
"deployments",
reconcile.Deployments,
reconcile.TADeployments,
true,
},
{
"services",
reconcile.Services,
reconcile.TAServices,
true,
},
}
Expand Down
18 changes: 9 additions & 9 deletions controllers/targetallocator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ import (
"github.com/open-telemetry/opentelemetry-operator/api/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/controllers"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
"github.com/open-telemetry/opentelemetry-operator/pkg/targetallocator/reconcile"
"github.com/open-telemetry/opentelemetry-operator/pkg/reconcile"
)

func TestNewObjectsOnTargetAllocatorReconciliation(t *testing.T) {
// prepare
cfg := config.New()
configYAML, err := ioutil.ReadFile("../pkg/targetallocator/reconcile/suite_test.yaml")
configYAML, err := ioutil.ReadFile("../pkg/reconcile/suite_test.yaml")
require.NoError(t, err)

nsn := types.NamespacedName{Name: "my-instance", Namespace: "default"}
reconciler := controllers.NewTargetAllocatorReconciler(controllers.TgAlParams{
reconciler := controllers.NewTargetAllocatorReconciler(controllers.Params{
Client: k8sClient,
Log: logger,
Scheme: testScheme,
Expand Down Expand Up @@ -113,9 +113,9 @@ func TestNewObjectsOnTargetAllocatorReconciliation(t *testing.T) {
func TestContinueOnRecoverableTargetAllocatorFailure(t *testing.T) {
// prepare
taskCalled := false
reconciler := controllers.NewTargetAllocatorReconciler(controllers.TgAlParams{
reconciler := controllers.NewTargetAllocatorReconciler(controllers.Params{
Log: logger,
Tasks: []controllers.TgAlTask{
Tasks: []controllers.Task{
{
Name: "should-fail",
Do: func(context.Context, reconcile.Params) error {
Expand Down Expand Up @@ -147,12 +147,12 @@ func TestBreakOnUnrecoverableTargetAllocatorError(t *testing.T) {
taskCalled := false
expectedErr := errors.New("should fail!")
nsn := types.NamespacedName{Name: "my-instance", Namespace: "default"}
reconciler := controllers.NewTargetAllocatorReconciler(controllers.TgAlParams{
reconciler := controllers.NewTargetAllocatorReconciler(controllers.Params{
Client: k8sClient,
Log: logger,
Scheme: scheme.Scheme,
Config: cfg,
Tasks: []controllers.TgAlTask{
Tasks: []controllers.Task{
{
Name: "should-fail",
Do: func(context.Context, reconcile.Params) error {
Expand Down Expand Up @@ -197,12 +197,12 @@ func TestTargetAllocatorSkipWhenInstanceDoesNotExist(t *testing.T) {
// prepare
cfg := config.New()
nsn := types.NamespacedName{Name: "non-existing-my-instance", Namespace: "default"}
reconciler := controllers.NewTargetAllocatorReconciler(controllers.TgAlParams{
reconciler := controllers.NewTargetAllocatorReconciler(controllers.Params{
Client: k8sClient,
Log: logger,
Scheme: scheme.Scheme,
Config: cfg,
Tasks: []controllers.TgAlTask{
Tasks: []controllers.Task{
{
Name: "should-not-be-called",
Do: func(context.Context, reconcile.Params) error {
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ func main() {
os.Exit(1)
}

if err = controllers.NewTargetAllocatorReconciler(controllers.TgAlParams{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("TargetAllocator"),
Scheme: mgr.GetScheme(),
Config: cfg,
if err = controllers.NewTargetAllocatorReconciler(controllers.Params{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("TargetAllocator"),
Scheme: mgr.GetScheme(),
Config: cfg,
Recorder: mgr.GetEventRecorderFor("opentelemetry-targetallocator"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "TargetAllocator")
os.Exit(1)
Expand Down
3 changes: 1 addition & 2 deletions pkg/collector/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ package collector_test
import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-operator/api/v1alpha1"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
. "github.com/open-telemetry/opentelemetry-operator/pkg/collector"
Expand Down
1 change: 0 additions & 1 deletion pkg/collector/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down
174 changes: 0 additions & 174 deletions pkg/collector/reconcile/configmap.go

This file was deleted.

Loading

0 comments on commit 367c623

Please sign in to comment.