Adds PutApplyStatus/ListApplyStatus so each process can report which version of a control-plane resource it is running, and the UI can read it back. docs/prds/control-plane-apply-status.md is the approved design. It goes on the State Store contract rather than the control plane, whose service is for editing a resource: a status report is not an edit. The config-snapshot pair is the precedent, and the deny-by-default arm of authorizesTaskScopedCall already refuses both new RPCs to a task grant, which TestOnlyAdminReportsApplyStatus pins. The row is keyed on (process, kind) and replaced in place, because a process re-stamps on an interval and nothing reads a history of re-stamps. An error leaves applied_version alone, so a rejected edit never erases the fact that an older version is live. The read is a list and an empty one is not an error, unlike the config snapshot's found=false singleton. Contract PRD inventory and RPC count follow in the same series. Relates to archie-core-pskb.
78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const applyStatusSchema = `
|
|
CREATE TABLE IF NOT EXISTS apply_status (
|
|
process TEXT NOT NULL,
|
|
kind TEXT NOT NULL,
|
|
applied_version INTEGER NOT NULL DEFAULT 0,
|
|
error TEXT NOT NULL DEFAULT '',
|
|
reported_at TEXT NOT NULL,
|
|
PRIMARY KEY (process, kind)
|
|
);
|
|
`
|
|
|
|
// applyStatusTimeLayout matches the rest of the store's timestamp columns.
|
|
const applyStatusTimeLayout = time.RFC3339
|
|
|
|
// PutApplyStatus records what one process applied for one resource kind. The
|
|
// key is (process, kind) and a report replaces its own row: a process
|
|
// re-stamps on an interval, and a history of re-stamps is not something any
|
|
// reader wants.
|
|
func (s *Store) PutApplyStatus(ctx context.Context, status ApplyStatus) error {
|
|
reported := status.ReportedAt
|
|
if reported.IsZero() {
|
|
reported = time.Now()
|
|
}
|
|
_, err := s.db.ExecContext(ctx, `
|
|
INSERT INTO apply_status (process, kind, applied_version, error, reported_at)
|
|
VALUES (?, ?, ?, ?, ?)
|
|
ON CONFLICT(process, kind) DO UPDATE SET
|
|
applied_version=excluded.applied_version,
|
|
error=excluded.error,
|
|
reported_at=excluded.reported_at`,
|
|
status.Process, status.Kind, status.AppliedVersion, status.Error,
|
|
reported.UTC().Format(applyStatusTimeLayout))
|
|
if err != nil {
|
|
return fmt.Errorf("store: put apply status: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListApplyStatus returns every process's report. An empty slice is the
|
|
// normal state of a store whose processes have not booted yet, not an error.
|
|
func (s *Store) ListApplyStatus(ctx context.Context) ([]ApplyStatus, error) {
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
SELECT process, kind, applied_version, error, reported_at
|
|
FROM apply_status ORDER BY process, kind`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list apply status: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
statuses := []ApplyStatus{}
|
|
for rows.Next() {
|
|
var (
|
|
status ApplyStatus
|
|
reported string
|
|
)
|
|
if err := rows.Scan(&status.Process, &status.Kind, &status.AppliedVersion, &status.Error, &reported); err != nil {
|
|
return nil, fmt.Errorf("store: scan apply status: %w", err)
|
|
}
|
|
status.ReportedAt, err = time.Parse(applyStatusTimeLayout, reported)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: parse apply status time: %w", err)
|
|
}
|
|
statuses = append(statuses, status)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("store: list apply status: %w", err)
|
|
}
|
|
return statuses, nil
|
|
}
|