Skip to content

Commit

Permalink
validate as anon func
Browse files Browse the repository at this point in the history
  • Loading branch information
taniyourstruly committed Sep 4, 2024
1 parent 4289b44 commit 42f9a1f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
42 changes: 12 additions & 30 deletions controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/tls"
"flag"
"fmt"
collectorManifests "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector"
"os"
"regexp"
"runtime"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Check failure on line 412 in main.go

View workflow job for this annotation

GitHub Actions / Code standards (linting)

shadow: declaration of "err" shadows declaration at line 274 (govet)
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)
}
Expand Down

0 comments on commit 42f9a1f

Please sign in to comment.