Skip to content

Commit 4b82974

Browse files
[chore] make Config a struct with config fields, but no logic - move the logic out to autodetect
1 parent dce238e commit 4b82974

22 files changed

+184
-224
lines changed

controllers/opampbridge_controller_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"k8s.io/apimachinery/pkg/types"
1717
"k8s.io/client-go/kubernetes/scheme"
1818
"k8s.io/client-go/tools/record"
19+
ctrl "sigs.k8s.io/controller-runtime"
1920
"sigs.k8s.io/controller-runtime/pkg/client"
2021
k8sconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
2122
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -24,6 +25,7 @@ import (
2425

2526
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
2627
"github.com/open-telemetry/opentelemetry-operator/controllers"
28+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
2729
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
2830
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2931
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
@@ -47,7 +49,6 @@ func TestNewObjectsOnReconciliation_OpAMPBridge(t *testing.T) {
4749
// prepare
4850
cfg := config.New(
4951
config.WithOperatorOpAMPBridgeImage("default-opamp-bridge"),
50-
config.WithAutoDetect(opampBridgeMockAutoDetector),
5152
)
5253
nsn := types.NamespacedName{Name: "my-instance", Namespace: "default"}
5354
reconciler := controllers.NewOpAMPBridgeReconciler(controllers.OpAMPBridgeReconcilerParams{
@@ -57,7 +58,7 @@ func TestNewObjectsOnReconciliation_OpAMPBridge(t *testing.T) {
5758
Recorder: record.NewFakeRecorder(10),
5859
Config: cfg,
5960
})
60-
require.NoError(t, cfg.AutoDetect())
61+
require.NoError(t, autodetect.ApplyAutoDetect(opampBridgeMockAutoDetector, &cfg, ctrl.Log.WithName("autodetect")))
6162
created := &v1alpha1.OpAMPBridge{
6263
ObjectMeta: metav1.ObjectMeta{
6364
Name: nsn.Name,

controllers/opentelemetrycollector_controller.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -364,17 +364,17 @@ func (r *OpenTelemetryCollectorReconciler) GetOwnedResourceTypes() []client.Obje
364364
&policyV1.PodDisruptionBudget{},
365365
}
366366

367-
if r.config.CreateRBACPermissions() == rbac.Available {
367+
if r.config.CreateRBACPermissions == rbac.Available {
368368
ownedResources = append(ownedResources, &rbacv1.ClusterRole{})
369369
ownedResources = append(ownedResources, &rbacv1.ClusterRoleBinding{})
370370
}
371371

372-
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() && r.config.PrometheusCRAvailability() == prometheus.Available {
372+
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() && r.config.PrometheusCRAvailability == prometheus.Available {
373373
ownedResources = append(ownedResources, &monitoringv1.PodMonitor{})
374374
ownedResources = append(ownedResources, &monitoringv1.ServiceMonitor{})
375375
}
376376

377-
if r.config.OpenShiftRoutesAvailability() == openshift.RoutesAvailable {
377+
if r.config.OpenshiftRoutesAvailability == openshift.RoutesAvailable {
378378
ownedResources = append(ownedResources, &routev1.Route{})
379379
}
380380

@@ -385,7 +385,7 @@ const collectorFinalizer = "opentelemetrycollector.opentelemetry.io/finalizer"
385385

386386
func (r *OpenTelemetryCollectorReconciler) finalizeCollector(ctx context.Context, params manifests.Params) error {
387387
// The cluster scope objects do not have owner reference. They need to be deleted explicitly
388-
if params.Config.CreateRBACPermissions() == rbac.Available {
388+
if params.Config.CreateRBACPermissions == rbac.Available {
389389
objects, err := r.findClusterRoleObjects(ctx, params)
390390
if err != nil {
391391
return err

controllers/suite_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ func testCollectorWithPDB(minAvailable, maxUnavailable int32) v1alpha1.OpenTelem
395395
fmt.Printf("Error getting yaml file: %v", err)
396396
}
397397

398-
configuration := config.New(config.WithAutoDetect(mockAutoDetector), config.WithCollectorImage(defaultCollectorImage), config.WithTargetAllocatorImage(defaultTaAllocationImage))
399-
err = configuration.AutoDetect()
398+
configuration := config.New(config.WithCollectorImage(defaultCollectorImage), config.WithTargetAllocatorImage(defaultTaAllocationImage))
399+
err = autodetect.ApplyAutoDetect(mockAutoDetector, &configuration, ctrl.Log.WithName("autodetect"))
400400
if err != nil {
401401
logger.Error(err, "configuration.autodetect failed")
402402
}

controllers/targetallocator_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (r *TargetAllocatorReconciler) SetupWithManager(mgr ctrl.Manager) error {
191191
Owns(&appsv1.Deployment{}).
192192
Owns(&policyV1.PodDisruptionBudget{})
193193

194-
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() && r.config.PrometheusCRAvailability() == prometheus.Available {
194+
if featuregate.PrometheusOperatorIsAvailable.IsEnabled() && r.config.PrometheusCRAvailability == prometheus.Available {
195195
ctrlBuilder.Owns(&monitoringv1.ServiceMonitor{})
196196
ctrlBuilder.Owns(&monitoringv1.PodMonitor{})
197197
}

internal/autodetect/main.go

+44
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"slices"
1111

12+
"github.com/go-logr/logr"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
"k8s.io/client-go/discovery"
1415
"k8s.io/client-go/rest"
@@ -19,6 +20,7 @@ import (
1920
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2021
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
2122
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
23+
"github.com/open-telemetry/opentelemetry-operator/internal/config"
2224
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
2325
)
2426

@@ -185,3 +187,45 @@ func (a *autoDetect) TargetAllocatorAvailability() (targetallocator.Availability
185187
func (a *autoDetect) FIPSEnabled(_ context.Context) bool {
186188
return fips.IsFipsEnabled()
187189
}
190+
191+
// ApplyAutoDetect attempts to automatically detect relevant information for this operator.
192+
func ApplyAutoDetect(autoDetect AutoDetect, c *config.Config, logger logr.Logger) error {
193+
logger.V(2).Info("auto-detecting the configuration based on the environment")
194+
195+
ora, err := autoDetect.OpenShiftRoutesAvailability()
196+
if err != nil {
197+
return err
198+
}
199+
c.OpenshiftRoutesAvailability = ora
200+
logger.V(2).Info("openshift routes detected", "availability", ora)
201+
202+
pcrd, err := autoDetect.PrometheusCRsAvailability()
203+
if err != nil {
204+
return err
205+
}
206+
c.PrometheusCRAvailability = pcrd
207+
logger.V(2).Info("prometheus cr detected", "availability", pcrd)
208+
209+
rAuto, err := autoDetect.RBACPermissions(context.Background())
210+
if err != nil {
211+
logger.V(2).Info("the rbac permissions are not set for the operator", "reason", err)
212+
}
213+
c.CreateRBACPermissions = rAuto
214+
logger.V(2).Info("create rbac permissions detected", "availability", rAuto)
215+
216+
cmAvl, err := autoDetect.CertManagerAvailability(context.Background())
217+
if err != nil {
218+
logger.V(2).Info("the cert manager crd and permissions are not set for the operator", "reason", err)
219+
}
220+
c.CertManagerAvailability = cmAvl
221+
logger.V(2).Info("the cert manager crd and permissions are set for the operator", "availability", cmAvl)
222+
223+
taAvl, err := autoDetect.TargetAllocatorAvailability()
224+
if err != nil {
225+
return err
226+
}
227+
c.TargetAllocatorAvailability = taAvl
228+
logger.V(2).Info("determined TargetAllocator CRD availability", "availability", cmAvl)
229+
230+
return nil
231+
}

internal/autodetect/main_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ import (
2020
"k8s.io/client-go/kubernetes/fake"
2121
"k8s.io/client-go/rest"
2222
kubeTesting "k8s.io/client-go/testing"
23+
ctrl "sigs.k8s.io/controller-runtime"
2324

2425
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
2526
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/autodetectutils"
2627
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/certmanager"
2728
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
2829
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus"
2930
autoRBAC "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/rbac"
31+
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/targetallocator"
32+
"github.com/open-telemetry/opentelemetry-operator/internal/config"
3033
"github.com/open-telemetry/opentelemetry-operator/internal/rbac"
3134
)
3235

@@ -349,3 +352,93 @@ func TestCertManagerAvailability(t *testing.T) {
349352
})
350353
}
351354
}
355+
356+
func TestConfigChangesOnAutoDetect(t *testing.T) {
357+
// prepare
358+
mock := &mockAutoDetect{
359+
OpenShiftRoutesAvailabilityFunc: func() (openshift.RoutesAvailability, error) {
360+
return openshift.RoutesAvailable, nil
361+
},
362+
PrometheusCRsAvailabilityFunc: func() (prometheus.Availability, error) {
363+
return prometheus.Available, nil
364+
},
365+
RBACPermissionsFunc: func(ctx context.Context) (autoRBAC.Availability, error) {
366+
return autoRBAC.Available, nil
367+
},
368+
CertManagerAvailabilityFunc: func(ctx context.Context) (certmanager.Availability, error) {
369+
return certmanager.Available, nil
370+
},
371+
TargetAllocatorAvailabilityFunc: func() (targetallocator.Availability, error) {
372+
return targetallocator.Available, nil
373+
},
374+
}
375+
cfg := config.New()
376+
autodetect.ApplyAutoDetect(mock, &cfg, ctrl.Log.WithName("autodetect"))
377+
378+
// sanity check
379+
require.Equal(t, openshift.RoutesNotAvailable, cfg.OpenshiftRoutesAvailability)
380+
require.Equal(t, prometheus.NotAvailable, cfg.PrometheusCRAvailability)
381+
require.Equal(t, autoRBAC.NotAvailable, cfg.CreateRBACPermissions)
382+
require.Equal(t, certmanager.NotAvailable, cfg.CertManagerAvailability)
383+
require.Equal(t, targetallocator.NotAvailable, cfg.TargetAllocatorAvailability)
384+
385+
// test
386+
err := autodetect.ApplyAutoDetect(mock, &cfg, ctrl.Log.WithName("test"))
387+
require.NoError(t, err)
388+
389+
// verify
390+
assert.Equal(t, openshift.RoutesAvailable, cfg.OpenshiftRoutesAvailability)
391+
require.Equal(t, prometheus.Available, cfg.PrometheusCRAvailability)
392+
require.Equal(t, autoRBAC.Available, cfg.CreateRBACPermissions)
393+
require.Equal(t, certmanager.Available, cfg.CertManagerAvailability)
394+
require.Equal(t, targetallocator.Available, cfg.TargetAllocatorAvailability)
395+
}
396+
397+
var _ autodetect.AutoDetect = (*mockAutoDetect)(nil)
398+
399+
type mockAutoDetect struct {
400+
OpenShiftRoutesAvailabilityFunc func() (openshift.RoutesAvailability, error)
401+
PrometheusCRsAvailabilityFunc func() (prometheus.Availability, error)
402+
RBACPermissionsFunc func(ctx context.Context) (autoRBAC.Availability, error)
403+
CertManagerAvailabilityFunc func(ctx context.Context) (certmanager.Availability, error)
404+
TargetAllocatorAvailabilityFunc func() (targetallocator.Availability, error)
405+
}
406+
407+
func (m *mockAutoDetect) FIPSEnabled(_ context.Context) bool {
408+
return false
409+
}
410+
411+
func (m *mockAutoDetect) OpenShiftRoutesAvailability() (openshift.RoutesAvailability, error) {
412+
if m.OpenShiftRoutesAvailabilityFunc != nil {
413+
return m.OpenShiftRoutesAvailabilityFunc()
414+
}
415+
return openshift.RoutesNotAvailable, nil
416+
}
417+
418+
func (m *mockAutoDetect) PrometheusCRsAvailability() (prometheus.Availability, error) {
419+
if m.PrometheusCRsAvailabilityFunc != nil {
420+
return m.PrometheusCRsAvailabilityFunc()
421+
}
422+
return prometheus.NotAvailable, nil
423+
}
424+
425+
func (m *mockAutoDetect) RBACPermissions(ctx context.Context) (autoRBAC.Availability, error) {
426+
if m.RBACPermissionsFunc != nil {
427+
return m.RBACPermissionsFunc(ctx)
428+
}
429+
return autoRBAC.NotAvailable, nil
430+
}
431+
432+
func (m *mockAutoDetect) CertManagerAvailability(ctx context.Context) (certmanager.Availability, error) {
433+
if m.CertManagerAvailabilityFunc != nil {
434+
return m.CertManagerAvailabilityFunc(ctx)
435+
}
436+
return certmanager.NotAvailable, nil
437+
}
438+
439+
func (m *mockAutoDetect) TargetAllocatorAvailability() (targetallocator.Availability, error) {
440+
if m.TargetAllocatorAvailabilityFunc != nil {
441+
return m.TargetAllocatorAvailabilityFunc()
442+
}
443+
return targetallocator.NotAvailable, nil
444+
}

0 commit comments

Comments
 (0)