diff --git a/apis/v1beta1/collector_webhook.go b/apis/v1beta1/collector_webhook.go index a7bbfca5fa..abe5ff9f87 100644 --- a/apis/v1beta1/collector_webhook.go +++ b/apis/v1beta1/collector_webhook.go @@ -445,8 +445,10 @@ func checkAutoscalerSpec(autoscaler *AutoscalerSpec) error { return nil } -// BuildValidator is mostly used for testing purposes. +// BuildValidator is //purpose and description +// Kubebuilder is used for testing // +kubebuilder:object:generate=false + type BuildValidator func(c OpenTelemetryCollector) admission.Warnings func NewCollectorWebhook( @@ -454,6 +456,7 @@ func NewCollectorWebhook( scheme *runtime.Scheme, cfg config.Config, reviewer *rbac.Reviewer, + metrics *Metrics, bv BuildValidator, ) *CollectorWebhook { return &CollectorWebhook{ @@ -461,19 +464,13 @@ func NewCollectorWebhook( scheme: scheme, cfg: cfg, reviewer: reviewer, + metrics: metrics, bv: bv, } } func SetupCollectorWebhook(mgr ctrl.Manager, cfg config.Config, reviewer *rbac.Reviewer, metrics *Metrics, bv BuildValidator) error { - cvw := &CollectorWebhook{ - reviewer: reviewer, - logger: mgr.GetLogger().WithValues("handler", "CollectorWebhook", "version", "v1beta1"), - scheme: mgr.GetScheme(), - cfg: cfg, - metrics: metrics, - bv: bv, - } + cvw := NewCollectorWebhook(mgr.GetLogger().WithValues("handler", "CollectorWebhook", "version", "v1beta1"), mgr.GetScheme(), cfg, reviewer, metrics, bv) return ctrl.NewWebhookManagedBy(mgr). For(&OpenTelemetryCollector{}). WithValidator(cvw). diff --git a/apis/v1beta1/collector_webhook_test.go b/apis/v1beta1/collector_webhook_test.go index 930b4e5579..9e3b34268c 100644 --- a/apis/v1beta1/collector_webhook_test.go +++ b/apis/v1beta1/collector_webhook_test.go @@ -17,10 +17,16 @@ package v1beta1_test import ( "context" "fmt" + "github.com/open-telemetry/opentelemetry-operator/internal/manifests" + collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector" "os" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "testing" "github.com/go-logr/logr" + "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" + "github.com/open-telemetry/opentelemetry-operator/internal/config" + "github.com/open-telemetry/opentelemetry-operator/internal/rbac" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gopkg.in/yaml.v3" @@ -35,13 +41,6 @@ import ( "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes/scheme" kubeTesting "k8s.io/client-go/testing" - "sigs.k8s.io/controller-runtime/pkg/webhook/admission" - - "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" - "github.com/open-telemetry/opentelemetry-operator/internal/config" - "github.com/open-telemetry/opentelemetry-operator/internal/manifests" - collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector" - "github.com/open-telemetry/opentelemetry-operator/internal/rbac" ) var ( @@ -127,6 +126,7 @@ func TestValidate(t *testing.T) { config.WithTargetAllocatorImage("ta:v0.0.0"), ), getReviewer(test.shouldFailSar), + nil, bv, ) t.Run(tt.name, func(t *testing.T) { @@ -587,6 +587,7 @@ func TestCollectorDefaultingWebhook(t *testing.T) { config.WithTargetAllocatorImage("ta:v0.0.0"), ), getReviewer(test.shouldFailSar), + nil, bv, ) ctx := context.Background() @@ -1354,6 +1355,7 @@ func TestOTELColValidatingWebhook(t *testing.T) { config.WithTargetAllocatorImage("ta:v0.0.0"), ), getReviewer(test.shouldFailSar), + nil, bv, ) ctx := context.Background() @@ -1389,6 +1391,7 @@ func TestOTELColValidateUpdateWebhook(t *testing.T) { expectedErr: "which does not support modification", }, } + bv := func(collector v1beta1.OpenTelemetryCollector) admission.Warnings { var warnings admission.Warnings cfg := config.New( @@ -1419,6 +1422,7 @@ func TestOTELColValidateUpdateWebhook(t *testing.T) { config.WithTargetAllocatorImage("ta:v0.0.0"), ), getReviewer(test.shouldFailSar), + nil, bv, ) ctx := context.Background() diff --git a/controllers/opentelemetrycollector_controller.go b/controllers/opentelemetrycollector_controller.go index 88a6f39ed9..6b73e43e13 100644 --- a/controllers/opentelemetrycollector_controller.go +++ b/controllers/opentelemetrycollector_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" "fmt" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "sort" "github.com/go-logr/logr" @@ -75,6 +76,25 @@ type Params struct { Config config.Config } +func (r *OpenTelemetryCollectorReconciler) Validate(otelcol v1beta1.OpenTelemetryCollector) admission.Warnings { + var warnings admission.Warnings + cfg := config.New( + config.WithCollectorImage("default-collector"), + config.WithTargetAllocatorImage("default-ta-allocator"), + ) + params := manifests.Params{ + Log: logr.Discard(), + Config: cfg, + OtelCol: otelcol, + } + _, err := collector.Build(params) + if err != nil { + warnings = append(warnings, err.Error()) + return warnings + } + return nil +} + func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Context, params manifests.Params) (map[types.UID]client.Object, error) { ownedObjects := map[types.UID]client.Object{} ownedObjectTypes := []client.Object{ diff --git a/main.go b/main.go index 0b5cb03520..94490133ba 100644 --- a/main.go +++ b/main.go @@ -54,7 +54,6 @@ import ( "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift" "github.com/open-telemetry/opentelemetry-operator/internal/autodetect/prometheus" "github.com/open-telemetry/opentelemetry-operator/internal/config" - collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector" openshiftDashboards "github.com/open-telemetry/opentelemetry-operator/internal/openshift/dashboards" "github.com/open-telemetry/opentelemetry-operator/internal/rbac" "github.com/open-telemetry/opentelemetry-operator/internal/version" @@ -407,22 +406,10 @@ func main() { } - bv := func(collector otelv1beta1.OpenTelemetryCollector) admission.Warnings { - var warnings admission.Warnings - params, newErr := collectorReconciler.GetParams(collector) - if newErr != nil { - warnings = append(warnings, newErr.Error()) - return warnings - } - _, err = collectorManifests.Build(params) - if err != nil { - warnings = append(warnings, err.Error()) - return warnings - } - return warnings - } + reconciler := controllers.NewReconciler(controllers.Params{}) + bv := otelv1beta1.SetupCollectorWebhook(mgr, cfg, reviewer, crdMetrics, reconciler.Validate) - if err = otelv1beta1.SetupCollectorWebhook(mgr, cfg, reviewer, crdMetrics, bv); err != nil { + if err = bv; err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "OpenTelemetryCollector") os.Exit(1) }