@@ -17,9 +17,8 @@ limitations under the License.
1717package statusz
1818
1919import (
20- "bytes"
2120"fmt"
22- "html/template "
21+ "html"
2322"math/rand"
2423"net/http"
2524"time"
@@ -35,30 +34,11 @@ var (
3534
3635const DefaultStatuszPath = "/statusz"
3736
38- const (
39- headerFmt = `
37+ const headerFmt = `
4038%s statusz
4139Warning: This endpoint is not meant to be machine parseable, has no formatting compatibility guarantees and is for debugging purposes only.
4240`
4341
44- dataTemplate = `
45- Started{{.Delim}} {{.StartTime}}
46- Up{{.Delim}} {{.Uptime}}
47- Go version{{.Delim}} {{.GoVersion}}
48- Binary version{{.Delim}} {{.BinaryVersion}}
49- {{if .EmulationVersion}}Emulation version{{.Delim}} {{.EmulationVersion}}{{end}}
50- `
51- )
52-
53- type contentFields struct {
54- Delim string
55- StartTime string
56- Uptime string
57- GoVersion string
58- BinaryVersion string
59- EmulationVersion string
60- }
61-
6242type mux interface {
6343Handle (path string , handler http.Handler )
6444}
@@ -68,33 +48,18 @@ func NewRegistry(effectiveVersion compatibility.EffectiveVersion) statuszRegistr
6848}
6949
7050func Install (m mux , componentName string , reg statuszRegistry ) {
71- dataTmpl , err := initializeTemplates ()
72- if err != nil {
73- klog .Errorf ("error while parsing gotemplates: %v" , err )
74- return
75- }
76- m .Handle (DefaultStatuszPath , handleStatusz (componentName , dataTmpl , reg ))
77- }
78-
79- func initializeTemplates () (* template.Template , error ) {
80- d := template .New ("data" )
81- dataTmpl , err := d .Parse (dataTemplate )
82- if err != nil {
83- return nil , err
84- }
85-
86- return dataTmpl , nil
51+ m .Handle (DefaultStatuszPath , handleStatusz (componentName , reg ))
8752}
8853
89- func handleStatusz (componentName string , dataTmpl * template. Template , reg statuszRegistry ) http.HandlerFunc {
54+ func handleStatusz (componentName string , reg statuszRegistry ) http.HandlerFunc {
9055return func (w http.ResponseWriter , r * http.Request ) {
9156if ! httputil .AcceptableMediaType (r ) {
9257http .Error (w , httputil .ErrUnsupportedMediaType .Error (), http .StatusNotAcceptable )
9358return
9459}
9560
9661fmt .Fprintf (w , headerFmt , componentName )
97- data , err := populateStatuszData (dataTmpl , reg )
62+ data , err := populateStatuszData (reg )
9863if err != nil {
9964klog .Errorf ("error while populating statusz data: %v" , err )
10065http .Error (w , "error while populating statusz data" , http .StatusInternalServerError )
@@ -106,31 +71,28 @@ func handleStatusz(componentName string, dataTmpl *template.Template, reg status
10671}
10772}
10873
109- func populateStatuszData (tmpl * template.Template , reg statuszRegistry ) (string , error ) {
110- if tmpl == nil {
111- return "" , fmt .Errorf ("received nil template" )
112- }
113-
74+ func populateStatuszData (reg statuszRegistry ) (string , error ) {
11475randomIndex := rand .Intn (len (delimiters ))
115- data := contentFields {
116- Delim : delimiters [randomIndex ],
117- StartTime : reg .processStartTime ().Format (time .UnixDate ),
118- Uptime : uptime (reg .processStartTime ()),
119- GoVersion : reg .goVersion (),
120- BinaryVersion : reg .binaryVersion ().String (),
121- }
76+ delim := html .EscapeString (delimiters [randomIndex ])
77+ startTime := html .EscapeString (reg .processStartTime ().Format (time .UnixDate ))
78+ uptime := html .EscapeString (uptime (reg .processStartTime ()))
79+ goVersion := html .EscapeString (reg .goVersion ())
80+ binaryVersion := html .EscapeString (reg .binaryVersion ().String ())
12281
82+ var emulationVersion string
12383if reg .emulationVersion () != nil {
124- data . EmulationVersion = reg .emulationVersion ().String ()
84+ emulationVersion = fmt . Sprintf ( `Emulation version%s %s` , delim , html . EscapeString ( reg .emulationVersion ().String ()) )
12585}
12686
127- var tpl bytes.Buffer
128- err := tmpl .Execute (& tpl , data )
129- if err != nil {
130- return "" , fmt .Errorf ("error executing statusz template: %w" , err )
131- }
87+ status := fmt .Sprintf (`
88+ Started%[1]s %[2]s
89+ Up%[1]s %[3]s
90+ Go version%[1]s %[4]s
91+ Binary version%[1]s %[5]s
92+ %[6]s
93+ ` , delim , startTime , uptime , goVersion , binaryVersion , emulationVersion )
13294
133- return tpl . String () , nil
95+ return status , nil
13496}
13597
13698func uptime (t time.Time ) string {
0 commit comments