Skip to content

Commit

Permalink
Live reload of probe history
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeda committed Jan 18, 2017
1 parent dc307da commit fb3ef6f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
21 changes: 15 additions & 6 deletions cmd/kuard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ type pageContext struct {
}

type kuard struct {
tg htmlutils.TemplateGroup
tg *htmlutils.TemplateGroup

live debugprobe.Probe
ready debugprobe.Probe
live *debugprobe.Probe
ready *debugprobe.Probe
}

func (k *kuard) getPageContext(r *http.Request) *pageContext {
Expand Down Expand Up @@ -112,8 +112,17 @@ func (k *kuard) addRoutes(router *httprouter.Router) {
router.Handler("GET", "/static/*filepath", http.StripPrefix("/static/", http.FileServer(fs)))
router.Handler("GET", "/fs/*filepath", http.StripPrefix("/fs", http.FileServer(http.Dir("/"))))

k.live.AddRoutes("/healthy", router)
k.ready.AddRoutes("/ready", router)
k.live.AddRoutes(router)
k.ready.AddRoutes(router)
}

func NewApp() *kuard {
k := &kuard{
tg: &htmlutils.TemplateGroup{},
}
k.live = debugprobe.New("/healthy", k.tg)
k.ready = debugprobe.New("/ready", k.tg)
return k
}

func main() {
Expand All @@ -126,7 +135,7 @@ func main() {
log.Println("* and secret information. Be careful.")
log.Println(strings.Repeat("*", 70))

app := kuard{}
app := NewApp()
router := httprouter.New()
app.addRoutes(router)

Expand Down
22 changes: 18 additions & 4 deletions pkg/debugprobe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sync"
"time"

"github.com/jbeda/kuard/pkg/htmlutils"
"github.com/julienschmidt/httprouter"
)

Expand All @@ -31,6 +32,8 @@ const maxHistory = 20
type Probe struct {
mu sync.Mutex

tg *htmlutils.TemplateGroup

basePath string
// If failNext > 0, then fail next probe and decrement. If failNext < 0, then
// fail forever.
Expand All @@ -50,10 +53,17 @@ type ProbeContext struct {
History []ProbeHistory
}

func (p *Probe) AddRoutes(base string, r *httprouter.Router) {
p.basePath = base
r.GET(base, p.Handle)
r.POST(base+"/config", p.Config)
func New(base string, tg *htmlutils.TemplateGroup) *Probe {
return &Probe{
basePath: base,
tg: tg,
}
}

func (p *Probe) AddRoutes(r *httprouter.Router) {
r.GET(p.basePath, p.Handle)
r.POST(p.basePath+"/config", p.Config)
r.GET(p.basePath+"/render", p.Render)
}

func (p *Probe) Handle(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
Expand Down Expand Up @@ -93,6 +103,10 @@ func (p *Probe) Config(w http.ResponseWriter, r *http.Request, _ httprouter.Para
http.Redirect(w, r, "/", http.StatusFound)
}

func (p *Probe) Render(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
p.tg.Render(w, "probe.html", p.GetContext())
}

func (p *Probe) recordRequest(_ *http.Request, code int) {
entry := &ProbeHistory{
When: time.Now(),
Expand Down
4 changes: 4 additions & 0 deletions sitedata/static/js/jquery-3.1.1.min.js

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions sitedata/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
color: {{ .VersionColor }};
}
</style>

<script src="/static/js/jquery-3.1.1.min.js"></script>
</head>

<body>
Expand Down Expand Up @@ -72,12 +74,12 @@ <h1>Kubernetes Up and Running</h1>

<details open>
<summary>Liveness Probe</summary>
{{template "probe" .Liveness}}
<div id=liveness></div>
</details>

<details open>
<summary>Readiness Probe</summary>
{{template "probe" .Readiness }}
<div id=readiness></div>
</details>

<details>
Expand All @@ -86,5 +88,18 @@ <h1>Kubernetes Up and Running</h1>
<a href="/fs/">Browse the root file system for this server.</a>
</div>
</details>
<script>
function renderLiveness() {
$("#liveness").load("{{.Liveness.BasePath}}/render")
}
renderLiveness()
setInterval(renderLiveness, 1000)

function renderReadiness() {
$("#readiness").load("{{.Readiness.BasePath}}/render")
}
renderReadiness()
setInterval(renderReadiness, 1000)
</script>
</body>
</html>
21 changes: 21 additions & 0 deletions sitedata/templates/probe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div>
<p>Probe is being served on <a href="{{.BasePath}}">{{.BasePath}}</a></p>
<p>{{if eq .FailNext 0 }}Probe will permanently succeed
{{- else if gt .FailNext 0 }}Probe will fail for next {{.FailNext}} calls
{{- else}}Probe will permanently fail{{end}}
</p>
{{- if .History}}
<table>
<tr>
<th>When</th><th>&nbsp;</th><th>Status</th>
</tr>
{{- range .History }}
<tr>
<td>{{.When|friendlytime}}</td><td>{{.When|reltime}}</td><td>{{.Code}}</td>
</tr>
{{- end}}
</table>
{{- else}}
<p>No recorded probe history</p>
{{- end}}
</div>

0 comments on commit fb3ef6f

Please sign in to comment.