From 42f9a1fb1ad28e8a316152ab4b5ad89334a81f01 Mon Sep 17 00:00:00 2001 From: "tania.pham" <tania.pham@servicenow.com> Date: Wed, 4 Sep 2024 09:42:50 -0700 Subject: [PATCH] validate as anon func --- .../opentelemetrycollector_controller.go | 42 ++++++------------- main.go | 25 ++++++++--- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/controllers/opentelemetrycollector_controller.go b/controllers/opentelemetrycollector_controller.go index f94d1f40d5..71c0676416 100644 --- a/controllers/opentelemetrycollector_controller.go +++ b/controllers/opentelemetrycollector_controller.go @@ -21,6 +21,16 @@ import ( "sort" "github.com/go-logr/logr" + "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" + "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/autodetect/rbac" + "github.com/open-telemetry/opentelemetry-operator/internal/config" + "github.com/open-telemetry/opentelemetry-operator/internal/manifests" + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector" + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils" + collectorStatus "github.com/open-telemetry/opentelemetry-operator/internal/status/collector" + "github.com/open-telemetry/opentelemetry-operator/pkg/featuregate" routev1 "github.com/openshift/api/route/v1" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" appsv1 "k8s.io/api/apps/v1" @@ -37,18 +47,6 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "sigs.k8s.io/controller-runtime/pkg/webhook/admission" - - "github.com/open-telemetry/opentelemetry-operator/apis/v1beta1" - "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/autodetect/rbac" - "github.com/open-telemetry/opentelemetry-operator/internal/config" - "github.com/open-telemetry/opentelemetry-operator/internal/manifests" - "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector" - "github.com/open-telemetry/opentelemetry-operator/internal/manifests/manifestutils" - collectorStatus "github.com/open-telemetry/opentelemetry-operator/internal/status/collector" - "github.com/open-telemetry/opentelemetry-operator/pkg/featuregate" ) var ( @@ -76,22 +74,6 @@ type Params struct { Config config.Config } -func (r *OpenTelemetryCollectorReconciler) Validate(otelcol v1beta1.OpenTelemetryCollector) admission.Warnings { - var warnings admission.Warnings - - params := manifests.Params{ - Log: logr.Discard(), - Config: r.config, - 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{ @@ -185,7 +167,7 @@ func (r *OpenTelemetryCollectorReconciler) getConfigMapsToRemove(configVersionsT return ownedConfigMaps } -func (r *OpenTelemetryCollectorReconciler) getParams(instance v1beta1.OpenTelemetryCollector) (manifests.Params, error) { +func (r *OpenTelemetryCollectorReconciler) GetParams(instance v1beta1.OpenTelemetryCollector) (manifests.Params, error) { p := manifests.Params{ Config: r.config, Client: r.Client, @@ -246,7 +228,7 @@ func (r *OpenTelemetryCollectorReconciler) Reconcile(ctx context.Context, req ct return ctrl.Result{}, client.IgnoreNotFound(err) } - params, err := r.getParams(instance) + params, err := r.GetParams(instance) if err != nil { log.Error(err, "Failed to create manifest.Params") return ctrl.Result{}, err diff --git a/main.go b/main.go index 6d605a6c75..00ed5b6cbb 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "crypto/tls" "flag" "fmt" + collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector" "os" "regexp" "runtime" @@ -366,13 +367,15 @@ func main() { os.Exit(1) } - if err = controllers.NewReconciler(controllers.Params{ + collectorReconciler := controllers.NewReconciler(controllers.Params{ Client: mgr.GetClient(), Log: ctrl.Log.WithName("controllers").WithName("OpenTelemetryCollector"), Scheme: mgr.GetScheme(), Config: cfg, Recorder: mgr.GetEventRecorderFor("opentelemetry-operator"), - }).SetupWithManager(mgr); err != nil { + }) + + if err = collectorReconciler.SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "OpenTelemetryCollector") os.Exit(1) } @@ -404,10 +407,22 @@ func main() { } - reconciler := controllers.NewReconciler(controllers.Params{}) - bv := otelv1beta1.SetupCollectorWebhook(mgr, cfg, reviewer, crdMetrics, reconciler.Validate) + bv := func(collector otelv1beta1.OpenTelemetryCollector) admission.Warnings { + var warnings admission.Warnings + params, err := collectorReconciler.GetParams(collector) + if err != nil { + warnings = append(warnings, err.Error()) + return warnings + } + _, err = collectorManifests.Build(params) + if err != nil { + warnings = append(warnings, err.Error()) + return warnings + } + return warnings + } - if err = bv; err != nil { + if err = otelv1beta1.SetupCollectorWebhook(mgr, cfg, reviewer, crdMetrics, bv); err != nil { setupLog.Error(err, "unable to create webhook", "webhook", "OpenTelemetryCollector") os.Exit(1) }