move admin function to specefic admin api

This commit is contained in:
Hyatt 2023-03-24 09:46:15 -05:00
parent 8094816911
commit 55964f3790
Signed by: nhyatt
GPG Key ID: C50D0BBB5BC40BEA
2 changed files with 27 additions and 7 deletions

View File

@ -101,10 +101,12 @@ func webServe() http.HandlerFunc {
msg := fmt.Sprintf("incorrect method: got request type %s, expected request type %s", r.Method, http.MethodPost)
log.Printf("[DEBUG] %s", msg)
tmpltError(w, http.StatusMethodNotAllowed, msg)
case r.URL.Path == "/api/v1/admin":
tmpltAdminToggle(w, r.URL.Query())
case r.URL.Path == "/healthcheck":
tmpltHealthCheck(w)
case r.URL.Path == "/":
tmpltWebRoot(w, r.URL.Query())
tmpltWebRoot(w)
default:
msg := fmt.Sprintf("Unable to locate requested path: '%s'", r.URL.Path)
log.Printf("[DEBUG] %s", msg)

View File

@ -54,7 +54,7 @@ func tmpltHealthCheck(w http.ResponseWriter) {
w.Write(output) //nolint:errcheck
}
func tmpltWebRoot(w http.ResponseWriter, urlPrams url.Values) {
func tmpltWebRoot(w http.ResponseWriter) {
o := struct {
Application string `json:"application" yaml:"application"`
Description string `json:"description" yaml:"description"`
@ -66,17 +66,35 @@ func tmpltWebRoot(w http.ResponseWriter, urlPrams url.Values) {
}
w.Header().Add(cT, cTjson)
output, err := json.MarshalIndent(o, "", " ")
if err != nil {
log.Printf(marshalErrorMsg, err)
}
w.Write(output) //nolint:errcheck
}
func tmpltAdminToggle(w http.ResponseWriter, urlPrams url.Values) {
o := struct {
Application string `json:"application" yaml:"application"`
Description string `json:"description" yaml:"description"`
Version string `json:"version" yaml:"version"`
AdminNoMutate bool `json:"admin-no-mutate" yaml:"admin-no-mutate"`
}{
Application: "Mutating-Webhook API",
Description: "Mutating Webhook for Simple Sidecar Injection",
Version: "v1.0.0",
}
w.Header().Add(cT, cTjson)
for k, v := range urlPrams {
if k == "admin" && strings.ToLower(v[0]) == strings.ToLower(cfg.AllowAdminNoMutateToggle) {
if cfg.AllowAdminNoMutate {
cfg.AllowAdminNoMutate = false
} else {
cfg.AllowAdminNoMutate = true
}
cfg.AllowAdminNoMutate = !cfg.AllowAdminNoMutate
log.Printf("[INFO] Admin allow no mutate accepted current value: %v", cfg.AllowAdminNoMutate)
}
}
o.AdminNoMutate = cfg.AllowAdminNoMutate
output, err := json.MarshalIndent(o, "", " ")
if err != nil {
log.Printf(marshalErrorMsg, err)