From a6c8c401ff9f892f48525ed40fb71d1265f46328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Wed, 23 Nov 2022 08:28:19 +0100 Subject: [PATCH] feat: add signal in internal cmd package (#5444) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add signal in internal cmd package Signed-off-by: Charles-Edouard Brétéché * logger Signed-off-by: Charles-Edouard Brétéché Signed-off-by: Charles-Edouard Brétéché --- cmd/cleanup-controller/main.go | 8 +------- cmd/initContainer/main.go | 17 ++++++----------- cmd/internal/maxprocs.go | 1 + cmd/internal/profiling.go | 2 +- cmd/internal/signal.go | 18 ++++++++++++++++++ cmd/internal/tracing.go | 2 +- cmd/kyverno/main.go | 8 +------- 7 files changed, 29 insertions(+), 27 deletions(-) create mode 100644 cmd/internal/signal.go diff --git a/cmd/cleanup-controller/main.go b/cmd/cleanup-controller/main.go index e9b6f5307a..e6121d0f6a 100644 --- a/cmd/cleanup-controller/main.go +++ b/cmd/cleanup-controller/main.go @@ -5,8 +5,6 @@ import ( "flag" "net/http" "os" - "os/signal" - "syscall" "time" "github.com/go-logr/logr" @@ -130,10 +128,6 @@ func setupMetrics(logger logr.Logger, kubeClient kubernetes.Interface) (*metrics return metricsConfig, cancel, nil } -func setupSignals() (context.Context, context.CancelFunc) { - return signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) -} - func main() { // config appConfig := internal.NewConfiguration(internal.WithProfiling(), internal.WithTracing()) @@ -154,7 +148,7 @@ func main() { os.Exit(1) } // setup signals - signalCtx, signalCancel := setupSignals() + signalCtx, signalCancel := internal.SetupSignals(logger) defer signalCancel() // setup metrics metricsConfig, metricsShutdown, err := setupMetrics(logger, rawClient) diff --git a/cmd/initContainer/main.go b/cmd/initContainer/main.go index 7bca841ec3..aea14dabd4 100644 --- a/cmd/initContainer/main.go +++ b/cmd/initContainer/main.go @@ -8,9 +8,7 @@ import ( "encoding/json" "flag" "os" - "os/signal" "sync" - "syscall" "time" kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1" @@ -64,11 +62,8 @@ func main() { // show version internal.ShowVersion(logger) // os signal handler - signalCtx, signalCancel := signal.NotifyContext(logging.Background(), os.Interrupt, syscall.SIGTERM) + signalCtx, signalCancel := internal.SetupSignals(logger) defer signalCancel() - - stopCh := signalCtx.Done() - // create client config clientConfig, err := config.CreateClientConfig(kubeconfig, clientRateLimitQPS, clientRateLimitBurst) if err != nil { @@ -120,7 +115,7 @@ func main() { go func() { defer signalCancel() - <-stopCh + <-signalCtx.Done() }() done := make(chan struct{}) @@ -152,13 +147,13 @@ func main() { } // use pipeline to pass request to cleanup resources - in := gen(done, stopCh, requests...) + in := gen(done, signalCtx.Done(), requests...) // process requests // processing routine count : 2 - p1 := process(client, pclient, done, stopCh, in) - p2 := process(client, pclient, done, stopCh, in) + p1 := process(client, pclient, done, signalCtx.Done(), in) + p2 := process(client, pclient, done, signalCtx.Done(), in) // merge results from processing routines - for err := range merge(done, stopCh, p1, p2) { + for err := range merge(done, signalCtx.Done(), p1, p2) { if err != nil { failure = true logging.Error(err, "failed to cleanup resource") diff --git a/cmd/internal/maxprocs.go b/cmd/internal/maxprocs.go index 2971a5e63f..002cbeb97f 100644 --- a/cmd/internal/maxprocs.go +++ b/cmd/internal/maxprocs.go @@ -9,6 +9,7 @@ import ( func SetupMaxProcs(logger logr.Logger) func() { logger = logger.WithName("maxprocs") + logger.Info("setup maxprocs...") undo, err := maxprocs.Set( maxprocs.Logger( func(format string, args ...interface{}) { diff --git a/cmd/internal/profiling.go b/cmd/internal/profiling.go index b1cd3bfd8a..c73e70df58 100644 --- a/cmd/internal/profiling.go +++ b/cmd/internal/profiling.go @@ -9,8 +9,8 @@ import ( func SetupProfiling(logger logr.Logger) { logger = logger.WithName("profiling").WithValues("enabled", profilingEnabled, "address", profilingAddress, "port", profilingPort) - logger.Info("start profiling...") if profilingEnabled { + logger.Info("setup profiling...") profiling.Start(logger, net.JoinHostPort(profilingAddress, profilingPort)) } } diff --git a/cmd/internal/signal.go b/cmd/internal/signal.go new file mode 100644 index 0000000000..e639d65852 --- /dev/null +++ b/cmd/internal/signal.go @@ -0,0 +1,18 @@ +package internal + +import ( + "context" + "os" + "os/signal" + "syscall" + + "github.com/go-logr/logr" +) + +var Context = context.Background() + +func SetupSignals(logger logr.Logger) (context.Context, context.CancelFunc) { + logger = logger.WithName("signals") + logger.Info("setup signals...") + return signal.NotifyContext(Context, os.Interrupt, syscall.SIGTERM) +} diff --git a/cmd/internal/tracing.go b/cmd/internal/tracing.go index 4ff29db6ed..11c37bbb5b 100644 --- a/cmd/internal/tracing.go +++ b/cmd/internal/tracing.go @@ -11,8 +11,8 @@ import ( func SetupTracing(logger logr.Logger, name string, kubeClient kubernetes.Interface) context.CancelFunc { logger = logger.WithName("tracing").WithValues("enabled", tracingEnabled, "address", tracingAddress, "port", tracingPort, "creds", tracingCreds) - logger.Info("setup tracing...") if tracingEnabled { + logger.Info("setup tracing...") shutdown, err := tracing.NewTraceConfig( logger, name, diff --git a/cmd/kyverno/main.go b/cmd/kyverno/main.go index e89c4d5da6..4ced3a5a11 100644 --- a/cmd/kyverno/main.go +++ b/cmd/kyverno/main.go @@ -8,10 +8,8 @@ import ( "fmt" "net/http" "os" - "os/signal" "strings" "sync" - "syscall" "time" "github.com/go-logr/logr" @@ -260,10 +258,6 @@ func setupCosign(logger logr.Logger) { } } -func setupSignals() (context.Context, context.CancelFunc) { - return signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) -} - func showWarnings(logger logr.Logger) { logger = logger.WithName("warnings") // DEPRECATED: remove in 1.9 @@ -499,7 +493,7 @@ func main() { defer metricsShutdown() } // setup signals - signalCtx, signalCancel := setupSignals() + signalCtx, signalCancel := internal.SetupSignals(logger) defer signalCancel() // create instrumented clients kubeClient, kubeClientLeaderElection, kyvernoClient, dynamicClient, err := createInstrumentedClients(signalCtx, logger, clientConfig, metricsConfig)