diff --git a/hscontrol/api/v2/devices.go b/hscontrol/api/v2/devices.go index 61c96d1f0..d57fc9dd7 100644 --- a/hscontrol/api/v2/devices.go +++ b/hscontrol/api/v2/devices.go @@ -108,6 +108,11 @@ type DevicePostureAttributes struct { Expiries map[string]time.Time `json:"expiries,omitempty"` } +// DeviceUpdateStatus is the GET /device/{id}/update-status response. +type DeviceUpdateStatus struct { + Status string `enum:"updating,not-updating" json:"status"` +} + // Request bodies match the Tailscale SDK wire shapes and, like Tailscale's, // ignore properties they do not define. type ( @@ -186,6 +191,8 @@ type ( } } emptyOutput struct{ Body struct{} } + + deviceUpdateStatusOutput struct{ Body DeviceUpdateStatus } ) func registerDevices(api huma.API, b Backend) { @@ -541,8 +548,38 @@ func registerDevices(api huma.API, b Backend) { return nil, huma.Error404NotFound("not found") } + b.State.ClientUpdateStarted(node.ID(), hi.IPNVersion()) + return &emptyOutput{}, nil }) + + huma.Register(api, requireScope(huma.Operation{ + OperationID: "getDeviceUpdateStatus", + Method: http.MethodGet, + Path: "/api/v2/device/{id}/update-status", + Summary: "Get a device's client update status", + Description: "updating from a started client update until the device runs a new version, else not-updating.", + Tags: deviceTags, + Security: security, + Errors: []int{http.StatusUnauthorized, http.StatusForbidden, http.StatusNotFound}, + }, scope.DevicesCoreRead), func(_ context.Context, in *deviceIDInput) (*deviceUpdateStatusOutput, error) { + node, err := lookupNode(b, in.DeviceID) + if err != nil { + return nil, err + } + + var version string + if hi := node.Hostinfo(); hi.Valid() { + version = hi.IPNVersion() + } + + status := "not-updating" + if b.State.ClientUpdating(node.ID(), version) { + status = "updating" + } + + return &deviceUpdateStatusOutput{Body: DeviceUpdateStatus{Status: status}}, nil + }) } // lookupNode resolves a device id to its NodeView, mapping a malformed or diff --git a/hscontrol/apiv2_oauth_matrix_test.go b/hscontrol/apiv2_oauth_matrix_test.go index 1ef5bda3b..0d79aa1ce 100644 --- a/hscontrol/apiv2_oauth_matrix_test.go +++ b/hscontrol/apiv2_oauth_matrix_test.go @@ -125,6 +125,7 @@ func TestAPIv2OAuthMatrix_Enforcement(t *testing.T) { {"updateDeviceClient", http.MethodPost, "/api/v2/device/1/update-client", scope.DevicesCore, nil, false}, {"setDeviceRoutes", http.MethodPost, "/api/v2/device/1/routes", scope.DevicesRoutes, map[string]any{}, false}, {"getDeviceRoutes", http.MethodGet, "/api/v2/device/1/routes", scope.DevicesRoutesRead, nil, false}, + {"getDeviceUpdateStatus", http.MethodGet, "/api/v2/device/1/update-status", scope.DevicesCoreRead, nil, false}, {"getACL", http.MethodGet, "/api/v2/tailnet/-/acl", scope.PolicyFileRead, nil, false}, {"setACL", http.MethodPost, "/api/v2/tailnet/-/acl", scope.PolicyFile, map[string]any{}, false}, {"getSettings", http.MethodGet, "/api/v2/tailnet/-/settings", scope.FeatureSettingsRead, nil, false}, diff --git a/hscontrol/state/ping.go b/hscontrol/state/ping.go index 2433d8e38..b58c27315 100644 --- a/hscontrol/state/ping.go +++ b/hscontrol/state/ping.go @@ -10,14 +10,20 @@ import ( const pingIDLength = 16 +// clientUpdateWindow is how long a started client update reads as updating +// while the node keeps its old version, so a failed update ends. +const clientUpdateWindow = 10 * time.Minute + // pingTracker correlates outgoing [tailcfg.PingRequest]s with incoming HEAD // callbacks. Entries have no server-side TTL: callers are responsible // for cleaning up via [pingTracker.cancel] or by reading from the response -// channel within their own timeout. +// channel within their own timeout. It also remembers the client updates +// nodes started over c2n. type pingTracker struct { mu sync.Mutex pending map[string]*pendingPing c2n map[string]*pendingC2N + updates map[types.NodeID]startedUpdate } type pendingPing struct { @@ -33,10 +39,17 @@ type pendingC2N struct { responseCh chan []byte } +// startedUpdate is a client update a node started while running from. +type startedUpdate struct { + from string + at time.Time +} + func newPingTracker() *pingTracker { return &pingTracker{ pending: make(map[string]*pendingPing), c2n: make(map[string]*pendingC2N), + updates: make(map[types.NodeID]startedUpdate), } } @@ -165,6 +178,31 @@ func (pt *pingTracker) cancelC2N(c2nID string) { pt.mu.Unlock() } +// updateStarted records that nodeID started a client update at now while +// running version. +func (pt *pingTracker) updateStarted(nodeID types.NodeID, version string, now time.Time) { + pt.mu.Lock() + pt.updates[nodeID] = startedUpdate{from: version, at: now} + pt.mu.Unlock() +} + +// updating reports whether nodeID's started update still runs: the node +// still runs version and the window has not passed. An ended update is +// forgotten. +func (pt *pingTracker) updating(nodeID types.NodeID, version string, now time.Time) bool { + pt.mu.Lock() + defer pt.mu.Unlock() + + u, ok := pt.updates[nodeID] + if ok && u.from == version && now.Sub(u.at) < clientUpdateWindow { + return true + } + + delete(pt.updates, nodeID) + + return false +} + // RegisterPing tracks a pending ping and returns its ID and a channel // for the latency. Callers must defer [State.CancelPing] or read the channel // within their own timeout; there is no server-side TTL. @@ -207,3 +245,16 @@ func (s *State) CompleteC2N(c2nID string, body []byte) bool { func (s *State) CancelC2N(c2nID string) { s.pings.cancelC2N(c2nID) } + +// ClientUpdateStarted records that nodeID started a client update while +// running version. +func (s *State) ClientUpdateStarted(nodeID types.NodeID, version string) { + s.pings.updateStarted(nodeID, version, time.Now()) +} + +// ClientUpdating reports whether nodeID is still updating its client: it +// started an update while running version, still runs it, and +// [clientUpdateWindow] has not passed. +func (s *State) ClientUpdating(nodeID types.NodeID, version string) bool { + return s.pings.updating(nodeID, version, time.Now()) +} diff --git a/hscontrol/state/ping_test.go b/hscontrol/state/ping_test.go index 1aced946e..45f041bca 100644 --- a/hscontrol/state/ping_test.go +++ b/hscontrol/state/ping_test.go @@ -243,3 +243,20 @@ func TestPingTracker_DrainC2N(t *testing.T) { assert.Empty(t, pt.c2n, "c2n map should be empty after drain") } + +func TestPingTracker_ClientUpdate(t *testing.T) { + pt := newPingTracker() + start := time.Now() + + assert.False(t, pt.updating(types.NodeID(1), "1.0.0", start), "no update started") + + pt.updateStarted(types.NodeID(1), "1.0.0", start) + assert.True(t, pt.updating(types.NodeID(1), "1.0.0", start.Add(time.Minute)), "still on the old version") + assert.False(t, pt.updating(types.NodeID(2), "1.0.0", start), "another node") + + assert.False(t, pt.updating(types.NodeID(1), "1.2.0", start.Add(time.Minute)), "on a new version") + assert.False(t, pt.updating(types.NodeID(1), "1.0.0", start.Add(time.Minute)), "an ended update is forgotten") + + pt.updateStarted(types.NodeID(1), "1.0.0", start) + assert.False(t, pt.updating(types.NodeID(1), "1.0.0", start.Add(clientUpdateWindow)), "past the window") +}