1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-15 20:20:22 +00:00

refactor: make server owner of the cleanup chan (#4765)

* refactor: make server owner of the cleanup chan

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

* gofumpt

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

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
This commit is contained in:
Charles-Edouard Brétéché 2022-09-30 16:13:29 +02:00 committed by GitHub
parent 9aca37fe9f
commit ac8f4ba59c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View file

@ -158,7 +158,6 @@ func main() {
signalCtx, signalCancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) signalCtx, signalCancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer signalCancel() defer signalCancel()
cleanUp := make(chan struct{})
stopCh := signalCtx.Done() stopCh := signalCtx.Done()
debug := serverIP != "" debug := serverIP != ""
@ -434,7 +433,6 @@ func main() {
configuration, configuration,
webhookCfg, webhookCfg,
webhookMonitor, webhookMonitor,
cleanUp,
) )
// wrap all controllers that need leaderelection // wrap all controllers that need leaderelection
@ -532,7 +530,7 @@ func main() {
// resource cleanup // resource cleanup
// remove webhook configurations // remove webhook configurations
<-cleanUp <-server.Cleanup()
logger.V(2).Info("Kyverno shutdown successful") logger.V(2).Info("Kyverno shutdown successful")
} }

View file

@ -26,6 +26,8 @@ type Server interface {
Run(<-chan struct{}) Run(<-chan struct{})
// Stop TLS server and returns control after the server is shut down // Stop TLS server and returns control after the server is shut down
Stop(context.Context) Stop(context.Context)
// Cleanup returns the chanel used to wait for the server to clean up resources
Cleanup() <-chan struct{}
} }
type PolicyHandlers interface { type PolicyHandlers interface {
@ -45,7 +47,7 @@ type ResourceHandlers interface {
type server struct { type server struct {
server *http.Server server *http.Server
webhookRegister *webhookconfig.Register webhookRegister *webhookconfig.Register
cleanUp chan<- struct{} cleanUp chan struct{}
} }
type TlsProvider func() ([]byte, []byte, error) type TlsProvider func() ([]byte, []byte, error)
@ -58,7 +60,6 @@ func NewServer(
configuration config.Configuration, configuration config.Configuration,
register *webhookconfig.Register, register *webhookconfig.Register,
monitor *webhookconfig.Monitor, monitor *webhookconfig.Monitor,
cleanUp chan<- struct{},
) Server { ) Server {
mux := httprouter.New() mux := httprouter.New()
resourceLogger := logger.WithName("resource") resourceLogger := logger.WithName("resource")
@ -94,7 +95,7 @@ func NewServer(
ReadHeaderTimeout: 30 * time.Second, ReadHeaderTimeout: 30 * time.Second,
}, },
webhookRegister: register, webhookRegister: register,
cleanUp: cleanUp, cleanUp: make(chan struct{}),
} }
} }
@ -120,6 +121,10 @@ func (s *server) Stop(ctx context.Context) {
} }
} }
func (s *server) Cleanup() <-chan struct{} {
return s.cleanUp
}
func (s *server) cleanup(ctx context.Context) { func (s *server) cleanup(ctx context.Context) {
cleanupKyvernoResource := s.webhookRegister.ShouldCleanupKyvernoResource() cleanupKyvernoResource := s.webhookRegister.ShouldCleanupKyvernoResource()