8000 🎨 Fix dashboard brand drift and fold Releases into issues by abahmed · Pull Request #598 · abahmed/kwatch · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 128 additions & 77 deletions docs/dashboard.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/dashboard/bad_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestPageListenerAnswersABrokenEscapeWithThePage(t *testing.T) {
got := rawGet(t, "/issues/%E0%A4%A")
if !strings.HasPrefix(got, "HTTP/1.1 400 Bad Request\r\n") ||
!strings.Contains(got, "text/html") ||
!strings.Contains(got, "<title>Not found · kwatch</title>") ||
!strings.Contains(got, "<title>Not found - kwatch</title>") ||
!strings.Contains(got, "Content-Security-Policy: ") {
t.Errorf("reply = %q", got)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/dashboard/bad_request_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestTLSServeAnswersABrokenEscapeWithThePage(t *testing.T) {
got := string(body)
if !strings.HasPrefix(got, "HTTP/1.1 400 Bad Request\r\n") ||
!strings.Contains(got, "text/html") ||
!strings.Contains(got, "<title>Not found · kwatch</title>") ||
!strings.Contains(got, "<title>Not found - kwatch</title>") ||
!strings.Contains(got, "Content-Security-Policy: ") {
t.Errorf("reply = %q", got)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

// browserReleases is a release book with one release of each kind of
// row the Releases page draws: healthy, failed with an undo command, and
// down under GitOps with an undo in Git.
// row the issue page and Overview draw: healthy, failed with an undo
// command, and down under GitOps with an undo in Git.
func browserReleases() *releaseBook {
at := time.Now().Add(-time.Hour)
after := 12.0
Expand All @@ -27,6 +27,8 @@ func browserReleases() *releaseBook {
{ID: "b", Kind: "statefulset", Namespace: "data", Name: "db",
At: at.Add(-time.Hour), Verdict: release.Down,
App: "argocd/data", Changed: []string{"environment"}},
{ID: "d", Kind: "deployment", Namespace: "shop", Name: "web",
At: at.Add(-30 * time.Minute), Verdict: release.Regressed},
{ID: "c", Kind: "daemonset", Namespace: "ops", Name: "agent",
At: at.Add(-2 * time.Hour), Verdict: release.Healthy,
RolledOut: at.Add(-2*time.Hour + time.Minute),
Expand Down
2 changes: 1 addition & 1 deletion internal/dashboard/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func browserIssues() []incident.Incident {
func TestBrowserDashboardPages(t *testing.T) {
ctx, srv := browserSetup(t)
pages := []string{"/", "/issues", "/issues/inc-001", "/activity",
"/kwatch", "/map", "/map?view=failing", "/releases"}
"/kwatch", "/map", "/map?view=failing"}
for _, size := range browserSizes {
tab, problems := browserTab(t, ctx)
signInBrowser(t, tab, srv.URL)
Expand Down
4 changes: 3 additions & 1 deletion internal/dashboard/detail_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func (s *Server) detailOf(p incident.Incident) IncidentDetail {
if u := s.sources.usage.Load(); u != nil {
c.usage = *u
}
return buildDetail(p, c)
d := buildDetail(p, c)
d.Release = s.releaseFor(p.ID, p.Opened)
return d
}

// addContext fills the page's context parts of d. Each list is bounded
Expand Down
191 changes: 191 additions & 0 deletions internal/dashboard/detail_release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package dashboard

import (
"time"

"github.com/abahmed/kwatch/internal/release"
)

// ReleaseCause is the release linked to an incident: what changed,
// how it went and how to undo it. The dashboard only shows it; the
// command is for the person to run.
type ReleaseCause struct {
release.Record
// RolloutSeconds is how long the rollout took, once it finished.
RolloutSeconds *float64 `json:"rolloutSeconds,omitempty"`
Undo UndoStep `json:"undo"`
// Newest says no later release of the workload exists.
Newest bool `json:"newest"`
}

// UndoStep is how to undo a release: a text and, when one does it, the
// command.
type UndoStep struct {
Text string `json:"text"`
Command string `json:"command,omitempty"`
Mutating bool `json:"mutating,omitempty"`
}

// ReleaseSummary is the Overview's line about the last 24 hours.
type ReleaseSummary struct {
Total int `json:"total"`
Bad int `json:"bad"`
// BadIncidents are the issues the releases that went wrong
// caused, for the link from "went wrong".
BadIncidents []string `json:"badIncidents"`
// Wrong lists the releases that went wrong, newest first, so one
// that opened no issue (a regression) is still seen. Incident is
// empty for it.
Wrong []WrongRelease `json:"wrong"`
}

// maxWrong bounds Wrong.
const maxWrong = 5

// WrongRelease is a release that went wrong, in brief.
type WrongRelease struct {
ID string `json:"id"`
Kind string `json:"kind"`
Namespace string `json:"namespace"`
Name string `json:"name"`
At time.Time `json:"at"`
Verdict release.Verdict `json:"verdict"`
Incident string `json:"incident,omitempty"`
}

// releaseWindow is the window of the Overview's line.
const releaseWindow = 24 * time.Hour

// releaseBookSnapshot is a copy of the book, without its detail when
// the change history (Pro) is off: a book kept from a Pro run serves
// none of it.
func (s *Server) releaseBookSnapshot() (release.Book, bool) {
book := s.sources.releases.Load()
if book == nil {
return release.Book{}, false
}
snap := book.snapshot()
if !s.opts.ChangeHistory {
stripDetail(&snap)
}
return snap, true
}

// releaseFor is the release that caused the incident, or nil. A release
// that rolled out after the incident started cannot have caused it.
func (s *Server) releaseFor(id string, opened time.Time) *ReleaseCause {
snap, ok := s.releaseBookSnapshot()
if !ok {
return nil
}
r := releaseOf(snap, id)
if r == nil || (!opened.IsZero() && r.At.After(opened.Add(releaseSlack))) {
return nil
}
return r
}

// releaseSlack is how far after an incident's start a release may be
// stamped and still count: the two are noticed on different ticks.
const releaseSlack = time.Minute

// releaseOf finds the newest release of b linked to incident id.
func releaseOf(b release.Book, id string) *ReleaseCause {
newest := newestReleases(b)
var best *release.Record
for i := range b.Records {
r := &b.Records[i]
if r.Incident == id && (best == nil || r.At.After(best.At)) {
best = r
}
}
if best == nil {
return nil
}
c := releaseCause(*best, newest[workloadKey(*best)].Equal(best.At))
return &c
}

// summarizeReleases counts the releases since from and the issues the
// bad ones caused.
func summarizeReleases(b release.Book, from time.Time) *ReleaseSummary {
out := &ReleaseSummary{BadIncidents: []string{},
Wrong: []WrongRelease{}}
for _, r := range b.Records {
if r.At.Before(from) {
continue
}
out.Total++
if !r.Verdict.Bad() {
continue
}
out.Bad++
if len(out.Wrong) < maxWrong {
out.Wrong = append(out.Wrong, WrongRelease{ID: r.ID,
Kind: r.Kind, Namespace: r.Namespace, Name: r.Name,
At: r.At, Verdict: r.Verdict, Incident: r.Incident})
}
if r.Incident != "" {
out.BadIncidents = append(out.BadIncidents, r.Incident)
}
}
return out
}

// stripDetail drops every release's health comparison and error line.
func stripDetail(b *release.Book) {
for i := range b.Records {
b.Records[i].Detail = nil
}
}

// workloadKey names a release's workload.
func workloadKey(r release.Record) string {
return r.Kind + "/" + r.Namespace + "/" + r.Name
}

// newestReleases is the start of each workload's newest release.
func newestReleases(b release.Book) map[string]time.Time {
out := map[string]time.Time{}
for _, r := range b.Records {
if key := workloadKey(r); r.At.After(out[key]) {
out[key] = r.At
}
}
return out
}

// releaseCause is r for the page. Only the workload's newest release
// offers an undo command: a rollback goes back one revision from what
// runs now, so on an older release it would bring back the newer one
// (or the bad release a rollback replaced).
func releaseCause(r release.Record, newest bool) ReleaseCause {
u := r.Undo()
if !newest && u.Command != "" {
u = release.Undo{Text: "A newer release of this workload came " +
"after this one: undo from the newest release"}
}
item := ReleaseCause{Record: r, Newest: newest,
Undo: UndoStep{Text: u.Text, Command: u.Command,
Mutating: u.Mutating}}
if !r.RolledOut.IsZero() && !r.RolledOut.Before(r.At) {
took := r.RolledOut.Sub(r.At).Seconds()
item.RolloutSeconds = &took
}
return item
}

// wrongReleaseIssues is the IDs of the issues that releases of the last
// 24 hours that went wrong caused (?release=bad).
func (s *Server) wrongReleaseIssues(now time.Time) map[string]bool {
out := map[string]bool{}
if snap, ok := s.releaseBookSnapshot(); ok {
for _, r := range snap.Records {
if r.Verdict.Bad() && r.Incident != "" &&
!r.At.Before(now.Add(-releaseWindow)) {
out[r.Incident] = true
}
}
}
return out
}
122 changes: 122 additions & 0 deletions internal/dashboard/detail_release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package dashboard

import (
"encoding/json"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/abahmed/kwatch/internal/incident"
"github.com/abahmed/kwatch/internal/release"
)

// releasesServer is a test server whose release book holds records.
func releasesServer(records ...release.Record) *Server {
s := newTestServer()
s.sources.releases.Store(&releaseBook{
book: release.Book{Records: records}})
return s
}

func releaseOfIncident(t *testing.T, s *Server, id string) *ReleaseCause {
t.Helper()
w := call(t, s, http.MethodGet, "/api/incidents/"+id, testToken)
require.Equal(t, 200, w.Code, w.Body.String())
var out IncidentDetail
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &out))
10BC8 return out.Release
}

// A release stamped after the incident started is not its cause.
func TestReleaseAfterTheIncidentIsNotLinked(t *testing.T) {
now := testEpoch
s := releasesServer(release.Record{ID: "a", Kind: "deployment",
Namespace: "shop", Name: "api", At: now.Add(time.Hour),
Verdict: release.Failed, Incident: "inc-1"})
s.SetIncidents(&fakeIncidents{items: []incident.Incident{
testIncident("inc-1", "shop", 0)}})
assert.Nil(t, releaseOfIncident(t, s, "inc-1"))
}

func TestScopeCovers(t *testing.T) {
assert.True(t, Scope{}.covers("any"))
assert.False(t, Scope{Excluded: []string{"kube-system"}}.
covers("kube-system"))
assert.True(t, Scope{Watched: []string{"shop"}}.covers("shop"))
assert.False(t, Scope{Watched: []string{"shop"}}.covers("data"))
}

func TestIncidentDetailCarriesItsRelease(t *testing.T) {
now := testEpoch
s := releasesServer(release.Record{ID: "a", Kind: "deployment",
Namespace: "shop", Name: "api", At: now.Add(-time.Hour),
RolledOut: now.Add(-time.Hour + time.Minute),
Verdict: release.Failed, App: "argocd/shop",
Incident: "inc-1"})
s.SetIncidents(&fakeIncidents{items: []incident.Incident{
testIncident("inc-1", "shop", 0)}})
got := releaseOfIncident(t, s, "inc-1")
require.NotNil(t, got)
assert.Equal(t, release.Failed, got.Verdict)
assert.True(t, got.Newest)
assert.Contains(t, got.Undo.Text, "Revert the change in Git")
assert.Empty(t, got.Undo.Command)
if assert.NotNil(t, got.RolloutSeconds) {
assert.InDelta(t, 60, *got.RolloutSeconds, 0.1)
}
assert.Nil(t, releaseOf(release.Book{}, "inc-1"))
}

// Only a workload's newest release offers an undo command: on an older
// one it would bring back what came after it. Without change history
// (not Pro) no release serves its detail, even one a Pro run kept.
func TestReleaseUndoOnNewestAndDetailOnlyWithHistory(t *testing.T) {
now := testEpoch
restarts := 9.0
book := release.Book{Records: []release.Record{
{ID: "new", Kind: "deployment", Namespace: "shop", Name: "api",
At: now.Add(-time.Hour), Verdict: release.Healthy,
Incident: "n"},
{ID: "old", Kind: "deployment", Namespace: "shop", Name: "api",
At: now.Add(-2 * time.Hour), Verdict: release.Failed,
Incident: "o",
Detail: &release.Detail{RestartsAfter: &restarts}}}}
newest := releaseOf(book, "n")
assert.Contains(t, newest.Undo.Command, "rollout undo")
old := releaseOf(book, "o")
assert.Empty(t, old.Undo.Command)
assert.False(t, old.Newest)
assert.Contains(t, old.Undo.Text, "newer release")
stripDetail(&book)
assert.Nil(t, releaseOf(book, "o").Detail)
}

// A release that went wrong without opening an issue (a regression)
// still counts and is listed, so the Overview shows it.
func TestSummarizeReleasesKeepsRegressionsWithoutAnIssue(t *testing.T) {
now := testEpoch
sum := summarizeReleases(release.Book{Records: []release.Record{
{ID: "a", Name: "a", At: now.Add(-time.Hour),
Verdict: release.Failed, Incident: "inc-1"},
{ID: "b", Name: "b", At: now.Add(-2 * time.Hour),
Verdict: release.Regressed},
{ID: "c", Name: "c", At: now.Add(-3 * time.Hour),
Verdict: release.Healthy},
{ID: "d", Name: "d", At: now.Add(-3 * 24 * time.Hour),
Verdict: release.Down},
}}, now.Add(-releaseWindow))
assert.Equal(t, 3, sum.Total)
assert.Equal(t, 2, sum.Bad)
assert.Equal(t, []string{"inc-1"}, sum.BadIncidents)
require.Len(t, sum.Wrong, 2)
assert.Empty(t, sum.Wrong[1].Incident)
}

func TestReleasesRouteIsGone(t *testing.T) {
s := releasesServer()
w := call(t, s, http.MethodGet, "/api/releases", testToken)
assert.Equal(t, http.StatusNotFound, w.Code)
}
Loading
0