Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2018 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package routes |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "html/template" |
| 22 | "io/ioutil" |
| 23 | "net/http" |
| 24 | "path" |
| 25 | "sync" |
| 26 | |
| 27 | "github.com/golang/glog" |
| 28 | |
| 29 | "k8s.io/apiserver/pkg/server/mux" |
| 30 | ) |
| 31 | |
| 32 | var ( |
| 33 | lock = &sync.RWMutex{} |
| 34 | registeredFlags = map[string]debugFlag{} |
| 35 | ) |
| 36 | |
| 37 | // DebugFlags adds handlers for flags under /debug/flags. |
| 38 | type DebugFlags struct { |
| 39 | } |
| 40 | |
| 41 | // Install registers the APIServer's flags handler. |
| 42 | func (f DebugFlags) Install(c *mux.PathRecorderMux, flag string, handler func(http.ResponseWriter, *http.Request)) { |
| 43 | c.UnlistedHandle("/debug/flags", http.HandlerFunc(f.Index)) |
| 44 | c.UnlistedHandlePrefix("/debug/flags/", http.HandlerFunc(f.Index)) |
| 45 | |
| 46 | url := path.Join("/debug/flags", flag) |
| 47 | c.UnlistedHandleFunc(url, handler) |
| 48 | |
| 49 | f.addFlag(flag) |
| 50 | } |
| 51 | |
| 52 | // Index responds with the `/debug/flags` request. |
| 53 | // For example, "/debug/flags/v" serves the "--v" flag. |
| 54 | // Index responds to a request for "/debug/flags/" with an HTML page |
| 55 | // listing the available flags. |
| 56 | func (f DebugFlags) Index(w http.ResponseWriter, r *http.Request) { |
| 57 | lock.RLock() |
| 58 | defer lock.RUnlock() |
| 59 | if err := indexTmpl.Execute(w, registeredFlags); err != nil { |
| 60 | glog.Error(err) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | var indexTmpl = template.Must(template.New("index").Parse(`<html> |
| 65 | <head> |
| 66 | <title>/debug/flags/</title> |
| 67 | </head> |
| 68 | <body> |
| 69 | /debug/flags/<br> |
| 70 | <br> |
| 71 | flags:<br> |
| 72 | <table> |
| 73 | {{range .}} |
| 74 | <tr>{{.Flag}}<br> |
| 75 | {{end}} |
| 76 | </table> |
| 77 | <br> |
| 78 | full flags configurable<br> |
| 79 | </body> |
| 80 | </html> |
| 81 | `)) |
| 82 | |
| 83 | type debugFlag struct { |
| 84 | Flag string |
| 85 | } |
| 86 | |
| 87 | func (f DebugFlags) addFlag(flag string) { |
| 88 | lock.Lock() |
| 89 | defer lock.Unlock() |
| 90 | registeredFlags[flag] = debugFlag{flag} |
| 91 | } |
| 92 | |
| 93 | // StringFlagSetterFunc is a func used for setting string type flag. |
| 94 | type StringFlagSetterFunc func(string) (string, error) |
| 95 | |
| 96 | // StringFlagPutHandler wraps an http Handler to set string type flag. |
| 97 | func StringFlagPutHandler(setter StringFlagSetterFunc) http.HandlerFunc { |
| 98 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 99 | switch { |
| 100 | case req.Method == "PUT": |
| 101 | body, err := ioutil.ReadAll(req.Body) |
| 102 | if err != nil { |
| 103 | writePlainText(http.StatusBadRequest, "error reading request body: "+err.Error(), w) |
| 104 | return |
| 105 | } |
| 106 | defer req.Body.Close() |
| 107 | response, err := setter(string(body)) |
| 108 | if err != nil { |
| 109 | writePlainText(http.StatusBadRequest, err.Error(), w) |
| 110 | return |
| 111 | } |
| 112 | writePlainText(http.StatusOK, response, w) |
| 113 | return |
| 114 | default: |
| 115 | writePlainText(http.StatusNotAcceptable, "unsupported http method", w) |
| 116 | return |
| 117 | } |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | // writePlainText renders a simple string response. |
| 122 | func writePlainText(statusCode int, text string, w http.ResponseWriter) { |
| 123 | w.Header().Set("Content-Type", "text/plain") |
| 124 | w.WriteHeader(statusCode) |
| 125 | fmt.Fprintln(w, text) |
| 126 | } |