1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-30 19:35:06 +00:00

refactor: replace signal package by signal.NotifyContext (#4691)

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-09-26 16:24:32 +02:00 committed by GitHub
parent 6a8085522a
commit 79bff1c19c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 69 deletions

View file

@ -8,7 +8,9 @@ import (
"encoding/json"
"flag"
"os"
"os/signal"
"sync"
"syscall"
"time"
kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
@ -17,7 +19,6 @@ import (
"github.com/kyverno/kyverno/pkg/config"
"github.com/kyverno/kyverno/pkg/leaderelection"
"github.com/kyverno/kyverno/pkg/policyreport"
"github.com/kyverno/kyverno/pkg/signal"
"github.com/kyverno/kyverno/pkg/tls"
"github.com/kyverno/kyverno/pkg/utils"
"go.uber.org/multierr"
@ -75,7 +76,11 @@ func main() {
flag.Parse()
// os signal handler
stopCh := signal.SetupSignalHandler()
signalCtx, signalCancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer signalCancel()
stopCh := signalCtx.Done()
// create client config
clientConfig, err := config.CreateClientConfig(kubeconfig, clientRateLimitQPS, clientRateLimitBurst)
if err != nil {
@ -118,11 +123,9 @@ func main() {
{convertGenerateRequest},
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer signalCancel()
<-stopCh
cancel()
}()
addPolicyReportSelectorLabel(client)
@ -151,7 +154,7 @@ func main() {
}
}
if err = acquireLeader(ctx, kubeClient); err != nil {
if err = acquireLeader(signalCtx, kubeClient); err != nil {
log.Log.V(2).Info("Failed to create lease 'kyvernopre-lock'")
os.Exit(1)
}
@ -184,7 +187,7 @@ func main() {
os.Exit(1)
}
le.Run(ctx)
le.Run(signalCtx)
}
func acquireLeader(ctx context.Context, kubeClient kubernetes.Interface) error {

View file

@ -8,7 +8,9 @@ import (
"net/http"
_ "net/http/pprof" // #nosec
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/kyverno/kyverno/pkg/background"
@ -29,7 +31,6 @@ import (
"github.com/kyverno/kyverno/pkg/policycache"
"github.com/kyverno/kyverno/pkg/policyreport"
"github.com/kyverno/kyverno/pkg/registryclient"
"github.com/kyverno/kyverno/pkg/signal"
"github.com/kyverno/kyverno/pkg/tls"
"github.com/kyverno/kyverno/pkg/toggle"
"github.com/kyverno/kyverno/pkg/tracing"
@ -122,8 +123,12 @@ func main() {
version.PrintVersionInfo(log.Log)
// os signal handler
signalCtx, signalCancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer signalCancel()
cleanUp := make(chan struct{})
stopCh := signal.SetupSignalHandler()
stopCh := signalCtx.Done()
debug := serverIP != ""
// clients
@ -422,14 +427,10 @@ func main() {
webhookCfg.UpdateWebhookChan <- true
}
// leader election context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// cancel leader election context on shutdown signals
go func() {
defer signalCancel()
<-stopCh
cancel()
}()
// webhookconfigurations are registered by the leader only
@ -439,7 +440,7 @@ func main() {
os.Exit(1)
}
go webhookRegisterLeader.Run(ctx)
go webhookRegisterLeader.Run(signalCtx)
// the webhook server runs across all instances
openAPIController := startOpenAPIController(dynamicClient, stopCh)
@ -519,7 +520,7 @@ func main() {
// start Kyverno controllers
go policyCacheController.Run(stopCh)
go urc.Run(genWorkers, stopCh)
go le.Run(ctx)
go le.Run(signalCtx)
go reportReqGen.Run(2, stopCh)
go configurationController.Run(stopCh)
go eventGenerator.Run(3, stopCh)

View file

@ -1,45 +0,0 @@
package signal
import (
"os"
"os/signal"
)
var (
onlyOneSignalHandler = make(chan struct{})
shutdownHandler chan os.Signal
)
// SetupSignalHandler registered for SIGTERM and SIGINT. A stop channel is returned
// which is closed on one of these signals. If a second signal is caught, the program
// is terminated with exit code 1.
func SetupSignalHandler() <-chan struct{} {
close(onlyOneSignalHandler) // panics when called twice
shutdownHandler = make(chan os.Signal, 2)
stop := make(chan struct{})
signal.Notify(shutdownHandler, shutdownSignals...)
go func() {
<-shutdownHandler
close(stop)
<-shutdownHandler
os.Exit(1) // second signal. Exit directly.
}()
return stop
}
// RequestShutdown emulates a received event that is considered as shutdown signal (SIGTERM/SIGINT)
// This returns whether a handler was notified
func RequestShutdown() bool {
if shutdownHandler != nil {
select {
case shutdownHandler <- shutdownSignals[0]:
return true
default:
}
}
return false
}

View file

@ -1,8 +0,0 @@
package signal
import (
"os"
"syscall"
)
var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}