2022-11-14 17:30:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2022-12-09 17:12:37 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/config"
|
2022-12-07 10:30:47 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/controllers/cleanup"
|
2022-11-14 17:30:12 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
2022-12-09 17:12:37 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/metrics"
|
|
|
|
"github.com/kyverno/kyverno/pkg/webhooks"
|
2022-11-14 17:30:12 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/webhooks/handlers"
|
2022-12-07 10:30:47 +00:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2022-11-14 17:30:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server interface {
|
|
|
|
// Run TLS server in separate thread and returns control immediately
|
|
|
|
Run(<-chan struct{})
|
|
|
|
// Stop TLS server and returns control after the server is shut down
|
|
|
|
Stop(context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
server *http.Server
|
|
|
|
}
|
|
|
|
|
2022-12-07 10:30:47 +00:00
|
|
|
type (
|
|
|
|
TlsProvider = func() ([]byte, []byte, error)
|
2023-04-04 05:11:18 +00:00
|
|
|
ValidationHandler = func(context.Context, logr.Logger, handlers.AdmissionRequest, time.Time) handlers.AdmissionResponse
|
2023-01-02 17:14:40 +00:00
|
|
|
CleanupHandler = func(context.Context, logr.Logger, string, time.Time, config.Configuration) error
|
2022-12-07 10:30:47 +00:00
|
|
|
)
|
2022-11-14 17:30:12 +00:00
|
|
|
|
2022-12-09 17:12:37 +00:00
|
|
|
type Probes interface {
|
|
|
|
IsReady() bool
|
|
|
|
IsLive() bool
|
|
|
|
}
|
|
|
|
|
2022-11-14 17:30:12 +00:00
|
|
|
// NewServer creates new instance of server accordingly to given configuration
|
|
|
|
func NewServer(
|
|
|
|
tlsProvider TlsProvider,
|
2022-12-07 10:30:47 +00:00
|
|
|
validationHandler ValidationHandler,
|
|
|
|
cleanupHandler CleanupHandler,
|
2022-12-09 17:12:37 +00:00
|
|
|
metricsConfig metrics.MetricsConfigManager,
|
|
|
|
debugModeOpts webhooks.DebugModeOptions,
|
|
|
|
probes Probes,
|
2023-01-02 17:14:40 +00:00
|
|
|
cfg config.Configuration,
|
2022-11-14 17:30:12 +00:00
|
|
|
) Server {
|
2022-11-29 09:16:07 +00:00
|
|
|
policyLogger := logging.WithName("cleanup-policy")
|
2022-12-07 10:30:47 +00:00
|
|
|
cleanupLogger := logging.WithName("cleanup")
|
2022-12-09 17:12:37 +00:00
|
|
|
cleanupHandlerFunc := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
policy := r.URL.Query().Get("policy")
|
|
|
|
logger := cleanupLogger.WithValues("policy", policy)
|
2023-01-02 17:14:40 +00:00
|
|
|
err := cleanupHandler(r.Context(), logger, policy, time.Now(), cfg)
|
2022-12-09 17:12:37 +00:00
|
|
|
if err == nil {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
} else {
|
|
|
|
if apierrors.IsNotFound(err) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-14 17:30:12 +00:00
|
|
|
mux := httprouter.New()
|
|
|
|
mux.HandlerFunc(
|
|
|
|
"POST",
|
2022-12-22 06:13:32 +00:00
|
|
|
config.CleanupValidatingWebhookServicePath,
|
2022-12-07 10:30:47 +00:00
|
|
|
handlers.FromAdmissionFunc("VALIDATE", validationHandler).
|
2023-04-04 10:23:20 +00:00
|
|
|
WithDump(debugModeOpts.DumpPayload).
|
2022-11-29 09:16:07 +00:00
|
|
|
WithSubResourceFilter().
|
2022-12-09 17:12:37 +00:00
|
|
|
WithMetrics(policyLogger, metricsConfig.Config(), metrics.WebhookValidating).
|
2022-11-29 09:16:07 +00:00
|
|
|
WithAdmission(policyLogger.WithName("validate")).
|
2022-11-23 06:50:55 +00:00
|
|
|
ToHandlerFunc(),
|
2022-11-14 17:30:12 +00:00
|
|
|
)
|
2022-12-07 10:30:47 +00:00
|
|
|
mux.HandlerFunc(
|
|
|
|
"GET",
|
|
|
|
cleanup.CleanupServicePath,
|
2022-12-09 17:12:37 +00:00
|
|
|
handlers.HttpHandler(cleanupHandlerFunc).
|
|
|
|
WithMetrics(policyLogger).
|
|
|
|
WithTrace("CLEANUP").
|
|
|
|
ToHandlerFunc(),
|
2022-12-07 10:30:47 +00:00
|
|
|
)
|
2022-12-09 17:12:37 +00:00
|
|
|
mux.HandlerFunc("GET", config.LivenessServicePath, handlers.Probe(probes.IsLive))
|
|
|
|
mux.HandlerFunc("GET", config.ReadinessServicePath, handlers.Probe(probes.IsReady))
|
2022-11-14 17:30:12 +00:00
|
|
|
return &server{
|
|
|
|
server: &http.Server{
|
|
|
|
Addr: ":9443",
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
certPem, keyPem, err := tlsProvider()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pair, err := tls.X509KeyPair(certPem, keyPem)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pair, nil
|
|
|
|
},
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
},
|
|
|
|
Handler: mux,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
ReadHeaderTimeout: 30 * time.Second,
|
|
|
|
IdleTimeout: 5 * time.Minute,
|
2022-11-29 09:16:07 +00:00
|
|
|
ErrorLog: logging.StdLogger(logging.WithName("server"), ""),
|
2022-11-14 17:30:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Run(stopCh <-chan struct{}) {
|
|
|
|
go func() {
|
|
|
|
if err := s.server.ListenAndServeTLS("", ""); err != nil {
|
|
|
|
logging.Error(err, "failed to start server")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Stop(ctx context.Context) {
|
|
|
|
err := s.server.Shutdown(ctx)
|
|
|
|
if err != nil {
|
|
|
|
err = s.server.Close()
|
|
|
|
if err != nil {
|
|
|
|
logging.Error(err, "failed to start server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|