From 98c18408cba5cc8ee39cd4ce28c68570ea7bc1ca Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Thu, 16 May 2024 12:12:22 +0200 Subject: [PATCH] feat: add automatic GOMAXPROCS to admission webhook Signed-off-by: Simon Pasquier --- cmd/admission-webhook/main.go | 3 +++ cmd/operator/main.go | 11 ++------- cmd/prometheus-config-reloader/main.go | 10 ++------ internal/goruntime/cpu.go | 34 ++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 internal/goruntime/cpu.go diff --git a/cmd/admission-webhook/main.go b/cmd/admission-webhook/main.go index ff818cc92..6996c141d 100644 --- a/cmd/admission-webhook/main.go +++ b/cmd/admission-webhook/main.go @@ -31,6 +31,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "golang.org/x/sync/errgroup" + "github.com/prometheus-operator/prometheus-operator/internal/goruntime" logging "github.com/prometheus-operator/prometheus-operator/internal/log" "github.com/prometheus-operator/prometheus-operator/pkg/admission" "github.com/prometheus-operator/prometheus-operator/pkg/server" @@ -60,6 +61,8 @@ func main() { stdlog.Fatal(err) } + goruntime.SetMaxProcs(logger) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() wg, ctx := errgroup.WithContext(ctx) diff --git a/cmd/operator/main.go b/cmd/operator/main.go index 674ad63a9..cda219f00 100644 --- a/cmd/operator/main.go +++ b/cmd/operator/main.go @@ -24,7 +24,6 @@ import ( "os" "os/signal" "regexp" - "strings" "syscall" "github.com/go-kit/log" @@ -34,7 +33,6 @@ import ( versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/version" - "go.uber.org/automaxprocs/maxprocs" "golang.org/x/sync/errgroup" corev1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" @@ -44,6 +42,7 @@ import ( k8sflag "k8s.io/component-base/cli/flag" "k8s.io/utils/ptr" + "github.com/prometheus-operator/prometheus-operator/internal/goruntime" logging "github.com/prometheus-operator/prometheus-operator/internal/log" "github.com/prometheus-operator/prometheus-operator/pkg/admission" alertmanagercontroller "github.com/prometheus-operator/prometheus-operator/pkg/alertmanager" @@ -195,13 +194,6 @@ func run(fs *flag.FlagSet) int { stdlog.Fatal(err) } - l := func(format string, a ...interface{}) { - level.Info(logger).Log("component", "automaxprocs", "msg", fmt.Sprintf(strings.TrimPrefix(format, "maxprocs: "), a...)) - } - if _, err := maxprocs.Set(maxprocs.Logger(l)); err != nil { - level.Warn(logger).Log("msg", "Failed to set GOMAXPROCS automatically", "err", err) - } - gates, err := operator.ValidateFeatureGates(featureGates) if err != nil { level.Error(logger).Log( @@ -213,6 +205,7 @@ func run(fs *flag.FlagSet) int { level.Info(logger).Log("msg", "Starting Prometheus Operator", "version", version.Info()) level.Info(logger).Log("build_context", version.BuildContext()) level.Info(logger).Log("feature_gates", gates) + goruntime.SetMaxProcs(logger) if len(cfg.Namespaces.AllowList) > 0 && len(cfg.Namespaces.DenyList) > 0 { level.Error(logger).Log( diff --git a/cmd/prometheus-config-reloader/main.go b/cmd/prometheus-config-reloader/main.go index 5072850d2..5c90cec4d 100644 --- a/cmd/prometheus-config-reloader/main.go +++ b/cmd/prometheus-config-reloader/main.go @@ -37,8 +37,8 @@ import ( "github.com/prometheus/common/version" "github.com/prometheus/exporter-toolkit/web" "github.com/thanos-io/thanos/pkg/reloader" - "go.uber.org/automaxprocs/maxprocs" + "github.com/prometheus-operator/prometheus-operator/internal/goruntime" logging "github.com/prometheus-operator/prometheus-operator/internal/log" "github.com/prometheus-operator/prometheus-operator/pkg/operator" "github.com/prometheus-operator/prometheus-operator/pkg/versionutil" @@ -123,13 +123,6 @@ func main() { stdlog.Fatal(err) } - l := func(format string, a ...interface{}) { - level.Info(logger).Log("component", "automaxprocs", "msg", fmt.Sprintf(strings.TrimPrefix(format, "maxprocs: "), a...)) - } - if _, err := maxprocs.Set(maxprocs.Logger(l)); err != nil { - level.Warn(logger).Log("msg", "Failed to set GOMAXPROCS automatically", "err", err) - } - err = web.Validate(*webConfig) if err != nil { level.Error(logger).Log("msg", "Unable to validate web configuration file", "err", err) @@ -144,6 +137,7 @@ func main() { level.Info(logger).Log("msg", "Starting prometheus-config-reloader", "version", version.Info()) level.Info(logger).Log("build_context", version.BuildContext()) + goruntime.SetMaxProcs(logger) r := prometheus.NewRegistry() r.MustRegister( diff --git a/internal/goruntime/cpu.go b/internal/goruntime/cpu.go new file mode 100644 index 000000000..03ff9bc7b --- /dev/null +++ b/internal/goruntime/cpu.go @@ -0,0 +1,34 @@ +// Copyright 2024 The prometheus-operator Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package goruntime + +import ( + "fmt" + "strings" + + "github.com/go-kit/log" + "github.com/go-kit/log/level" + "go.uber.org/automaxprocs/maxprocs" +) + +func SetMaxProcs(logger log.Logger) { + l := func(format string, a ...interface{}) { + level.Info(logger).Log("msg", fmt.Sprintf(strings.TrimPrefix(format, "maxprocs: "), a...)) + } + + if _, err := maxprocs.Set(maxprocs.Logger(l)); err != nil { + level.Warn(logger).Log("msg", "Failed to set GOMAXPROCS automatically", "err", err) + } +}