Skip to content

Commit

Permalink
Add endpoint for changing verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Sardo committed Feb 6, 2018
1 parent 5291f96 commit cc18d24
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions cmd/glbc/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package app

import (
"flag"
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
Expand All @@ -28,6 +30,7 @@ import (

"k8s.io/ingress-gce/pkg/controller"
"k8s.io/ingress-gce/pkg/flags"
"k8s.io/ingress-gce/pkg/version"
)

func RunHTTPServer(lbc *controller.LoadBalancerController) {
Expand All @@ -41,6 +44,7 @@ func RunHTTPServer(lbc *controller.LoadBalancerController) {
w.Write([]byte("ok"))
})
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/flag", flagHandler)
http.HandleFunc("/delete-all-and-quit", func(w http.ResponseWriter, r *http.Request) {
// TODO: Retry failures during shutdown.
lbc.Stop(true)
Expand All @@ -67,3 +71,59 @@ func RunSIGTERMHandler(lbc *controller.LoadBalancerController, deleteAll bool) {
glog.Infof("Exiting with %v", exitCode)
os.Exit(exitCode)
}

func flagHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
getFlagPage(w, r)
return
case "PUT":
putFlag(w, r)
default:
w.WriteHeader(http.StatusBadRequest)
return
}
}

func putFlag(w http.ResponseWriter, r *http.Request) {
for key, values := range r.URL.Query() {
if len(values) != 1 {
glog.Warningln("No query string params provided")
w.WriteHeader(http.StatusBadRequest)
return
}
v := values[0]
switch key {
case "v":
setVerbosity(v)
default:
glog.Warningf("Unrecognized key: %q", key)
w.WriteHeader(http.StatusBadRequest)
return
}

w.WriteHeader(http.StatusOK)
}
}

func setVerbosity(v string) {
flag.Lookup("v").Value.Set(v)
glog.V(0).Infof("Setting verbosity level to %q", v)
}

func getFlagPage(w http.ResponseWriter, r *http.Request) {
s := struct {
Version string
Verbosity string
}{
Version: version.Version,
Verbosity: flag.Lookup("v").Value.String(),
}
flagPageTemplate.Execute(w, s)
}

var flagPageTemplate = template.Must(template.New("").Parse(`GCE Ingress Controller "GLBC"
Version: {{.Version}}
Verbosity ('v'): {{.Verbosity}}
`))

0 comments on commit cc18d24

Please sign in to comment.