Enable ability to flip configuration of allownomutate

This commit is contained in:
2023-03-24 09:38:00 -05:00
parent 80ef925ff4
commit 8094816911
9 changed files with 50 additions and 57 deletions

View File

@@ -37,7 +37,7 @@ func strictTransport(w http.ResponseWriter) {
w.Header().Add("Strict-Transport-Security", "max-age=63072000")
}
func httpServer(cfg *config.Config) {
func httpServer() {
var serverCertificate tls.Certificate
if config.DefaultConfig().WebServerCertificate == "" || cfg.WebServerKey == "" {
log.Printf("[INFO] No webserver certificate configured, automatically generating self signed certificate.")
@@ -73,11 +73,7 @@ func httpServer(cfg *config.Config) {
ah := &admissionHandler{
decoder: serializer.NewCodecFactory(runtime.NewScheme()).UniversalDeserializer(),
config: cfg,
}
wh := &webHandler{
config: cfg,
config: &cfg,
}
// pod admission
@@ -87,18 +83,14 @@ func httpServer(cfg *config.Config) {
// pod mutation
path.HandleFunc("/api/v1/mutate/pod", ah.ahServe(operations.PodsMutation()))
// web root
path.HandleFunc("/", wh.webServe())
path.HandleFunc("/", webServe())
if err := connection.ListenAndServeTLS("", ""); err != nil {
log.Fatalf("[ERROR] %s\n", err)
}
}
type webHandler struct {
config *config.Config
}
func (h *webHandler) webServe() http.HandlerFunc {
func webServe() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
httpAccessLog(r)
crossSiteOrigin(w)
@@ -112,7 +104,7 @@ func (h *webHandler) webServe() http.HandlerFunc {
case r.URL.Path == "/healthcheck":
tmpltHealthCheck(w)
case r.URL.Path == "/":
tmpltWebRoot(w, r.URL.Query(), *h.config)
tmpltWebRoot(w, r.URL.Query())
default:
msg := fmt.Sprintf("Unable to locate requested path: '%s'", r.URL.Path)
log.Printf("[DEBUG] %s", msg)
@@ -170,7 +162,7 @@ func (h *admissionHandler) ahServe(hook operations.Hook) http.HandlerFunc {
return
}
result, err := hook.Execute(review.Request, h.config)
result, err := hook.Execute(review.Request, &cfg)
if err != nil {
msg := err.Error()
log.Printf("[ERROR] Internal Server Error: %s", msg)

View File

@@ -7,8 +7,6 @@ import (
"encoding/json"
"net/http"
"net/url"
"mutating-webhook/internal/config"
)
const cT string = "Content-Type"
@@ -56,7 +54,7 @@ func tmpltHealthCheck(w http.ResponseWriter) {
w.Write(output) //nolint:errcheck
}
func tmpltWebRoot(w http.ResponseWriter, urlPrams url.Values, cfg config.Config) {
func tmpltWebRoot(w http.ResponseWriter, urlPrams url.Values) {
o := struct {
Application string `json:"application" yaml:"application"`
Description string `json:"description" yaml:"description"`

View File

@@ -10,6 +10,9 @@ import (
"mutating-webhook/internal/config"
)
// global configuration
var cfg config.Config
func forever() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
@@ -24,9 +27,9 @@ func main() {
}()
// initialize application configuration
cfg := config.Init()
cfg = config.Init()
go httpServer(cfg)
go httpServer()
forever()
}