feat: master/agent phase 5 — drift and merge, backend (#10) (#30)
Docker Build / docker (push) Successful in 42s

## Summary
- Drift detection: `model.Service.Drifted()` compares desired and running version by digest (tag when the digest is unknown). `services.awaiting_inventory` hides drift between a job result and the next inventory write.
- Poll and webhook jobs are refused for a drifted service; `POST /services/{id}/restore` (manual job to `desired_ref`) and `POST /services/{id}/accept` (running version becomes desired, guarded by `actual` = `Service.RunningRef`).
- Move suggestions (`store.MoveCandidates`, Compose only) and `POST /services/{id}/merge`: the old row keeps its id, mode, `desired_ref` and history and takes the new location; the duplicate is deleted.
- Backend only. The buttons and badges come with #28, on top of the Ledger UI (#18).

Closes #10

## Test plan
- [x] `gofmt -l .`, `go vet ./...`, `go test ./...`
- [x] `go test -race ./internal/agent/... ./internal/master/... ./internal/e2e/`
- [x] `go test -tags integration ./internal/master/store/` against postgres:17-alpine
- [ ] On the server: migration 0005 applies; no drift shown after a normal update
- [ ] On the server: manual `docker compose up` with another tag gives drift; webhook for an auto service is refused
- [ ] On the server: restore and accept via POST; merge after moving a stack

Reviewed-on: #30
This commit was merged in pull request #30.
This commit is contained in:
2026-10-13 09:04:36 +00:00
parent 307e46e4c7
commit 919ad91710
21 changed files with 2139 additions and 39 deletions
+3 -5
View File
@@ -11,7 +11,6 @@ import (
"net"
"net/http"
"net/http/httptest"
"regexp"
"sort"
"strings"
"sync"
@@ -25,6 +24,7 @@ import (
"github.com/shcizo/package-updater/internal/agent/conn"
"github.com/shcizo/package-updater/internal/agent/updater"
"github.com/shcizo/package-updater/internal/agenttoken"
"github.com/shcizo/package-updater/internal/imageref"
"github.com/shcizo/package-updater/internal/master/events"
"github.com/shcizo/package-updater/internal/master/hub"
"github.com/shcizo/package-updater/internal/master/model"
@@ -32,9 +32,6 @@ import (
"github.com/shcizo/package-updater/internal/protocol"
)
// digestPattern is the digest format the real store records.
var digestPattern = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`)
type memStore struct {
mu sync.Mutex
agents map[string]model.Agent
@@ -245,7 +242,8 @@ func (m *memStore) FinishJob(_ context.Context, agentID, jobID int64, f model.Jo
if f.Status == model.JobSucceeded {
svc := m.svcRows[j.ServiceID]
svc.DesiredRef = f.Ref
if _, d, _ := strings.Cut(f.Ref, "@"); !strings.Contains(requestedRef, "@") && digestPattern.MatchString(d) {
svc.AwaitingInventory = true
if _, d, _ := strings.Cut(f.Ref, "@"); !strings.Contains(requestedRef, "@") && imageref.IsDigest(d) {
svc.LastTagDigest = d
}
// Same rule as the SQL: the job was created for the suggested ref after it was reported.
+113
View File
@@ -0,0 +1,113 @@
package e2e
import (
"context"
"io"
"log/slog"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/shcizo/package-updater/internal/master/model"
"github.com/shcizo/package-updater/internal/master/poller"
"github.com/shcizo/package-updater/internal/protocol"
)
const (
driftDesired = "sha256:" + "d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1d1"
driftOther = "sha256:" + "0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a"
)
// seedDrifted replaces the environment's service with an auto service that was
// updated to slusk:1.4.2 at driftDesired but runs driftOther now.
func (e *jobEnv) seedDrifted() {
e.store.seedService(model.Service{ID: 1, Kind: protocol.KindCompose, ComposeProject: "slusk", ComposeService: "web",
AgentID: ptr(agentID), ImageRepo: slusk, UpdateMode: model.ModeAuto,
DesiredRef: slusk + ":1.4.2@" + driftDesired,
ActualRef: slusk + ":1.4.2", ActualDigest: driftOther})
}
// inventoryWrite is what the store does for an agent's upsert of the service.
func (e *jobEnv) inventoryWrite(id int64, actualRef, actualDigest string) {
e.store.mu.Lock()
defer e.store.mu.Unlock()
svc := e.store.svcRows[id]
svc.ActualRef, svc.ActualDigest, svc.AwaitingInventory = actualRef, actualDigest, false
e.store.svcRows[id] = svc
}
func TestDriftedAutoServiceIsNotUpdatedByPollOrWebhook(t *testing.T) {
e := newJobEnv(t)
e.seedDrifted()
reg := &fixedRegistry{digests: map[string]string{slusk + ":1.4.2": "sha256:new"}}
p := poller.New(e.store, e.jobs, reg, e.m.broker, poller.Config{Workers: 1}, slog.New(slog.NewTextHandler(io.Discard, nil)))
p.Cycle(context.Background())
assert.Equal(t, 1, reg.calls)
assert.Zero(t, e.store.jobCount(), "polling starts no job for a drifted service")
svc, err := e.store.GetService(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, slusk+":1.4.2", svc.AvailableRef, "the suggestion is still recorded")
code, body := callWebhook(t, e.webhookServer(t), `{"image":"`+slusk+`:ignored","tag":"2.0.0"}`)
assert.Equal(t, 200, code)
require.Len(t, body.Results, 1)
assert.Equal(t, "refused", body.Results[0].Status)
assert.Contains(t, body.Results[0].Error, "drifted")
assert.Zero(t, e.store.jobCount(), "the webhook starts no job either")
}
func TestRestoreDeploysThePinnedRefOnTheAgent(t *testing.T) {
e := newJobEnv(t)
e.seedDrifted()
close(e.exec.release)
job, err := e.jobs.RestoreDesired(context.Background(), 1, "alice")
require.NoError(t, err)
assert.Equal(t, model.TriggerManual, job.Trigger)
select {
case ref := <-e.exec.started:
assert.Equal(t, slusk+":1.4.2@"+driftDesired, ref, "the agent receives exactly the desired ref")
case <-time.After(waitFor):
t.Fatal("the agent never received the job")
}
done := e.waitStatus(t, job.ID, model.JobSucceeded)
assert.Equal(t, slusk+":1.4.2@"+driftDesired, done.ToRef)
svc, err := e.store.GetService(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, driftOther, svc.LastTagDigest, "the drifted digest is remembered so polling does not redeploy it")
assert.False(t, svc.Drifted(), "the inventory has not caught up yet")
e.inventoryWrite(1, slusk+":1.4.2", driftDesired)
svc, err = e.store.GetService(context.Background(), 1)
require.NoError(t, err)
assert.False(t, svc.Drifted())
}
func TestNoDriftAfterASuccessfulJob(t *testing.T) {
e := newJobEnv(t)
close(e.exec.release)
job, err := e.jobs.Trigger(context.Background(), 1, "alice")
require.NoError(t, err)
e.waitStatus(t, job.ID, model.JobSucceeded)
svc, err := e.store.GetService(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, slusk+":1.4.2@"+e2eDigest, svc.DesiredRef)
assert.Equal(t, slusk+":1.4.2@sha256:old", svc.ActualRef, "the agent's inventory write has not arrived")
assert.False(t, svc.Drifted(), "between job.result and the inventory write the row is not drifted")
e.inventoryWrite(1, slusk+":1.4.2", e2eDigest)
svc, _ = e.store.GetService(context.Background(), 1)
assert.False(t, svc.Drifted())
e.inventoryWrite(1, slusk+":1.4.2", driftOther)
svc, _ = e.store.GetService(context.Background(), 1)
assert.True(t, svc.Drifted(), "a later inventory write of another digest is drift")
}