mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
NK-8: Implemented simple webserver with empty mutation handler
This commit is contained in:
parent
b3dff90019
commit
44ddeb9a68
3 changed files with 67 additions and 1 deletions
16
main.go
16
main.go
|
@ -3,8 +3,22 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/nirmata/kube-policy/server"
|
||||
)
|
||||
|
||||
var (
|
||||
kubeConfigFile string
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello from Policy Controller!")
|
||||
server := server.NewWebhookServer()
|
||||
fmt.Println("WebHook server is running!")
|
||||
|
||||
server.RunAsync()
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
server.Stop()
|
||||
fmt.Println("WebHook server is stopped.")
|
||||
}
|
||||
|
|
BIN
server/server.debug
Normal file
BIN
server/server.debug
Normal file
Binary file not shown.
52
server/server.go
Normal file
52
server/server.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const ( // TODO: read these files from ~/.kube/config
|
||||
clientCertFile = "/home/quest/.minikube/client.crt"
|
||||
clientKeyFile = "/home/quest/.minikube/client.key"
|
||||
)
|
||||
|
||||
type WebhookServer struct {
|
||||
server http.Server
|
||||
}
|
||||
|
||||
func (ws *WebhookServer) serve(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("/mutate is called!")
|
||||
}
|
||||
|
||||
func (ws *WebhookServer) RunAsync() {
|
||||
go func(server http.Server) {
|
||||
err := server.ListenAndServeTLS(clientCertFile, clientKeyFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}(ws.server)
|
||||
}
|
||||
|
||||
func (ws *WebhookServer) Stop() {
|
||||
err := ws.server.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
// Error from closing listeners, or context timeout:
|
||||
log.Printf("Server Shutdown error: %v", err)
|
||||
ws.server.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func NewWebhookServer() WebhookServer {
|
||||
var ws WebhookServer
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/mutate", ws.serve)
|
||||
ws.server = http.Server{
|
||||
Addr: ":443",
|
||||
Handler: mux,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 5 * time.Second}
|
||||
return ws
|
||||
}
|
Loading…
Add table
Reference in a new issue