feat(webui): surface per-component update status on /api/version and Configuration
The prior two changes gave every component an InstallType/Reference and a way to observe what's actually running (installtype.Type(), daemon.AgentStatus, [nats] config), but nothing consumed any of it -- an operator still had no single place to see whether a component's actual running state agrees with what the update-check command claims. Add GET /api/version: per component, install type, observed running version, the check command's installed/available claims, and a computed status -- ok / update_available / drift / unknown. drift takes priority over update_available (a claim the running system contradicts is worse than a newer release merely existing), and a component with no installed claim at all reports unknown rather than guessing "ok" or "update_available" from a comparison that was never actually made. Surface it as an "Update status" card above the Configuration page's existing config view, sourced independently so its own failure (or a 501 on deployments with no update-check command configured, which renders nothing rather than an error) never blocks the config view that's already there. Closes archie-core-1786751948782-3-837a8fd0.3 and, with it, archie-core-1786751948782-3-837a8fd0 (all 5 design points landed).
This commit is contained in:
1 parent
25af279d7a
commit
08fa79e5af
9 files changed
+417
-25
No files matched your search
@@ -0,0 +1,101 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
|
||||
// componentStatus is the per-component update status enum surfaced to the
|
||||
// dashboard: an operator scanning this list needs "is this component fine,
|
||||
// or does it need attention, and why" at a glance, not a bare color that
|
||||
// means something different on every row.
|
||||
const (
|
||||
// statusOK means the running version matches what the check command
|
||||
// reports installed, and nothing newer is available.
|
||||
statusOK = "ok"
|
||||
// statusUpdateAvailable means a newer version exists and nothing
|
||||
// contradicts the check command's claim of what's currently running.
|
||||
statusUpdateAvailable = "update_available"
|
||||
// statusDrift means a running version was actually observed and it
|
||||
// does not match what the check command claims is installed -- the
|
||||
// same false-success shape releaseupdate.Report.Verify catches after
|
||||
// an update, but here for the steady-state view.
|
||||
statusDrift = "drift"
|
||||
// statusUnknown means no running version could be confirmed for this
|
||||
// component, so neither ok nor drift can be asserted honestly.
|
||||
statusUnknown = "unknown"
|
||||
)
|
||||
|
||||
// componentVersionStatus is one row of GET /api/version: what a component
|
||||
// claims to have installed, what was actually observed running (if
|
||||
// anything), what's available, how it's deployed, and the resulting status.
|
||||
type componentVersionStatus struct {
|
||||
ID string `json:"id"`
|
||||
Label string `json:"label"`
|
||||
InstallType string `json:"install_type,omitempty"`
|
||||
Reference string `json:"reference,omitempty"`
|
||||
RunningVersion string `json:"running_version,omitempty"`
|
||||
InstalledClaim string `json:"installed_claim,omitempty"`
|
||||
LatestAvailable string `json:"latest_available,omitempty"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// versionStatusFor decides one component's status from its check-command
|
||||
// claim (installed) against the version actually observed running (running,
|
||||
// ok). Order matters: drift is checked before update_available because a
|
||||
// claim the running system actively contradicts is a more fundamental
|
||||
// problem than a newer release merely existing -- an operator who only
|
||||
// sees "update available" on a component that is silently not running what
|
||||
// it claims would look in the wrong place.
|
||||
func versionStatusFor(component releaseupdate.Component, running string, ok bool) string {
|
||||
if ok && component.Installed != "" && running != component.Installed {
|
||||
return statusDrift
|
||||
}
|
||||
if !ok || component.Installed == "" {
|
||||
// No baseline to compare against -- an empty Installed means the
|
||||
// check command never reported one, so Available (if any) cannot
|
||||
// be judged newer than nothing. Reporting update_available here
|
||||
// would assert a comparison that was never actually made.
|
||||
return statusUnknown
|
||||
}
|
||||
if component.Available != "" && component.Available != component.Installed {
|
||||
return statusUpdateAvailable
|
||||
}
|
||||
return statusOK
|
||||
}
|
||||
|
||||
// handleVersion reports, per component, how it's deployed and whether what
|
||||
// is actually running matches what the operator's update-check command
|
||||
// claims -- the steady-state counterpart to the phase-2 update report
|
||||
// (update_report.go), which only fires around an update itself.
|
||||
func (s *Server) handleVersion(w http.ResponseWriter, r *http.Request) {
|
||||
if s.Chat == nil || !chatUpdateServiceConfigured(s.Chat.Updates) {
|
||||
http.Error(w, "updates are not configured", http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
snapshot, err := s.Chat.Updates.Check(r.Context(), 0)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
running := map[string]string{}
|
||||
if s.RunningVersions != nil {
|
||||
running = s.RunningVersions()
|
||||
}
|
||||
components := make([]componentVersionStatus, 0, len(snapshot.Components))
|
||||
for _, component := range snapshot.Components {
|
||||
runningVersion, ok := running[component.ID]
|
||||
if ok && runningVersion == "" {
|
||||
ok = false
|
||||
}
|
||||
components = append(components, componentVersionStatus{
|
||||
ID: component.ID, Label: component.Label,
|
||||
InstallType: component.InstallType, Reference: component.Reference,
|
||||
RunningVersion: runningVersion, InstalledClaim: component.Installed,
|
||||
LatestAvailable: component.Available,
|
||||
Status: versionStatusFor(component, runningVersion, ok),
|
||||
})
|
||||
}
|
||||
writeJSON(w, map[string]any{"components": components})
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
|
||||
func TestVersionStatusForCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
component releaseupdate.Component
|
||||
running string
|
||||
ok bool
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "matches, nothing newer",
|
||||
component: releaseupdate.Component{Installed: "1.0.0"},
|
||||
running: "1.0.0", ok: true,
|
||||
want: statusOK,
|
||||
},
|
||||
{
|
||||
name: "newer available, running matches installed claim",
|
||||
component: releaseupdate.Component{Installed: "1.0.0", Available: "1.1.0"},
|
||||
running: "1.0.0", ok: true,
|
||||
want: statusUpdateAvailable,
|
||||
},
|
||||
{
|
||||
name: "running contradicts the installed claim",
|
||||
component: releaseupdate.Component{Installed: "1.1.0"},
|
||||
running: "1.0.0", ok: true,
|
||||
want: statusDrift,
|
||||
},
|
||||
{
|
||||
name: "nothing observed running",
|
||||
component: releaseupdate.Component{Installed: "1.0.0"},
|
||||
running: "", ok: false,
|
||||
want: statusUnknown,
|
||||
},
|
||||
{
|
||||
name: "drift takes priority over update_available",
|
||||
component: releaseupdate.Component{Installed: "1.1.0", Available: "1.2.0"},
|
||||
running: "1.0.0", ok: true,
|
||||
want: statusDrift,
|
||||
},
|
||||
{
|
||||
name: "check command reported no installed claim -- nothing to compare against",
|
||||
component: releaseupdate.Component{},
|
||||
running: "1.0.0", ok: true,
|
||||
want: statusUnknown,
|
||||
},
|
||||
{
|
||||
name: "available with no installed claim must not read as an update, since nothing was compared",
|
||||
component: releaseupdate.Component{Available: "1.0.0"},
|
||||
running: "1.0.0", ok: true,
|
||||
want: statusUnknown,
|
||||
},
|
||||
{
|
||||
name: "installed equals available -- nothing new, matches Snapshot.Available's own semantics",
|
||||
component: releaseupdate.Component{Installed: "1.0.0", Available: "1.0.0"},
|
||||
running: "1.0.0", ok: true,
|
||||
want: statusOK,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := versionStatusFor(test.component, test.running, test.ok); got != test.want {
|
||||
t.Errorf("versionStatusFor(%+v, %q, %v) = %q, want %q", test.component, test.running, test.ok, got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleVersionComposesCheckAndRunningVersions(t *testing.T) {
|
||||
sessions := gateway.NewSessionStoreMemory()
|
||||
t.Cleanup(func() { _ = sessions.Close() })
|
||||
updates := &chatUpdateStub{snapshot: releaseupdate.Snapshot{Components: []releaseupdate.Component{
|
||||
{ID: "daemon", Label: "Gateway", Installed: "1.0.0", Available: "1.1.0", InstallType: "binary"},
|
||||
{ID: "agent", Label: "Runtime", Installed: "1.0.0", InstallType: "container", Reference: "img@sha256:abc"},
|
||||
}}}
|
||||
server := &Server{
|
||||
Chat: &ChatService{Router: gateway.NewRouter(chatStatusStub{}, nil, "web"), Sessions: sessions, Updates: updates},
|
||||
RunningVersions: func() map[string]string { return map[string]string{"daemon": "1.0.0"} },
|
||||
}
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(res, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/version", nil))
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", res.Code, res.Body)
|
||||
}
|
||||
var got struct {
|
||||
Components []componentVersionStatus `json:"components"`
|
||||
}
|
||||
if err := json.Unmarshal(res.Body.Bytes(), &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got.Components) != 2 {
|
||||
t.Fatalf("components = %#v, want 2", got.Components)
|
||||
}
|
||||
daemon := got.Components[0]
|
||||
if daemon.Status != statusUpdateAvailable || daemon.RunningVersion != "1.0.0" || daemon.InstallType != "binary" {
|
||||
t.Errorf("daemon = %+v, want status=%q running_version=1.0.0 install_type=binary", daemon, statusUpdateAvailable)
|
||||
}
|
||||
agent := got.Components[1]
|
||||
if agent.Status != statusUnknown || agent.RunningVersion != "" {
|
||||
t.Errorf("agent = %+v, want status=%q with no observed running version (not wired into RunningVersions in this test)", agent, statusUnknown)
|
||||
}
|
||||
if agent.Reference != "img@sha256:abc" {
|
||||
t.Errorf("agent.Reference = %q, want %q", agent.Reference, "img@sha256:abc")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleVersionTreatsPresentButEmptyRunningVersionAsUnobserved covers a
|
||||
// map entry that IS present but holds "" -- distinct from a missing key,
|
||||
// and reachable in production: a malformed or pre-this-change archie-agent
|
||||
// build could self-report an empty AgentVersion, which daemon.AgentStatus
|
||||
// would still record as observed=true, version="". An empty string is not
|
||||
// a version any component is actually running, so it must read as
|
||||
// unobserved rather than as a running value to compare against.
|
||||
func TestHandleVersionTreatsPresentButEmptyRunningVersionAsUnobserved(t *testing.T) {
|
||||
sessions := gateway.NewSessionStoreMemory()
|
||||
t.Cleanup(func() { _ = sessions.Close() })
|
||||
updates := &chatUpdateStub{snapshot: releaseupdate.Snapshot{Components: []releaseupdate.Component{
|
||||
{ID: "agent", Label: "Runtime", Installed: "1.0.0"},
|
||||
}}}
|
||||
server := &Server{
|
||||
Chat: &ChatService{Router: gateway.NewRouter(chatStatusStub{}, nil, "web"), Sessions: sessions, Updates: updates},
|
||||
RunningVersions: func() map[string]string { return map[string]string{"agent": ""} },
|
||||
}
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(res, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/version", nil))
|
||||
var got struct {
|
||||
Components []componentVersionStatus `json:"components"`
|
||||
}
|
||||
if err := json.Unmarshal(res.Body.Bytes(), &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Components[0].Status != statusUnknown {
|
||||
t.Errorf("status = %q, want %q for a present-but-empty running version", got.Components[0].Status, statusUnknown)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleVersionUnconfigured(t *testing.T) {
|
||||
server := &Server{}
|
||||
|
||||
res := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(res, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/version", nil))
|
||||
if res.Code != http.StatusNotImplemented {
|
||||
t.Fatalf("status = %d, want %d", res.Code, http.StatusNotImplemented)
|
||||
}
|
||||
}
|
||||
@@ -236,6 +236,7 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("GET /api/channels", s.handleChannels)
|
||||
mux.HandleFunc("POST /api/channels/{id}/reload", s.handleChannelReload)
|
||||
mux.HandleFunc("GET /api/config", s.handleConfig)
|
||||
mux.HandleFunc("GET /api/version", s.handleVersion)
|
||||
mux.HandleFunc("PATCH /api/config", s.handleConfigUpdate)
|
||||
mux.HandleFunc("POST /api/config/reset", s.handleConfigReset)
|
||||
mux.HandleFunc("GET /api/logs", s.handleLogs)
|
||||
|
||||
Reference in new issue
Block a user