mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
chore(tests): merge upstream main with LINSTOR bootstrap fix
Keep the fork test environment aligned with the platform images so LINSTOR database migration and controller versions agree during setup. Signed-off-by: Ahmed Kamal <email.ahmedkamal@googlemail.com> Assisted-by: LLM
This commit is contained in:
commit
36082b280f
200 files changed
+8148
-4720
No files matched your search
@@ -139,13 +139,14 @@ rd-presets-check:
|
||||
migrations-target-check:
|
||||
hack/check-migrations-target.sh
|
||||
|
||||
# Scoped go test over the cozystack-api surface that this repo owns. Kept
|
||||
# narrow intentionally - running `go test ./...` pulls in generated code
|
||||
# round-trip suites whose behavior depends on tool versions outside this
|
||||
# repo's control (kubebuilder, openapi-gen, etc.) and is better exercised
|
||||
# from their generator workflows.
|
||||
# Go tests of the cozystack-api surface and of the standalone packages and
|
||||
# commands listed here, whose tests need no cluster. Kept narrow
|
||||
# intentionally - running `go test ./...` pulls in generated code round-trip
|
||||
# suites whose behavior depends on tool versions outside this repo's control
|
||||
# (kubebuilder, openapi-gen, etc.) and is better exercised from their
|
||||
# generator workflows.
|
||||
go-unit-tests:
|
||||
go test ./pkg/registry/... ./pkg/config/... ./pkg/cmd/server/...
|
||||
go test ./pkg/registry/... ./pkg/config/... ./pkg/cmd/server/... ./pkg/lineage/... ./pkg/ovnstatus/... ./pkg/reservation/... ./cmd/cozypkg/... ./cmd/check-readiness/...
|
||||
|
||||
# The nested Go modules (image sources and the published API types) are
|
||||
# outside ./... of the root module, so no other target reaches their tests.
|
||||
@@ -158,9 +159,7 @@ go-module-tests:
|
||||
done
|
||||
|
||||
# Go tests for the controllers and supporting packages under ./internal.
|
||||
# Excludes ./pkg/... and ./cmd/... — those are run separately by
|
||||
# go-unit-tests above (pkg subset) and skipped (cmd) until their tests
|
||||
# stabilise. CI schedules this target in the same four-slot make invocation as
|
||||
# CI schedules this target in the same four-slot make invocation as
|
||||
# unit-tests; locally invoke it directly or chain the two targets.
|
||||
test-controllers:
|
||||
go test ./internal/... -count=1
|
||||
@@ -195,7 +194,8 @@ test-controllers-envtest: $(SETUP_ENVTEST)
|
||||
test-check-readiness:
|
||||
go test ./test/check-readiness/ -count=1
|
||||
|
||||
# ./cmd/... is excluded from go-unit-tests, so the batch tool needs its own target.
|
||||
# The batch tool's suite drives real git repositories for minutes, so it is
|
||||
# kept out of go-unit-tests in a target of its own.
|
||||
test-backport-batch:
|
||||
go test ./cmd/backport-batch/... -count=1
|
||||
|
||||
|
||||
Generated
+11
@@ -51,6 +51,14 @@ type ConfigSpec struct {
|
||||
// Redis `min-replicas-max-lag`, in seconds: a replica whose last acknowledgement is older than this does not count towards `minReplicasToWrite`. Unset by default, which leaves the Redis default (10). Has no effect unless `minReplicasToWrite` is set. Must be at least 1 when `minReplicasToWrite` is set: Redis treats 0 as disabling the check, so the chart refuses it.
|
||||
// +kubebuilder:validation:Minimum=0
|
||||
MinReplicasMaxLag *int `json:"minReplicasMaxLag,omitempty"`
|
||||
// Redis `maxmemory`: the dataset size at which Redis applies `maxmemoryPolicy`. Unset by default, which renders `maxmemory 0` (no limit, the Redis default), so a value set by hand with `CONFIG SET` is reset on the next reconcile. Must be at most 90% of the memory request of each Redis pod (from `resources` or `resourcesPreset`): the operator silently drops a larger value, so the chart refuses it. Leave headroom below that for replication and snapshot buffers.
|
||||
Maxmemory resource.Quantity `json:"maxmemory,omitempty"`
|
||||
// Redis `maxmemory-policy`: what Redis does once `maxmemory` is reached. `noeviction` refuses writes; `allkeys-lru` evicts the least recently used keys, which suits an instance used purely as a cache. Has no effect while `maxmemory` is unset.
|
||||
// +kubebuilder:default:="noeviction"
|
||||
MaxmemoryPolicy MaxmemoryPolicy `json:"maxmemoryPolicy"`
|
||||
// Write RDB snapshots to the persistent volume (the Redis `save` directive, with the Redis default schedule). Set to `false` for an instance used purely as a cache: `save ""` is rendered and the volume is kept, so no further scheduled snapshot is written while replicas still have room for the full-sync RDB they write to disk. An RDB file already on the volume is not removed, and Redis loads it on restart, so turning snapshots off does not by itself empty the dataset. Always off when `size` is empty.
|
||||
// +kubebuilder:default:=true
|
||||
Snapshots bool `json:"snapshots"`
|
||||
// Sentinel configuration. When `sentinel.resources` is empty and `sentinel.resourcesPreset` is unset, the Sentinel pods are sized like the Redis pods (`resources` and `resourcesPreset`). An empty `sentinel.resources` map counts as unset, as an empty top-level `resources` does.
|
||||
// +kubebuilder:default:={}
|
||||
Sentinel Sentinel `json:"sentinel,omitempty"`
|
||||
@@ -80,6 +88,9 @@ type TLS struct {
|
||||
// +kubebuilder:validation:Enum="no";"optional";"yes"
|
||||
type AuthClients string
|
||||
|
||||
// +kubebuilder:validation:Enum="noeviction";"allkeys-lru";"allkeys-lfu";"allkeys-random";"volatile-lru";"volatile-lfu";"volatile-random";"volatile-ttl"
|
||||
type MaxmemoryPolicy string
|
||||
|
||||
// +kubebuilder:validation:Enum="t1.nano";"t1.micro";"t1.small";"t1.medium";"t1.large";"t1.xlarge";"t1.2xlarge";"t1.4xlarge";"c1.nano";"c1.micro";"c1.small";"c1.medium";"c1.large";"c1.xlarge";"c1.2xlarge";"c1.4xlarge";"s1.nano";"s1.micro";"s1.small";"s1.medium";"s1.large";"s1.xlarge";"s1.2xlarge";"s1.4xlarge";"u1.nano";"u1.micro";"u1.small";"u1.medium";"u1.large";"u1.xlarge";"u1.2xlarge";"u1.4xlarge";"m1.nano";"m1.micro";"m1.small";"m1.medium";"m1.large";"m1.xlarge";"m1.2xlarge";"m1.4xlarge";"nano";"micro";"small";"medium";"large";"xlarge";"2xlarge"
|
||||
type ResourcesPreset string
|
||||
|
||||
|
||||
@@ -66,6 +66,7 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
*out = new(int)
|
||||
**out = **in
|
||||
}
|
||||
out.Maxmemory = in.Maxmemory.DeepCopy()
|
||||
in.Sentinel.DeepCopyInto(&out.Sentinel)
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@ Requires `git` and an authenticated `gh` on PATH, and a remote called `origin`.
|
||||
|
||||
Build the binary wherever an exit code matters: `go run` turns any non-zero exit into its own `1`, so under it a check that could not be completed looks the same as one that found something missing.
|
||||
|
||||
Output is written before a push or a publish. Once a write to stdout or stderr fails, a closed pipe included, the run changes nothing more: no push, no GitHub write, no local branch move, and no new pick; a pick already in flight still finishes its rollback. It exits 2 where it would have exited 0, saying on stderr, while it still can, that the output was lost. A confirmation lost after a push or a publish fails the run the same way.
|
||||
|
||||
### run
|
||||
|
||||
Switches the current working tree to `backport-batch/<line>-<yyyymmdd>`, created at `origin/<line>`, and picks onto it one original at a time, in the order they merged into `main`, as one unit each on the branch's first-parent line. A pull request merged into `main` with a merge commit `M` goes on as its own commits, `git rev-list --reverse --topo-order --no-merges M^1..M^2`, each picked with `git cherry-pick -x --signoff`, so each keeps its author, message and sign-offs and gains the `(cherry picked from commit …)` line; the merges that brought `main` into its branch are left out, since `main` already has what they brought. A merge commit then joins the picks to the batch: its first parent is the batch before the original, its second the last pick, and its message is `M`'s with a paragraph of its own, `Backported-from: M`. A pull request squash-merged into `main` goes on as one pick. When any commit of an original conflicts, the pick is aborted, the branch goes back to where it was before that original, and the original is recorded with the commit that failed and its files; the run goes on with the next one. A commit the branch already carries is skipped as empty, and an original none of whose commits changes anything is reported as carried. The tree has to be clean. A local branch of that name is reset to where the run starts; when that drops commits only it has, the run first prints its tip and the `git branch` command that restores them, and so does `--defer`. Signing follows your git configuration; hooks are off for the picks and the merge, so each commit carries its message as git writes it.
|
||||
|
||||
@@ -99,7 +99,7 @@ func apiSnapshot(commit string) (apigate.Snapshot, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
defer func() { _ = os.RemoveAll(dir) }()
|
||||
for name, blob := range paths {
|
||||
file := filepath.Join(dir, filepath.FromSlash(name))
|
||||
if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
|
||||
|
||||
@@ -153,7 +153,7 @@ func (a *app) run(args []string) int {
|
||||
}
|
||||
// The scheduled job has half an hour for every line; this says where it
|
||||
// goes.
|
||||
fmt.Fprintf(a.stderr, "prepared %s in %s, %s of it judging partial units and what originals build on\n", l.branch(), time.Since(started).Round(time.Millisecond), b.detecting.Round(time.Millisecond))
|
||||
_, _ = fmt.Fprintf(a.stderr, "prepared %s in %s, %s of it judging partial units and what originals build on\n", l.branch(), time.Since(started).Round(time.Millisecond), b.detecting.Round(time.Millisecond))
|
||||
if drops != nil {
|
||||
return a.redrop(b, drops, reason, *undropArg != "", *push)
|
||||
}
|
||||
@@ -273,7 +273,7 @@ func (a *app) run(args []string) int {
|
||||
continue
|
||||
}
|
||||
}
|
||||
p, err := pickOriginal(v, func(base, head string) ([]string, error) {
|
||||
p, err := a.pickUnit(v, func(base, head string) ([]string, error) {
|
||||
omitted, diag, err := b.omittedEffects(v, base, head)
|
||||
v.addDiag(diag...)
|
||||
return omitted, err
|
||||
@@ -449,20 +449,20 @@ func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, r
|
||||
}
|
||||
body, left := r.body(template)
|
||||
p.body = body
|
||||
fmt.Fprint(a.stdout, body)
|
||||
_, _ = fmt.Fprint(a.stdout, body)
|
||||
skipped := 0
|
||||
for _, items := range r.skipped {
|
||||
skipped += len(items)
|
||||
}
|
||||
for _, rule := range left {
|
||||
for _, v := range r.skipped[rule] {
|
||||
fmt.Fprintf(a.stderr, "skipped by %s: %s %s (%s)\n", ruleName(rule), v.o.url(), terminalSafe(v.o.title), terminalSafe(v.reason))
|
||||
_, _ = fmt.Fprintf(a.stderr, "skipped by %s: %s %s (%s)\n", ruleName(rule), v.o.url(), terminalSafe(v.o.title), terminalSafe(v.reason))
|
||||
}
|
||||
}
|
||||
for _, p := range r.waiting {
|
||||
fmt.Fprintf(a.stderr, "waiting for a maintainer: %s %s changes %s\n", p.v.o.url(), terminalSafe(p.v.o.title), terminalSafe(strings.Join(p.files, ", ")))
|
||||
_, _ = fmt.Fprintf(a.stderr, "waiting for a maintainer: %s %s changes %s\n", p.v.o.url(), terminalSafe(p.v.o.title), terminalSafe(strings.Join(p.files, ", ")))
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "%s at %.12s: %d on the branch, %d deferred, %d waiting for a maintainer, %d skipped\ntitle: %s\nreport: %s\n",
|
||||
_, _ = fmt.Fprintf(a.stderr, "%s at %.12s: %d on the branch, %d deferred, %d waiting for a maintainer, %d skipped\ntitle: %s\nreport: %s\n",
|
||||
branch, head, len(r.onBranch), len(r.deferred)+len(r.held), len(r.waiting), skipped, p.title, doc.ID)
|
||||
if !push {
|
||||
return 0
|
||||
@@ -471,8 +471,8 @@ func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, r
|
||||
// Picking nothing is not a failure of the run; check still reports
|
||||
// whatever the line lacks. Merged and closed batches are never
|
||||
// written, so there is nowhere to keep what the run found.
|
||||
fmt.Fprintf(a.stderr, "%s carries nothing beyond %s; nothing to push\n", branch, b.line.branch())
|
||||
fmt.Fprintf(a.stderr, "no batch pull request is open on %s; this run's classification is not retained\n", b.line.branch())
|
||||
_, _ = fmt.Fprintf(a.stderr, "%s carries nothing beyond %s; nothing to push\n", branch, b.line.branch())
|
||||
_, _ = fmt.Fprintf(a.stderr, "no batch pull request is open on %s; this run's classification is not retained\n", b.line.branch())
|
||||
return 0
|
||||
}
|
||||
if err := a.publish(b, p, rebuilt); err != nil {
|
||||
@@ -529,6 +529,9 @@ func cleanTree() error {
|
||||
// the move drops commits of and how to get them back: a clean tree says
|
||||
// nothing about commits only a local branch has.
|
||||
func (a *app) warnReset(branch, start string) error {
|
||||
if err := a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
old, err := lookup("refs/heads/" + branch)
|
||||
if err != nil || old == "" || old == start {
|
||||
return err
|
||||
@@ -537,7 +540,11 @@ func (a *app) warnReset(branch, start string) error {
|
||||
if err != nil || kept {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "resetting the local %s from %s, dropping commits %.12s does not carry; git branch %s-%.12s %s restores them\n", branch, old, start, branch, old, old)
|
||||
// The hint is the only way back to the dropped commits, so the reset does
|
||||
// not run when it could not be written.
|
||||
if _, err := fmt.Fprintf(a.stderr, "resetting the local %s from %s, dropping commits %.12s does not carry; git branch %s-%.12s %s restores them\n", branch, old, start, branch, old, old); err != nil {
|
||||
return fmt.Errorf("not resetting the local %s: %w", branch, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -719,6 +726,9 @@ func (a *app) publish(b *batch, p publication, rebuilt bool) error {
|
||||
}
|
||||
}
|
||||
if pushed != p.head {
|
||||
if err := a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := git(pushArgs(p.branch, p.head, lease)...); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -727,13 +737,13 @@ func (a *app) publish(b *batch, p publication, rebuilt bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
defer func() { _ = os.Remove(name) }()
|
||||
if b.pr == nil {
|
||||
n, url, err := a.gh.createPull(b.line.branch(), p.branch, p.title, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "opened draft %s\n", url)
|
||||
_, _ = fmt.Fprintf(a.stderr, "opened draft %s\n", url)
|
||||
if err := a.postReport(n, p.report); err != nil {
|
||||
return fmt.Errorf("posting report %.12s on #%d: %w; the body keeps it, and the next run --push posts it", p.report.doc.ID, n, err)
|
||||
}
|
||||
@@ -745,7 +755,7 @@ func (a *app) publish(b *batch, p publication, rebuilt bool) error {
|
||||
if err := a.gh.editPull(b.pr.Number, p.title, name); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "updated %s\n", pullURL(b.pr))
|
||||
_, _ = fmt.Fprintf(a.stderr, "updated %s\n", pullURL(b.pr))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -828,7 +838,7 @@ func (a *app) closeBatch(b *batch, p publication) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
defer func() { _ = os.Remove(name) }()
|
||||
if err := a.stillOpen(c.PR); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -861,7 +871,7 @@ func (a *app) finishClose(l line, url string, c closing) error {
|
||||
if err := a.deleteBranch(l, c); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "closed %s: without what is deferred it carries nothing beyond %s; the next batch tries them again\n", url, l.branch())
|
||||
_, _ = fmt.Fprintf(a.stderr, "closed %s: without what is deferred it carries nothing beyond %s; the next batch tries them again\n", url, l.branch())
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -878,6 +888,9 @@ func (a *app) deleteBranch(l line, c closing) error {
|
||||
case h != c.Head:
|
||||
return movedBranch(c, h)
|
||||
}
|
||||
if err := a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := git(deleteArgs(c.Branch, c.Head)...); err != nil {
|
||||
return fmt.Errorf("deleting %s, left by the close of #%d: %w; backport-batch run --line %s --push deletes it", c.Branch, c.PR, err, l.branch())
|
||||
}
|
||||
@@ -933,7 +946,7 @@ func (a *app) resumeClose(b *batch, push, changing bool) (bool, int) {
|
||||
if err := a.deleteBranch(b.line, c); err != nil {
|
||||
return false, a.fail(err)
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "deleted %s, which the close of #%d left\n", c.Branch, c.PR)
|
||||
_, _ = fmt.Fprintf(a.stderr, "deleted %s, which the close of #%d left\n", c.Branch, c.PR)
|
||||
return false, a.finished(c, changing)
|
||||
}
|
||||
|
||||
|
||||
@@ -73,14 +73,14 @@ func (a *app) check(args []string) int {
|
||||
case d.kind == outcomeWarning:
|
||||
a.warn("%s", d.text)
|
||||
case d.kind == outcomeNote:
|
||||
fmt.Fprintf(a.stderr, "note: %s\n", d.text)
|
||||
_, _ = fmt.Fprintf(a.stderr, "note: %s\n", d.text)
|
||||
}
|
||||
}
|
||||
if len(missing)+len(failing) == 0 {
|
||||
if *cut {
|
||||
fmt.Fprintf(a.stdout, "%s: no change the policy takes is missing before the cut\n", l.branch())
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: no change the policy takes is missing before the cut\n", l.branch())
|
||||
} else {
|
||||
fmt.Fprintf(a.stdout, "%s: every change the policy takes is on the line or on the batch\n", l.branch())
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: every change the policy takes is on the line or on the batch\n", l.branch())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -89,13 +89,13 @@ func (a *app) check(args []string) int {
|
||||
where = "nor on batch pull request #" + strconv.Itoa(b.pr.Number)
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d changes the policy takes are not on the line, %s:\n", l.branch(), len(missing), where)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d changes the policy takes are not on the line, %s:\n", l.branch(), len(missing), where)
|
||||
for _, text := range missing {
|
||||
fmt.Fprintf(a.stdout, " %s\n", text)
|
||||
_, _ = fmt.Fprintf(a.stdout, " %s\n", text)
|
||||
}
|
||||
}
|
||||
for _, text := range failing {
|
||||
fmt.Fprintf(a.stdout, "%s: %s\n", l.branch(), text)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %s\n", l.branch(), text)
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@@ -326,10 +326,10 @@ func (a *app) fold(args []string) int {
|
||||
return a.fail(err)
|
||||
}
|
||||
if why != "" {
|
||||
fmt.Fprintf(a.stdout, "refused: %s\n", why)
|
||||
_, _ = fmt.Fprintf(a.stdout, "refused: %s\n", why)
|
||||
return 1
|
||||
}
|
||||
fmt.Fprintln(a.stdout, "ok")
|
||||
_, _ = fmt.Fprintln(a.stdout, "ok")
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ func (a *app) reassemble(b *batch, out, hold map[int]string, drops []droppedEntr
|
||||
return a.fail(err)
|
||||
}
|
||||
for _, u := range replay {
|
||||
failed, files, err := replayUnit(u)
|
||||
failed, files, err := a.replayNext(u)
|
||||
switch {
|
||||
case err != nil:
|
||||
return a.fail(err)
|
||||
|
||||
@@ -367,7 +367,7 @@ func (b *batch) alreadyApplied(pre, cur, post string) (applied, unsupported bool
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
defer func() { _ = os.RemoveAll(dir) }()
|
||||
names := map[string]string{}
|
||||
for _, f := range []struct{ name, oid string }{{"cur", cur}, {"pre", pre}, {"post", post}} {
|
||||
names[f.name] = filepath.Join(dir, f.name)
|
||||
|
||||
@@ -53,7 +53,7 @@ func emptyBatch(f *fixture, gh *fakeGH, body string) *foldWorld {
|
||||
// and every check of the batch's shape takes the old name as the same file:
|
||||
// verify, a hand fold, and a rebuild by --defer.
|
||||
func TestRenamePickThroughFoldAndVerify(t *testing.T) {
|
||||
f, mc5 := renameWorld(t)
|
||||
f, _ := renameWorld(t)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
head := openBatch(t, f, gh)
|
||||
@@ -74,7 +74,7 @@ func TestRenamePickThroughFoldAndVerify(t *testing.T) {
|
||||
t.Errorf("verify after the rebuild = %d\n%s\n%s", code, out, stderr)
|
||||
}
|
||||
|
||||
f, mc5 = renameWorld(t)
|
||||
f, mc5 := renameWorld(t)
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/old.txt": strings.Replace(appFile, "three", "THREE", 1)})
|
||||
fold := w.merge(w.head, pick, "fix(app): fix the app file (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
|
||||
@@ -522,15 +522,6 @@ func (f *fixture) batchPR(n int, base, branch, body string) pullRequest {
|
||||
return pr
|
||||
}
|
||||
|
||||
// botBatchPR is batchPR opened by the bot the scheduled batch runs as.
|
||||
func (f *fixture) botBatchPR(gh *fakeGH, n int, base, branch, body string) pullRequest {
|
||||
f.t.Helper()
|
||||
gh.bot = defaultBatchBot
|
||||
pr := f.batchPR(n, base, branch, body)
|
||||
pr.User.Login, pr.User.Type, pr.AuthorAssociation = defaultBatchBot, "Bot", "CONTRIBUTOR"
|
||||
return pr
|
||||
}
|
||||
|
||||
// mergeBatch merges the open batch into release-1.1 with a merge commit, as
|
||||
// GitHub does, and deletes its branch, as the repository's
|
||||
// delete_branch_on_merge does. The pull request stays, merged, with its body
|
||||
|
||||
@@ -137,6 +137,54 @@ type github interface {
|
||||
batchBot() string
|
||||
}
|
||||
|
||||
// guardedGitHub refuses every GitHub write once the run's output was lost.
|
||||
type guardedGitHub struct {
|
||||
github
|
||||
a *app
|
||||
}
|
||||
|
||||
func (g guardedGitHub) createPull(base, head, title, bodyFile string) (int, string, error) {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
return g.github.createPull(base, head, title, bodyFile)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) editPull(number int, title, bodyFile string) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.editPull(number, title, bodyFile)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) editBody(number int, bodyFile string) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.editBody(number, bodyFile)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) closePull(number int) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.closePull(number)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) createComment(number int, bodyFile string) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.createComment(number, bodyFile)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) editComment(id int64, bodyFile string) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.editComment(id, bodyFile)
|
||||
}
|
||||
|
||||
type ghCLI struct{ bot string }
|
||||
|
||||
func (g ghCLI) batchBot() string { return g.bot }
|
||||
|
||||
@@ -142,11 +142,11 @@ func TestOptionInjection(t *testing.T) {
|
||||
// no bidi override reorders it and no zero-width character hides in it.
|
||||
func TestDeferredBlockCleansFileNames(t *testing.T) {
|
||||
l, _ := parseLine("release-1.6")
|
||||
files := []string{"app/txt.exe", "app/zerowidth.yaml"}
|
||||
files := []string{"app/\u202etxt.exe", "app/zero\u200bwidth.yaml"}
|
||||
conflict := pick{v: &verdict{o: &original{number: 5}}, files: files, failed: strings.Repeat("a", 40)}
|
||||
waiting := pick{v: &verdict{o: &original{number: 6}}, files: files}
|
||||
block := deferredBlock(l, []pick{conflict}, nil, []pick{waiting})
|
||||
if strings.ContainsAny(block, "") {
|
||||
if strings.ContainsAny(block, "\u202e\u200b") {
|
||||
t.Errorf("the block carries format characters: %q", block)
|
||||
}
|
||||
want := []string{"app/txt.exe", "app/zerowidth.yaml"}
|
||||
@@ -154,7 +154,7 @@ func TestDeferredBlockCleansFileNames(t *testing.T) {
|
||||
if err != nil || !slices.Equal(got[5].Files, want) || !slices.Equal(got[6].Files, want) {
|
||||
t.Errorf("parseDeferred = %+v, %v; want both originals with %q", got, err, want)
|
||||
}
|
||||
if files[0] != "app/txt.exe" {
|
||||
if files[0] != "app/\u202etxt.exe" {
|
||||
t.Errorf("deferredBlock rewrote the pick's own file names: %q", files)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -111,18 +113,42 @@ type app struct {
|
||||
policyRef string
|
||||
// tool is the commit backport-batch was built from, for the record.
|
||||
tool string
|
||||
// unwritten is the first error writing to stdout or stderr. Output goes
|
||||
// out before a push or a publish, so a run that lost it acts on nothing,
|
||||
// and one that lost a confirmation afterwards fails.
|
||||
unwritten error
|
||||
}
|
||||
|
||||
func main() {
|
||||
catchSIGPIPE()
|
||||
a := &app{gh: ghCLI{bot: batchBotLogin(os.LookupEnv)}, stdout: os.Stdout, stderr: os.Stderr, now: time.Now, tool: toolVersion()}
|
||||
os.Exit(a.main(os.Args[1:]))
|
||||
}
|
||||
|
||||
// catchSIGPIPE makes a write to a closed stdout or stderr fail with EPIPE like
|
||||
// any other write, so the run stops before it pushes or publishes and says
|
||||
// why, instead of dying silently. signal.Ignore would do the same here but
|
||||
// would also leave SIGPIPE ignored in the git and gh children, across exec.
|
||||
func catchSIGPIPE() {
|
||||
signal.Notify(make(chan os.Signal, 1), syscall.SIGPIPE)
|
||||
}
|
||||
|
||||
func (a *app) main(args []string) int {
|
||||
a.stdout, a.stderr = textWriter{a.stdout}, textWriter{a.stderr}
|
||||
a.gh = &permCache{github: a.gh, perms: map[string]string{}}
|
||||
a.stdout, a.stderr = textWriter{w: a.stdout, failed: &a.unwritten}, textWriter{w: a.stderr, failed: &a.unwritten}
|
||||
code := a.command(args)
|
||||
if a.unwritten != nil {
|
||||
_, _ = fmt.Fprintf(a.stderr, "backport-batch: output was lost: %v\n", a.unwritten)
|
||||
if code == 0 {
|
||||
code = 2
|
||||
}
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
func (a *app) command(args []string) int {
|
||||
a.gh = guardedGitHub{github: &permCache{github: a.gh, perms: map[string]string{}}, a: a}
|
||||
if len(args) == 0 {
|
||||
fmt.Fprint(a.stderr, usageText)
|
||||
_, _ = fmt.Fprint(a.stderr, usageText)
|
||||
return 2
|
||||
}
|
||||
if bot := a.gh.batchBot(); bot != "" && !botLoginRE.MatchString(bot) {
|
||||
@@ -144,20 +170,26 @@ func (a *app) main(args []string) int {
|
||||
case "lines":
|
||||
return a.lines(args[1:])
|
||||
case "-h", "--help", "help":
|
||||
fmt.Fprint(a.stdout, usageText)
|
||||
_, _ = fmt.Fprint(a.stdout, usageText)
|
||||
return 0
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "unknown command %q\n\n%s", args[0], usageText)
|
||||
_, _ = fmt.Fprintf(a.stderr, "unknown command %q\n\n%s", args[0], usageText)
|
||||
return 2
|
||||
}
|
||||
|
||||
// textWriter keeps output fit for a terminal. Titles, file names and the
|
||||
// messages git relays come from other people, and a control sequence among
|
||||
// them would move the cursor or rewrite what was printed before.
|
||||
type textWriter struct{ w io.Writer }
|
||||
type textWriter struct {
|
||||
w io.Writer
|
||||
failed *error
|
||||
}
|
||||
|
||||
func (t textWriter) Write(p []byte) (int, error) {
|
||||
if _, err := io.WriteString(t.w, clean(string(p), true)); err != nil {
|
||||
if t.failed != nil && *t.failed == nil {
|
||||
*t.failed = err
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return len(p), nil
|
||||
@@ -166,7 +198,7 @@ func (t textWriter) Write(p []byte) (int, error) {
|
||||
func (a *app) flags(name string) *flag.FlagSet {
|
||||
fs := flag.NewFlagSet(name, flag.ContinueOnError)
|
||||
fs.SetOutput(a.stderr)
|
||||
fs.Usage = func() { fmt.Fprint(a.stderr, usageText) }
|
||||
fs.Usage = func() { _, _ = fmt.Fprint(a.stderr, usageText) }
|
||||
return fs
|
||||
}
|
||||
|
||||
@@ -188,13 +220,24 @@ func parse(fs *flag.FlagSet, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// mayAct refuses any further change once output was lost: the run would act
|
||||
// on what it printed while nobody read it. Every push and every move of a
|
||||
// local batch branch go through it, and every GitHub write through
|
||||
// guardedGitHub.
|
||||
func (a *app) mayAct() error {
|
||||
if a.unwritten != nil {
|
||||
return fmt.Errorf("output was lost: %w; nothing more is pushed or written to GitHub", a.unwritten)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *app) fail(err error) int {
|
||||
fmt.Fprintf(a.stderr, "backport-batch: %v\n", err)
|
||||
_, _ = fmt.Fprintf(a.stderr, "backport-batch: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
|
||||
func (a *app) warn(format string, args ...any) {
|
||||
fmt.Fprintf(a.stderr, "warning: "+format+"\n", args...)
|
||||
_, _ = fmt.Fprintf(a.stderr, "warning: "+format+"\n", args...)
|
||||
}
|
||||
|
||||
// verdict is what the policy says about one original for the line.
|
||||
|
||||
@@ -102,10 +102,10 @@ func (a *app) notify(args []string) int {
|
||||
var conflicts, waiting []int
|
||||
after := map[int][]int{}
|
||||
for n, e := range deferred {
|
||||
switch place := placement(deferred, n, map[int]bool{}); {
|
||||
case place == "workflow":
|
||||
switch placement(deferred, n, map[int]bool{}) {
|
||||
case "workflow":
|
||||
waiting = append(waiting, n)
|
||||
case place == "conflict":
|
||||
case "conflict":
|
||||
conflicts = append(conflicts, n)
|
||||
}
|
||||
if len(e.Needs) > 0 {
|
||||
@@ -135,11 +135,11 @@ func (a *app) notify(args []string) int {
|
||||
stalled := days >= stalledAfter
|
||||
var text []string
|
||||
if stalled {
|
||||
fmt.Fprintf(a.stdout, "%s: batch pull request #%d has been open since %s, %d days\n", l.branch(), pr.Number, opened.Format(time.DateOnly), days)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: batch pull request #%d has been open since %s, %d days\n", l.branch(), pr.Number, opened.Format(time.DateOnly), days)
|
||||
text = append(text, fmt.Sprintf("This batch has been open since %s, %d days, through at least one scheduled run. Merge it, or close it and the next run starts a new one; until then each run adds what the rules take to it.", opened.Format(time.DateOnly), days))
|
||||
}
|
||||
if len(partial) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d units on #%d carry only part of their originals\n", l.branch(), len(partial), pr.Number)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d units on #%d carry only part of their originals\n", l.branch(), len(partial), pr.Number)
|
||||
var b strings.Builder
|
||||
b.WriteString("These are on the batch but carry only part of their originals on this line; take each out with `backport-batch run --defer` before the cut, and port what applies as a pull request of its own:\n")
|
||||
for _, n := range partial {
|
||||
@@ -148,7 +148,7 @@ func (a *app) notify(args []string) int {
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if len(conflicts) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d deferred conflicts on #%d wait for a maintainer\n", l.branch(), count(conflicts), pr.Number)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d deferred conflicts on #%d wait for a maintainer\n", l.branch(), count(conflicts), pr.Number)
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These conflicts wait for a maintainer, as [resolving a conflict](%sresolve-conflict.md) describes; unresolved, each goes to the next batch, and so does what waits on one:\n", tasksURL)
|
||||
for _, n := range conflicts {
|
||||
@@ -158,9 +158,9 @@ func (a *app) notify(args []string) int {
|
||||
}
|
||||
if len(waiting) > 0 {
|
||||
if count(waiting) == len(waiting) {
|
||||
fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s and wait for a maintainer\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s and wait for a maintainer\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
} else {
|
||||
fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s or wait on one that does\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s or wait on one that does\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These change `%s`, which the batch's token may not push, and wait for a maintainer to bring them over, as [changes to workflows](%s#changes-to-workflows) describes, or wait on one that does:\n", workflowsDir, policyURL)
|
||||
@@ -170,7 +170,7 @@ func (a *app) notify(args []string) int {
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if len(parks) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d parked originals on #%d, named once\n", l.branch(), len(parks), pr.Number)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d parked originals on #%d, named once\n", l.branch(), len(parks), pr.Number)
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These cannot land on this line whole, so the batch parked them; each is named here once. A maintainer ports what applies as a pull request of its own, as [fixes that exist only on a release line](%s#fixes-that-exist-only-on-a-release-line) describes, or drops the original with `backport-batch run --drop`:\n", policyURL)
|
||||
for _, p := range parks {
|
||||
@@ -229,7 +229,7 @@ func (a *app) announced(pr *pullRequest, rec *lineRecord, parks []parkedEntry) e
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
defer func() { _ = os.Remove(name) }()
|
||||
return a.gh.editBody(pr.Number, name)
|
||||
}
|
||||
|
||||
@@ -259,10 +259,10 @@ func placement(deferred map[int]deferredEntry, n int, seen map[int]bool) string
|
||||
seen[n] = true
|
||||
place := ""
|
||||
for _, f := range e.Needs {
|
||||
switch p := placement(deferred, f, seen); {
|
||||
case p == "workflow":
|
||||
switch p := placement(deferred, f, seen); p {
|
||||
case "workflow":
|
||||
return p
|
||||
case p == "conflict":
|
||||
case "conflict":
|
||||
place = p
|
||||
}
|
||||
}
|
||||
@@ -309,9 +309,9 @@ func (a *app) comment(pr *pullRequest, mention string, today time.Time, text []s
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
defer func() { _ = os.Remove(f.Name()) }()
|
||||
if _, err := f.WriteString(b.String()); err != nil {
|
||||
f.Close()
|
||||
_ = f.Close()
|
||||
return a.fail(err)
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
@@ -354,7 +354,7 @@ func (a *app) lines(args []string) int {
|
||||
return a.fail(err)
|
||||
}
|
||||
for _, l := range slices.Backward(lines) {
|
||||
fmt.Fprintln(a.stdout, l.branch())
|
||||
_, _ = fmt.Fprintln(a.stdout, l.branch())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -41,12 +41,12 @@ func TestOntoContinues(t *testing.T) {
|
||||
f.lines()
|
||||
head := pushed(t, f)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
code, stdout, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head, "--date", "2026-10-29")
|
||||
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head, "--date", "2026-10-29")
|
||||
if code != 2 || !strings.Contains(stderr, "not the head of backport-batch/release-1.1-20261029") {
|
||||
t.Errorf("--onto with another date = %d (%s), want a refusal", code, stderr)
|
||||
}
|
||||
f.now = f.now.AddDate(0, 0, 3)
|
||||
code, stdout, stderr = f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head)
|
||||
code, stdout, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head)
|
||||
if code != 0 {
|
||||
t.Fatalf("run --onto = %d\n%s", code, stderr)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,385 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type failingWriter struct{}
|
||||
|
||||
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("broken pipe") }
|
||||
|
||||
// Output that never reached its reader fails the command, and stderr says why.
|
||||
func TestUnwritableOutputFailsTheCommand(t *testing.T) {
|
||||
var stderr strings.Builder
|
||||
a := &app{gh: &fakeGH{}, stdout: failingWriter{}, stderr: &stderr}
|
||||
if code := a.main([]string{"help"}); code != 2 {
|
||||
t.Fatalf("help with an unwritable stdout = %d, want 2", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "broken pipe") {
|
||||
t.Errorf("stderr does not name the write failure:\n%s", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A command that already failed keeps its own exit code when its output is
|
||||
// lost as well: check still says something is missing.
|
||||
func TestUnwritableOutputKeepsTheCommandsFailure(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
var stderr strings.Builder
|
||||
a := &app{gh: &fakeGH{remoteHead: f.remoteHead}, stdout: failingWriter{}, stderr: &stderr, now: func() time.Time { return f.now }, tool: f.tool}
|
||||
if code := a.main([]string{"check", "--line", "release-1.1"}); code != 1 {
|
||||
t.Fatalf("check with #6 missing and an unwritable stdout = %d, want 1\n%s", code, stderr.String())
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "broken pipe") {
|
||||
t.Errorf("stderr does not name the write failure:\n%s", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A closed pipe on stdout does not kill the run before it can say what
|
||||
// already happened: the write fails like any other, and the run exits 2.
|
||||
func TestClosedStdoutPipeIsReported(t *testing.T) {
|
||||
if os.Getenv("BACKPORT_BATCH_CLOSED_STDOUT_HELPER") == "1" {
|
||||
os.Args = []string{"backport-batch", "help"}
|
||||
main()
|
||||
return
|
||||
}
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = r.Close()
|
||||
t.Cleanup(func() { _ = w.Close() })
|
||||
cmd := exec.Command(os.Args[0], "-test.run=^TestClosedStdoutPipeIsReported$")
|
||||
cmd.Env = append(os.Environ(), "BACKPORT_BATCH_CLOSED_STDOUT_HELPER=1")
|
||||
var stderr strings.Builder
|
||||
cmd.Stdout, cmd.Stderr = w, &stderr
|
||||
err = cmd.Run()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 2 {
|
||||
t.Fatalf("help into a closed pipe = %v, want exit 2\nstderr:\n%s", err, stderr.String())
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "output was lost") {
|
||||
t.Errorf("stderr does not say the output was lost:\n%s", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// failOn fails every write that contains want, and takes every other one.
|
||||
type failOn struct {
|
||||
want string
|
||||
buf strings.Builder
|
||||
}
|
||||
|
||||
func (w *failOn) Write(p []byte) (int, error) {
|
||||
if strings.Contains(string(p), w.want) {
|
||||
return 0, errors.New("broken pipe")
|
||||
}
|
||||
return w.buf.Write(p)
|
||||
}
|
||||
|
||||
func runWith(f *fixture, gh *fakeGH, stdout, stderr io.Writer, args ...string) int {
|
||||
f.t.Helper()
|
||||
if gh.remoteHead == nil {
|
||||
gh.remoteHead = f.remoteHead
|
||||
}
|
||||
a := &app{gh: gh, stdout: stdout, stderr: stderr, now: func() time.Time { return f.now }, tool: f.tool}
|
||||
return a.main(args)
|
||||
}
|
||||
|
||||
// Output is written before the push; when it is lost, on either stream,
|
||||
// nothing is pushed and no pull request is opened.
|
||||
func TestLostOutputPushesNothing(t *testing.T) {
|
||||
for _, stream := range []string{"stdout", "stderr"} {
|
||||
t.Run(stream, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
var stdout, stderr io.Writer = &strings.Builder{}, &strings.Builder{}
|
||||
if stream == "stdout" {
|
||||
stdout = failingWriter{}
|
||||
} else {
|
||||
stderr = failingWriter{}
|
||||
}
|
||||
if code := runWith(f, gh, stdout, stderr, "run", "--line", "release-1.1", "--push"); code != 2 {
|
||||
t.Fatalf("run --push with a failing %s = %d, want 2", stream, code)
|
||||
}
|
||||
if head := f.remoteHead(batchBranch1); head != "" {
|
||||
t.Errorf("the batch was pushed at %.12s", head)
|
||||
}
|
||||
if len(gh.prs) != 0 {
|
||||
t.Errorf("a pull request was opened: %+v", gh.prs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A confirmation lost after the push fails the run: the pull request was
|
||||
// opened, and the run does not exit 0 without having said so.
|
||||
func TestLostConfirmationFailsTheRun(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "opened draft"}, "run", "--line", "release-1.1", "--push"); code != 2 {
|
||||
t.Fatalf("run --push losing its confirmation = %d, want 2", code)
|
||||
}
|
||||
if len(gh.prs) != 1 {
|
||||
t.Errorf("%d pull requests, want the one the run opened", len(gh.prs))
|
||||
}
|
||||
if len(gh.issueComments) != 0 {
|
||||
t.Errorf("the report was posted after the confirmation was lost: %+v", gh.issueComments)
|
||||
}
|
||||
}
|
||||
|
||||
// notify writes what it found before it comments; when that is lost, it
|
||||
// does not comment.
|
||||
func TestLostOutputCommentsNothing(t *testing.T) {
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
|
||||
var stderr strings.Builder
|
||||
a := &app{gh: gh, stdout: failingWriter{}, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
|
||||
if code := a.main([]string{"notify", "--line", "release-1.1", "--push"}); code != 2 {
|
||||
t.Fatalf("notify --push with a failing stdout = %d, want 2\n%s", code, stderr.String())
|
||||
}
|
||||
if len(gh.issueComments) != 0 {
|
||||
t.Errorf("notify commented: %+v", gh.issueComments)
|
||||
}
|
||||
}
|
||||
|
||||
// writesGitHub records the GitHub writes that reach it.
|
||||
type writesGitHub struct {
|
||||
github
|
||||
writes []string
|
||||
}
|
||||
|
||||
func (g *writesGitHub) createPull(string, string, string, string) (int, string, error) {
|
||||
g.writes = append(g.writes, "createPull")
|
||||
return 1, "", nil
|
||||
}
|
||||
func (g *writesGitHub) editPull(int, string, string) error {
|
||||
g.writes = append(g.writes, "editPull")
|
||||
return nil
|
||||
}
|
||||
func (g *writesGitHub) editBody(int, string) error {
|
||||
g.writes = append(g.writes, "editBody")
|
||||
return nil
|
||||
}
|
||||
func (g *writesGitHub) closePull(int) error {
|
||||
g.writes = append(g.writes, "closePull")
|
||||
return nil
|
||||
}
|
||||
func (g *writesGitHub) createComment(int, string) error {
|
||||
g.writes = append(g.writes, "createComment")
|
||||
return nil
|
||||
}
|
||||
func (g *writesGitHub) editComment(int64, string) error {
|
||||
g.writes = append(g.writes, "editComment")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Every GitHub write is refused once output was lost. Every method of
|
||||
// github is either a read or in this table, so a write added later without
|
||||
// a guard fails here.
|
||||
func TestEveryGitHubWriteIsGuarded(t *testing.T) {
|
||||
reads := map[string]bool{"openPulls": true, "recentPulls": true, "permission": true, "labelled": true, "pull": true, "comments": true, "batchBot": true}
|
||||
inner := &writesGitHub{}
|
||||
g := guardedGitHub{github: inner, a: &app{unwritten: errors.New("broken pipe")}}
|
||||
writes := map[string]func() error{
|
||||
"createPull": func() error { _, _, err := g.createPull("b", "h", "t", "f"); return err },
|
||||
"editPull": func() error { return g.editPull(1, "t", "f") },
|
||||
"editBody": func() error { return g.editBody(1, "f") },
|
||||
"closePull": func() error { return g.closePull(1) },
|
||||
"createComment": func() error { return g.createComment(1, "f") },
|
||||
"editComment": func() error { return g.editComment(1, "f") },
|
||||
}
|
||||
iface := reflect.TypeFor[github]()
|
||||
for i := range iface.NumMethod() {
|
||||
name := iface.Method(i).Name
|
||||
if _, ok := writes[name]; !ok && !reads[name] {
|
||||
t.Errorf("github.%s is neither a known read nor a guarded write", name)
|
||||
}
|
||||
}
|
||||
for name, write := range writes {
|
||||
if err := write(); err == nil || !strings.Contains(err.Error(), "output was lost") {
|
||||
t.Errorf("%s with output lost = %v, want a refusal", name, err)
|
||||
}
|
||||
}
|
||||
if len(inner.writes) != 0 {
|
||||
t.Errorf("writes reached GitHub after output was lost: %q", inner.writes)
|
||||
}
|
||||
}
|
||||
|
||||
// A warning lost while the open batch is being updated stops the update:
|
||||
// nothing is pushed, posted or edited.
|
||||
func TestLostWarningLeavesTheOpenBatchAlone(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
bad := issueComment{ID: 8, Body: reportMarker + "names none\n"}
|
||||
bad.User.Login, bad.User.Type = "maintainer", "User"
|
||||
gh.issueComments = append(gh.issueComments, bad)
|
||||
head, comments, body := f.remoteHead(batchBranch1), len(gh.issueComments), gh.find(90).Body
|
||||
f.now = f.now.AddDate(0, 0, 1)
|
||||
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "does not count as a report"}, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
|
||||
t.Fatalf("run --continue --push losing a warning = %d, want 2", code)
|
||||
}
|
||||
if got := f.remoteHead(batchBranch1); got != head {
|
||||
t.Errorf("the batch moved to %.12s", got)
|
||||
}
|
||||
if len(gh.issueComments) != comments {
|
||||
t.Errorf("a comment was posted: %d comments, want %d", len(gh.issueComments), comments)
|
||||
}
|
||||
if gh.find(90).Body != body {
|
||||
t.Error("the batch pull request was edited")
|
||||
}
|
||||
}
|
||||
|
||||
// A warning lost while a batch is being closed leaves it open, with no
|
||||
// closing record half written.
|
||||
func TestLostWarningLeavesTheBatchUnclosed(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
|
||||
bad := issueComment{ID: 8, Body: reportMarker + "names none\n"}
|
||||
bad.User.Login, bad.User.Type = "maintainer", "User"
|
||||
gh.issueComments = append(gh.issueComments, bad)
|
||||
body := gh.find(90).Body
|
||||
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "does not count as a report"}, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x", "--push"); code != 2 {
|
||||
t.Fatalf("run --defer losing a warning = %d, want 2", code)
|
||||
}
|
||||
if gh.find(90).Body != body {
|
||||
t.Error("the batch pull request was edited")
|
||||
}
|
||||
if !gh.isOpen(90) {
|
||||
t.Error("the batch pull request was closed")
|
||||
}
|
||||
}
|
||||
|
||||
// The git and gh children keep the default SIGPIPE: catching it here must not
|
||||
// leave it ignored in them, as signal.Ignore would through exec.
|
||||
func TestChildrenKeepTheDefaultSIGPIPE(t *testing.T) {
|
||||
catchSIGPIPE()
|
||||
out, err := exec.Command("sh", "-c", "kill -PIPE $$; echo survived").Output()
|
||||
if err == nil || strings.Contains(string(out), "survived") {
|
||||
t.Fatalf("a child survived SIGPIPE: %q, %v", out, err)
|
||||
}
|
||||
}
|
||||
|
||||
// The branch a close left is not deleted once output was lost: a deletion
|
||||
// is a push like any other.
|
||||
func TestLostOutputDeletesNoBranch(t *testing.T) {
|
||||
f, gh, h1 := closeWorld(t)
|
||||
f.git("switch", "-q", "main")
|
||||
l, err := parseLine("release-1.1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a := &app{gh: gh, unwritten: errors.New("broken pipe")}
|
||||
if err := a.deleteBranch(l, closing{PR: 90, Branch: batchBranch1, Head: h1}); err == nil || !strings.Contains(err.Error(), "output was lost") {
|
||||
t.Fatalf("deleteBranch with output lost = %v, want a refusal", err)
|
||||
}
|
||||
if got := f.remoteHead(batchBranch1); got != h1 {
|
||||
t.Errorf("the branch is at %q, want it left at %.12s", got, h1)
|
||||
}
|
||||
}
|
||||
|
||||
// A park line lost on stdout stops notify before it writes: no comment, and
|
||||
// the record is not marked as announced.
|
||||
func TestLostParkLineWritesNothing(t *testing.T) {
|
||||
park := parkedEntry{Original: 5, Merge: strings.Repeat("c", 40), Kind: parkDependency, Needs: []int{3}}
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
|
||||
body := gh.prs[0].Body
|
||||
var stderr strings.Builder
|
||||
a := &app{gh: gh, stdout: &failOn{want: "parked originals"}, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
|
||||
if code := a.main([]string{"notify", "--line", "release-1.1", "--push"}); code != 2 {
|
||||
t.Fatalf("notify --push losing the park line = %d, want 2\n%s", code, stderr.String())
|
||||
}
|
||||
if len(gh.issueComments) != 0 {
|
||||
t.Errorf("notify commented: %+v", gh.issueComments)
|
||||
}
|
||||
if gh.prs[0].Body != body {
|
||||
t.Error("notify marked the park as announced")
|
||||
}
|
||||
}
|
||||
|
||||
// The hint naming the commits a reset drops is the only way back to them, so
|
||||
// the local branch is not reset when the hint is lost.
|
||||
func TestLostResetHintKeepsTheBranch(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.git("switch", "-q", "-C", batchBranch1, "origin/release-1.1")
|
||||
f.write("app/mine.txt", "unpushed\n")
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "an unpushed resolution")
|
||||
tip := f.git("rev-parse", "HEAD")
|
||||
f.git("switch", "-q", "main")
|
||||
if code := runWith(f, &fakeGH{}, &strings.Builder{}, &failOn{want: "resetting the local"}, "run", "--line", "release-1.1"); code != 2 {
|
||||
t.Fatalf("run losing the reset hint = %d, want 2", code)
|
||||
}
|
||||
if got := f.git("rev-parse", batchBranch1); got != tip {
|
||||
t.Errorf("the local %s moved to %.12s, want it kept at %.12s", batchBranch1, got, tip)
|
||||
}
|
||||
}
|
||||
|
||||
// Once output is lost the run moves no local branch, even one a reset would
|
||||
// only fast-forward.
|
||||
func TestLostOutputMovesNoLocalBranch(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.git("branch", "-f", batchBranch1, "origin/release-1.1^")
|
||||
before := f.git("rev-parse", batchBranch1)
|
||||
if code := runWith(f, &fakeGH{}, &strings.Builder{}, &failOn{want: "prepared"}, "run", "--line", "release-1.1"); code != 2 {
|
||||
t.Fatalf("run losing an early line = %d, want 2", code)
|
||||
}
|
||||
if got := f.git("rev-parse", batchBranch1); got != before {
|
||||
t.Errorf("the local %s moved to %.12s, want it kept at %.12s", batchBranch1, got, before)
|
||||
}
|
||||
}
|
||||
|
||||
// No new unit starts once output is lost: neither a pick nor a replay touches
|
||||
// the branch.
|
||||
func TestLostOutputStartsNoUnit(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
before := f.git("rev-parse", "HEAD")
|
||||
a := &app{unwritten: errors.New("broken pipe")}
|
||||
if _, err := a.pickUnit(&verdict{}, nil); err == nil || !strings.Contains(err.Error(), "output was lost") {
|
||||
t.Errorf("pickUnit with output lost = %v, want a refusal", err)
|
||||
}
|
||||
if _, _, err := a.replayNext(unit{}); err == nil || !strings.Contains(err.Error(), "output was lost") {
|
||||
t.Errorf("replayNext with output lost = %v, want a refusal", err)
|
||||
}
|
||||
if got := f.git("rev-parse", "HEAD"); got != before {
|
||||
t.Errorf("HEAD moved to %.12s", got)
|
||||
}
|
||||
}
|
||||
@@ -176,7 +176,7 @@ func TestPolicyPageMatchesCode(t *testing.T) {
|
||||
t.Errorf("the page's block gives %d released lines, want 2 written out", pol.released)
|
||||
}
|
||||
|
||||
rule2 := regexp.MustCompile("(?m)^2\\. Its title type is (.*)$").FindStringSubmatch(page)
|
||||
rule2 := regexp.MustCompile(`(?m)^2\. Its title type is (.*)$`).FindStringSubmatch(page)
|
||||
if rule2 == nil {
|
||||
t.Fatal("the page has no rule 2")
|
||||
}
|
||||
@@ -187,7 +187,7 @@ func TestPolicyPageMatchesCode(t *testing.T) {
|
||||
if !slices.Equal(types, takenTypes) {
|
||||
t.Errorf("rule 2 on the page takes %v, the code %v", types, takenTypes)
|
||||
}
|
||||
never := regexp.MustCompile("(?m)^### What waits for the next minor\\n\\n(.*)$").FindStringSubmatch(page)
|
||||
never := regexp.MustCompile(`(?m)^### What waits for the next minor\n\n(.*)$`).FindStringSubmatch(page)
|
||||
if never == nil {
|
||||
t.Fatal("the page has no list of what never goes into a patch release")
|
||||
}
|
||||
|
||||
@@ -708,7 +708,7 @@ func (a *app) report(args []string) int {
|
||||
return a.fail(fmt.Errorf("batch pull request #%d keeps no report %.12s", pr.Number, *id))
|
||||
}
|
||||
if *asJSON {
|
||||
fmt.Fprintln(a.stdout, s.raw)
|
||||
_, _ = fmt.Fprintln(a.stdout, s.raw)
|
||||
return 0
|
||||
}
|
||||
a.printReport(&pr, s, inBody, inComment)
|
||||
@@ -721,29 +721,29 @@ func (a *app) report(args []string) int {
|
||||
func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport, inComment bool) {
|
||||
d, b := s.doc, s.doc.Built
|
||||
safe := terminalSafe
|
||||
fmt.Fprintf(a.stdout, "report %s for %s, made %s by a %s run", safe(d.ID), safe(d.Line), safe(b.At), safe(b.Options.Mode))
|
||||
_, _ = fmt.Fprintf(a.stdout, "report %s for %s, made %s by a %s run", safe(d.ID), safe(d.Line), safe(b.At), safe(b.Options.Mode))
|
||||
if b.Options.NoWorkflowFiles {
|
||||
fmt.Fprint(a.stdout, " with --no-workflow-files")
|
||||
_, _ = fmt.Fprint(a.stdout, " with --no-workflow-files")
|
||||
}
|
||||
fmt.Fprintln(a.stdout)
|
||||
fmt.Fprintf(a.stdout, " main %s, line %s, newer line %s, policy %s\n batch %s, tool %s, run %s\n", safe(b.Main), safe(b.Line), safe(or(b.Newer, "none")), safe(b.Policy), safe(b.Batch), safe(or(b.Tool, "unknown")), safe(or(b.Run, "none")))
|
||||
_, _ = fmt.Fprintln(a.stdout)
|
||||
_, _ = fmt.Fprintf(a.stdout, " main %s, line %s, newer line %s, policy %s\n batch %s, tool %s, run %s\n", safe(b.Main), safe(b.Line), safe(or(b.Newer, "none")), safe(b.Policy), safe(b.Batch), safe(or(b.Tool, "unknown")), safe(or(b.Run, "none")))
|
||||
current := "none"
|
||||
if inBody != nil {
|
||||
current = safe(inBody.doc.ID)
|
||||
}
|
||||
switch {
|
||||
case inBody != nil && inBody.doc.ID == d.ID:
|
||||
fmt.Fprintf(a.stdout, " #%d's body carries this report\n", pr.Number)
|
||||
_, _ = fmt.Fprintf(a.stdout, " #%d's body carries this report\n", pr.Number)
|
||||
case pr.Head.SHA == b.Batch:
|
||||
fmt.Fprintf(a.stdout, " #%d's head is %.12s, the batch this report describes, but its body carries report %.12s\n", pr.Number, safe(b.Batch), current)
|
||||
_, _ = fmt.Fprintf(a.stdout, " #%d's head is %.12s, the batch this report describes, but its body carries report %.12s\n", pr.Number, safe(b.Batch), current)
|
||||
default:
|
||||
fmt.Fprintf(a.stdout, " publication unconfirmed: #%d's head is %.12s, not %.12s, and its body carries report %.12s\n", pr.Number, safe(pr.Head.SHA), safe(b.Batch), current)
|
||||
_, _ = fmt.Fprintf(a.stdout, " publication unconfirmed: #%d's head is %.12s, not %.12s, and its body carries report %.12s\n", pr.Number, safe(pr.Head.SHA), safe(b.Batch), current)
|
||||
}
|
||||
if !inComment {
|
||||
if prState(pr) == "open" {
|
||||
fmt.Fprintln(a.stdout, " comment not posted; the next backport-batch run --push posts it")
|
||||
_, _ = fmt.Fprintln(a.stdout, " comment not posted; the next backport-batch run --push posts it")
|
||||
} else {
|
||||
fmt.Fprintf(a.stdout, " comment not posted; kept in the body only, since nothing is written to a %s pull request\n", prState(pr))
|
||||
_, _ = fmt.Fprintf(a.stdout, " comment not posted; kept in the body only, since nothing is written to a %s pull request\n", prState(pr))
|
||||
}
|
||||
}
|
||||
type line struct {
|
||||
@@ -780,8 +780,8 @@ func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport,
|
||||
}
|
||||
for _, e := range d.Primary.Parked {
|
||||
text := "parked: "
|
||||
switch {
|
||||
case e.Kind == parkPartial:
|
||||
switch e.Kind {
|
||||
case parkPartial:
|
||||
text += "only part of it applies, it would leave " + strings.Join(e.Files, ", ") + " without effect"
|
||||
default:
|
||||
var needs []string
|
||||
@@ -802,22 +802,22 @@ func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport,
|
||||
}
|
||||
slices.SortFunc(lines, func(x, y line) int { return cmp.Compare(x.n, y.n) })
|
||||
for _, l := range lines {
|
||||
fmt.Fprintf(a.stdout, "#%d %s\n", l.n, terminalSafe(l.text))
|
||||
_, _ = fmt.Fprintf(a.stdout, "#%d %s\n", l.n, terminalSafe(l.text))
|
||||
}
|
||||
overlay := func(name string, list []int) {
|
||||
if len(list) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %s\n", name, strings.Trim(fmt.Sprint(list), "[]"))
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %s\n", name, strings.Trim(fmt.Sprint(list), "[]"))
|
||||
}
|
||||
}
|
||||
overlay("stale, still on the batch", d.Overlays.Stale)
|
||||
overlay("only part of it applies, still on the batch", d.Overlays.Partial)
|
||||
for _, e := range d.Overlays.Dropped {
|
||||
fmt.Fprintf(a.stdout, "dropped: #%d, merged as %.12s, on #%d: %s\n", e.Original, terminalSafe(e.Merge), e.On, terminalSafe(e.Reason))
|
||||
_, _ = fmt.Fprintf(a.stdout, "dropped: #%d, merged as %.12s, on #%d: %s\n", e.Original, terminalSafe(e.Merge), e.On, terminalSafe(e.Reason))
|
||||
}
|
||||
overlay("changed in the pick", d.Overlays.Changed)
|
||||
for _, n := range slices.Sorted(maps.Keys(d.Diag)) {
|
||||
for _, ln := range d.Diag[n] {
|
||||
fmt.Fprintf(a.stdout, "#%s: %s\n", terminalSafe(n), terminalSafe(ln))
|
||||
_, _ = fmt.Fprintf(a.stdout, "#%s: %s\n", terminalSafe(n), terminalSafe(ln))
|
||||
}
|
||||
}
|
||||
if len(d.Other) > 0 {
|
||||
@@ -825,7 +825,7 @@ func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport,
|
||||
for i, sha := range d.Other {
|
||||
other[i] = safe(sha)
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "commits on main that merged no pull request: %s\n", strings.Join(other, ", "))
|
||||
_, _ = fmt.Fprintf(a.stdout, "commits on main that merged no pull request: %s\n", strings.Join(other, ", "))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -850,7 +850,7 @@ func (a *app) postReport(n int, s storedReport) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
defer func() { _ = os.Remove(name) }()
|
||||
return a.gh.createComment(n, name)
|
||||
}
|
||||
|
||||
@@ -873,12 +873,12 @@ func writeTemp(pattern, content string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
if _, err := f.WriteString(content); err != nil {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
_ = f.Close()
|
||||
_ = os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
os.Remove(f.Name())
|
||||
_ = os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
return f.Name(), nil
|
||||
|
||||
@@ -224,7 +224,9 @@ func TestReportPartition(t *testing.T) {
|
||||
}
|
||||
t.Setenv("GIT_COMMITTER_DATE", "2026-01-01T00:00:00Z")
|
||||
f.pr(4, "fix(app): four, before the start", "", map[string]string{"app/four.txt": "4\n"})
|
||||
os.Unsetenv("GIT_COMMITTER_DATE")
|
||||
if err := os.Unsetenv("GIT_COMMITTER_DATE"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.setPolicy("release-1.0: 2026-06-01\n")
|
||||
f.onLine("release-1.0", "fix(app): a line-only eleven", map[string]string{"app/eleven.txt": "line\n"})
|
||||
pr(5, "fix(app): five", map[string]string{"app/five.txt": "5\n"}, true)
|
||||
|
||||
@@ -114,6 +114,23 @@ func mergeMessage(message, merge string) string {
|
||||
return strings.TrimRight(message, " \t\r\n") + "\n\n" + batchTrailer + ": " + merge + "\n"
|
||||
}
|
||||
|
||||
// pickUnit and replayNext start the next unit only while the run's output is
|
||||
// intact: once it was lost no new unit starts, and a unit already in flight
|
||||
// still finishes its rollback.
|
||||
func (a *app) pickUnit(v *verdict, validate func(base, head string) ([]string, error)) (pick, error) {
|
||||
if err := a.mayAct(); err != nil {
|
||||
return pick{}, err
|
||||
}
|
||||
return pickOriginal(v, validate)
|
||||
}
|
||||
|
||||
func (a *app) replayNext(u unit) (string, []string, error) {
|
||||
if err := a.mayAct(); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return replayUnit(u)
|
||||
}
|
||||
|
||||
// commitMerge makes the merge of tip onto base, whose tree is tip's since tip
|
||||
// descends from base, and moves the current branch from tip to it.
|
||||
func commitMerge(base, tip, message string) (string, error) {
|
||||
@@ -121,9 +138,9 @@ func commitMerge(base, tip, message string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
defer func() { _ = os.Remove(f.Name()) }()
|
||||
if _, err := f.WriteString(message); err != nil {
|
||||
f.Close()
|
||||
_ = f.Close()
|
||||
return "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
|
||||
@@ -66,7 +66,7 @@ func (a *app) verify(args []string) int {
|
||||
return a.fail(err)
|
||||
}
|
||||
if pr == nil {
|
||||
fmt.Fprintf(a.stdout, "%s: no batch pull request is open; nothing to verify\n", l.branch())
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: no batch pull request is open; nothing to verify\n", l.branch())
|
||||
return 0
|
||||
}
|
||||
if head, err = batchHead(pr); err != nil {
|
||||
@@ -102,7 +102,7 @@ func (a *app) verify(args []string) int {
|
||||
}
|
||||
if why != "" {
|
||||
refused = append(refused, why)
|
||||
fmt.Fprintf(a.stdout, "refused %.12s: %s\n", u.commit, why)
|
||||
_, _ = fmt.Fprintf(a.stdout, "refused %.12s: %s\n", u.commit, why)
|
||||
continue
|
||||
}
|
||||
seen[v] = u.commit
|
||||
@@ -110,13 +110,13 @@ func (a *app) verify(args []string) int {
|
||||
if !u.merge() {
|
||||
n = 1
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "ok %.12s #%d, %d %s\n", u.commit, v.o.number, n, plural(n, "commit"))
|
||||
_, _ = fmt.Fprintf(a.stdout, "ok %.12s #%d, %d %s\n", u.commit, v.o.number, n, plural(n, "commit"))
|
||||
}
|
||||
if len(refused) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d of %d units at %.12s are not originals in the batch's shape\n", l.branch(), len(refused), len(units), head)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d of %d units at %.12s are not originals in the batch's shape\n", l.branch(), len(refused), len(units), head)
|
||||
return 1
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "%s: %d %s at %.12s, each one original in the batch's shape\n", l.branch(), len(units), plural(len(units), "unit"), head)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d %s at %.12s, each one original in the batch's shape\n", l.branch(), len(units), plural(len(units), "unit"), head)
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
+40
-15
@@ -22,6 +22,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
@@ -58,6 +59,10 @@ Options:
|
||||
--no-color Disable color output (auto-disabled on non-TTY)
|
||||
-h, --help Show this help
|
||||
|
||||
Exit status: 0 when everything is ready, 1 when something is not ready or a
|
||||
write to stdout fails, 2 on invalid arguments. A closed pipe ends the process
|
||||
with SIGPIPE, like other Unix tools.
|
||||
|
||||
`
|
||||
|
||||
// entry is a resource catalog item.
|
||||
@@ -181,10 +186,16 @@ func run(args []string) int {
|
||||
cfg.coreOnly = true
|
||||
i++
|
||||
case "-h", "--help":
|
||||
fmt.Fprint(os.Stdout, usageText)
|
||||
if _, err := fmt.Fprint(os.Stdout, usageText); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "writing usage: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case "--version":
|
||||
fmt.Fprintln(os.Stdout, Version)
|
||||
if _, err := fmt.Fprintln(os.Stdout, Version); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "writing version: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown argument: %s\n", a)
|
||||
@@ -213,14 +224,14 @@ func run(args []string) int {
|
||||
cfg.timeoutS = sec
|
||||
|
||||
if cfg.waitMode {
|
||||
return cfg.runWait()
|
||||
return cfg.runWait(os.Stdout)
|
||||
}
|
||||
if cfg.watch {
|
||||
cfg.runWatch()
|
||||
return 0
|
||||
return cfg.runWatch(os.Stdout)
|
||||
}
|
||||
if writeOutput(os.Stdout, cfg.runOnce()) != nil {
|
||||
return 1
|
||||
}
|
||||
out := cfg.runOnce()
|
||||
fmt.Print(out)
|
||||
if cfg.foundIssues {
|
||||
return 1
|
||||
}
|
||||
@@ -281,18 +292,31 @@ func parseTimeout(raw string) (int, bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (cfg *config) runWait() int {
|
||||
// writeOutput prints a result. A result its reader never got fails the run,
|
||||
// whatever the resources' state, since the exit code alone would vouch for it.
|
||||
func writeOutput(w io.Writer, parts ...string) error {
|
||||
for _, part := range parts {
|
||||
if _, err := io.WriteString(w, part); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "writing output: %v\n", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *config) runWait(w io.Writer) int {
|
||||
start := time.Now()
|
||||
for {
|
||||
output := cfg.runOnce()
|
||||
elapsed := int(time.Since(start).Seconds())
|
||||
if strings.Contains(output, "All resources are ready.") {
|
||||
fmt.Print(output)
|
||||
fmt.Printf("%sReady after %ds.%s\n", cfg.green, elapsed, cfg.reset)
|
||||
if writeOutput(w, output, fmt.Sprintf("%sReady after %ds.%s\n", cfg.green, elapsed, cfg.reset)) != nil {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
if elapsed >= cfg.timeoutS {
|
||||
fmt.Print(output)
|
||||
_ = writeOutput(w, output)
|
||||
fmt.Fprintf(os.Stderr, "%s%sTimeout after %ds — resources still not ready.%s\n", cfg.red, cfg.bold, cfg.timeoutS, cfg.reset)
|
||||
return 1
|
||||
}
|
||||
@@ -300,13 +324,14 @@ func (cfg *config) runWait() int {
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg *config) runWatch() {
|
||||
func (cfg *config) runWatch(w io.Writer) int {
|
||||
for {
|
||||
output := cfg.runOnce()
|
||||
// Smooth clear is cosmetic and untested; just reprint.
|
||||
fmt.Printf("%sLast updated: %s (refreshing every %ds, Ctrl+C to stop)%s\n", cfg.bold, time.Now().Format(time.UnixDate), cfg.interval, cfg.reset)
|
||||
fmt.Println()
|
||||
fmt.Print(output)
|
||||
header := fmt.Sprintf("%sLast updated: %s (refreshing every %ds, Ctrl+C to stop)%s\n\n", cfg.bold, time.Now().Format(time.UnixDate), cfg.interval, cfg.reset)
|
||||
if writeOutput(w, header, output) != nil {
|
||||
return 1
|
||||
}
|
||||
time.Sleep(time.Duration(cfg.interval) * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHelpAndVersionSucceedOnWritableStdout(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = r.Close() })
|
||||
stdout := os.Stdout
|
||||
os.Stdout = w
|
||||
t.Cleanup(func() { os.Stdout = stdout })
|
||||
read := make(chan string, 1)
|
||||
go func() {
|
||||
b, _ := io.ReadAll(r)
|
||||
read <- string(b)
|
||||
}()
|
||||
|
||||
for _, arg := range []string{"--help", "--version"} {
|
||||
if code := run([]string{arg}); code != 0 {
|
||||
t.Errorf("run(%s) = %d, want 0", arg, code)
|
||||
}
|
||||
}
|
||||
_ = w.Close()
|
||||
if out := <-read; !strings.Contains(out, "Usage:") || !strings.Contains(out, Version) {
|
||||
t.Errorf("stdout = %q, want the usage text and the version", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpAndVersionFailOnUnwritableStdout(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = r.Close()
|
||||
_ = w.Close()
|
||||
stdout := os.Stdout
|
||||
os.Stdout = w
|
||||
t.Cleanup(func() { os.Stdout = stdout })
|
||||
|
||||
for _, arg := range []string{"--help", "--version"} {
|
||||
if code := run([]string{arg}); code == 0 {
|
||||
t.Errorf("run(%s) with an unwritable stdout = 0, want a failure", arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -390,7 +390,7 @@ func createPackageFromFile(ctx context.Context, k8sClient client.Client, filePat
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Package %s not found in file", packageName)
|
||||
return fmt.Errorf("no Package %s in file", packageName)
|
||||
}
|
||||
|
||||
func installPackage(ctx context.Context, k8sClient client.Client, packageSourceName string) error {
|
||||
|
||||
+27
-21
@@ -19,7 +19,7 @@ package cmd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"io"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
@@ -78,24 +78,25 @@ Use --components flag to show components on separate lines.`,
|
||||
}
|
||||
|
||||
if listCmdFlags.installed {
|
||||
return listPackages(ctx, k8sClient, listCmdFlags.components)
|
||||
return listPackages(ctx, k8sClient, cmd.OutOrStdout(), listCmdFlags.components)
|
||||
}
|
||||
return listPackageSources(ctx, k8sClient, listCmdFlags.components)
|
||||
return listPackageSources(ctx, k8sClient, cmd.OutOrStdout(), listCmdFlags.components)
|
||||
},
|
||||
}
|
||||
|
||||
func listPackageSources(ctx context.Context, k8sClient client.Client, showComponents bool) error {
|
||||
func listPackageSources(ctx context.Context, k8sClient client.Client, out io.Writer, showComponents bool) error {
|
||||
var psList cozyv1alpha1.PackageSourceList
|
||||
if err := k8sClient.List(ctx, &psList); err != nil {
|
||||
return fmt.Errorf("failed to list PackageSources: %w", err)
|
||||
}
|
||||
|
||||
// Use tabwriter for better column alignment
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
||||
defer w.Flush()
|
||||
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
|
||||
|
||||
// Print header
|
||||
fmt.Fprintln(w, "NAME\tVARIANTS\tREADY\tSTATUS")
|
||||
if _, err := fmt.Fprintln(w, "NAME\tVARIANTS\tREADY\tSTATUS"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Print rows
|
||||
for _, ps := range psList.Items {
|
||||
@@ -123,24 +124,26 @@ func listPackageSources(ctx context.Context, k8sClient client.Client, showCompon
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", ps.Name, variantsStr, ready, status)
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", ps.Name, variantsStr, ready, status); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Show components if requested
|
||||
if showComponents {
|
||||
for _, variant := range ps.Spec.Variants {
|
||||
for _, component := range variant.Components {
|
||||
fmt.Fprintf(w, " %s\t%s\t\t\n",
|
||||
fmt.Sprintf("%s.%s", ps.Name, component.Name),
|
||||
variant.Name)
|
||||
if _, err := fmt.Fprintf(w, " %s.%s\t%s\t\t\n", ps.Name, component.Name, variant.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
func listPackages(ctx context.Context, k8sClient client.Client, showComponents bool) error {
|
||||
func listPackages(ctx context.Context, k8sClient client.Client, out io.Writer, showComponents bool) error {
|
||||
var pkgList cozyv1alpha1.PackageList
|
||||
if err := k8sClient.List(ctx, &pkgList); err != nil {
|
||||
return fmt.Errorf("failed to list Packages: %w", err)
|
||||
@@ -160,11 +163,12 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
|
||||
}
|
||||
|
||||
// Use tabwriter for better column alignment
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
||||
defer w.Flush()
|
||||
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
|
||||
|
||||
// Print header
|
||||
fmt.Fprintln(w, "NAME\tVARIANT\tREADY\tSTATUS")
|
||||
if _, err := fmt.Fprintln(w, "NAME\tVARIANT\tREADY\tSTATUS"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Print rows
|
||||
for _, pkg := range pkgList.Items {
|
||||
@@ -187,7 +191,9 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pkg.Name, variant, ready, status)
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pkg.Name, variant, ready, status); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Show components if requested
|
||||
if showComponents {
|
||||
@@ -197,9 +203,9 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
|
||||
for _, v := range ps.Spec.Variants {
|
||||
if v.Name == variant {
|
||||
for _, component := range v.Components {
|
||||
fmt.Fprintf(w, " %s\t%s\t\t\n",
|
||||
fmt.Sprintf("%s.%s", pkg.Name, component.Name),
|
||||
variant)
|
||||
if _, err := fmt.Fprintf(w, " %s.%s\t%s\t\t\n", pkg.Name, component.Name, variant); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -208,7 +214,7 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
"github.com/spf13/cobra"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
)
|
||||
|
||||
type failingWriter struct{}
|
||||
|
||||
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("broken pipe") }
|
||||
|
||||
func TestPrintReportReturnsWriteError(t *testing.T) {
|
||||
r := &Report{Findings: []Finding{{Severity: SeverityError, Code: "X", Message: "m"}}}
|
||||
if err := printReport(r, failingWriter{}); err == nil {
|
||||
t.Fatal("printReport to an unwritable writer returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
// The table is buffered until the tabwriter flushes, so a failing reader shows
|
||||
// up only as a Flush error, which list has to return.
|
||||
func TestListReturnsWriteError(t *testing.T) {
|
||||
s := runtime.NewScheme()
|
||||
utilruntime.Must(cozyv1alpha1.AddToScheme(s))
|
||||
ps := &cozyv1alpha1.PackageSource{ObjectMeta: metav1.ObjectMeta{Name: "acme.app"}}
|
||||
pkg := &cozyv1alpha1.Package{ObjectMeta: metav1.ObjectMeta{Name: "acme.app"}}
|
||||
cl := fake.NewClientBuilder().WithScheme(s).WithObjects(ps, pkg).Build()
|
||||
|
||||
for name, list := range map[string]func(io.Writer) error{
|
||||
"sources": func(w io.Writer) error { return listPackageSources(context.Background(), cl, w, true) },
|
||||
"packages": func(w io.Writer) error { return listPackages(context.Background(), cl, w, true) },
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if err := list(failingWriter{}); err == nil {
|
||||
t.Fatal("list to an unwritable writer returned nil")
|
||||
}
|
||||
var out strings.Builder
|
||||
if err := list(&out); err != nil || !strings.Contains(out.String(), "acme.app") {
|
||||
t.Fatalf("list = %v, output %q; want the row", err, out.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A command that cannot print its validation report still fails, and a
|
||||
// refusal keeps validation as its reason with the write error joined to it.
|
||||
func TestValidationCommandsReportUnwritableReport(t *testing.T) {
|
||||
fakeFlux := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(fakeFlux, "flux"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", fakeFlux+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
|
||||
emptyRepo := func(t *testing.T) string {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "packages"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return root
|
||||
}
|
||||
run := func(c *cobra.Command, args ...string) error {
|
||||
c.SetOut(failingWriter{})
|
||||
t.Cleanup(func() { c.SetOut(nil) })
|
||||
return c.RunE(c, args)
|
||||
}
|
||||
push := func(root string) error {
|
||||
pushCmdFlags.path = root
|
||||
t.Cleanup(func() { pushCmdFlags.path = "." })
|
||||
return run(pushCmd, "oci://registry.example/acme/apps:v1")
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
run func(t *testing.T) error
|
||||
refusal string
|
||||
}{
|
||||
{name: "validate, valid", run: func(t *testing.T) error { return run(validateCmd, validRepo(t)) }},
|
||||
{name: "validate, invalid", run: func(t *testing.T) error { return run(validateCmd, emptyRepo(t)) }, refusal: "validation failed"},
|
||||
{name: "push, valid", run: func(t *testing.T) error { return push(validRepo(t)) }, refusal: "nothing was pushed"},
|
||||
{name: "push, invalid", run: func(t *testing.T) error { return push(emptyRepo(t)) }, refusal: "refusing to push"},
|
||||
{name: "tap, invalid", run: func(t *testing.T) error { return run(tapCmd, "oci://registry.example/acme/apps:v1") }, refusal: "refusing to tap"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.run(t)
|
||||
if err == nil || !strings.Contains(err.Error(), "broken pipe") {
|
||||
t.Fatalf("err = %v, want the write error", err)
|
||||
}
|
||||
if tc.refusal != "" && !strings.Contains(err.Error(), tc.refusal) {
|
||||
t.Errorf("err = %v, want the refusal %q kept", err, tc.refusal)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A tap that applied is reported as applied even when its summary is lost,
|
||||
// and the lost summary still fails the command.
|
||||
func TestTapSummaryReportsUnwritableOutput(t *testing.T) {
|
||||
err := writeTapSummary(failingWriter{}, "oci://registry.example/acme/apps:v1", "tap-acme", []string{"acme.app"})
|
||||
if err == nil || !strings.Contains(err.Error(), "tap applied") || !strings.Contains(err.Error(), "broken pipe") {
|
||||
t.Fatalf("err = %v, want the write error with the tap reported as applied", err)
|
||||
}
|
||||
var out strings.Builder
|
||||
if err := writeTapSummary(&out, "oci://registry.example/acme/apps:v1", "tap-acme", []string{"acme.app"}); err != nil || !strings.Contains(out.String(), "cozypkg add acme.app") {
|
||||
t.Fatalf("writeTapSummary = %v, output %q", err, out.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A clean report has no finding lines, so its summary is its only write and
|
||||
// alone decides the result.
|
||||
func TestPrintReportReturnsSummaryWriteError(t *testing.T) {
|
||||
if err := printReport(&Report{}, failingWriter{}); err == nil {
|
||||
t.Fatal("printReport of a clean report to an unwritable writer returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
// push runs flux only after its status line was written, with validation
|
||||
// skipped and after a report that was written.
|
||||
func TestPushStopsBeforeFluxWhenItsStatusIsLost(t *testing.T) {
|
||||
fakeFlux := t.TempDir()
|
||||
ran := filepath.Join(t.TempDir(), "flux-ran")
|
||||
if err := os.WriteFile(filepath.Join(fakeFlux, "flux"), []byte("#!/bin/sh\ntouch "+ran+"\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", fakeFlux+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
for _, skip := range []bool{true, false} {
|
||||
t.Run(fmt.Sprintf("skip-validate=%v", skip), func(t *testing.T) {
|
||||
pushCmdFlags.path, pushCmdFlags.skipValidate = validRepo(t), skip
|
||||
pushCmdFlags.source, pushCmdFlags.revision = "https://example.com/acme.git", "v1"
|
||||
t.Cleanup(func() {
|
||||
pushCmdFlags.path, pushCmdFlags.skipValidate = ".", false
|
||||
pushCmdFlags.source, pushCmdFlags.revision = "", ""
|
||||
})
|
||||
pushCmd.SetOut(&failOn{want: "Pushing"})
|
||||
t.Cleanup(func() { pushCmd.SetOut(nil) })
|
||||
err := pushCmd.RunE(pushCmd, []string{"oci://registry.example/acme/apps:v1"})
|
||||
if err == nil || !strings.Contains(err.Error(), "nothing was pushed") {
|
||||
t.Fatalf("push losing its status = %v, want nothing pushed", err)
|
||||
}
|
||||
if _, statErr := os.Stat(ran); statErr == nil {
|
||||
t.Fatal("flux ran after the status line was lost")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// failOn fails every write that contains want, and takes every other one.
|
||||
type failOn struct {
|
||||
want string
|
||||
buf strings.Builder
|
||||
}
|
||||
|
||||
func (w *failOn) Write(p []byte) (int, error) {
|
||||
if strings.Contains(string(p), w.want) {
|
||||
return 0, errors.New("broken pipe")
|
||||
}
|
||||
return w.buf.Write(p)
|
||||
}
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -106,10 +107,13 @@ published. Source URL and revision are derived from git when not given.`,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printReport(rep, cmd.OutOrStdout())
|
||||
printErr := printReport(rep, cmd.OutOrStdout())
|
||||
if rep.HasErrors() {
|
||||
errs, _, _ := rep.Counts()
|
||||
return fmt.Errorf("refusing to push: validation failed with %d error(s) (use --skip-validate to override)", errs)
|
||||
return errors.Join(fmt.Errorf("refusing to push: validation failed with %d error(s) (use --skip-validate to override)", errs), printErr)
|
||||
}
|
||||
if printErr != nil {
|
||||
return fmt.Errorf("nothing was pushed: writing the validation report failed: %w", printErr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +133,9 @@ published. Source URL and revision are derived from git when not given.`,
|
||||
}
|
||||
|
||||
fluxArgs := buildFluxPushArgs(ref, packagesDir, source, revision, pushCmdFlags.reproducible)
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "Pushing %s (source=%s revision=%s)\n", ref, source, revision)
|
||||
if _, err := fmt.Fprintf(cmd.OutOrStdout(), "Pushing %s (source=%s revision=%s)\n", ref, source, revision); err != nil {
|
||||
return fmt.Errorf("nothing was pushed: writing the push status failed: %w", err)
|
||||
}
|
||||
flux := exec.Command("flux", fluxArgs...)
|
||||
flux.Stdout = cmd.OutOrStdout()
|
||||
flux.Stderr = cmd.ErrOrStderr()
|
||||
|
||||
+22
-12
@@ -18,6 +18,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
@@ -402,9 +403,8 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
return err
|
||||
}
|
||||
if rep.HasErrors() {
|
||||
printReport(rep, cmd.OutOrStdout())
|
||||
errs, _, _ := rep.Counts()
|
||||
return fmt.Errorf("refusing to tap: artifact failed validation with %d error(s) (use --skip-validate to override)", errs)
|
||||
return errors.Join(fmt.Errorf("refusing to tap: artifact failed validation with %d error(s) (use --skip-validate to override)", errs), printReport(rep, cmd.OutOrStdout()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -487,19 +487,29 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "warning: could not reset materialization state on OCIRepository/%s: %v\n", srcName, err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "Tapped %s\n OCIRepository/%s\n", fullRef, srcName)
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), " PackageSource/%s\n", n)
|
||||
}
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Tapping installs nothing. Install an app with:\n")
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), " cozypkg add %s\n", n)
|
||||
}
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "A privileged install component is not installed until you confirm it with 'cozypkg add --allow-privileged'.\n")
|
||||
return nil
|
||||
return writeTapSummary(cmd.OutOrStdout(), fullRef, srcName, names)
|
||||
},
|
||||
}
|
||||
|
||||
// writeTapSummary reports what a tap created. The tap is applied by then, so a
|
||||
// summary that cannot be written fails the command while saying so.
|
||||
func writeTapSummary(w io.Writer, fullRef, srcName string, names []string) error {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "Tapped %s\n OCIRepository/%s\n", fullRef, srcName)
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(&b, " PackageSource/%s\n", n)
|
||||
}
|
||||
b.WriteString("Tapping installs nothing. Install an app with:\n")
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(&b, " cozypkg add %s\n", n)
|
||||
}
|
||||
b.WriteString("A privileged install component is not installed until you confirm it with 'cozypkg add --allow-privileged'.\n")
|
||||
if _, err := io.WriteString(w, b.String()); err != nil {
|
||||
return fmt.Errorf("tap applied, but writing its summary failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var untapCmd = &cobra.Command{
|
||||
Use: "untap <packagesource-name>",
|
||||
Short: "Remove a tapped repository",
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -535,16 +536,19 @@ func pullOCIArtifact(ref string) (string, func(), error) {
|
||||
return dir, cleanup, nil
|
||||
}
|
||||
|
||||
func printReport(r *Report, w io.Writer) {
|
||||
func printReport(r *Report, w io.Writer) error {
|
||||
for _, f := range r.Findings {
|
||||
loc := f.Location
|
||||
if loc == "" {
|
||||
loc = "-"
|
||||
}
|
||||
fmt.Fprintf(w, "[%s] %s (%s): %s\n", f.Severity, loc, f.Code, f.Message)
|
||||
if _, err := fmt.Fprintf(w, "[%s] %s (%s): %s\n", f.Severity, loc, f.Code, f.Message); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
errs, warns, infos := r.Counts()
|
||||
fmt.Fprintf(w, "\nSummary: %d error(s), %d warning(s), %d info\n", errs, warns, infos)
|
||||
_, err := fmt.Fprintf(w, "\nSummary: %d error(s), %d warning(s), %d info\n", errs, warns, infos)
|
||||
return err
|
||||
}
|
||||
|
||||
var validateCmdFlags struct {
|
||||
@@ -663,10 +667,13 @@ runs "helm lint" on every component chart.`,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printReport(rep, cmd.OutOrStdout())
|
||||
printErr := printReport(rep, cmd.OutOrStdout())
|
||||
if rep.HasErrors() {
|
||||
errs, _, _ := rep.Counts()
|
||||
return fmt.Errorf("validation failed: %d error(s)", errs)
|
||||
return errors.Join(fmt.Errorf("validation failed: %d error(s)", errs), printErr)
|
||||
}
|
||||
if printErr != nil {
|
||||
return fmt.Errorf("writing the validation report failed: %w", printErr)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -663,7 +663,7 @@ func validateGitRef(refMap map[string]string) error {
|
||||
return fmt.Errorf("commit SHA should be at least 7 characters, got: %s", commit)
|
||||
}
|
||||
for _, c := range commit {
|
||||
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
|
||||
if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') {
|
||||
return fmt.Errorf("commit SHA should be a hexadecimal string, got: %s", commit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Command observability-proxy serves tenant logs from the VictoriaLogs
|
||||
// stores behind a Kubernetes TokenReview and SubjectAccessReview, with the
|
||||
// tenant's namespace scope forced into every upstream request.
|
||||
// Command observability-proxy serves tenant logs and metrics from the
|
||||
// VictoriaLogs and VictoriaMetrics stores behind a Kubernetes TokenReview
|
||||
// and SubjectAccessReview, with the tenant's namespace scope forced into
|
||||
// every upstream request.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -37,6 +38,7 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
@@ -56,6 +58,7 @@ type options struct {
|
||||
tailIdleTimeout time.Duration
|
||||
shutdownTimeout time.Duration
|
||||
logs observabilityproxy.LogsLimits
|
||||
metrics observabilityproxy.MetricsConfig
|
||||
limiter observabilityproxy.LimiterConfig
|
||||
}
|
||||
|
||||
@@ -81,6 +84,14 @@ func parseFlags(args []string) (options, error) {
|
||||
fs.IntVar(&o.logs.DefaultLimit, "logs-default-limit", 1000, "entries returned when a request sets no limit")
|
||||
fs.IntVar(&o.logs.MaxLimit, "logs-max-limit", 10000, "largest limit a request may set")
|
||||
fs.DurationVar(&o.logs.MaxTailResume, "logs-max-tail-resume", 15*time.Minute, "how far back a tail may resume with since or start_offset")
|
||||
// Store names and retention follow metricsStorages in
|
||||
// packages/system/monitoring/values.yaml.
|
||||
fs.StringVar(&o.metrics.ShortTermStore, "metrics-shortterm-store", "shortterm", "VMCluster name of the short-term metrics store of the root tenant")
|
||||
fs.StringVar(&o.metrics.LongTermStore, "metrics-longterm-store", "longterm", "VMCluster name of the long-term metrics store of the root tenant")
|
||||
fs.DurationVar(&o.metrics.ShortTermRetention, "metrics-shortterm-retention", 72*time.Hour, "retention of the short-term store; older windows are read from the long-term one")
|
||||
fs.DurationVar(&o.metrics.Limits.MaxRange, "metrics-max-range", 30*24*time.Hour, "longest [start, end] and step a metrics request may use")
|
||||
fs.IntVar(&o.metrics.Limits.MaxPoints, "metrics-max-points", 11000, "most points per series a range query may return")
|
||||
fs.DurationVar(&o.metrics.Limits.MaxTimeout, "metrics-max-timeout", 30*time.Second, "timeout forwarded to vmselect; a longer client timeout is clamped; must be below --request-timeout when that is set")
|
||||
fs.IntVar(&o.limiter.MaxConcurrentPerUser, "max-concurrent-per-user", 10, "concurrent non-tail requests per user; 0 disables")
|
||||
fs.Float64Var(&o.limiter.RatePerUser, "rate-limit-per-user", 10, "sustained requests per second per user; 0 disables")
|
||||
fs.IntVar(&o.limiter.RateBurst, "rate-limit-burst", 20, "request burst per user")
|
||||
@@ -94,6 +105,13 @@ func parseFlags(args []string) (options, error) {
|
||||
if fs.NArg() > 0 {
|
||||
return o, fmt.Errorf("unexpected argument %q", fs.Arg(0))
|
||||
}
|
||||
o.metrics.RootTenant = o.rootTenant
|
||||
for _, store := range []string{o.metrics.ShortTermStore, o.metrics.LongTermStore} {
|
||||
// The name becomes part of a Service host name.
|
||||
if errs := validation.IsDNS1123Label("vmselect-" + store); store == "" || len(errs) > 0 {
|
||||
return o, fmt.Errorf("invalid metrics store name %q", store)
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case o.rootTenant == "":
|
||||
return o, errors.New("--root-tenant must not be empty")
|
||||
@@ -110,8 +128,17 @@ func parseFlags(args []string) (options, error) {
|
||||
return o, errors.New("--logs-max-range must be at least 3s")
|
||||
case o.logs.DefaultLimit <= 0 || o.logs.MaxLimit < o.logs.DefaultLimit:
|
||||
return o, errors.New("--logs-default-limit must be positive and not above --logs-max-limit")
|
||||
case o.metrics.ShortTermRetention <= 0 || o.metrics.Limits.MaxRange <= 0 || o.metrics.Limits.MaxTimeout <= 0:
|
||||
return o, errors.New("--metrics-shortterm-retention, --metrics-max-range and --metrics-max-timeout must be positive")
|
||||
case o.metrics.Limits.MaxPoints < 2:
|
||||
return o, errors.New("--metrics-max-points must be at least 2")
|
||||
case o.logs.MaxTailResume < 0:
|
||||
return o, errors.New("--logs-max-tail-resume must not be negative")
|
||||
// The proxy's deadline starts before the request reaches vmselect, so
|
||||
// at an equal or longer timeout it cuts a query vmselect is still
|
||||
// allowed to run and answers 504 for a store that is within its time.
|
||||
case o.requestTimeout > 0 && o.metrics.Limits.MaxTimeout >= o.requestTimeout:
|
||||
return o, errors.New("--metrics-max-timeout must be below --request-timeout")
|
||||
case o.requestTimeout < 0 || o.tailIdleTimeout < 0 || o.authCacheTTL < 0 || o.shutdownTimeout < 0:
|
||||
return o, errors.New("timeouts and the auth cache TTL must not be negative")
|
||||
// NaN passes every comparison and +Inf never empties a bucket; either
|
||||
@@ -167,6 +194,7 @@ func run(o options) error {
|
||||
proxy := observabilityproxy.NewServer(observabilityproxy.Config{
|
||||
ClusterDomain: o.clusterDomain,
|
||||
Logs: o.logs,
|
||||
Metrics: o.metrics,
|
||||
Limiter: o.limiter,
|
||||
RequestTimeout: o.requestTimeout,
|
||||
TailIdleTimeout: o.tailIdleTimeout,
|
||||
|
||||
@@ -32,6 +32,17 @@ func TestParseFlagsDefaults(t *testing.T) {
|
||||
if o.authCacheTTL != 10*time.Second || o.logs.MaxTailResume != 15*time.Minute || o.logs.MaxRange != 7*24*time.Hour {
|
||||
t.Errorf("limit defaults changed: %+v", o)
|
||||
}
|
||||
// Retention and store names of packages/system/monitoring/values.yaml.
|
||||
m := o.metrics
|
||||
if m.ShortTermStore != "shortterm" || m.LongTermStore != "longterm" || m.ShortTermRetention != 72*time.Hour {
|
||||
t.Errorf("metrics store defaults changed: %+v", m)
|
||||
}
|
||||
if m.Limits.MaxRange != 30*24*time.Hour || m.Limits.MaxPoints != 11000 || m.Limits.MaxTimeout != 30*time.Second {
|
||||
t.Errorf("metrics limit defaults changed: %+v", m.Limits)
|
||||
}
|
||||
if m.RootTenant != o.rootTenant {
|
||||
t.Errorf("metrics root tenant = %q, want --root-tenant", m.RootTenant)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsOverrides(t *testing.T) {
|
||||
@@ -48,6 +59,21 @@ func TestParseFlagsOverrides(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsMetricsOverrides(t *testing.T) {
|
||||
o, err := parseFlags([]string{
|
||||
"--root-tenant=tenant-x", "--metrics-shortterm-store=fast", "--metrics-longterm-store=slow",
|
||||
"--metrics-shortterm-retention=24h", "--metrics-max-range=168h", "--metrics-max-points=500", "--metrics-max-timeout=10s",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := o.metrics
|
||||
if m.RootTenant != "tenant-x" || m.ShortTermStore != "fast" || m.LongTermStore != "slow" || m.ShortTermRetention != 24*time.Hour ||
|
||||
m.Limits.MaxRange != 168*time.Hour || m.Limits.MaxPoints != 500 || m.Limits.MaxTimeout != 10*time.Second {
|
||||
t.Errorf("overrides not applied: %+v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsInvalid(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"--root-tenant="},
|
||||
@@ -69,7 +95,17 @@ func TestParseFlagsInvalid(t *testing.T) {
|
||||
{"--max-concurrent-per-user=-1"},
|
||||
{"--max-tails-per-user=-1"},
|
||||
{"--max-tails=-1"},
|
||||
{"--metrics-shortterm-store="},
|
||||
{"--metrics-longterm-store=Long.Term"},
|
||||
{"--metrics-shortterm-retention=0s"},
|
||||
{"--metrics-max-range=0s"},
|
||||
{"--metrics-max-points=1"},
|
||||
{"--metrics-max-timeout=0s"},
|
||||
{"--unknown"},
|
||||
// The proxy would cut the request while vmselect is still within
|
||||
// the timeout it was given.
|
||||
{"--request-timeout=10s", "--metrics-max-timeout=30s"},
|
||||
{"--request-timeout=30s", "--metrics-max-timeout=30s"},
|
||||
{"unexpected", "--root-tenant=tenant-x"},
|
||||
} {
|
||||
if _, err := parseFlags(args); err == nil {
|
||||
@@ -77,3 +113,16 @@ func TestParseFlagsInvalid(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The vmselect timeout must stay strictly below a request timeout, and is
|
||||
// free when the request has none.
|
||||
func TestParseFlagsTimeoutPairs(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"--request-timeout=31s", "--metrics-max-timeout=30s"},
|
||||
{"--request-timeout=0", "--metrics-max-timeout=10m"},
|
||||
} {
|
||||
if _, err := parseFlags(args); err != nil {
|
||||
t.Errorf("parseFlags(%v) refused: %v", args, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<!--
|
||||
https://github.com/cozystack/cozystack/releases/tag/v1.7.1
|
||||
-->
|
||||
|
||||
# Cozystack v1.7.1
|
||||
|
||||
Cozystack v1.7.1 is a patch release on the v1.7 line. It fixes the COSI driver bug that could leave freshly granted S3 keys rejected for good, makes marketplace taps digest-pinned, makes MariaDB, PostgreSQL and ClickHouse backups report their real outcome, and repairs several tenant-facing issues: tenant cluster metrics, SecurityGroups on Kubernetes workers, Redis with a single replica, the Proxmox cloud-controller-manager and the tcp-balancer. Read the Migration and Upgrades section before upgrading.
|
||||
|
||||
## Fixes
|
||||
|
||||
* **SeaweedFS COSI driver v0.4.0**: bucket users granted together could get S3 keys that never work (`403 InvalidAccessKeyId`) because the v0.3.1 driver rewrote a legacy IAM file the S3 gateways were migrating. The driver is bumped to v0.4.0, which writes each grant to a per-identity IAM file. Keys already lost on running clusters stay broken: remove the user from the Bucket, wait until its BucketAccess is gone, and add it back ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5284, backport #5290).
|
||||
* **Marketplace taps are pinned to a digest**: a tapped repository no longer follows a mutable tag, so moving the tag upstream cannot change what runs in the cluster. `tap connect` accepts `oci://...@sha256:...` references and an optional keyless cosign verify policy, anchored to an exact identity and preserved across re-connects; with the verify flags the CLI verifies the signature and pins the verified digest at tap time. Community index entries can record a digest too ([**@IvanHunters**](https://github.com/IvanHunters) in #5082, backport #5246).
|
||||
* **Tenant cluster metrics reach the tenant's Monitoring**: the vmagent of tenant Kubernetes clusters now writes to the tenant's vminsert and labels series with the owning tenant instead of `cozy-monitoring`; before, no tenant cluster metric reached the tenant's Monitoring ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5024, backport #5246).
|
||||
* **SecurityGroups apply to Kubernetes worker nodes**: the virt-launcher pods of Kubernetes worker nodes are now resolved to their Kubernetes application and marked as managed by Cozystack, so a SecurityGroup attached to a Kubernetes cluster applies to its workers, not only to its control plane ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5124, backport #5246).
|
||||
* **Membership-only SecurityGroups**: a SecurityGroup with attachments and no rules no longer leaves an invalid CiliumNetworkPolicy behind; its backing policy is accepted by Cilium and still allows or denies nothing ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5133, backport #5246).
|
||||
* **Proxmox CCM and CSI images**: the Proxmox cloud-controller-manager runs v0.16.1 and the Proxmox CCM and CSI driver images are pinned by digest. The v0.15.0 tag was re-pushed upstream to a build that panics on start, which left every new Proxmox worker with the uninitialized taint ([**@themoriarti**](https://github.com/themoriarti) in #4917, backport #5246).
|
||||
* **Autoscaler capacity of KubernetesNodes pools**: on the kubevirt substrate the cluster-autoscaler capacity annotation is planned on the cores the VM actually gets, so a leading-zero `resources.cpu` such as `"010"` (decoded as YAML, eight cores) no longer yields a wrong capacity. No pool rolls ([**@themoriarti**](https://github.com/themoriarti) in #4974, backport #5246).
|
||||
* **Tenant quota admission counts extended resources**: admission now reads the parent pool's usage of extended resources and hugepages under the `requests.*` key the controller counts, so a child tenant can no longer get a share the parent has already used ([**@lexfrei**](https://github.com/lexfrei) in #5184, backport #5246).
|
||||
* **Postgres backup jobs fail as soon as the CloudNativePG Backup fails**: a Postgres `BackupJob` now fails with the recorded error instead of after 30 minutes, and reports a `WaitingForCNPGBackup` condition with the Backup's phase while it waits ([**@scooby87**](https://github.com/scooby87) in #5211, backport #5246).
|
||||
* **MariaDB backups and restores report real failures**: a `BackupJob` or `RestoreJob` whose operator Job failed now ends Failed instead of Succeeded, and a failed backup no longer creates a Ready Backup without an archive ([**@scooby87**](https://github.com/scooby87) in #5207, backport #5246).
|
||||
* **ClickHouse restores use the right archive**: a `RestoreJob` restores the archive its Backup was taken as, not the newest one. New Backups record the archive name; Backups taken before this release are restored best effort from the newest archive stamped no later than their `takenAt`, with a warning in the restore pod log. A ClickHouse `BackupJob` created under a name whose Backup still exists now fails instead of reporting that Backup as its result ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5208, backport #5246).
|
||||
* **Backup S3 endpoint changes are followed**: the backup controller re-renders the Velero BSL, the default backup strategies and the platform bucket's credentials Secret when their S3 endpoint is stale, so after the platform bucket's BucketAccess is re-granted for a new `publishing.host`, backups move to the new endpoint without forcing releases by hand ([**@scooby87**](https://github.com/scooby87) in #5230, backport #5246).
|
||||
* **Backup clients run on a dedicated image**: the ClickHouse (Altinity) and RabbitMQ backup strategy Pods now run a small `backup-http-client` image instead of the 349 MB `platform-migrations` image, so a platform migration change no longer re-digests the backup clients. Air-gapped mirrors need the new `ghcr.io/cozystack/cozystack/backup-http-client` repository ([**@androndo**](https://github.com/androndo) in #4756, backport #5246).
|
||||
* **PostgreSQL system databases**: `databases` refuses `postgres`, `template0` and `template1`, and the init job never runs `DROP DATABASE` on them, which used to fail the init job on every upgrade and leave the HelmRelease in `UpgradeFailed` ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5155, backport #5246).
|
||||
* **PostgreSQL init script is stable**: the declared databases render in a stable order, so an upgrade that leaves the databases unchanged no longer rewrites the init-script Secret ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5091, backport #5246).
|
||||
* **Redis with a single replica**: an instance with `replicas: 1` runs one Sentinel instead of three, without a Sentinel PodDisruptionBudget, so node drains are no longer blocked by it; instances with two replicas or more are unchanged ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5151, backport #5246).
|
||||
* **MariaDB operator on a custom cluster domain**: the operator uses the platform's `networking.clusterDomain` instead of a hard-coded `cozy.local`, so replication and User/Database/Grant reconciliation work on clusters with a non-default domain ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5123, backport #5246).
|
||||
* **tcp-balancer no longer OOMs**: HAProxy takes its fd and thread caps from the replica's own memory and CPU limits instead of the node, so a `t1.nano` balancer is no longer OOMKilled on nodes with many CPUs ([**@yankawai**](https://github.com/yankawai) in #5005, backport #5246).
|
||||
* **etcd alert noise**: `etcdHighNumberOfFailedGRPCRequests` counts only the gRPC codes the etcd mixin counts, so the Canceled watch streams of a stopping Kubernetes API server no longer fire it on every member ([**@yankawai**](https://github.com/yankawai) in #4985, backport #5246).
|
||||
* **Cilium IPAM hook on larger clusters**: the hook passes the CiliumNode list to `jq` on stdin and no longer fails with "Argument list too long" on clusters with more than a few nodes, which blocked the cilium upgrade and every package depending on networking ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5212, backport #5246).
|
||||
* **OIDC bootstrap Job with many users**: the Job no longer fails to start for a tenant cluster with about 240 or more `oidc.users`; the bindings now come from a ConfigMap the Job mounts ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5213, backport #5246).
|
||||
* **Dashboard tenant list links to the tenant page**: the tenant name in the tenant list opens its detail page, where Delete lives, and top-level tenants get their Edit button back ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #4004, backport #5246).
|
||||
* **Dead `registries` platform value removed**: the platform chart no longer has the `registries` value or the `patch-containerd` Secret, which nothing read. Mirrors for tenant workers are the per-pool `talos.registryMirrors` of `KubernetesNodes` ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5190, backport #5246).
|
||||
|
||||
## Security
|
||||
|
||||
* **Marketplace digest pinning and signature verification**: see the marketplace entry under Fixes (#5082).
|
||||
|
||||
## Development, Testing, and CI/CD
|
||||
|
||||
* **Chainsaw asserts survive transient API errors**: an e2e assert keeps polling through an error such as an etcd leader change instead of failing the suite early ([**@lexfrei**](https://github.com/lexfrei) in #4867, backport #5246).
|
||||
* **Proxmox e2e hygiene**: the Proxmox tenant kubeconfig is kept per run and removed afterwards, the HelmRelease guard reads the `KubernetesNodes` pool release, and the leak allowlist is validated against every suite layout ([**@themoriarti**](https://github.com/themoriarti) in #4971, #4970 and #4972, backports #5246).
|
||||
* **Frozen-line builds start with `GITHUB_TOKEN`**: the initial packages build dispatched when a release line is frozen now runs ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #4888, backport #5246).
|
||||
* **Unit tests after the release image restamp**: two unit tests that read fields the release build rewrites stay green on `release-1.7`; this is a release-branch-only fix ([**@themoriarti**](https://github.com/themoriarti) in #5179).
|
||||
|
||||
## Documentation
|
||||
|
||||
* **[website] Documentation**: the v1.7 docs gain the guides that lived only in the cozystack repository, the pending v1.7 docs PRs, a DRBD note for the replicated storage class, a refreshed Chainsaw e2e guide, restored legacy URLs and a grouping of older doc versions ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in cozystack/website#763, cozystack/website#782, cozystack/website#675, cozystack/website#611, cozystack/website#775 and cozystack/website#767).
|
||||
* **[website] Blog and demo**: a full-stack observability post for v1.7, the Cozystack 1.7 announcement, a three-board Raspberry Pi 5 post, and a collapsible navbar with a closable hint on the live demo page ([**@tym83**](https://github.com/tym83) in cozystack/website#790, cozystack/website#801 and cozystack/website#802; [**@kvaps**](https://github.com/kvaps) in cozystack/website#789).
|
||||
* **[website] Pages deploy**: the deploy workflow drops the Dart Sass install ([**@kvaps**](https://github.com/kvaps) in cozystack/website#798).
|
||||
* **[ansible-cozystack] Installer v1.7.0**: tracks the Cozystack installer image at v1.7.0, released in ansible-cozystack v1.7.0 ([**@app/renovate**](https://github.com/apps/renovate) in cozystack/ansible-cozystack#89).
|
||||
|
||||
## Migration and Upgrades
|
||||
|
||||
Platform migrations do not change in this release: `migrations.targetVersion` stays at 61.
|
||||
|
||||
* **Remove system databases from PostgreSQL `databases` before upgrading (#5155).** A release that declares `postgres`, `template0` or `template1` under `databases` is refused at render; remove the entry first. A system database a previous version took over is handed back to the superuser on the next upgrade: the event trigger is removed, ownership and the `public` schema are restored, and its `<db>_admin` and `<db>_readonly` roles are dropped.
|
||||
* **Tenant quota updates can be rejected after upgrading (#5184).** Admission now counts the parent's usage of extended resources and hugepages, so an update to an existing tenant can be rejected when its quota for these resources exceeds the parent's remaining budget, including an update that leaves the quota unchanged.
|
||||
* **Air-gapped mirrors need `backup-http-client` (#4756).** Mirror `ghcr.io/cozystack/cozystack/backup-http-client` before upgrading, or ClickHouse and RabbitMQ backup Pods cannot pull their image.
|
||||
* **tcp-balancer pods roll once (#5005).** The new HAProxy `global` section changes the rendered config; `maxconn` follows the memory limit at about 16 connections per MiB (2035 on `t1.nano`).
|
||||
* **Redis scaled to one replica loses its Sentinel PDB (#5151).** A post-upgrade hook deletes the Sentinel PodDisruptionBudget the operator created earlier for an instance with `replicas: 1`; scaling an instance between 1 and 2 replicas scales its Sentinel Deployment between 1 and 3.
|
||||
* **Backups created by failed MariaDB runs are not cleaned up (#5207).** Backups made by failed runs before this fix are not removed; restoring from one can fail or select another dump from the shared prefix.
|
||||
* **Keys lost to the COSI grant bug stay lost (#5284).** Re-add the affected user to the Bucket as described in the Fixes entry.
|
||||
* **`registries` in the platform Package is ignored (#5190).** A Package that still sets it keeps installing; the `patch-containerd` Secret in `cozy-system` is deleted by the release.
|
||||
* **SecurityGroups now reach Kubernetes workers (#5124).** A SecurityGroup attached to a `Kubernetes` application starts applying to its worker nodes after the upgrade, so review attached groups before upgrading.
|
||||
|
||||
## Contributors
|
||||
|
||||
We'd like to thank all contributors who made this release possible:
|
||||
|
||||
* [**@androndo**](https://github.com/androndo)
|
||||
* [**@IvanHunters**](https://github.com/IvanHunters)
|
||||
* [**@kvaps**](https://github.com/kvaps)
|
||||
* [**@lexfrei**](https://github.com/lexfrei)
|
||||
* [**@mattia-eleuteri**](https://github.com/mattia-eleuteri)
|
||||
* [**@myasnikovdaniil**](https://github.com/myasnikovdaniil)
|
||||
* [**@scooby87**](https://github.com/scooby87)
|
||||
* [**@themoriarti**](https://github.com/themoriarti)
|
||||
* [**@tym83**](https://github.com/tym83)
|
||||
* [**@yankawai**](https://github.com/yankawai)
|
||||
|
||||
**Full Changelog**: https://github.com/cozystack/cozystack/compare/v1.7.0...v1.7.1
|
||||
@@ -9,6 +9,10 @@ load test_helper
|
||||
# scope: they are re-vendored by `make update`, so a bad severity there is
|
||||
# fixed upstream or in patches/, not by editing the label.
|
||||
ALERT_SEVERITY_ALLOWED=" security critical major minor warning indeterminate informational normal ok cleared debug trace unknown "
|
||||
# The alerts that exist only to be dropped, the one place `none` is right.
|
||||
# InfoInhibitor is the source side of an Alertmanager inhibit rule, which the
|
||||
# platform config does not have, so delivering it at any severity is noise.
|
||||
ALERT_SEVERITY_BLACKHOLED=" InfoInhibitor "
|
||||
|
||||
helm_actions_stripped() {
|
||||
awk '
|
||||
@@ -104,6 +108,14 @@ alert_severity_contract() {
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ "$severity" = "none" ]; then
|
||||
for blackholed in $ALERT_SEVERITY_BLACKHOLED; do
|
||||
if [ "$blackholed" = "$alert" ]; then
|
||||
accepted=1
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
if [ "$accepted" -eq 0 ]; then
|
||||
echo "$file: $alert -> '$severity'" >&2
|
||||
bad=$((bad + 1))
|
||||
@@ -128,6 +140,83 @@ alert_severity_contract() {
|
||||
alert_severity_contract
|
||||
}
|
||||
|
||||
@test "InfoInhibitor is labelled for the blackhole route" {
|
||||
rows=$(mktemp) || return 1
|
||||
if ! alert_severity_rows > "$rows"; then
|
||||
rm -f "$rows"
|
||||
return 1
|
||||
fi
|
||||
tab=$(printf '\t')
|
||||
found=$(grep -c "${tab}InfoInhibitor${tab}" "$rows" || true)
|
||||
delivered=$(grep "${tab}InfoInhibitor${tab}" "$rows" | grep -vc "${tab}none\$" || true)
|
||||
rm -f "$rows"
|
||||
[ "$found" -gt 0 ]
|
||||
[ "$delivered" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "severity none passes only on an alert that exists to be dropped" {
|
||||
fixture=$(mktemp -d)
|
||||
mkdir -p "$fixture/packages/system/example/alerts"
|
||||
printf '%s\n' \
|
||||
'apiVersion: operator.victoriametrics.com/v1beta1' \
|
||||
'kind: VMRule' \
|
||||
'spec:' \
|
||||
' groups:' \
|
||||
' - name: general' \
|
||||
' rules:' \
|
||||
' - alert: InfoInhibitor' \
|
||||
' labels:' \
|
||||
' severity: none' \
|
||||
> "$fixture/packages/system/example/alerts/00-inhibitor.yaml"
|
||||
git -C "$fixture" init -q
|
||||
git -C "$fixture" add packages/system/example/alerts/00-inhibitor.yaml
|
||||
|
||||
inhibitor_succeeded=0
|
||||
ALERT_SEVERITY_ROOT="$fixture" alert_severity_contract >/dev/null 2>&1 || inhibitor_succeeded=$?
|
||||
|
||||
printf '%s\n' \
|
||||
'apiVersion: operator.victoriametrics.com/v1beta1' \
|
||||
'kind: VMRule' \
|
||||
'spec:' \
|
||||
' groups:' \
|
||||
' - name: silent' \
|
||||
' rules:' \
|
||||
' - alert: SilentAlert' \
|
||||
' labels:' \
|
||||
' severity: none' \
|
||||
> "$fixture/packages/system/example/alerts/01-silent.yaml"
|
||||
git -C "$fixture" add packages/system/example/alerts/01-silent.yaml
|
||||
|
||||
silent_succeeded=0
|
||||
ALERT_SEVERITY_ROOT="$fixture" alert_severity_contract >/dev/null 2>&1 || silent_succeeded=$?
|
||||
rm -rf "$fixture"
|
||||
[ "$inhibitor_succeeded" -eq 0 ]
|
||||
[ "$silent_succeeded" -ne 0 ]
|
||||
}
|
||||
|
||||
@test "an alert that exists to be dropped still fails on a refused severity" {
|
||||
fixture=$(mktemp -d)
|
||||
mkdir -p "$fixture/packages/system/example/alerts"
|
||||
printf '%s\n' \
|
||||
'apiVersion: operator.victoriametrics.com/v1beta1' \
|
||||
'kind: VMRule' \
|
||||
'spec:' \
|
||||
' groups:' \
|
||||
' - name: general' \
|
||||
' rules:' \
|
||||
' - alert: InfoInhibitor' \
|
||||
' labels:' \
|
||||
' severity: info' \
|
||||
> "$fixture/packages/system/example/alerts/00-inhibitor.yaml"
|
||||
git -C "$fixture" init -q
|
||||
git -C "$fixture" add packages/system/example/alerts/00-inhibitor.yaml
|
||||
|
||||
contract_succeeded=0
|
||||
ALERT_SEVERITY_ROOT="$fixture" alert_severity_contract >/dev/null 2>&1 || contract_succeeded=$?
|
||||
rm -rf "$fixture"
|
||||
[ "$contract_succeeded" -ne 0 ]
|
||||
}
|
||||
|
||||
@test "a parser failure cannot leave the contract green" {
|
||||
fixture=$(mktemp -d)
|
||||
mkdir -p "$fixture/packages/system/example/alerts"
|
||||
|
||||
@@ -631,7 +631,7 @@ func (r *BackupJobReconciler) createAltinityBackupArtifact(
|
||||
// used name meets the earlier run's Backup here. Only one recording
|
||||
// this run's archive was created by an earlier reconcile of this run.
|
||||
if got := existing.Spec.DriverMetadata[altinityArchiveMetaKey]; got != archive {
|
||||
return nil, fmt.Errorf("Backup %s/%s already exists and records archive %q, not %q that this run wrote",
|
||||
return nil, fmt.Errorf("existing Backup %s/%s records archive %q, not %q that this run wrote",
|
||||
backup.Namespace, backup.Name, got, archive)
|
||||
}
|
||||
return existing, nil
|
||||
|
||||
@@ -222,13 +222,13 @@ func cnpgListStub(prefix string, seen func(*http.Request)) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
if _, ok := req.URL.Query()["location"]; ok {
|
||||
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"></LocationConstraint>`)
|
||||
_, _ = fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"></LocationConstraint>`)
|
||||
return
|
||||
}
|
||||
if seen != nil {
|
||||
seen(req)
|
||||
}
|
||||
fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8"?>
|
||||
_, _ = fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<Name>bkt</Name><Prefix>%[1]s</Prefix><KeyCount>2</KeyCount><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated>
|
||||
<Contents><Key>%[1]sbackup.info</Key><Size>7</Size><ETag>"a"</ETag><LastModified>2026-10-21T10:01:00.000Z</LastModified></Contents>
|
||||
|
||||
@@ -2718,9 +2718,12 @@ func buildBarmanPlugin(objectStoreName, serverName string) cnpgtypes.PluginConfi
|
||||
// high-water mark on a 38 MB database is 254 MiB, so 128Mi cannot hold it. A
|
||||
// tenant that leaves resourceQuotas empty has no LimitRange, and there the
|
||||
// sidecar had no requests and no limit at all. 256Mi holds the measured working
|
||||
// set and 1Gi is four times the measurement. No CPU limit is set, matching the
|
||||
// entityOperator precedent in packages/apps/kafka: a throttled sidecar stalls
|
||||
// WAL archiving instead of failing it.
|
||||
// set and 1Gi is four times the measurement. The CPU limit is explicit for the
|
||||
// same reason: left unset, that LimitRange gives the sidecar its 250m default,
|
||||
// so gzip compression of full backups and WAL segments runs on a quarter of a
|
||||
// core (a 1.6 GB database took 30 minutes to back up). One core keeps the
|
||||
// sidecar from starving the instance while removing that ceiling; it counts
|
||||
// against the tenant's limits.cpu budget, one core per instance pod.
|
||||
//
|
||||
// Checksum: since botocore ~1.36 the default (when_supported) attaches a
|
||||
// flexible checksum to every PutObject, which non-AWS S3-compatible backends
|
||||
@@ -2740,6 +2743,7 @@ func barmanSidecarConfiguration() *cnpgtypes.InstanceSidecarConfiguration {
|
||||
corev1.ResourceMemory: resource.MustParse("256Mi"),
|
||||
},
|
||||
Limits: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("1"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -2156,8 +2156,11 @@ func TestApplyClusterPluginBackup_PatchesExistingCluster(t *testing.T) {
|
||||
sc.Resources.Requests.Cpu().MilliValue() != 100 {
|
||||
t.Errorf("ObjectStore leaves Barman subject to undersized tenant resource defaults: %+v", sc.Resources)
|
||||
}
|
||||
if _, ok := sc.Resources.Limits[corev1.ResourceCPU]; ok {
|
||||
t.Errorf("ObjectStore caps sidecar CPU, which throttles WAL archiving instead of failing it: %+v", sc.Resources.Limits)
|
||||
// Without an explicit CPU limit the tenant LimitRange caps the sidecar at
|
||||
// its 250m default, and every backup and WAL upload compresses on a
|
||||
// quarter of a core.
|
||||
if sc.Resources.Limits.Cpu().MilliValue() != 1000 {
|
||||
t.Errorf("ObjectStore leaves the sidecar CPU limit to the tenant LimitRange default: %+v", sc.Resources.Limits)
|
||||
}
|
||||
// The ObjectStore must be owner-referenced to the Cluster so Kubernetes GC
|
||||
// removes it when the Cluster is deleted (no orphan in the platform flow,
|
||||
@@ -2830,7 +2833,7 @@ func TestReconcileCNPG_ObjectStoreApplyRecovers(t *testing.T) {
|
||||
}
|
||||
|
||||
failing = false
|
||||
j, _ = f.pass()
|
||||
_, _ = f.pass()
|
||||
f.settleDestination()
|
||||
j, backups := f.pass()
|
||||
if len(backups) != 1 {
|
||||
|
||||
@@ -134,14 +134,14 @@ var etcdClusterGVR = schema.GroupVersionResource{
|
||||
// the wrong CRD via the typed client.
|
||||
func validateEtcdApplicationRef(ref corev1.TypedLocalObjectReference) error {
|
||||
if ref.Kind != etcdAppKind {
|
||||
return fmt.Errorf("Etcd strategy supports applicationRef.kind=%q, got %q", etcdAppKind, ref.Kind)
|
||||
return fmt.Errorf("the Etcd strategy supports applicationRef.kind=%q, got %q", etcdAppKind, ref.Kind)
|
||||
}
|
||||
apiGroup := ""
|
||||
if ref.APIGroup != nil {
|
||||
apiGroup = *ref.APIGroup
|
||||
}
|
||||
if apiGroup != "" && apiGroup != etcdapp.GroupName {
|
||||
return fmt.Errorf("Etcd strategy supports applicationRef.apiGroup=%q, got %q", etcdapp.GroupName, apiGroup)
|
||||
return fmt.Errorf("the Etcd strategy supports applicationRef.apiGroup=%q, got %q", etcdapp.GroupName, apiGroup)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -104,10 +104,10 @@ func kafkaClusterName(appName string) string {
|
||||
// apps.cozystack.io/Kafka application.
|
||||
func validateKafkaApplicationRef(ref corev1.TypedLocalObjectReference) error {
|
||||
if ref.Kind != kafkaApplicationKind {
|
||||
return fmt.Errorf("Kafka strategy only supports applicationRef.kind=%q, got %q", kafkaApplicationKind, ref.Kind)
|
||||
return fmt.Errorf("the Kafka strategy only supports applicationRef.kind=%q, got %q", kafkaApplicationKind, ref.Kind)
|
||||
}
|
||||
if ref.APIGroup != nil && *ref.APIGroup != "" && *ref.APIGroup != backupsv1alpha1.DefaultApplicationAPIGroup {
|
||||
return fmt.Errorf("Kafka strategy only supports applicationRef.apiGroup=%q, got %q", backupsv1alpha1.DefaultApplicationAPIGroup, *ref.APIGroup)
|
||||
return fmt.Errorf("the Kafka strategy only supports applicationRef.apiGroup=%q, got %q", backupsv1alpha1.DefaultApplicationAPIGroup, *ref.APIGroup)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -528,7 +528,7 @@ func (r *BackupJobReconciler) createKafkaBackupArtifact(
|
||||
// RabbitMQ driver's guard).
|
||||
if existing.Spec.ApplicationRef.Kind != backup.Spec.ApplicationRef.Kind ||
|
||||
existing.Spec.ApplicationRef.Name != backup.Spec.ApplicationRef.Name {
|
||||
return nil, fmt.Errorf("Backup %s/%s already exists for a different application (%s/%s, not %s/%s)",
|
||||
return nil, fmt.Errorf("existing Backup %s/%s belongs to a different application (%s/%s, not %s/%s)",
|
||||
backup.Namespace, backup.Name,
|
||||
existing.Spec.ApplicationRef.Kind, existing.Spec.ApplicationRef.Name,
|
||||
backup.Spec.ApplicationRef.Kind, backup.Spec.ApplicationRef.Name)
|
||||
|
||||
@@ -464,7 +464,7 @@ func TestMariaDBArtifactSizeBytes_MinioClient(t *testing.T) {
|
||||
gotPath, gotPrefix, gotAuth = req.URL.Path, req.URL.Query().Get("prefix"), req.Header.Get("Authorization")
|
||||
gotDelimiter = req.URL.Query().Get("delimiter")
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8"?>
|
||||
_, _ = fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<Name>bkt</Name><Prefix>tenant/db/</Prefix><KeyCount>1</KeyCount><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated>
|
||||
<Contents><Key>%s</Key><Size>31337</Size><ETag>"x"</ETag><LastModified>2026-10-21T10:01:00.000Z</LastModified></Contents>
|
||||
|
||||
@@ -30,8 +30,6 @@ import (
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const (
|
||||
mariadbFieldManager = "cozystack-mariadb-backup-driver"
|
||||
|
||||
// mariadb-operator's MariaDB ApplicationDefinition (release.prefix). The
|
||||
// cozystack mariadb-rd renders the HelmRelease with releaseName =
|
||||
// "mariadb-" + appName (see packages/system/mariadb-rd/cozyrds/mariadb.yaml,
|
||||
|
||||
@@ -528,8 +528,8 @@ func (r *BackupJobReconciler) reconcileMongoDB(ctx context.Context, j *backupsv1
|
||||
}
|
||||
}
|
||||
|
||||
switch state := mdbBackup.Status.State; {
|
||||
case state == psmdbtypes.StateReady:
|
||||
switch state := mdbBackup.Status.State; state {
|
||||
case psmdbtypes.StateReady:
|
||||
// Backup completed successfully. Materialise the Cozystack artifact and
|
||||
// finalise the BackupJob. Idempotent: reuse the existing artifact if a
|
||||
// previous reconcile created it and then raced on the status update.
|
||||
@@ -570,7 +570,7 @@ func (r *BackupJobReconciler) reconcileMongoDB(ctx context.Context, j *backupsv1
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
|
||||
case state == psmdbtypes.StateError || state == psmdbtypes.StateRejected:
|
||||
case psmdbtypes.StateError, psmdbtypes.StateRejected:
|
||||
message := mdbBackup.Status.Error
|
||||
// The residual half of the injection race: on the useSystemBucket flow the
|
||||
// operator can latch state=error because it resolved the injected storage
|
||||
@@ -1740,8 +1740,8 @@ func (r *RestoreJobReconciler) reconcileMongoDBRestore(ctx context.Context, rest
|
||||
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf("failed to ensure psmdb.percona.com/PerconaServerMongoDBRestore: %v", err))
|
||||
}
|
||||
|
||||
switch state := mdbRestore.Status.State; {
|
||||
case state == psmdbtypes.StateReady:
|
||||
switch state := mdbRestore.Status.State; state {
|
||||
case psmdbtypes.StateReady:
|
||||
now := metav1.Now()
|
||||
restoreJob.Status.CompletedAt = &now
|
||||
restoreJob.Status.Phase = backupsv1alpha1.RestoreJobPhaseSucceeded
|
||||
@@ -1756,7 +1756,7 @@ func (r *RestoreJobReconciler) reconcileMongoDBRestore(ctx context.Context, rest
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
|
||||
case state == psmdbtypes.StateError || state == psmdbtypes.StateRejected:
|
||||
case psmdbtypes.StateError, psmdbtypes.StateRejected:
|
||||
message := mdbRestore.Status.Error
|
||||
if message == "" {
|
||||
message = fmt.Sprintf("psmdb.percona.com/PerconaServerMongoDBRestore %s/%s reported state=%s", mdbRestore.Namespace, mdbRestore.Name, state)
|
||||
@@ -1855,7 +1855,7 @@ func (r *RestoreJobReconciler) resolveMongoDBBackupSource(ctx context.Context, b
|
||||
}
|
||||
if destination == "" {
|
||||
return nil, fmt.Errorf(
|
||||
"Backup has no restorable destination (the operator PerconaServerMongoDBBackup was reaped and no snapshot destination is persisted); re-run the BackupJob")
|
||||
"no restorable destination for the Backup (the operator PerconaServerMongoDBBackup was reaped and no snapshot destination is persisted); re-run the BackupJob")
|
||||
}
|
||||
src := &psmdbtypes.BackupSource{
|
||||
Type: psmdbtypes.BackupTypeLogical,
|
||||
|
||||
@@ -513,7 +513,7 @@ func (r *BackupJobReconciler) createRabbitmqBackupArtifact(
|
||||
// this run's success while describing something else.
|
||||
if existing.Spec.ApplicationRef.Kind != backup.Spec.ApplicationRef.Kind ||
|
||||
existing.Spec.ApplicationRef.Name != backup.Spec.ApplicationRef.Name {
|
||||
return nil, fmt.Errorf("Backup %s/%s already exists for a different application (%s/%s, not %s/%s)",
|
||||
return nil, fmt.Errorf("existing Backup %s/%s belongs to a different application (%s/%s, not %s/%s)",
|
||||
backup.Namespace, backup.Name,
|
||||
existing.Spec.ApplicationRef.Kind, existing.Spec.ApplicationRef.Name,
|
||||
backup.Spec.ApplicationRef.Kind, backup.Spec.ApplicationRef.Name)
|
||||
|
||||
@@ -65,12 +65,14 @@ func (s captureSink) WithName(string) logr.LogSink { return s }
|
||||
func TestRunThenDrain_FencesLeftBehindAreAnErrorNamingThem(t *testing.T) {
|
||||
logger, entries := newCaptureLogger()
|
||||
ctx := log.IntoContext(context.Background(), logger)
|
||||
RunThenDrain(ctx,
|
||||
if err := RunThenDrain(ctx,
|
||||
func(context.Context) error { return nil },
|
||||
func(context.Context) (int, error) {
|
||||
return 1, errors.Join(context.DeadlineExceeded, errors.New("still fenced: tenant-root/vm-disk-0"))
|
||||
},
|
||||
time.Minute)
|
||||
time.Minute); err != nil {
|
||||
t.Fatalf("RunThenDrain = %v, want the run's nil error", err)
|
||||
}
|
||||
for _, e := range *entries {
|
||||
if e.err != nil && strings.Contains(e.err.Error(), "tenant-root/vm-disk-0") {
|
||||
return
|
||||
@@ -84,10 +86,12 @@ func TestRunThenDrain_FencesLeftBehindAreAnErrorNamingThem(t *testing.T) {
|
||||
func TestRunThenDrain_AnUnansweredListingIsNotACountOfMinusOne(t *testing.T) {
|
||||
logger, entries := newCaptureLogger()
|
||||
ctx := log.IntoContext(context.Background(), logger)
|
||||
RunThenDrain(ctx,
|
||||
if err := RunThenDrain(ctx,
|
||||
func(context.Context) error { return nil },
|
||||
func(context.Context) (int, error) { return -1, context.DeadlineExceeded },
|
||||
time.Minute)
|
||||
time.Minute); err != nil {
|
||||
t.Fatalf("RunThenDrain = %v, want the run's nil error", err)
|
||||
}
|
||||
found := false
|
||||
for _, e := range *entries {
|
||||
for i := 0; i+1 < len(e.kv); i += 2 {
|
||||
@@ -108,10 +112,12 @@ func TestRunThenDrain_AnUnansweredListingIsNotACountOfMinusOne(t *testing.T) {
|
||||
func TestRunThenDrain_ACompleteDrainSaysSo(t *testing.T) {
|
||||
logger, entries := newCaptureLogger()
|
||||
ctx := log.IntoContext(context.Background(), logger)
|
||||
RunThenDrain(ctx,
|
||||
if err := RunThenDrain(ctx,
|
||||
func(context.Context) error { return nil },
|
||||
func(context.Context) (int, error) { return 0, nil },
|
||||
time.Minute)
|
||||
time.Minute); err != nil {
|
||||
t.Fatalf("RunThenDrain = %v, want the run's nil error", err)
|
||||
}
|
||||
for _, e := range *entries {
|
||||
if e.err == nil && strings.Contains(e.msg, "Released every uninstall fence") {
|
||||
return
|
||||
|
||||
@@ -486,11 +486,12 @@ func ownsSuspensionManagedFields(hr *helmv2.HelmRelease) bool {
|
||||
// fieldsManageSuspend reports whether a managed-fields set includes spec.suspend,
|
||||
// encoded by the apiserver as {"f:spec":{"f:suspend":{}}}.
|
||||
func fieldsManageSuspend(fs *metav1.FieldsV1) bool {
|
||||
if fs == nil || len(fs.Raw) == 0 {
|
||||
raw := fs.GetRawBytes()
|
||||
if len(raw) == 0 {
|
||||
return false
|
||||
}
|
||||
var top map[string]json.RawMessage
|
||||
if err := json.Unmarshal(fs.Raw, &top); err != nil {
|
||||
if err := json.Unmarshal(raw, &top); err != nil {
|
||||
return false
|
||||
}
|
||||
specRaw, ok := top["f:spec"]
|
||||
|
||||
@@ -83,7 +83,7 @@ func suspendManagedFields(manager string, op metav1.ManagedFieldsOperationType)
|
||||
Operation: op,
|
||||
APIVersion: helmv2.GroupVersion.String(),
|
||||
FieldsType: "FieldsV1",
|
||||
FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:suspend":{}}}`)},
|
||||
FieldsV1: metav1.NewFieldsV1(`{"f:spec":{"f:suspend":{}}}`),
|
||||
}}
|
||||
}
|
||||
|
||||
@@ -286,7 +286,7 @@ func TestOwnsSuspensionManagedFields(t *testing.T) {
|
||||
{"no managed fields", nil, false},
|
||||
{"plunger apply of other fields", []metav1.ManagedFieldsEntry{{
|
||||
Manager: suspendFieldManager, Operation: metav1.ManagedFieldsOperationApply,
|
||||
FieldsType: "FieldsV1", FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:chart":{}}}`)},
|
||||
FieldsType: "FieldsV1", FieldsV1: metav1.NewFieldsV1(`{"f:spec":{"f:chart":{}}}`),
|
||||
}}, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
|
||||
@@ -3,8 +3,10 @@ package kubeovnplunger
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -153,12 +155,9 @@ func (r *KubeOVNPlunger) SetupWithManager(mgr ctrl.Manager, kubeOVNNamespace, ap
|
||||
Complete(r); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = mgr.Add(manager.RunnableFunc(func(ctx context.Context) error {
|
||||
go srv.ListenAndServe()
|
||||
<-ctx.Done()
|
||||
_ = srv.Shutdown(context.Background())
|
||||
return nil
|
||||
}))
|
||||
if err := mgr.Add(serveStatus(srv)); err != nil {
|
||||
return err
|
||||
}
|
||||
return mgr.Add(manager.RunnableFunc(func(ctx context.Context) error {
|
||||
ticker := time.NewTicker(rescanInterval)
|
||||
defer ticker.Stop()
|
||||
@@ -180,6 +179,25 @@ func (r *KubeOVNPlunger) SetupWithManager(mgr ctrl.Manager, kubeOVNNamespace, ap
|
||||
}))
|
||||
}
|
||||
|
||||
// serveStatus runs the status stream for as long as the manager does. A server
|
||||
// that cannot start fails the manager, as the metrics server does.
|
||||
func serveStatus(s *sse.Server) manager.RunnableFunc {
|
||||
return func(ctx context.Context) error {
|
||||
served := make(chan error, 1)
|
||||
go func() { served <- s.ListenAndServe() }()
|
||||
select {
|
||||
case err := <-served:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
}
|
||||
_ = s.Shutdown(context.Background())
|
||||
if err := <-served; !errors.Is(err, http.ErrServerClosed) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type ExecOptions struct {
|
||||
Namespace string
|
||||
Pod string
|
||||
|
||||
@@ -219,7 +219,7 @@ func (r *KubeOVNPlunger) WriteClusterMetrics(db string, snaps []ovnstatus.Health
|
||||
r.metrics.allAgree.WithLabelValues(db, cid).Set(b2f(ecv.AllAgree))
|
||||
r.metrics.membersExpected.WithLabelValues(db, cid).Set(float64(expectedReplicas))
|
||||
r.metrics.membersObserved.WithLabelValues(db, cid).Set(float64(ecv.MembersCount))
|
||||
r.metrics.ipsExpected.WithLabelValues(db, cid).Set(float64(len(ecv.ConsensusResult.TruthView.Members))) // optional; or len(hints.ExpectedIPs)
|
||||
r.metrics.ipsExpected.WithLabelValues(db, cid).Set(float64(len(ecv.TruthView.Members))) // optional; or len(hints.ExpectedIPs)
|
||||
r.metrics.ipsObserved.WithLabelValues(db, cid).Set(float64(ecv.DistinctIPCount))
|
||||
r.metrics.excessMembers.WithLabelValues(db, cid).Set(float64(ecv.ExpectedExcess))
|
||||
r.metrics.missingMembers.WithLabelValues(db, cid).Set(float64(ecv.ExpectedShortfall))
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package kubeovnplunger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cozystack/cozystack/internal/sse"
|
||||
)
|
||||
|
||||
func runStatusServer(ctx context.Context, t *testing.T, addr string) (bool, error) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
t.Cleanup(cancel)
|
||||
s := sse.New(sse.Options{Addr: addr, Logger: log.New(io.Discard, "", 0)})
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- serveStatus(s)(ctx) }()
|
||||
select {
|
||||
case err := <-done:
|
||||
return true, err
|
||||
case <-time.After(2 * time.Second):
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// A status server that cannot listen fails its runnable, and with it the
|
||||
// manager, rather than leaving the plunger running without it.
|
||||
func TestStatusServerReportsListenFailure(t *testing.T) {
|
||||
taken, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = taken.Close() })
|
||||
returned, err := runStatusServer(context.Background(), t, taken.Addr().String())
|
||||
if !returned || err == nil {
|
||||
t.Fatalf("serveStatus on an occupied address = %v (returned %v), want an error", err, returned)
|
||||
}
|
||||
}
|
||||
|
||||
// Stopping the manager stops the server, and that is not an error.
|
||||
func TestStatusServerStopsWithoutError(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
time.AfterFunc(100*time.Millisecond, cancel)
|
||||
returned, err := runStatusServer(ctx, t, "127.0.0.1:0")
|
||||
if !returned || err != nil {
|
||||
t.Fatalf("serveStatus after cancel = %v (returned %v), want nil", err, returned)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -15,10 +14,6 @@ import (
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
)
|
||||
|
||||
const (
|
||||
deletionRequeueDelay = 30 * time.Second
|
||||
)
|
||||
|
||||
// WorkloadMonitorReconciler reconciles a WorkloadMonitor object
|
||||
type WorkloadReconciler struct {
|
||||
client.Client
|
||||
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
"k8s.io/client-go/tools/record"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"k8s.io/client-go/util/workqueue"
|
||||
"k8s.io/utils/pointer"
|
||||
"k8s.io/utils/ptr"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/cache"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
@@ -423,8 +423,8 @@ func updateOwnerReferences(obj metav1.Object, monitor client.Object) {
|
||||
Name: monitor.GetName(),
|
||||
UID: monitor.GetUID(),
|
||||
// Set Controller to false to avoid conflict as multiple controllers are not allowed
|
||||
Controller: pointer.BoolPtr(false),
|
||||
BlockOwnerDeletion: pointer.BoolPtr(true),
|
||||
Controller: ptr.To(false),
|
||||
BlockOwnerDeletion: ptr.To(true),
|
||||
}
|
||||
owners = append(owners, newOwnerRef)
|
||||
}
|
||||
@@ -525,10 +525,10 @@ func (r *WorkloadMonitorReconciler) queryAllBucketMetrics(ctx context.Context, p
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("querying Prometheus: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("Prometheus returned status %d", resp.StatusCode)
|
||||
return nil, fmt.Errorf("unexpected status %d from Prometheus", resp.StatusCode)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
@@ -549,7 +549,7 @@ func (r *WorkloadMonitorReconciler) queryAllBucketMetrics(ctx context.Context, p
|
||||
return nil, fmt.Errorf("parsing Prometheus response: %w", err)
|
||||
}
|
||||
if promResp.Status != "success" {
|
||||
return nil, fmt.Errorf("Prometheus query finished with status %q", promResp.Status)
|
||||
return nil, fmt.Errorf("the Prometheus query finished with status %q", promResp.Status)
|
||||
}
|
||||
|
||||
for _, r := range promResp.Data.Result {
|
||||
@@ -1174,9 +1174,9 @@ func (r *WorkloadMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
|
||||
// Default to operational = true, but check MinReplicas if set.
|
||||
// Use fresh.Spec to avoid making decisions based on a stale cached copy
|
||||
// when the spec was updated between the initial read and this retry.
|
||||
fresh.Status.Operational = pointer.Bool(true)
|
||||
fresh.Status.Operational = ptr.To(true)
|
||||
if fresh.Spec.MinReplicas != nil && availableReplicas < *fresh.Spec.MinReplicas {
|
||||
fresh.Status.Operational = pointer.Bool(false)
|
||||
fresh.Status.Operational = ptr.To(false)
|
||||
}
|
||||
// Only the DataVolume check writes Message and Reason, so the stored ones
|
||||
// are the last DataVolume verdict: kept while no reader is installed yet, so a
|
||||
@@ -1189,7 +1189,7 @@ func (r *WorkloadMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
|
||||
}
|
||||
}
|
||||
if fresh.Status.Message != "" {
|
||||
fresh.Status.Operational = pointer.Bool(false)
|
||||
fresh.Status.Operational = ptr.To(false)
|
||||
}
|
||||
if equality.Semantic.DeepEqual(stored, &fresh.Status) {
|
||||
return nil
|
||||
|
||||
@@ -870,7 +870,7 @@ func TestReconcileNoBucketClaimSkips(t *testing.T) {
|
||||
|
||||
func TestQueryAllBucketMetrics(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[
|
||||
_, _ = fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[
|
||||
{"metric":{"__name__":"SeaweedFS_s3_bucket_size_bytes","bucket":"bucket-aaa"},"value":[1713000000,"10485864"]},
|
||||
{"metric":{"__name__":"SeaweedFS_s3_bucket_physical_size_bytes","bucket":"bucket-aaa"},"value":[1713000000,"20971728"]},
|
||||
{"metric":{"__name__":"SeaweedFS_s3_bucket_size_bytes","bucket":"bucket-bbb"},"value":[1713000000,"0"]}
|
||||
@@ -909,7 +909,7 @@ func TestQueryAllBucketMetrics(t *testing.T) {
|
||||
|
||||
func TestQueryAllBucketMetricsEmpty(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[]}}`)
|
||||
_, _ = fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[]}}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
@@ -953,7 +953,7 @@ func TestQueryAllBucketMetricsPathPrefixAndUserinfo(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
gotUser, gotPass, gotOK = r.BasicAuth()
|
||||
fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[]}}`)
|
||||
_, _ = fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[]}}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
@@ -1219,7 +1219,7 @@ func TestReconcileBucketMetricsFromEndpointOverride(t *testing.T) {
|
||||
if r.URL.Path != "/vm/select/0/prometheus/api/v1/query" {
|
||||
t.Errorf("unexpected query path %q", r.URL.Path)
|
||||
}
|
||||
fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[
|
||||
_, _ = fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[
|
||||
{"metric":{"__name__":"SeaweedFS_s3_bucket_size_bytes","bucket":"cosi-abc123"},"value":[1713000000,"4096"]}
|
||||
]}}`)
|
||||
}))
|
||||
@@ -1331,7 +1331,7 @@ func TestReconcileRetainsLastKnownSizesOnQueryFailure(t *testing.T) {
|
||||
|
||||
func TestReconcileRetainsLastKnownSizesWhenBucketMissingFromResult(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[]}}`)
|
||||
_, _ = fmt.Fprint(w, `{"status":"success","data":{"resultType":"vector","result":[]}}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ func Install(ctx context.Context, k8sClient client.Client, writeEmbeddedManifest
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create temp directory: %w", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
defer func() { _ = os.RemoveAll(tmpDir) }()
|
||||
|
||||
manifestsDir := filepath.Join(tmpDir, "manifests")
|
||||
if err := os.MkdirAll(manifestsDir, 0755); err != nil {
|
||||
|
||||
@@ -41,7 +41,7 @@ func Install(ctx context.Context, k8sClient client.Client, writeEmbeddedManifest
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create temp directory: %w", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
defer func() { _ = os.RemoveAll(tmpDir) }()
|
||||
|
||||
// Extract embedded manifests (generated by cozypkg)
|
||||
manifestsDir := filepath.Join(tmpDir, "manifests")
|
||||
@@ -171,7 +171,7 @@ func extractNamespace(objects []*unstructured.Unstructured) (string, error) {
|
||||
if obj.GetKind() == "Namespace" {
|
||||
namespace := obj.GetName()
|
||||
if namespace == "" {
|
||||
return "", fmt.Errorf("Namespace object has no name")
|
||||
return "", fmt.Errorf("the Namespace object has no name")
|
||||
}
|
||||
return namespace, nil
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
NoAncestors = errors.New("no managed apps found in lineage")
|
||||
AncestryAmbiguous = errors.New("object ancestry is ambiguous")
|
||||
ErrNoAncestors = errors.New("no managed apps found in lineage")
|
||||
ErrAncestryAmbiguous = errors.New("object ancestry is ambiguous")
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -115,9 +115,9 @@ func (h *LineageControllerWebhook) Handle(ctx context.Context, req admission.Req
|
||||
|
||||
owner, err := h.getOwner(ctx, obj)
|
||||
switch {
|
||||
case err != nil && errors.Is(err, AncestryAmbiguous):
|
||||
case err != nil && errors.Is(err, ErrAncestryAmbiguous):
|
||||
warn = append(warn, "object ancestry ambiguous, using first ancestor found")
|
||||
case err != nil && errors.Is(err, NoAncestors):
|
||||
case err != nil && errors.Is(err, ErrNoAncestors):
|
||||
// not a problem, mark object as unmanaged
|
||||
case err != nil:
|
||||
logger.Error(err, "error computing lineage labels")
|
||||
@@ -147,14 +147,14 @@ func (h *LineageControllerWebhook) Handle(ctx context.Context, req admission.Req
|
||||
func (h *LineageControllerWebhook) getOwner(ctx context.Context, o *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
||||
owners := lineage.WalkOwnershipGraphWithCache(ctx, h.dynClient, h.mapper, h, h.ownerCache, o)
|
||||
if len(owners) == 0 {
|
||||
return nil, NoAncestors
|
||||
return nil, ErrNoAncestors
|
||||
}
|
||||
obj, err := owners[0].GetUnstructuredCached(ctx, h.dynClient, h.mapper, h.ownerCache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(owners) > 1 {
|
||||
err = AncestryAmbiguous
|
||||
err = ErrAncestryAmbiguous
|
||||
}
|
||||
return obj, err
|
||||
}
|
||||
@@ -176,7 +176,7 @@ func (h *LineageControllerWebhook) computeLabels(ctx context.Context, obj *unstr
|
||||
return s
|
||||
}
|
||||
s = s[:63]
|
||||
for b := s[62]; !((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')); s = s[:len(s)-1] {
|
||||
for b := s[62]; (b < 'a' || b > 'z') && (b < 'A' || b > 'Z') && (b < '0' || b > '9'); s = s[:len(s)-1] {
|
||||
b = s[len(s)-1]
|
||||
}
|
||||
return s
|
||||
|
||||
@@ -146,7 +146,7 @@ func (c *inventoryClient) VMs(ctx context.Context, providerUID string) ([]invent
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("Forklift inventory returned %s listing VMs", resp.Status)
|
||||
return nil, fmt.Errorf("listing VMs: the Forklift inventory returned %s", resp.Status)
|
||||
}
|
||||
|
||||
var vms []inventoryVMRef
|
||||
@@ -195,7 +195,7 @@ func (c *inventoryClient) VM(ctx context.Context, providerUID, vmID string) (*in
|
||||
case http.StatusNotFound:
|
||||
return nil, ¬FoundError{vmID: vmID}
|
||||
default:
|
||||
return nil, fmt.Errorf("Forklift inventory returned %s for VM %q", resp.Status, vmID)
|
||||
return nil, fmt.Errorf("VM %q: the Forklift inventory returned %s", vmID, resp.Status)
|
||||
}
|
||||
|
||||
vm := &inventoryVM{}
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package observabilityproxy
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// instantDefaultStep is the step vmselect uses for an instant query without
|
||||
// one (defaultStep in app/vmselect/prometheus); it is also the implicit
|
||||
// rollup window of a bare selector.
|
||||
const instantDefaultStep = 5 * time.Minute
|
||||
|
||||
// silenceInterval is how far before a rollup or subquery window vmselect
|
||||
// starts reading (maxSilenceInterval in app/vmselect/promql: the value of
|
||||
// -search.minStalenessInterval, 5m when unset; cozystack does not set it).
|
||||
const silenceInterval = 5 * time.Minute
|
||||
|
||||
// latencyOffset is how far vmselect moves an instant query evaluated within
|
||||
// it of the current time back (-search.latencyOffset, 30s by default and
|
||||
// not set by cozystack); the proxy forwards neither nocache nor
|
||||
// latency_offset.
|
||||
const latencyOffset = 30 * time.Second
|
||||
|
||||
// queryDurationPart is the subset of the MetricsQL duration grammar
|
||||
// (scanSingleDuration in metricsql) the scan understands. Step multiples
|
||||
// (5i, $__interval), negative parts and upper-case units are left out on
|
||||
// purpose: anything else makes the bound unknown.
|
||||
var (
|
||||
queryDurationPart = regexp.MustCompile(`(\d+(?:\.\d+)?)(ms|s|m|h|d|w|y)`)
|
||||
queryDuration = regexp.MustCompile(`^(?:\d+(?:\.\d+)?(?:ms|s|m|h|d|w|y))+$`)
|
||||
querySeconds = regexp.MustCompile(`^\d+(?:\.\d+)?$`)
|
||||
)
|
||||
|
||||
var queryDurationUnits = map[string]time.Duration{
|
||||
"ms": time.Millisecond, "s": time.Second, "m": time.Minute, "h": time.Hour,
|
||||
"d": 24 * time.Hour, "w": 7 * 24 * time.Hour, "y": 365 * 24 * time.Hour,
|
||||
}
|
||||
|
||||
// queryStart is the earliest instant a query evaluated from start may read;
|
||||
// zero when the bound is unknown.
|
||||
func queryStart(query string, start time.Time, step time.Duration) time.Time {
|
||||
lookback, ok := queryLookback(query, step)
|
||||
if !ok {
|
||||
return time.Time{}
|
||||
}
|
||||
return start.Add(-lookback)
|
||||
}
|
||||
|
||||
// queryLookback is an upper bound on how far before its evaluation window
|
||||
// a query reads, from a lexical scan that never parses the query. Every
|
||||
// [...], offset and "(" counts, including ones inside quoted label values:
|
||||
// an extra hit only makes the bound larger or unknown, never smaller.
|
||||
// Windows and offsets are summed rather than maxed, which covers nesting;
|
||||
// WITH is refused because a template applies one window several times.
|
||||
// vmselect starts a rollup max(window, step) plus the silence interval
|
||||
// before the first point, a subquery window+step+silence before it and
|
||||
// aligns both down to a step. A rollup function or offset over anything
|
||||
// but a selector is an implicit subquery (getRollupExprArg), so every "("
|
||||
// is counted as one. Each step term uses the largest step seen.
|
||||
func queryLookback(query string, step time.Duration) (time.Duration, bool) {
|
||||
if strings.Contains(query, "@") {
|
||||
return 0, false
|
||||
}
|
||||
var sum float64
|
||||
subqueries := 0
|
||||
maxStep := float64(step)
|
||||
for rest := query; ; {
|
||||
open := strings.IndexByte(rest, '[')
|
||||
if open < 0 {
|
||||
break
|
||||
}
|
||||
closing := strings.IndexByte(rest[open:], ']')
|
||||
if closing < 0 {
|
||||
return 0, false
|
||||
}
|
||||
window, subStep, isSubquery := strings.Cut(rest[open+1:open+closing], ":")
|
||||
rest = rest[open+closing+1:]
|
||||
if isSubquery {
|
||||
subqueries++
|
||||
if strings.TrimSpace(window) == "" {
|
||||
window = "0s"
|
||||
}
|
||||
if strings.TrimSpace(subStep) != "" {
|
||||
d, ok := parseQueryDuration(subStep)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
maxStep = max(maxStep, d)
|
||||
}
|
||||
}
|
||||
d, ok := parseQueryDuration(window)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
sum += d
|
||||
}
|
||||
lower := strings.ToLower(query)
|
||||
for i := 0; ; {
|
||||
k := strings.Index(lower[i:], "offset")
|
||||
if k < 0 {
|
||||
break
|
||||
}
|
||||
k += i
|
||||
i = k + len("offset")
|
||||
if !isKeyword(lower, k, i) {
|
||||
continue
|
||||
}
|
||||
arg := strings.TrimLeft(lower[i:], " \t\n\r")
|
||||
negative := strings.HasPrefix(arg, "-")
|
||||
if negative {
|
||||
arg = strings.TrimLeft(arg[1:], " \t\n\r")
|
||||
}
|
||||
end := strings.IndexFunc(arg, func(r rune) bool { return !inDuration(r) })
|
||||
if end < 0 {
|
||||
end = len(arg)
|
||||
}
|
||||
d, ok := parseQueryDuration(arg[:end])
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
if !negative {
|
||||
sum += d
|
||||
}
|
||||
}
|
||||
if hasKeyword(lower, "with") {
|
||||
return 0, false
|
||||
}
|
||||
n := float64(subqueries + strings.Count(query, "("))
|
||||
sum += (2+3*n)*maxStep + (1+n)*float64(silenceInterval)
|
||||
if sum > float64(maxMetricsDuration) {
|
||||
return 0, false
|
||||
}
|
||||
return time.Duration(sum), true
|
||||
}
|
||||
|
||||
// isKeyword reports whether lower[start:end] stands on its own rather than
|
||||
// inside an identifier.
|
||||
func isKeyword(lower string, start, end int) bool {
|
||||
return (start == 0 || !continuesIdent(lower[start-1], false)) &&
|
||||
(end == len(lower) || !continuesIdent(lower[end], true))
|
||||
}
|
||||
|
||||
func inDuration(r rune) bool {
|
||||
return r >= '0' && r <= '9' || r >= 'a' && r <= 'z' || r == '.' || r == '_'
|
||||
}
|
||||
|
||||
func hasKeyword(lower, word string) bool {
|
||||
for i := 0; ; {
|
||||
k := strings.Index(lower[i:], word)
|
||||
if k < 0 {
|
||||
return false
|
||||
}
|
||||
k += i
|
||||
i = k + len(word)
|
||||
if isKeyword(lower, k, i) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// continuesIdent reports whether c, next to "offset", makes it part of an
|
||||
// identifier rather than the keyword. Digits and dots only continue an
|
||||
// identifier after it, so before it they still count as a keyword hit.
|
||||
func continuesIdent(c byte, after bool) bool {
|
||||
return c >= 'a' && c <= 'z' || c == '_' || c == ':' || c >= 0x80 ||
|
||||
after && (c >= '0' && c <= '9' || c == '.' || c == '\\')
|
||||
}
|
||||
|
||||
// parseQueryDuration returns nanoseconds as a float so that a sum of huge
|
||||
// durations cannot wrap; the caller compares it against the bound.
|
||||
func parseQueryDuration(s string) (float64, bool) {
|
||||
s = strings.TrimSpace(s)
|
||||
if querySeconds.MatchString(s) {
|
||||
f, err := strconv.ParseFloat(s, 64)
|
||||
return f * float64(time.Second), err == nil
|
||||
}
|
||||
if !queryDuration.MatchString(s) {
|
||||
return 0, false
|
||||
}
|
||||
var d float64
|
||||
for _, m := range queryDurationPart.FindAllStringSubmatch(s, -1) {
|
||||
f, err := strconv.ParseFloat(m[1], 64)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
d += f * float64(queryDurationUnits[m[2]])
|
||||
}
|
||||
return d, true
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package observabilityproxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MetricsLimits bound the window, resolution and timeout a metrics request
|
||||
// asks vmselect for. Windows and offsets inside the expression are not
|
||||
// limited here: they cannot read past the store's retention, vmselect caps
|
||||
// the series and samples one query may touch, and MaxTimeout bounds the
|
||||
// work of rollups over long overlapping windows.
|
||||
type MetricsLimits struct {
|
||||
MaxRange time.Duration
|
||||
MaxPoints int
|
||||
MaxTimeout time.Duration
|
||||
}
|
||||
|
||||
// MetricsConfig names the vmselect stores of the root tenant and the
|
||||
// retention that decides between them.
|
||||
type MetricsConfig struct {
|
||||
RootTenant string
|
||||
ShortTermStore string
|
||||
LongTermStore string
|
||||
ShortTermRetention time.Duration
|
||||
Limits MetricsLimits
|
||||
// UpstreamURL maps a store name to its Prometheus API base URL, without
|
||||
// a trailing slash; nil means the in-cluster vmselect Service.
|
||||
UpstreamURL func(store string) string
|
||||
}
|
||||
|
||||
// store picks the short-term store when everything from start on is still
|
||||
// within its retention, so a live graph never waits on the downsampled one.
|
||||
func (c MetricsConfig) store(start, now time.Time) string {
|
||||
if now.Sub(start) <= c.ShortTermRetention {
|
||||
return c.ShortTermStore
|
||||
}
|
||||
return c.LongTermStore
|
||||
}
|
||||
|
||||
// maxMetricsMatchers bounds match[]: vmselect runs one index search per
|
||||
// matcher and has no cap of its own.
|
||||
const maxMetricsMatchers = 20
|
||||
|
||||
// labelsDefaultWindow is vmselect's own default for the label and series
|
||||
// APIs (defaultStep in app/vmselect/prometheus); the proxy sends it
|
||||
// explicitly so the store choice and the range check see the same window.
|
||||
const labelsDefaultWindow = 5 * time.Minute
|
||||
|
||||
var labelName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
|
||||
|
||||
type metricsRequest struct {
|
||||
path string
|
||||
params url.Values
|
||||
// start is the earliest instant the request reads, for the store choice;
|
||||
// zero when that is unknown, which picks the long-term store.
|
||||
start time.Time
|
||||
}
|
||||
|
||||
// namespaceSelector quotes ns the way MetricsQL unquotes a label value
|
||||
// (strconv.Unquote in metricsql's extractStringValue).
|
||||
func namespaceSelector(ns string) string {
|
||||
return "{namespace=" + strconv.Quote(ns) + "}"
|
||||
}
|
||||
|
||||
// BuildMetricsParams rebuilds the upstream parameters from an allowlist and
|
||||
// adds the namespace scope as the only extra_filters[] value. vmselect ORs
|
||||
// repeated extra_filters, so a client value would widen the scope; it ANDs
|
||||
// the single one into every selector of the query and every match[], which
|
||||
// is what lets the query pass through unparsed.
|
||||
func BuildMetricsParams(endpoint string, in url.Values, namespace string, now time.Time, lim MetricsLimits) (metricsRequest, *HTTPError) {
|
||||
req := metricsRequest{path: endpoint, params: url.Values{}}
|
||||
out := req.params
|
||||
out.Set("extra_filters[]", namespaceSelector(namespace))
|
||||
timeout := lim.MaxTimeout
|
||||
if v := in.Get("timeout"); v != "" {
|
||||
d, herr := parseMetricsDuration("timeout", v)
|
||||
if herr != nil {
|
||||
return req, herr
|
||||
}
|
||||
timeout = min(d, lim.MaxTimeout)
|
||||
}
|
||||
out.Set("timeout", formatSeconds(timeout))
|
||||
|
||||
switch endpoint {
|
||||
case "api/v1/query":
|
||||
if herr := copyQuery(out, in); herr != nil {
|
||||
return req, herr
|
||||
}
|
||||
at := now
|
||||
if v := in.Get("time"); v != "" {
|
||||
t, herr := parseMetricsTime("time", v)
|
||||
if herr != nil {
|
||||
return req, herr
|
||||
}
|
||||
at = t
|
||||
out.Set("time", formatTime(t))
|
||||
}
|
||||
step := instantDefaultStep
|
||||
if v := in.Get("step"); v != "" {
|
||||
var herr *HTTPError
|
||||
if step, herr = parseStep(v, lim); herr != nil {
|
||||
return req, herr
|
||||
}
|
||||
out.Set("step", formatSeconds(step))
|
||||
}
|
||||
req.start = queryStart(out.Get("query"), at.Add(-latencyOffset), step)
|
||||
return req, nil
|
||||
|
||||
case "api/v1/query_range":
|
||||
if herr := copyQuery(out, in); herr != nil {
|
||||
return req, herr
|
||||
}
|
||||
for _, k := range []string{"start", "end", "step"} {
|
||||
if in.Get(k) == "" {
|
||||
return req, errorf(http.StatusBadRequest, "%s is required", k)
|
||||
}
|
||||
}
|
||||
start, end, herr := metricsWindow(in, now, lim)
|
||||
if herr != nil {
|
||||
return req, herr
|
||||
}
|
||||
step, herr := parseStep(in.Get("step"), lim)
|
||||
if herr != nil {
|
||||
return req, herr
|
||||
}
|
||||
if points := end.Sub(start)/step + 1; points > time.Duration(lim.MaxPoints) {
|
||||
return req, errorf(http.StatusUnprocessableEntity, "%d points per series exceed the limit of %d; raise step", points, lim.MaxPoints)
|
||||
}
|
||||
req.start = queryStart(out.Get("query"), start, step)
|
||||
out.Set("start", formatTime(start))
|
||||
out.Set("end", formatTime(end))
|
||||
out.Set("step", formatSeconds(step))
|
||||
return req, nil
|
||||
}
|
||||
|
||||
name, isLabelValues := strings.CutPrefix(endpoint, "api/v1/label/")
|
||||
if isLabelValues {
|
||||
var ok bool
|
||||
if name, ok = strings.CutSuffix(name, "/values"); !ok || strings.Contains(name, "/") {
|
||||
return req, errorf(http.StatusNotFound, "not found")
|
||||
}
|
||||
if !labelName.MatchString(name) {
|
||||
return req, errorf(http.StatusBadRequest, "invalid label name %q", name)
|
||||
}
|
||||
} else if endpoint != "api/v1/series" && endpoint != "api/v1/labels" {
|
||||
return req, errorf(http.StatusNotFound, "not found")
|
||||
}
|
||||
start, end, herr := metricsWindow(in, now, lim)
|
||||
if herr != nil {
|
||||
return req, herr
|
||||
}
|
||||
req.start = start
|
||||
out.Set("start", formatTime(start))
|
||||
out.Set("end", formatTime(end))
|
||||
|
||||
matchers := in["match[]"]
|
||||
if len(matchers) > maxMetricsMatchers {
|
||||
return req, errorf(http.StatusUnprocessableEntity, "at most %d match[] are allowed", maxMetricsMatchers)
|
||||
}
|
||||
for _, m := range matchers {
|
||||
if m == "" {
|
||||
return req, errorf(http.StatusBadRequest, "empty match[]")
|
||||
}
|
||||
out.Add("match[]", m)
|
||||
}
|
||||
if len(matchers) == 0 {
|
||||
if endpoint == "api/v1/series" {
|
||||
return req, errorf(http.StatusBadRequest, "match[] is required")
|
||||
}
|
||||
// vmselect started with -search.ignoreExtraFiltersAtLabelsAPI drops
|
||||
// extra_filters[] on the label APIs unless a match[] is present
|
||||
// (getCommonParamsInternal), so the scope is repeated here.
|
||||
out.Set("match[]", namespaceSelector(namespace))
|
||||
}
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func copyQuery(out, in url.Values) *HTTPError {
|
||||
q := in.Get("query")
|
||||
if q == "" {
|
||||
return errorf(http.StatusBadRequest, "query is required")
|
||||
}
|
||||
out.Set("query", q)
|
||||
return nil
|
||||
}
|
||||
|
||||
// metricsWindow reads start and end with vmselect's label API defaults:
|
||||
// end is now and start is five minutes before end.
|
||||
func metricsWindow(in url.Values, now time.Time, lim MetricsLimits) (time.Time, time.Time, *HTTPError) {
|
||||
end := now
|
||||
if v := in.Get("end"); v != "" {
|
||||
t, herr := parseMetricsTime("end", v)
|
||||
if herr != nil {
|
||||
return time.Time{}, time.Time{}, herr
|
||||
}
|
||||
end = t
|
||||
}
|
||||
start := end.Add(-labelsDefaultWindow)
|
||||
if v := in.Get("start"); v != "" {
|
||||
t, herr := parseMetricsTime("start", v)
|
||||
if herr != nil {
|
||||
return time.Time{}, time.Time{}, herr
|
||||
}
|
||||
start = t
|
||||
}
|
||||
if start.After(end) {
|
||||
return time.Time{}, time.Time{}, errorf(http.StatusBadRequest, "start is after end")
|
||||
}
|
||||
if end.Sub(start) > lim.MaxRange {
|
||||
return time.Time{}, time.Time{}, errorf(http.StatusUnprocessableEntity, "range %s exceeds the limit of %s", end.Sub(start), lim.MaxRange)
|
||||
}
|
||||
return start, end, nil
|
||||
}
|
||||
|
||||
// parseStep refuses a step above the range limit: vmselect aligns the
|
||||
// window outward to whole steps, and an implicit rollup window is one step,
|
||||
// so a huge step reads far more than the range it was sent with.
|
||||
func parseStep(v string, lim MetricsLimits) (time.Duration, *HTTPError) {
|
||||
step, herr := parseMetricsDuration("step", v)
|
||||
if herr != nil {
|
||||
return 0, herr
|
||||
}
|
||||
if step > lim.MaxRange {
|
||||
return 0, errorf(http.StatusUnprocessableEntity, "step %s exceeds the limit of %s", step, lim.MaxRange)
|
||||
}
|
||||
return step, nil
|
||||
}
|
||||
|
||||
// maxMetricsDuration is vmselect's own bound on step and timeout
|
||||
// (maxDurationMsecs in lib/httputil); it also keeps the rounding below
|
||||
// from overflowing.
|
||||
const maxMetricsDuration = 100 * 365 * 24 * time.Hour
|
||||
|
||||
// parseMetricsDuration takes Prometheus float seconds or a duration such as
|
||||
// 1m30s. vmselect works in whole milliseconds and refuses anything shorter;
|
||||
// the value is rounded up to the millisecond so the limits are checked
|
||||
// against what vmselect will use, not against a fraction it drops.
|
||||
func parseMetricsDuration(key, v string) (time.Duration, *HTTPError) {
|
||||
d, err := parseDuration(v)
|
||||
if err != nil || d < time.Millisecond || d > maxMetricsDuration {
|
||||
return 0, errorf(http.StatusBadRequest, "invalid %s %q", key, v)
|
||||
}
|
||||
return (d + time.Millisecond - 1).Truncate(time.Millisecond), nil
|
||||
}
|
||||
|
||||
// parseMetricsTime takes the two forms the Prometheus API defines: float
|
||||
// unix seconds and RFC3339. vmselect would also guess milliseconds from the
|
||||
// digit count and accept relative times; a guessed unit silently moves the
|
||||
// window, so those are refused. The result is cut to the millisecond, as
|
||||
// formatTime sends it, so the limits see the window vmselect will use.
|
||||
func parseMetricsTime(key, v string) (time.Time, *HTTPError) {
|
||||
if sec, frac, ok := strings.Cut(v, "."); isDigits(sec) && (!ok || isDigits(frac)) {
|
||||
if len(sec) > 10 {
|
||||
return time.Time{}, errorf(http.StatusBadRequest, "invalid %s %q: not unix seconds", key, v)
|
||||
}
|
||||
secs, _ := strconv.ParseInt(sec, 10, 64)
|
||||
var nanos int64
|
||||
if ok {
|
||||
nanos, _ = strconv.ParseInt((frac + "000000000")[:9], 10, 64)
|
||||
}
|
||||
return metricsTimeInRange(key, v, time.Unix(secs, nanos))
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, v)
|
||||
if err != nil {
|
||||
return time.Time{}, errorf(http.StatusBadRequest, "invalid %s %q", key, v)
|
||||
}
|
||||
return metricsTimeInRange(key, v, t)
|
||||
}
|
||||
|
||||
// maxMetricsTime is the last instant vmselect can read: it takes an
|
||||
// RFC3339 time through time.UnixNano (timeutil.ParseTimeAt).
|
||||
var maxMetricsTime = time.Unix(0, math.MaxInt64)
|
||||
|
||||
func metricsTimeInRange(key, v string, t time.Time) (time.Time, *HTTPError) {
|
||||
if t.Before(time.Unix(0, 0)) || !t.Before(maxMetricsTime) {
|
||||
return time.Time{}, errorf(http.StatusBadRequest, "invalid %s %q: outside 1970..2262", key, v)
|
||||
}
|
||||
return time.UnixMilli(t.UnixMilli()).UTC(), nil
|
||||
}
|
||||
|
||||
// formatTime writes RFC3339 with milliseconds. vmselect reads a number
|
||||
// without "-" as a unix time whose unit it guesses from its size
|
||||
// (timeutil.getUnixTimestampNanoseconds): "1000000.000" is taken as
|
||||
// 1000000000 seconds, and anything past 2038 in milliseconds as
|
||||
// microseconds. A dated form never reaches that guess.
|
||||
func formatTime(t time.Time) string {
|
||||
return t.UTC().Format("2006-01-02T15:04:05.000Z07:00")
|
||||
}
|
||||
|
||||
// formatSeconds writes a whole-millisecond duration so that vmselect reads
|
||||
// back exactly those milliseconds. vmselect multiplies the float seconds
|
||||
// by 1e3 and truncates (httputil.GetDuration), so "1.001" arrives as
|
||||
// 1000ms; where the shortest form truncates low, half a millisecond more
|
||||
// lands the truncation on the intended value without reaching the next.
|
||||
func formatSeconds(d time.Duration) string {
|
||||
ms := d.Milliseconds()
|
||||
s := strconv.FormatFloat(float64(ms)/1000, 'f', -1, 64)
|
||||
if secs, _ := strconv.ParseFloat(s, 64); int64(secs*1e3) != ms {
|
||||
s = fmt.Sprintf("%d.%03d5", ms/1000, ms%1000)
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -0,0 +1,431 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package observabilityproxy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func testMetricsLimits() MetricsLimits {
|
||||
return MetricsLimits{MaxRange: 30 * 24 * time.Hour, MaxPoints: 11000, MaxTimeout: 30 * time.Second}
|
||||
}
|
||||
|
||||
const wantMetricsScope = `{namespace="tenant-a"}`
|
||||
|
||||
func unix(t time.Time) string { return strconv.FormatInt(t.Unix(), 10) }
|
||||
|
||||
func buildMetrics(t *testing.T, endpoint string, in url.Values) metricsRequest {
|
||||
t.Helper()
|
||||
req, err := BuildMetricsParams(endpoint, in, "tenant-a", testNow, testMetricsLimits())
|
||||
if err != nil {
|
||||
t.Fatalf("BuildMetricsParams(%s, %v): %v", endpoint, in, err)
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
func metricsStatus(endpoint string, in url.Values) int {
|
||||
_, err := BuildMetricsParams(endpoint, in, "tenant-a", testNow, testMetricsLimits())
|
||||
if err == nil {
|
||||
return http.StatusOK
|
||||
}
|
||||
return err.Code
|
||||
}
|
||||
|
||||
func TestBuildMetricsParamsAllowlist(t *testing.T) {
|
||||
hostile := url.Values{
|
||||
"extra_filters": {`{namespace=~".+"}`},
|
||||
"extra_filters[]": {`{namespace="tenant-b"}`, `{namespace=~".*"}`},
|
||||
"extra_label": {"namespace=tenant-b"},
|
||||
"match": {`{namespace="tenant-b"}`},
|
||||
"nocache": {"1"},
|
||||
"limit": {"1"},
|
||||
"AccountID": {"7"},
|
||||
"latency_offset": {"1h"},
|
||||
"round_digits": {"2"},
|
||||
}
|
||||
cases := map[string]url.Values{
|
||||
"api/v1/query": {"query": {"up"}, "time": {unix(testNow)}, "step": {"30s"}},
|
||||
"api/v1/query_range": {"query": {"up"}, "start": {unix(testNow.Add(-time.Hour))}, "end": {unix(testNow)}, "step": {"60"}},
|
||||
"api/v1/series": {"match[]": {"up"}},
|
||||
"api/v1/labels": {"match[]": {"up"}},
|
||||
"api/v1/label/pod/values": {"match[]": {"up"}},
|
||||
}
|
||||
allowed := map[string]bool{"query": true, "start": true, "end": true, "step": true, "time": true, "match[]": true, "timeout": true, "extra_filters[]": true}
|
||||
for endpoint, in := range cases {
|
||||
t.Run(endpoint, func(t *testing.T) {
|
||||
for k, v := range hostile {
|
||||
in[k] = v
|
||||
}
|
||||
req := buildMetrics(t, endpoint, in)
|
||||
if req.path != endpoint {
|
||||
t.Errorf("path = %q", req.path)
|
||||
}
|
||||
for k := range req.params {
|
||||
if !allowed[k] {
|
||||
t.Errorf("parameter %q forwarded: %v", k, req.params)
|
||||
}
|
||||
}
|
||||
if got := req.params["extra_filters[]"]; len(got) != 1 || got[0] != wantMetricsScope {
|
||||
t.Errorf("extra_filters[] = %q, want exactly [%s]", got, wantMetricsScope)
|
||||
}
|
||||
for _, m := range req.params["match[]"] {
|
||||
if strings.Contains(m, "tenant-b") {
|
||||
t.Errorf("client match without brackets forwarded as match[]: %q", m)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNamespaceSelectorEscapes(t *testing.T) {
|
||||
for _, ns := range []string{"tenant-a", `a"} or {namespace=~".+`, `back\slash`, "new\nline"} {
|
||||
sel := namespaceSelector(ns)
|
||||
lit, ok := strings.CutPrefix(sel, "{namespace=")
|
||||
lit, ok2 := strings.CutSuffix(lit, "}")
|
||||
if !ok || !ok2 {
|
||||
t.Fatalf("selector %q has the wrong shape", sel)
|
||||
}
|
||||
// MetricsQL reads a double-quoted label value with strconv.Unquote
|
||||
// (metricsql extractStringValue).
|
||||
got, err := strconv.Unquote(lit)
|
||||
if err != nil || got != ns {
|
||||
t.Errorf("selector %q decodes to %q, %v; want %q", sel, got, err, ns)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildMetricsParamsLabelEndpointsAlwaysMatch(t *testing.T) {
|
||||
for _, endpoint := range []string{"api/v1/labels", "api/v1/label/__name__/values"} {
|
||||
req := buildMetrics(t, endpoint, url.Values{})
|
||||
if got := req.params["match[]"]; len(got) != 1 || got[0] != wantMetricsScope {
|
||||
t.Errorf("%s without match[] sends %q, want the namespace selector", endpoint, got)
|
||||
}
|
||||
req = buildMetrics(t, endpoint, url.Values{"match[]": {"up", `{job="x"}`}})
|
||||
if got := req.params["match[]"]; len(got) != 2 || got[0] != "up" || got[1] != `{job="x"}` {
|
||||
t.Errorf("%s client match[] = %q, want them kept", endpoint, got)
|
||||
}
|
||||
}
|
||||
if got := metricsStatus("api/v1/series", url.Values{}); got != http.StatusBadRequest {
|
||||
t.Errorf("series without match[] = %d, want 400", got)
|
||||
}
|
||||
if got := metricsStatus("api/v1/series", url.Values{"match[]": {""}}); got != http.StatusBadRequest {
|
||||
t.Errorf("empty match[] = %d, want 400", got)
|
||||
}
|
||||
many := url.Values{}
|
||||
for i := range maxMetricsMatchers + 1 {
|
||||
many.Add("match[]", "up"+strconv.Itoa(i))
|
||||
}
|
||||
if got := metricsStatus("api/v1/labels", many); got != http.StatusUnprocessableEntity {
|
||||
t.Errorf("%d match[] = %d, want 422", maxMetricsMatchers+1, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildMetricsParamsLimits(t *testing.T) {
|
||||
ago := func(d time.Duration) string { return unix(testNow.Add(-d)) }
|
||||
day := 24 * time.Hour
|
||||
rng := func(start, end, step string) url.Values {
|
||||
return url.Values{"query": {"up"}, "start": {start}, "end": {end}, "step": {step}}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
endpoint string
|
||||
in url.Values
|
||||
want int
|
||||
}{
|
||||
{"range at the limit", "api/v1/query_range", rng(ago(30*day), ago(0), "1h"), http.StatusOK},
|
||||
{"range above the limit", "api/v1/query_range", rng(ago(31*day), ago(0), "1h"), http.StatusUnprocessableEntity},
|
||||
{"points at the limit", "api/v1/query_range", rng(ago(10999*time.Minute), ago(0), "1m"), http.StatusOK},
|
||||
{"points above the limit", "api/v1/query_range", rng(ago(11000*time.Minute), ago(0), "1m"), http.StatusUnprocessableEntity},
|
||||
{"points above the limit after rounding the step up", "api/v1/query_range", rng(ago(22*time.Second), ago(0), "0.0019"), http.StatusUnprocessableEntity},
|
||||
// 10.9999s admits 11000 points, but vmselect gets both bounds cut to
|
||||
// the millisecond: 11.000s, 11001 points.
|
||||
{"points above the limit once the bounds are cut to milliseconds", "api/v1/query_range", rng("2026-11-04T11:59:49.0009Z", "2026-11-04T12:00:00.0008Z", "0.001"), http.StatusUnprocessableEntity},
|
||||
{"overflowing step", "api/v1/query_range", rng(ago(time.Hour), ago(0), "2562047h47m16.854775807s"), http.StatusBadRequest},
|
||||
{"overflowing timeout", "api/v1/query", url.Values{"query": {"up"}, "timeout": {"2562047h47m16.854775807s"}}, http.StatusBadRequest},
|
||||
{"step above the range limit", "api/v1/query_range", rng(ago(time.Hour), ago(0), "31d"), http.StatusUnprocessableEntity},
|
||||
{"zero step", "api/v1/query_range", rng(ago(time.Hour), ago(0), "0"), http.StatusBadRequest},
|
||||
{"sub-millisecond step", "api/v1/query_range", rng(ago(time.Hour), ago(0), "0.0001"), http.StatusBadRequest},
|
||||
{"missing step", "api/v1/query_range", url.Values{"query": {"up"}, "start": {ago(time.Hour)}, "end": {ago(0)}}, http.StatusBadRequest},
|
||||
{"missing start", "api/v1/query_range", url.Values{"query": {"up"}, "end": {ago(0)}, "step": {"1m"}}, http.StatusBadRequest},
|
||||
{"missing query", "api/v1/query_range", url.Values{"start": {ago(time.Hour)}, "end": {ago(0)}, "step": {"1m"}}, http.StatusBadRequest},
|
||||
{"inverted range", "api/v1/query_range", rng(ago(0), ago(time.Hour), "1m"), http.StatusBadRequest},
|
||||
{"millisecond timestamp", "api/v1/query_range", rng(ago(time.Hour)+"000", ago(0)+"000", "1m"), http.StatusBadRequest},
|
||||
{"relative time", "api/v1/query_range", rng("-1h", "now", "1m"), http.StatusBadRequest},
|
||||
{"rfc3339", "api/v1/query_range", rng("2026-11-04T11:00:00Z", "2026-11-04T12:00:00.5Z", "15s"), http.StatusOK},
|
||||
{"instant without time", "api/v1/query", url.Values{"query": {"up"}}, http.StatusOK},
|
||||
{"instant step above the range limit", "api/v1/query", url.Values{"query": {"up"}, "step": {"31d"}}, http.StatusUnprocessableEntity},
|
||||
{"instant bad time", "api/v1/query", url.Values{"query": {"up"}, "time": {"yesterday"}}, http.StatusBadRequest},
|
||||
{"negative timeout", "api/v1/query", url.Values{"query": {"up"}, "timeout": {"-1s"}}, http.StatusBadRequest},
|
||||
{"series range above the limit", "api/v1/series", url.Values{"match[]": {"up"}, "start": {ago(31 * day)}}, http.StatusUnprocessableEntity},
|
||||
{"labels inverted range", "api/v1/labels", url.Values{"start": {ago(0)}, "end": {ago(time.Hour)}}, http.StatusBadRequest},
|
||||
{"invalid label name", "api/v1/label/a-b/values", url.Values{}, http.StatusBadRequest},
|
||||
{"unknown endpoint", "api/v1/status/tsdb", url.Values{}, http.StatusNotFound},
|
||||
{"export", "api/v1/export", url.Values{"match[]": {"up"}}, http.StatusNotFound},
|
||||
{"label without values", "api/v1/label/pod", url.Values{}, http.StatusNotFound},
|
||||
{"root", "", url.Values{}, http.StatusNotFound},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := metricsStatus(tt.endpoint, tt.in); got != tt.want {
|
||||
t.Fatalf("status = %d, want %d", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildMetricsParamsCanonicalForms(t *testing.T) {
|
||||
req := buildMetrics(t, "api/v1/query_range", url.Values{
|
||||
"query": {"up"}, "start": {"2026-11-04T11:00:00.25Z"}, "end": {unix(testNow)}, "step": {"1m30s"},
|
||||
})
|
||||
want := url.Values{
|
||||
"query": {"up"}, "start": {"2026-11-04T11:00:00.250Z"}, "end": {"2026-11-04T12:00:00.000Z"}, "step": {"90"},
|
||||
"timeout": {"30"}, "extra_filters[]": {wantMetricsScope},
|
||||
}
|
||||
if req.params.Encode() != want.Encode() {
|
||||
t.Errorf("params = %v\nwant %v", req.params, want)
|
||||
}
|
||||
|
||||
// vmselect drops a sub-millisecond remainder; the points check has to
|
||||
// see the step it will use.
|
||||
for in, want := range map[string]string{"60.0001": "60.001", "0.0019": "0.002"} {
|
||||
req = buildMetrics(t, "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-20 * time.Second))}, "end": {unix(testNow)}, "step": {in}})
|
||||
if got := req.params.Get("step"); got != want {
|
||||
t.Errorf("step %s forwarded as %q, want %s", in, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
for in, want := range map[string]string{"": "30", "10s": "10", "5m": "30", "1.5": "1.5"} {
|
||||
v := url.Values{"query": {"up"}}
|
||||
if in != "" {
|
||||
v.Set("timeout", in)
|
||||
}
|
||||
if got := buildMetrics(t, "api/v1/query", v).params.Get("timeout"); got != want {
|
||||
t.Errorf("timeout %q forwarded as %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
req = buildMetrics(t, "api/v1/labels", url.Values{})
|
||||
if req.params.Get("end") != "2026-11-04T12:00:00.000Z" || req.params.Get("start") != "2026-11-04T11:55:00.000Z" {
|
||||
t.Errorf("labels window = %s..%s, want the last 5m", req.params.Get("start"), req.params.Get("end"))
|
||||
}
|
||||
if q := buildMetrics(t, "api/v1/query", url.Values{"query": {"up"}}); q.params.Has("time") || !q.start.Equal(testNow.Add(-15*time.Minute-30*time.Second)) {
|
||||
t.Errorf("instant query without time: params %v, start %v", q.params, q.start)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetricsStoreByRange(t *testing.T) {
|
||||
cfg := MetricsConfig{ShortTermStore: "shortterm", LongTermStore: "longterm", ShortTermRetention: 72 * time.Hour}
|
||||
tests := []struct {
|
||||
name string
|
||||
endpoint string
|
||||
in url.Values
|
||||
want string
|
||||
}{
|
||||
{"instant now", "api/v1/query", url.Values{"query": {"up"}}, "shortterm"},
|
||||
{"instant within retention", "api/v1/query", url.Values{"query": {"up"}, "time": {unix(testNow.Add(-71 * time.Hour))}}, "shortterm"},
|
||||
{"instant past retention", "api/v1/query", url.Values{"query": {"up"}, "time": {unix(testNow.Add(-73 * time.Hour))}}, "longterm"},
|
||||
{"live graph", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-6 * time.Hour))}, "end": {unix(testNow)}, "step": {"60"}}, "shortterm"},
|
||||
{"range fits with lookback", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-71 * time.Hour))}, "end": {unix(testNow)}, "step": {"600"}}, "shortterm"},
|
||||
{"range lookback past retention", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-72 * time.Hour))}, "end": {unix(testNow)}, "step": {"600"}}, "longterm"},
|
||||
{"range past retention", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-7 * 24 * time.Hour))}, "end": {unix(testNow)}, "step": {"600"}}, "longterm"},
|
||||
{"old window ending long ago", "api/v1/query_range", url.Values{"query": {"up"}, "start": {unix(testNow.Add(-5 * 24 * time.Hour))}, "end": {unix(testNow.Add(-4 * 24 * time.Hour))}, "step": {"600"}}, "longterm"},
|
||||
{"labels default window", "api/v1/labels", url.Values{}, "shortterm"},
|
||||
{"series past retention", "api/v1/series", url.Values{"match[]": {"up"}, "start": {unix(testNow.Add(-4 * 24 * time.Hour))}}, "longterm"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := buildMetrics(t, tt.endpoint, tt.in)
|
||||
if got := cfg.store(req.start, testNow); got != tt.want {
|
||||
t.Fatalf("store = %s, want %s", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryLookback(t *testing.T) {
|
||||
tests := []struct {
|
||||
query string
|
||||
step time.Duration
|
||||
want time.Duration
|
||||
}{
|
||||
{`up`, 5 * time.Minute, 15 * time.Minute},
|
||||
{`increase(x[7d])`, 5 * time.Minute, 7*24*time.Hour + 35*time.Minute},
|
||||
{`rate(x[300])`, time.Minute, 20 * time.Minute},
|
||||
{`rate(x[ 1h30m ])`, time.Minute, 105 * time.Minute},
|
||||
{`max_over_time(x[1h:5m])`, time.Minute, 115 * time.Minute},
|
||||
{`max_over_time(x[1h:])`, time.Minute, 83 * time.Minute},
|
||||
{`max_over_time(max_over_time(rate(x[5m])[1h:1m])[1d:1h])`, time.Minute, 42*time.Hour + 35*time.Minute},
|
||||
{`rate(rate(x))`, time.Hour, 2*(3*time.Hour+5*time.Minute) + 2*time.Hour + 5*time.Minute},
|
||||
{`x offset 10d`, 5 * time.Minute, 10*24*time.Hour + 15*time.Minute},
|
||||
{`sum(x OFFSET 2h)`, 5 * time.Minute, 155 * time.Minute},
|
||||
{`rate(x[5m] offset 1h)`, time.Minute, 80 * time.Minute},
|
||||
{`x offset -1h`, 5 * time.Minute, 15 * time.Minute},
|
||||
{`x offset - 1h`, 5 * time.Minute, 15 * time.Minute},
|
||||
{`node_offset_seconds + offset_total + with_x`, 5 * time.Minute, 15 * time.Minute},
|
||||
{`x{a="[1h]"}`, 5 * time.Minute, 75 * time.Minute},
|
||||
{`x{a="]"}[7d]`, 5 * time.Minute, 7*24*time.Hour + 15*time.Minute},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.query, func(t *testing.T) {
|
||||
got, ok := queryLookback(tt.query, tt.step)
|
||||
if !ok || got != tt.want {
|
||||
t.Fatalf("queryLookback = %s, %t; want %s, true", got, ok, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryLookbackUnknown(t *testing.T) {
|
||||
for _, q := range []string{
|
||||
`x @ 123`,
|
||||
`x @ start()`,
|
||||
`x{a="a@b"}`,
|
||||
`rate(x[5i])`,
|
||||
`rate(x[$__interval])`,
|
||||
`x[1h-5m]`,
|
||||
`x[7D]`,
|
||||
`x[1.d]`,
|
||||
`x[]`,
|
||||
`rate(x[7d)`,
|
||||
`x offset`,
|
||||
`x offset w`,
|
||||
`WITH (w = 7d) rate(x[w])`,
|
||||
`x{a="["}`,
|
||||
`x{a="[5m"}[7d]`,
|
||||
`x{path="/offset/1"}`,
|
||||
`x[99999999999y]`,
|
||||
`x offset 99999999999y`,
|
||||
`x offset 3_000_000`,
|
||||
`WITH (f(q) = max_over_time(q[2d:1h])) f(f(x))`,
|
||||
`with (s(m) = m offset 2d) s(s(x))`,
|
||||
} {
|
||||
t.Run(q, func(t *testing.T) {
|
||||
if got, ok := queryLookback(q, 5*time.Minute); ok {
|
||||
t.Fatalf("queryLookback = %s, true; want unknown", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetricsStoreByLookback(t *testing.T) {
|
||||
cfg := MetricsConfig{ShortTermStore: "shortterm", LongTermStore: "longterm", ShortTermRetention: 72 * time.Hour}
|
||||
rangeFrom := func(q string, ago time.Duration, step string) url.Values {
|
||||
return url.Values{"query": {q}, "start": {unix(testNow.Add(-ago))}, "end": {unix(testNow)}, "step": {step}}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
endpoint string
|
||||
in url.Values
|
||||
want string
|
||||
}{
|
||||
{"instant short window", "api/v1/query", url.Values{"query": {"rate(x[5m])"}}, "shortterm"},
|
||||
{"instant 7d window", "api/v1/query", url.Values{"query": {"increase(x[7d])"}}, "longterm"},
|
||||
{"instant offset 10d", "api/v1/query", url.Values{"query": {"x offset 10d"}}, "longterm"},
|
||||
{"instant negative offset", "api/v1/query", url.Values{"query": {"x offset -1h"}}, "shortterm"},
|
||||
{"instant at timestamp", "api/v1/query", url.Values{"query": {"x @ 123"}}, "longterm"},
|
||||
{"instant at start", "api/v1/query", url.Values{"query": {"x @ start()"}}, "longterm"},
|
||||
{"instant step window", "api/v1/query", url.Values{"query": {"rate(x[5i])"}}, "longterm"},
|
||||
{"instant step past retention", "api/v1/query", url.Values{"query": {"up"}, "step": {"259200"}}, "longterm"},
|
||||
{"instant latency offset at the edge", "api/v1/query", url.Values{"query": {"x[71h54m59s]"}, "step": {"0.001"}}, "longterm"},
|
||||
{"instant subquery past retention", "api/v1/query", url.Values{"query": {"max_over_time(rate(x[5m])[4d:1h])"}}, "longterm"},
|
||||
{"instant offset with underscores", "api/v1/query", url.Values{"query": {"x offset 3_000_000"}}, "longterm"},
|
||||
{"instant WITH template", "api/v1/query", url.Values{"query": {"WITH (f(q) = max_over_time(q[2d:1h])) f(f(x))"}}, "longterm"},
|
||||
{"instant implicit subqueries", "api/v1/query", url.Values{"query": {"rate(rate(rate(rate(rate(rate(x))))))"}, "step": {"43200"}}, "longterm"},
|
||||
{"quoted at sign", "api/v1/query", url.Values{"query": {`x{a="a@b"}`}}, "longterm"},
|
||||
{"quoted open bracket hides window", "api/v1/query", url.Values{"query": {`rate(x{a="[5m"}[7d])`}}, "longterm"},
|
||||
{"quoted close bracket before window", "api/v1/query", url.Values{"query": {`rate(x{a="]"}[7d])`}}, "longterm"},
|
||||
{"quoted offset before offset", "api/v1/query", url.Values{"query": {`x{a="offset 1m"} offset 10d`}}, "longterm"},
|
||||
{"range short window", "api/v1/query_range", rangeFrom("rate(x[1h])", 6*time.Hour, "60"), "shortterm"},
|
||||
{"range 7d window", "api/v1/query_range", rangeFrom("increase(x[7d])", 6*time.Hour, "60"), "longterm"},
|
||||
{"range offset past retention", "api/v1/query_range", rangeFrom("x offset 3d", time.Hour, "60"), "longterm"},
|
||||
{"range lookback fits", "api/v1/query_range", rangeFrom("rate(x[5m])", 70*time.Hour, "600"), "shortterm"},
|
||||
{"range lookback crosses retention", "api/v1/query_range", rangeFrom("rate(x[5m])", 71*time.Hour+50*time.Minute, "600"), "longterm"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := buildMetrics(t, tt.endpoint, tt.in)
|
||||
if got := cfg.store(req.start, testNow); got != tt.want {
|
||||
t.Fatalf("store = %s, want %s", got, tt.want)
|
||||
}
|
||||
if q := req.params.Get("query"); q != tt.in.Get("query") {
|
||||
t.Fatalf("query forwarded as %q, want it unchanged", q)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// vmselectMillis repeats how vmselect reads a duration argument
|
||||
// (httputil.GetDuration in lib/httputil/duration.go, v1.136.22 and
|
||||
// v1.139.0): float seconds, multiplied by 1e3 and truncated.
|
||||
func vmselectMillis(t *testing.T, v string) int64 {
|
||||
t.Helper()
|
||||
secs, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
t.Fatalf("vmselect cannot parse %q: %v", v, err)
|
||||
}
|
||||
return int64(secs * 1e3)
|
||||
}
|
||||
|
||||
// TestFormatSecondsVmselectExact sweeps every step up to 2000s and the
|
||||
// large end of the range: vmselect must read back the milliseconds the
|
||||
// limits were checked with.
|
||||
func TestFormatSecondsVmselectExact(t *testing.T) {
|
||||
check := func(ms int64) {
|
||||
if got := vmselectMillis(t, formatSeconds(time.Duration(ms)*time.Millisecond)); got != ms {
|
||||
t.Fatalf("%dms formatted as %q reaches vmselect as %dms", ms, formatSeconds(time.Duration(ms)*time.Millisecond), got)
|
||||
}
|
||||
}
|
||||
for ms := int64(1); ms <= 2_000_000; ms++ {
|
||||
check(ms)
|
||||
}
|
||||
for ms := maxMetricsDuration.Milliseconds() - 100_000; ms <= maxMetricsDuration.Milliseconds(); ms++ {
|
||||
check(ms)
|
||||
}
|
||||
// A spread of the range in between, on a stride no power of ten divides.
|
||||
for ms := int64(2_000_001); ms < maxMetricsDuration.Milliseconds(); ms += 3_153_599_999 {
|
||||
check(ms)
|
||||
}
|
||||
// Whole seconds keep their plain form.
|
||||
if got := formatSeconds(90 * time.Second); got != "90" {
|
||||
t.Errorf("90s formatted as %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildMetricsParamsPointsAsVmselectCounts: a 1.001s step is 1001ms
|
||||
// to the proxy; vmselect must count the same 11000 points at the limit.
|
||||
func TestBuildMetricsParamsPointsAsVmselectCounts(t *testing.T) {
|
||||
end := testNow
|
||||
step := 1001 * time.Millisecond
|
||||
rng := func(points int64) url.Values {
|
||||
start := end.Add(-time.Duration(points-1) * step)
|
||||
return url.Values{"query": {"up"}, "start": {start.Format(time.RFC3339Nano)}, "end": {end.Format(time.RFC3339Nano)}, "step": {"1.001"}}
|
||||
}
|
||||
req := buildMetrics(t, "api/v1/query_range", rng(11000))
|
||||
stepMs := vmselectMillis(t, req.params.Get("step"))
|
||||
startMs, endMs := vmselectTimeMillis(t, req.params.Get("start")), vmselectTimeMillis(t, req.params.Get("end"))
|
||||
if points := (endMs-startMs)/stepMs + 1; stepMs != 1001 || points != 11000 {
|
||||
t.Fatalf("vmselect reads step %dms and %d points, want 1001ms and 11000", stepMs, points)
|
||||
}
|
||||
if got := metricsStatus("api/v1/query_range", rng(11001)); got != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("11001 points answered %d, want 422", got)
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
"log/slog"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -56,6 +57,7 @@ type Config struct {
|
||||
// means the in-cluster Service of the "generic" logs storage.
|
||||
UpstreamURL func(store string) string
|
||||
Logs LogsLimits
|
||||
Metrics MetricsConfig
|
||||
Limiter LimiterConfig
|
||||
RequestTimeout time.Duration
|
||||
TailIdleTimeout time.Duration
|
||||
@@ -84,6 +86,12 @@ func NewServer(cfg Config, auth *Authorizer, stores StoreLookup, reg prometheus.
|
||||
return fmt.Sprintf("http://vlselect-generic.%s.svc.%s:9471", store, domain)
|
||||
}
|
||||
}
|
||||
if cfg.Metrics.UpstreamURL == nil {
|
||||
domain, root := cfg.ClusterDomain, cfg.Metrics.RootTenant
|
||||
cfg.Metrics.UpstreamURL = func(store string) string {
|
||||
return fmt.Sprintf("http://vmselect-%s.%s.svc.%s:8481/select/0/prometheus", store, root, domain)
|
||||
}
|
||||
}
|
||||
s := &Server{
|
||||
cfg: cfg,
|
||||
auth: auth,
|
||||
@@ -167,7 +175,7 @@ func writeError(w http.ResponseWriter, e *HTTPError) {
|
||||
// serve answers a root it has no handler for before authenticating, so an
|
||||
// unknown path costs no review calls.
|
||||
func (s *Server) serve(w http.ResponseWriter, r *http.Request, route Route) *HTTPError {
|
||||
if route.Kind != NamespaceLogs {
|
||||
if route.Kind != NamespaceLogs && route.Kind != NamespaceMetrics {
|
||||
return errorf(http.StatusNotFound, "not found")
|
||||
}
|
||||
user, herr := s.authorize(r, route)
|
||||
@@ -182,6 +190,9 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, route Route) *HTT
|
||||
w.Header().Set("Retry-After", strconv.FormatInt(int64(math.Ceil(wait.Seconds())), 10))
|
||||
return errorf(http.StatusTooManyRequests, "request rate limit exceeded")
|
||||
}
|
||||
if route.Kind == NamespaceMetrics {
|
||||
return s.serveMetrics(w, r, route, user)
|
||||
}
|
||||
endpoint, ok := strings.CutPrefix(route.Endpoint, "select/logsql/")
|
||||
if !ok {
|
||||
return errorf(http.StatusNotFound, "not found")
|
||||
@@ -226,29 +237,17 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, route Route) *HTT
|
||||
ctx, stop = context.WithTimeout(ctx, limit)
|
||||
defer stop()
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost,
|
||||
s.cfg.UpstreamURL(store)+"/select/logsql/"+endpoint, strings.NewReader(params.Encode()))
|
||||
if err != nil {
|
||||
return errorf(http.StatusInternalServerError, "building upstream request: %v", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("AccountID", "0")
|
||||
req.Header.Set("ProjectID", "0")
|
||||
tenant := http.Header{}
|
||||
tenant.Set("AccountID", "0")
|
||||
tenant.Set("ProjectID", "0")
|
||||
if tail {
|
||||
// vlselect holds even the headers of a gzip-encoded tail until the
|
||||
// first line; an explicit value stops the transport from asking for gzip.
|
||||
req.Header.Set("Accept-Encoding", "identity")
|
||||
tenant.Set("Accept-Encoding", "identity")
|
||||
}
|
||||
resp, err := s.client.Do(req)
|
||||
switch {
|
||||
case err == nil:
|
||||
case r.Context().Err() != nil:
|
||||
return errorf(http.StatusBadGateway, "client went away")
|
||||
case errors.Is(context.Cause(ctx), context.DeadlineExceeded):
|
||||
return errorf(http.StatusGatewayTimeout, "log store did not answer within %s", limit)
|
||||
default:
|
||||
slog.Warn("log store unreachable", "store", store, "error", err)
|
||||
return errorf(http.StatusBadGateway, "log store unreachable")
|
||||
resp, herr := s.post(ctx, r, s.cfg.UpstreamURL(store)+"/select/logsql/"+endpoint, params, "log store", tenant)
|
||||
if herr != nil {
|
||||
return herr
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
@@ -280,6 +279,75 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, route Route) *HTT
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) serveMetrics(w http.ResponseWriter, r *http.Request, route Route, user string) *HTTPError {
|
||||
release, ok := s.limits.acquire(user)
|
||||
if !ok {
|
||||
return tooManyConcurrent(w)
|
||||
}
|
||||
defer release()
|
||||
if herr := s.readForm(w, r); herr != nil {
|
||||
return herr
|
||||
}
|
||||
now := s.now()
|
||||
req, herr := BuildMetricsParams(route.Endpoint, r.Form, route.Namespace, now, s.cfg.Metrics.Limits)
|
||||
if herr != nil {
|
||||
return herr
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
if s.cfg.RequestTimeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, s.cfg.RequestTimeout)
|
||||
defer cancel()
|
||||
}
|
||||
store := s.cfg.Metrics.store(req.start, now)
|
||||
resp, herr := s.post(ctx, r, s.cfg.Metrics.UpstreamURL(store)+"/"+req.path, req.params, "metrics store", nil)
|
||||
if herr != nil {
|
||||
return herr
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if ct := resp.Header.Get("Content-Type"); ct != "" {
|
||||
w.Header().Set("Content-Type", ct)
|
||||
}
|
||||
// As for a bounded logs answer: the client must take it by the request
|
||||
// deadline, and a break-off cuts the connection.
|
||||
if deadline, ok := ctx.Deadline(); ok {
|
||||
_ = http.NewResponseController(w).SetWriteDeadline(deadline)
|
||||
}
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
if _, err := io.Copy(w, resp.Body); err != nil {
|
||||
slog.Debug("copying upstream response", "error", err)
|
||||
return errAborted
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// post sends params as a form with only the given headers, so nothing the
|
||||
// client sent reaches the store, and tells an unreachable store (502) from
|
||||
// one that missed its deadline or, for a tail, its idle timeout (504).
|
||||
func (s *Server) post(ctx context.Context, r *http.Request, target string, params url.Values, what string, header http.Header) (*http.Response, *HTTPError) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, target, strings.NewReader(params.Encode()))
|
||||
if err != nil {
|
||||
return nil, errorf(http.StatusInternalServerError, "building upstream request: %v", err)
|
||||
}
|
||||
for k, v := range header {
|
||||
req.Header[k] = v
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
resp, err := s.client.Do(req)
|
||||
switch {
|
||||
case err == nil:
|
||||
return resp, nil
|
||||
case r.Context().Err() != nil:
|
||||
return nil, errorf(http.StatusBadGateway, "client went away")
|
||||
case errors.Is(context.Cause(ctx), context.DeadlineExceeded):
|
||||
return nil, errorf(http.StatusGatewayTimeout, "%s did not answer in time", what)
|
||||
default:
|
||||
slog.Warn(what+" unreachable", "url", target, "error", err)
|
||||
return nil, errorf(http.StatusBadGateway, "%s unreachable", what)
|
||||
}
|
||||
}
|
||||
|
||||
// concurrencyRetryAfter is a hint only: a slot frees when another request
|
||||
// of the user ends, which the proxy cannot predict, and one second keeps
|
||||
// a polling client from retrying in a tight loop.
|
||||
|
||||
@@ -421,15 +421,20 @@ func stalledAPIServer(t *testing.T, stallTokens bool) kubernetes.Interface {
|
||||
// a bounded review call ends the request.
|
||||
func TestServerStalledReviewAnswersInTime(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
name, path string
|
||||
stallTokens bool
|
||||
}{{"token review", true}, {"subject access review", false}} {
|
||||
}{
|
||||
{"logs token review", logsRoot + "query", true},
|
||||
{"logs subject access review", logsRoot + "query", false},
|
||||
{"metrics token review", metricsRoot + "api/v1/query", true},
|
||||
{"metrics subject access review", metricsRoot + "api/v1/query", false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
h := newHarness(t, Config{RequestTimeout: 200 * time.Millisecond}, nil)
|
||||
h.server.auth = NewAuthorizer(stalledAPIServer(t, tc.stallTokens), 10*time.Second)
|
||||
got := make(chan int, 1)
|
||||
go func() {
|
||||
resp := h.do(http.MethodPost, logsRoot+"query", "good", url.Values{"query": {"*"}}, nil)
|
||||
resp := h.do(http.MethodPost, tc.path, "good", url.Values{"query": {"up"}}, nil)
|
||||
_ = resp.Body.Close()
|
||||
got <- resp.StatusCode
|
||||
}()
|
||||
@@ -444,3 +449,56 @@ func TestServerStalledReviewAnswersInTime(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestServerMetricsRequestLifetime repeats the body, write and break-off
|
||||
// checks on the metrics root, which has its own handler.
|
||||
func TestServerMetricsRequestLifetime(t *testing.T) {
|
||||
query := metricsRoot + "api/v1/query?query=up"
|
||||
t.Run("slow body", func(t *testing.T) {
|
||||
h := newHarness(t, Config{
|
||||
RequestTimeout: 200 * time.Millisecond,
|
||||
Limiter: LimiterConfig{MaxConcurrentPerUser: 1},
|
||||
}, promUpstream)
|
||||
pipe := servePipe(t, h.server)
|
||||
conn := pipe.send(t, "POST "+metricsRoot+"api/v1/query HTTP/1.1\r\nHost: proxy\r\nAuthorization: Bearer good\r\n"+
|
||||
"Content-Type: application/x-www-form-urlencoded\r\nContent-Length: 100\r\n\r\nquery=")
|
||||
eventually(t, time.Second, func() bool { return slotsHeld(h) == 1 },
|
||||
"a metrics request with an unfinished body does not hold a concurrency slot")
|
||||
if resp := readResponse(t, conn); resp.StatusCode != http.StatusRequestTimeout {
|
||||
t.Fatalf("unfinished body answered %d, want 408", resp.StatusCode)
|
||||
}
|
||||
eventually(t, time.Second, func() bool { return slotsHeld(h) == 0 }, "the slot of an unfinished body was not released")
|
||||
})
|
||||
t.Run("stalled client", func(t *testing.T) {
|
||||
// Larger than the server's write buffer, so the handler itself
|
||||
// blocks on the client.
|
||||
big := `{"status":"success","data":"` + strings.Repeat("x", 64<<10) + `"}`
|
||||
h := newHarness(t, Config{
|
||||
RequestTimeout: 200 * time.Millisecond,
|
||||
Limiter: LimiterConfig{MaxConcurrentPerUser: 1},
|
||||
}, func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, big) })
|
||||
pipe := servePipe(t, h.server)
|
||||
pipe.send(t, "GET "+query+" HTTP/1.1\r\nHost: proxy\r\nAuthorization: Bearer good\r\n\r\n")
|
||||
eventually(t, time.Second, func() bool { return slotsHeld(h) == 1 }, "the stalled metrics request never held its slot")
|
||||
eventually(t, 2*time.Second, func() bool { return slotsHeld(h) == 0 },
|
||||
"a metrics client that stopped reading keeps its concurrency slot")
|
||||
})
|
||||
t.Run("truncated answer", func(t *testing.T) {
|
||||
h := newHarness(t, Config{}, func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Length", "100")
|
||||
_, _ = io.WriteString(w, `{"status":"success",`)
|
||||
})
|
||||
// Nothing has been flushed yet when the copy fails, so the
|
||||
// connection may close before the status line.
|
||||
req, _ := http.NewRequest(http.MethodGet, h.proxy.URL+query, nil)
|
||||
req.Header.Set("Authorization", "Bearer good")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if _, err := io.ReadAll(resp.Body); err == nil {
|
||||
t.Fatal("a truncated store answer reached the client as a complete response")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package observabilityproxy
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
authorizationv1 "k8s.io/api/authorization/v1"
|
||||
)
|
||||
|
||||
const metricsRoot = "/apis/cozystack.io/v1alpha1/namespaces/tenant-a/metrics/"
|
||||
|
||||
func promUpstream(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"status":"success","data":{"resultType":"vector","result":[]}}`)
|
||||
}
|
||||
|
||||
func TestServerMetricsRebuildsUpstreamRequest(t *testing.T) {
|
||||
h := newHarness(t, Config{}, promUpstream)
|
||||
hostile := url.Values{
|
||||
"query": {"sum(rate(container_cpu_usage_seconds_total[5m]))"},
|
||||
"start": {unix(testNow.Add(-time.Hour))},
|
||||
"end": {unix(testNow)},
|
||||
"step": {"60"},
|
||||
"extra_filters": {`{namespace=~".+"}`},
|
||||
"extra_filters[]": {`{namespace="tenant-b"}`, `{namespace=~".*"}`},
|
||||
"extra_label": {"namespace=tenant-b"},
|
||||
"nocache": {"1"},
|
||||
}
|
||||
headers := map[string]string{"AccountID": "9", "X-Forwarded-For": "1.2.3.4", "Cookie": "kc-access=secret"}
|
||||
|
||||
resp := h.do(http.MethodGet, metricsRoot+"api/v1/query_range?"+hostile.Encode(), "good", nil, headers)
|
||||
_ = resp.Body.Close()
|
||||
resp = h.do(http.MethodPost, metricsRoot+"api/v1/query_range?extra_filters[]="+url.QueryEscape(`{namespace="x"}`), "good", hostile, headers)
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK || resp.Header.Get("Content-Type") != "application/json" {
|
||||
t.Fatalf("POST answered %d %s %q", resp.StatusCode, resp.Header.Get("Content-Type"), body)
|
||||
}
|
||||
|
||||
reqs := h.requests()
|
||||
if len(reqs) != 2 {
|
||||
t.Fatalf("upstream saw %d requests", len(reqs))
|
||||
}
|
||||
if reqs[0].form.Encode() != reqs[1].form.Encode() {
|
||||
t.Fatalf("GET and POST built different upstream requests:\n%v\n%v", reqs[0].form, reqs[1].form)
|
||||
}
|
||||
if n := h.kube.sars.Load(); n != 1 {
|
||||
t.Fatalf("SubjectAccessReviews = %d, want 1: POST must reuse the GET decision", n)
|
||||
}
|
||||
want := authorizationv1.ResourceAttributes{Verb: "list", Group: "cozystack.io", Resource: "metrics", Namespace: "tenant-a"}
|
||||
if got := h.kube.lastSAR.Load().ResourceAttributes; *got != want {
|
||||
t.Fatalf("SubjectAccessReview = %+v, want %+v", *got, want)
|
||||
}
|
||||
for _, r := range reqs {
|
||||
if r.path != "/api/v1/query_range" {
|
||||
t.Errorf("upstream path = %s", r.path)
|
||||
}
|
||||
if got := r.form["extra_filters[]"]; len(got) != 1 || got[0] != wantMetricsScope {
|
||||
t.Errorf("extra_filters[] = %q", got)
|
||||
}
|
||||
if r.form.Has("extra_filters") || r.form.Has("extra_label") || r.form.Has("nocache") {
|
||||
t.Errorf("client parameter forwarded: %v", r.form)
|
||||
}
|
||||
for _, k := range []string{"Authorization", "AccountID", "X-Forwarded-For", "Cookie"} {
|
||||
if r.headers.Get(k) != "" {
|
||||
t.Errorf("client header %s forwarded", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerMetricsStoreByRange(t *testing.T) {
|
||||
h := newHarness(t, Config{}, promUpstream)
|
||||
for _, path := range []string{
|
||||
"api/v1/query?query=up",
|
||||
"api/v1/query_range?query=up&step=600&start=" + unix(testNow.Add(-7*24*time.Hour)) + "&end=" + unix(testNow),
|
||||
"api/v1/label/pod/values",
|
||||
} {
|
||||
if got := h.status(http.MethodGet, metricsRoot+path, "good"); got != http.StatusOK {
|
||||
t.Fatalf("%s answered %d", path, got)
|
||||
}
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if want := []string{"shortterm", "longterm", "shortterm"}; len(h.stores) != 3 || h.stores[0] != want[0] || h.stores[1] != want[1] || h.stores[2] != want[2] {
|
||||
t.Fatalf("stores = %v, want %v", h.stores, want)
|
||||
}
|
||||
if r := h.got[2]; r.path != "/api/v1/label/pod/values" || r.form.Get("match[]") != wantMetricsScope {
|
||||
t.Fatalf("label values upstream = %s %v", r.path, r.form)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerMetricsStatusCodes(t *testing.T) {
|
||||
h := newHarness(t, Config{}, promUpstream)
|
||||
tooLong := "api/v1/query_range?query=up&step=3600&start=" + unix(testNow.Add(-31*24*time.Hour)) + "&end=" + unix(testNow)
|
||||
if got := h.status(http.MethodGet, metricsRoot+tooLong, "good"); got != http.StatusUnprocessableEntity {
|
||||
t.Errorf("range above the limit answered %d, want 422", got)
|
||||
}
|
||||
if got := h.status(http.MethodGet, metricsRoot+"api/v1/series", "good"); got != http.StatusBadRequest {
|
||||
t.Errorf("series without match[] answered %d, want 400", got)
|
||||
}
|
||||
if got := h.status(http.MethodPut, metricsRoot+"api/v1/query?query=up", "good"); got != http.StatusMethodNotAllowed {
|
||||
t.Errorf("PUT answered %d, want 405", got)
|
||||
}
|
||||
if n := len(h.requests()); n != 0 {
|
||||
t.Errorf("upstream saw %d requests for refused ones", n)
|
||||
}
|
||||
|
||||
h2 := newHarness(t, Config{}, func(w http.ResponseWriter, _ *http.Request) {
|
||||
http.Error(w, `{"status":"error","errorType":"422"}`, http.StatusUnprocessableEntity)
|
||||
})
|
||||
if got := h2.status(http.MethodGet, metricsRoot+"api/v1/query?query=up", "good"); got != http.StatusUnprocessableEntity {
|
||||
t.Errorf("upstream 422 answered as %d", got)
|
||||
}
|
||||
h2.upstream.Close()
|
||||
if got := h2.status(http.MethodGet, metricsRoot+"api/v1/query?query=up", "good"); got != http.StatusBadGateway {
|
||||
t.Errorf("unreachable upstream answered %d, want 502", got)
|
||||
}
|
||||
|
||||
release := make(chan struct{})
|
||||
t.Cleanup(func() { close(release) })
|
||||
h3 := newHarness(t, Config{RequestTimeout: 50 * time.Millisecond}, func(_ http.ResponseWriter, r *http.Request) {
|
||||
select {
|
||||
case <-release:
|
||||
case <-r.Context().Done():
|
||||
}
|
||||
})
|
||||
if got := h3.status(http.MethodGet, metricsRoot+"api/v1/query?query=up", "good"); got != http.StatusGatewayTimeout {
|
||||
t.Errorf("slow upstream answered %d, want 504", got)
|
||||
}
|
||||
|
||||
h4 := newHarness(t, Config{Limiter: LimiterConfig{RatePerUser: 0.001, RateBurst: 1}}, promUpstream)
|
||||
h4.status(http.MethodGet, metricsRoot+"api/v1/query?query=up", "good")
|
||||
limited := h4.do(http.MethodGet, metricsRoot+"api/v1/query?query=up", "good", nil, nil)
|
||||
_ = limited.Body.Close()
|
||||
if limited.StatusCode != http.StatusTooManyRequests || limited.Header.Get("Retry-After") != "1000" {
|
||||
t.Errorf("rate-limited request answered %d with Retry-After %q, want 429 with 1000",
|
||||
limited.StatusCode, limited.Header.Get("Retry-After"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerMetricsConcurrency(t *testing.T) {
|
||||
entered, release := make(chan struct{}, 3), make(chan struct{})
|
||||
h := newHarness(t, Config{Limiter: LimiterConfig{MaxConcurrentPerUser: 1}}, func(w http.ResponseWriter, _ *http.Request) {
|
||||
entered <- struct{}{}
|
||||
<-release
|
||||
promUpstream(w, nil)
|
||||
})
|
||||
// Registered after the harness so it runs before the servers close.
|
||||
var once sync.Once
|
||||
t.Cleanup(func() { once.Do(func() { close(release) }) })
|
||||
held := make(chan int, 2)
|
||||
hold := func(token string) {
|
||||
go func() { held <- h.status(http.MethodGet, metricsRoot+"api/v1/query?query=up", token) }()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatalf("request of %s did not reach the upstream", token)
|
||||
}
|
||||
}
|
||||
hold("good")
|
||||
refused := make(chan *http.Response, 1)
|
||||
go func() {
|
||||
resp := h.do(http.MethodPost, metricsRoot+"api/v1/query?query=up", "good", nil, nil)
|
||||
_ = resp.Body.Close()
|
||||
refused <- resp
|
||||
}()
|
||||
select {
|
||||
case got := <-refused:
|
||||
if got.StatusCode != http.StatusTooManyRequests || got.Header.Get("Retry-After") != "1" {
|
||||
t.Fatalf("concurrent request answered %d with Retry-After %q, want 429 with 1", got.StatusCode, got.Header.Get("Retry-After"))
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("concurrent request of the same user reached the upstream")
|
||||
}
|
||||
hold("bob-token")
|
||||
once.Do(func() { close(release) })
|
||||
for range 2 {
|
||||
if got := <-held; got != http.StatusOK {
|
||||
t.Fatalf("held request answered %d", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultMetricsUpstreamURL(t *testing.T) {
|
||||
s := NewServer(Config{ClusterDomain: "cozy.local", Metrics: MetricsConfig{RootTenant: "tenant-root"}}, nil, nil, nil)
|
||||
if got, want := s.cfg.Metrics.UpstreamURL("shortterm"), "http://vmselect-shortterm.tenant-root.svc.cozy.local:8481/select/0/prometheus"; got != want {
|
||||
t.Fatalf("upstream = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package observabilityproxy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -52,12 +53,12 @@ type mapStores map[string]string
|
||||
func (m mapStores) Resolve(ns string) (string, error) { return m[ns], nil }
|
||||
|
||||
// newHarness wires the proxy to a fake apiserver that knows tokens "good"
|
||||
// (alice) and "bob-token" (bob), both allowed list logs in tenant-a only,
|
||||
// and to an upstream served by handler.
|
||||
// (alice) and "bob-token" (bob), both allowed list logs and metrics in
|
||||
// tenant-a only, and to an upstream served by handler.
|
||||
func newHarness(t *testing.T, cfg Config, handler http.HandlerFunc) *harness {
|
||||
h := &harness{t: t}
|
||||
h.kube = newFakeKube(map[string]string{"good": "alice", "bob-token": "bob"}, func(_ string, a *authorizationv1.ResourceAttributes) bool {
|
||||
return a.Namespace == "tenant-a" && a.Resource == "logs"
|
||||
return a.Namespace == "tenant-a" && (a.Resource == "logs" || a.Resource == "metrics")
|
||||
})
|
||||
h.upstream = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
@@ -80,6 +81,13 @@ func newHarness(t *testing.T, cfg Config, handler http.HandlerFunc) *harness {
|
||||
h.mu.Unlock()
|
||||
return h.upstream.URL
|
||||
}
|
||||
cfg.Metrics.UpstreamURL = cfg.UpstreamURL
|
||||
if cfg.Metrics.ShortTermStore == "" {
|
||||
cfg.Metrics.ShortTermStore, cfg.Metrics.LongTermStore, cfg.Metrics.ShortTermRetention = "shortterm", "longterm", 72*time.Hour
|
||||
}
|
||||
if cfg.Metrics.Limits == (MetricsLimits{}) {
|
||||
cfg.Metrics.Limits = testMetricsLimits()
|
||||
}
|
||||
if cfg.Logs == (LogsLimits{}) {
|
||||
cfg.Logs = testLimits()
|
||||
}
|
||||
@@ -144,7 +152,9 @@ func TestServerStatusCodes(t *testing.T) {
|
||||
{"other namespace", http.MethodGet, "/apis/cozystack.io/v1alpha1/namespaces/tenant-b/logs/select/logsql/query", "good", http.StatusForbidden},
|
||||
{"invalid namespace", http.MethodGet, "/apis/cozystack.io/v1alpha1/namespaces/Tenant/logs/select/logsql/query", "good", http.StatusBadRequest},
|
||||
{"unknown root", http.MethodGet, "/api/v1/namespaces/tenant-a/pods", "good", http.StatusNotFound},
|
||||
{"namespace metrics not served yet", http.MethodGet, "/apis/cozystack.io/v1alpha1/namespaces/tenant-a/metrics/api/v1/query", "good", http.StatusNotFound},
|
||||
{"unknown metrics endpoint", http.MethodGet, "/apis/cozystack.io/v1alpha1/namespaces/tenant-a/metrics/api/v1/status/tsdb", "good", http.StatusNotFound},
|
||||
{"metrics in another namespace", http.MethodGet, "/apis/cozystack.io/v1alpha1/namespaces/tenant-b/metrics/api/v1/query?query=up", "good", http.StatusForbidden},
|
||||
{"metrics without token", http.MethodGet, "/apis/cozystack.io/v1alpha1/namespaces/tenant-a/metrics/api/v1/query?query=up", "", http.StatusUnauthorized},
|
||||
{"application log not served yet", http.MethodGet, "/apis/apps.cozystack.io/v1alpha1/namespaces/tenant-a/postgreses/db/log/select/logsql/query", "good", http.StatusNotFound},
|
||||
{"application metrics not served yet", http.MethodGet, "/apis/apps.cozystack.io/v1alpha1/namespaces/tenant-a/postgreses/db/metrics/api/v1/query", "good", http.StatusNotFound},
|
||||
{"tenant_ids not proxied", http.MethodGet, "/apis/cozystack.io/v1alpha1/namespaces/tenant-a/logs/select/tenant_ids", "good", http.StatusNotFound},
|
||||
@@ -413,7 +423,12 @@ func TestServerTailCapsAndIdle(t *testing.T) {
|
||||
closed <- err
|
||||
}()
|
||||
select {
|
||||
case <-closed:
|
||||
case err := <-closed:
|
||||
// The server cuts an idle tail off mid-stream rather than ending the
|
||||
// response, so the client reads an unexpected EOF; a clean end is fine too.
|
||||
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
t.Fatalf("idle tail ended with %v, want the stream to end or be cut", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("idle tail not closed")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package observabilityproxy
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// vmselectTimeMillis repeats how vmselect v1.136.0-cluster reads a time
|
||||
// argument (httputil.GetTime -> timeutil.ParseTimeMsec -> ParseTimeAt in
|
||||
// lib/timeutil/time.go) for the forms a client or the proxy may send: a
|
||||
// value without "-" is a number whose unit vmselect guesses from its size
|
||||
// (getUnixTimestampNanoseconds), anything else with a zone is RFC3339 read
|
||||
// through time.UnixNano. Relative and partial dates are left out; the
|
||||
// proxy refuses them.
|
||||
func vmselectTimeMillis(t *testing.T, s string) int64 {
|
||||
t.Helper()
|
||||
if strings.Contains(s, "-") {
|
||||
ts, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil {
|
||||
t.Fatalf("vmselect cannot parse %q: %v", s, err)
|
||||
}
|
||||
return int64(math.Round(float64(ts.UnixNano()) / 1e6))
|
||||
}
|
||||
intPart, frac, _ := strings.Cut(s, ".")
|
||||
n, err := strconv.ParseInt(intPart+frac, 10, 64)
|
||||
if err != nil {
|
||||
t.Fatalf("vmselect cannot parse %q: %v", s, err)
|
||||
}
|
||||
for e := len(frac); e%3 != 0; e++ {
|
||||
n *= 10
|
||||
}
|
||||
var nanos int64
|
||||
switch {
|
||||
case n < 1<<31:
|
||||
nanos = n * 1e9
|
||||
case n < 1e3*(1<<31):
|
||||
nanos = n * 1e6
|
||||
case n < 1e6*(1<<31):
|
||||
nanos = n * 1e3
|
||||
default:
|
||||
nanos = n
|
||||
}
|
||||
return int64(math.Round(float64(nanos) / 1e6))
|
||||
}
|
||||
|
||||
// TestVmselectReadsProxyTimes sends times across the range the proxy
|
||||
// accepts, in both client forms, and checks that vmselect reads the
|
||||
// forwarded value as the same instant the proxy validated. A number such
|
||||
// as "1000000.000" is seconds to the proxy but milliseconds-as-seconds
|
||||
// guesswork to vmselect, so the proxy must not forward one.
|
||||
func TestVmselectReadsProxyTimes(t *testing.T) {
|
||||
for _, client := range []string{
|
||||
"0", "1000000", "1762400", "2147483", "2147484", "1000000.5",
|
||||
"1793793600", "1793793600.123", "2147483647", "2147483648", "9223372035",
|
||||
"1970-01-01T00:00:00Z", "1970-01-12T13:46:40.001Z", "2026-11-04T12:00:00.123Z", "2262-01-01T00:00:00Z",
|
||||
} {
|
||||
want, herr := parseMetricsTime("time", client)
|
||||
if herr != nil {
|
||||
t.Fatalf("%q refused: %v", client, herr)
|
||||
}
|
||||
req := buildMetrics(t, "api/v1/query", url.Values{"query": {"up"}, "time": {client}})
|
||||
sent := req.params.Get("time")
|
||||
if got := vmselectTimeMillis(t, sent); got != want.UnixMilli() {
|
||||
t.Errorf("client time %q forwarded as %q reaches vmselect as %dms, want %dms", client, sent, got, want.UnixMilli())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestVmselectReadsSecondsWithFraction is the window that passed the proxy as
|
||||
// 8.8 days and 30 points while vmselect, reading "1000000.000" as a
|
||||
// thousand times that many seconds, saw 2001..2025 and 30000 points.
|
||||
func TestVmselectReadsSecondsWithFraction(t *testing.T) {
|
||||
in := url.Values{"query": {"up"}, "start": {"1000000"}, "end": {"1762400"}, "step": {"25414"}}
|
||||
req := buildMetrics(t, "api/v1/query_range", in)
|
||||
start, end := vmselectTimeMillis(t, req.params.Get("start")), vmselectTimeMillis(t, req.params.Get("end"))
|
||||
step := vmselectMillis(t, req.params.Get("step"))
|
||||
if start != 1000000*1000 || end != 1762400*1000 {
|
||||
t.Fatalf("vmselect reads %dms..%dms, want 1000000s..1762400s", start, end)
|
||||
}
|
||||
if points := (end-start)/step + 1; points != 30 {
|
||||
t.Fatalf("vmselect counts %d points, want the 30 the proxy checked", points)
|
||||
}
|
||||
labels := buildMetrics(t, "api/v1/labels", url.Values{"start": {"1000000"}, "end": {"1000300"}})
|
||||
if got := vmselectTimeMillis(t, labels.params.Get("start")); got != 1000000*1000 {
|
||||
t.Fatalf("label API start reaches vmselect as %dms, want 1000000s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMetricsTimeRange(t *testing.T) {
|
||||
// vmselect reads an RFC3339 time through time.UnixNano, which is
|
||||
// undefined from 2262-04-11 on.
|
||||
// Before 1970 vmselect clamps to 0, which would quietly answer for a
|
||||
// window other than the one asked for.
|
||||
for _, v := range []string{"9223372037", "9999999999", "2262-04-12T00:00:00Z", "9999-01-01T00:00:00Z", "1969-12-31T23:59:59Z", "1969-12-31T23:59:59.999Z"} {
|
||||
if _, herr := parseMetricsTime("time", v); herr == nil {
|
||||
t.Errorf("%q accepted, want refused", v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -790,15 +790,6 @@ func scanRecordingClient(scheme *runtime.Scheme, listed *[]string, writes *int,
|
||||
}).Build()
|
||||
}
|
||||
|
||||
func listedKind(listed []string, kind string) bool {
|
||||
for _, l := range listed {
|
||||
if strings.Contains(l, kind) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// The comparison guarding that write has to be semantic rather than structural, or a
|
||||
// LimitRange written in a different but equivalent notation would re-apply on every
|
||||
// reconcile — invisible in a test that only looked at the resulting object, since every one
|
||||
|
||||
+53
-18
@@ -51,6 +51,9 @@ type Server struct {
|
||||
|
||||
clientsMu sync.RWMutex
|
||||
clients map[*client]struct{}
|
||||
// closing is set by Shutdown under clientsMu, so no stream registers
|
||||
// after the clients it stops.
|
||||
closing bool
|
||||
|
||||
// latest holds the most recent payload (sent to new clients on connect).
|
||||
latestMu sync.RWMutex
|
||||
@@ -60,6 +63,7 @@ type Server struct {
|
||||
type client struct {
|
||||
ch chan string
|
||||
closeCh chan struct{}
|
||||
closeOnce sync.Once
|
||||
flusher http.Flusher
|
||||
w http.ResponseWriter
|
||||
req *http.Request
|
||||
@@ -121,8 +125,9 @@ func (s *Server) ListenAndServe() error {
|
||||
// Shutdown gracefully stops the server.
|
||||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
s.clientsMu.Lock()
|
||||
s.closing = true
|
||||
for c := range s.clients {
|
||||
close(c.closeCh)
|
||||
c.stop()
|
||||
}
|
||||
s.clientsMu.Unlock()
|
||||
return s.http.Shutdown(ctx)
|
||||
@@ -183,11 +188,24 @@ func (s *Server) handleStream(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Register client
|
||||
s.clientsMu.Lock()
|
||||
if s.closing {
|
||||
s.clientsMu.Unlock()
|
||||
http.Error(w, "server is shutting down", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
s.clients[c] = struct{}{}
|
||||
s.clientsMu.Unlock()
|
||||
|
||||
defer func() {
|
||||
s.clientsMu.Lock()
|
||||
delete(s.clients, c)
|
||||
s.clientsMu.Unlock()
|
||||
}()
|
||||
|
||||
// Initial comment to open the stream for some proxies
|
||||
fmt.Fprintf(w, ": connected %s\n\n", time.Now().Format(time.RFC3339))
|
||||
if _, err := fmt.Fprintf(w, ": connected %s\n\n", time.Now().Format(time.RFC3339)); err != nil {
|
||||
return
|
||||
}
|
||||
flusher.Flush()
|
||||
|
||||
// Send latest if any
|
||||
@@ -195,21 +213,31 @@ func (s *Server) handleStream(w http.ResponseWriter, r *http.Request) {
|
||||
latest := s.latest
|
||||
s.latestMu.RUnlock()
|
||||
if latest != "" {
|
||||
writeSSE(w, latest)
|
||||
if err := writeSSE(w, latest); err != nil {
|
||||
return
|
||||
}
|
||||
flusher.Flush()
|
||||
}
|
||||
|
||||
// Start pump
|
||||
go c.pump()
|
||||
// A write the client no longer takes ends the pump; the handler then
|
||||
// gives the client up without waiting for the request to end.
|
||||
pumped := make(chan struct{})
|
||||
go func() {
|
||||
c.pump()
|
||||
close(pumped)
|
||||
}()
|
||||
select {
|
||||
case <-r.Context().Done():
|
||||
case <-pumped:
|
||||
}
|
||||
c.stop()
|
||||
<-pumped
|
||||
}
|
||||
|
||||
// Block until client disconnects
|
||||
<-r.Context().Done()
|
||||
|
||||
// Unregister client
|
||||
close(c.closeCh)
|
||||
s.clientsMu.Lock()
|
||||
delete(s.clients, c)
|
||||
s.clientsMu.Unlock()
|
||||
// stop ends the client's pump. Shutdown and the client's own handler both
|
||||
// stop it, in either order.
|
||||
func (c *client) stop() {
|
||||
c.closeOnce.Do(func() { close(c.closeCh) })
|
||||
}
|
||||
|
||||
func (c *client) pump() {
|
||||
@@ -220,23 +248,30 @@ func (c *client) pump() {
|
||||
case <-c.closeCh:
|
||||
return
|
||||
case msg := <-c.ch:
|
||||
writeSSE(c.w, msg)
|
||||
if err := writeSSE(c.w, msg); err != nil {
|
||||
return
|
||||
}
|
||||
c.flusher.Flush()
|
||||
case <-t.C:
|
||||
// heartbeat comment (keeps connections alive through proxies)
|
||||
fmt.Fprint(c.w, ": hb\n\n")
|
||||
if _, err := fmt.Fprint(c.w, ": hb\n\n"); err != nil {
|
||||
return
|
||||
}
|
||||
c.flusher.Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeSSE(w http.ResponseWriter, msg string) {
|
||||
func writeSSE(w http.ResponseWriter, msg string) error {
|
||||
// Split on lines; each needs its own "data:" field per the SSE spec
|
||||
lines := strings.SplitSeq(strings.TrimRight(msg, "\n"), "\n")
|
||||
for ln := range lines {
|
||||
fmt.Fprintf(w, "data: %s\n", ln)
|
||||
if _, err := fmt.Fprintf(w, "data: %s\n", ln); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Fprint(w, "\n")
|
||||
_, err := fmt.Fprint(w, "\n")
|
||||
return err
|
||||
}
|
||||
|
||||
// Minimal index page with live updates
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package sse
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// streamWriter accepts the first okWrites writes and fails every one after.
|
||||
type streamWriter struct {
|
||||
mu sync.Mutex
|
||||
header http.Header
|
||||
okWrites int
|
||||
body strings.Builder
|
||||
}
|
||||
|
||||
func (w *streamWriter) Header() http.Header { return w.header }
|
||||
func (w *streamWriter) WriteHeader(int) {}
|
||||
func (w *streamWriter) Flush() {}
|
||||
|
||||
func (w *streamWriter) Write(p []byte) (int, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.okWrites == 0 {
|
||||
return 0, errors.New("connection reset")
|
||||
}
|
||||
w.okWrites--
|
||||
return w.body.Write(p)
|
||||
}
|
||||
|
||||
func (w *streamWriter) String() string {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
return w.body.String()
|
||||
}
|
||||
|
||||
// serve runs the stream handler for a client that never disconnects and
|
||||
// reports whether the handler gave the client up within a second.
|
||||
func serve(t *testing.T, s *Server, w *streamWriter, publish string) bool {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
r := httptest.NewRequest(http.MethodGet, "/stream", nil).WithContext(ctx)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
s.handleStream(w, r)
|
||||
close(done)
|
||||
}()
|
||||
if publish != "" {
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for len(s.snapshotClients()) == 0 && time.Now().Before(deadline) {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
// Onto the client's queue, which only the pump reads, so the failing
|
||||
// write is the pump's and not the handler's write of latest.
|
||||
for _, c := range s.snapshotClients() {
|
||||
c.ch <- publish
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
return len(s.snapshotClients()) == 0
|
||||
case <-time.After(time.Second):
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) snapshotClients() []*client {
|
||||
s.clientsMu.RLock()
|
||||
defer s.clientsMu.RUnlock()
|
||||
var out []*client
|
||||
for c := range s.clients {
|
||||
out = append(out, c)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func newTestServer(heartbeat time.Duration) *Server {
|
||||
return New(Options{Heartbeat: heartbeat, Logger: log.New(io.Discard, "", 0)})
|
||||
}
|
||||
|
||||
// A client whose connection stops taking writes is dropped at once, at
|
||||
// whichever write first fails, instead of holding its handler until the
|
||||
// request context ends.
|
||||
func TestStreamDropsClientOnWriteFailure(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
okWrites int
|
||||
latest string
|
||||
publish string
|
||||
heartbeat time.Duration
|
||||
}{
|
||||
{name: "connect comment", okWrites: 0, heartbeat: time.Hour},
|
||||
{name: "latest payload", okWrites: 1, latest: "state", heartbeat: time.Hour},
|
||||
{name: "published event", okWrites: 1, publish: "event", heartbeat: time.Hour},
|
||||
{name: "heartbeat", okWrites: 1, heartbeat: 10 * time.Millisecond},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := newTestServer(tc.heartbeat)
|
||||
if tc.latest != "" {
|
||||
s.Publish(tc.latest)
|
||||
}
|
||||
if !serve(t, s, &streamWriter{header: http.Header{}, okWrites: tc.okWrites}, tc.publish) {
|
||||
t.Fatal("the handler kept a client whose writes fail")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Each line of a payload is its own data field, and the event ends with an
|
||||
// empty line.
|
||||
func TestStreamFramesMultilinePayload(t *testing.T) {
|
||||
s := newTestServer(time.Hour)
|
||||
s.Publish("one\ntwo\n")
|
||||
w := &streamWriter{header: http.Header{}, okWrites: 100}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
r := httptest.NewRequest(http.MethodGet, "/stream", nil).WithContext(ctx)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
s.handleStream(w, r)
|
||||
close(done)
|
||||
}()
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for !strings.Contains(w.String(), "data: two\n\n") && time.Now().Before(deadline) {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
cancel()
|
||||
<-done
|
||||
if got := w.String(); !strings.Contains(got, "data: one\ndata: two\n\n") {
|
||||
t.Fatalf("stream = %q, want both lines framed as one event", got)
|
||||
}
|
||||
if len(s.snapshotClients()) != 0 {
|
||||
t.Fatal("a disconnected client stayed registered")
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown ends every open stream, and the handler that then returns does
|
||||
// not close the client a second time.
|
||||
func TestShutdownEndsOpenStreams(t *testing.T) {
|
||||
s := newTestServer(time.Hour)
|
||||
w := &streamWriter{header: http.Header{}, okWrites: 100}
|
||||
r := httptest.NewRequest(http.MethodGet, "/stream", nil)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
s.handleStream(w, r)
|
||||
close(done)
|
||||
}()
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for len(s.snapshotClients()) == 0 && time.Now().Before(deadline) {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
if err := s.Shutdown(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Shutdown left the stream open")
|
||||
}
|
||||
}
|
||||
|
||||
// blockingWriter holds every write after the first until release is closed.
|
||||
type blockingWriter struct {
|
||||
streamWriter
|
||||
writing chan struct{}
|
||||
release chan struct{}
|
||||
returned chan struct{}
|
||||
}
|
||||
|
||||
func (w *blockingWriter) Write(p []byte) (int, error) {
|
||||
if w.String() == "" {
|
||||
return w.streamWriter.Write(p)
|
||||
}
|
||||
select {
|
||||
case w.writing <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
<-w.release
|
||||
return w.streamWriter.Write(p)
|
||||
}
|
||||
|
||||
// The handler does not return while its pump is still writing: net/http does
|
||||
// not allow a write to a response after ServeHTTP has returned.
|
||||
func TestStreamWaitsForPumpBeforeReturning(t *testing.T) {
|
||||
s := newTestServer(time.Hour)
|
||||
w := &blockingWriter{
|
||||
streamWriter: streamWriter{header: http.Header{}, okWrites: 100},
|
||||
writing: make(chan struct{}, 1),
|
||||
release: make(chan struct{}),
|
||||
returned: make(chan struct{}),
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
r := httptest.NewRequest(http.MethodGet, "/stream", nil).WithContext(ctx)
|
||||
go func() {
|
||||
s.handleStream(w, r)
|
||||
close(w.returned)
|
||||
}()
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for len(s.snapshotClients()) == 0 && time.Now().Before(deadline) {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
// Straight onto the client's queue, which only the pump reads: Publish
|
||||
// would also set latest, which the handler itself may still write.
|
||||
s.snapshotClients()[0].ch <- "event"
|
||||
select {
|
||||
case <-w.writing:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("the pump never wrote the event")
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case <-w.returned:
|
||||
close(w.release)
|
||||
t.Fatal("the handler returned while its pump was writing")
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
close(w.release)
|
||||
select {
|
||||
case <-w.returned:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("the handler did not return once its pump finished")
|
||||
}
|
||||
}
|
||||
|
||||
// A stream that opens once Shutdown has run is refused instead of being
|
||||
// registered after the clients were stopped, where nothing would end it.
|
||||
func TestStreamOpenedDuringShutdownIsRefused(t *testing.T) {
|
||||
s := newTestServer(time.Hour)
|
||||
if err := s.Shutdown(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
rec := httptest.NewRecorder()
|
||||
r := httptest.NewRequest(http.MethodGet, "/stream", nil).WithContext(ctx)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
s.handleStream(rec, r)
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("a stream opened after Shutdown stayed open")
|
||||
}
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Errorf("status = %d, want %d", rec.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
if len(s.snapshotClients()) != 0 {
|
||||
t.Error("a stream opened after Shutdown was registered")
|
||||
}
|
||||
}
|
||||
@@ -118,11 +118,11 @@ func (c *Collector) collect(ctx context.Context) {
|
||||
|
||||
// Write application count metrics
|
||||
for kind, count := range appKindCounts {
|
||||
metrics.WriteString(fmt.Sprintf(
|
||||
fmt.Fprintf(&metrics,
|
||||
"cozy_application_count{kind=\"%s\"} %d\n",
|
||||
kind,
|
||||
count,
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
// Send metrics only if there's something to send
|
||||
@@ -147,7 +147,7 @@ func (c *Collector) sendMetrics(clusterID, metrics string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
|
||||
@@ -133,11 +133,11 @@ func (c *OperatorCollector) collect(ctx context.Context) {
|
||||
var metrics strings.Builder
|
||||
|
||||
// Add cluster info metric
|
||||
metrics.WriteString(fmt.Sprintf(
|
||||
fmt.Fprintf(&metrics,
|
||||
"cozy_cluster_info{cozystack_version=\"%s\",kubernetes_version=\"%s\"} 1\n",
|
||||
version.Version,
|
||||
k8sVersion.GitVersion,
|
||||
))
|
||||
)
|
||||
|
||||
// Collect node metrics grouped by OS and kernel
|
||||
nodeOSCount := make(map[string]map[string]int) // os -> kernel -> count
|
||||
@@ -153,12 +153,12 @@ func (c *OperatorCollector) collect(ctx context.Context) {
|
||||
|
||||
for osKey, kernels := range nodeOSCount {
|
||||
for kernel, count := range kernels {
|
||||
metrics.WriteString(fmt.Sprintf(
|
||||
fmt.Fprintf(&metrics,
|
||||
"cozy_nodes_count{os=\"%s\",kernel=\"%s\"} %d\n",
|
||||
osKey,
|
||||
kernel,
|
||||
count,
|
||||
))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,11 +174,11 @@ func (c *OperatorCollector) collect(ctx context.Context) {
|
||||
}
|
||||
|
||||
for resourceName, total := range capacityTotals {
|
||||
metrics.WriteString(fmt.Sprintf(
|
||||
fmt.Fprintf(&metrics,
|
||||
"cozy_cluster_capacity{resource=\"%s\"} %d\n",
|
||||
resourceName,
|
||||
total,
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
// Collect LoadBalancer services metrics
|
||||
@@ -192,7 +192,7 @@ func (c *OperatorCollector) collect(ctx context.Context) {
|
||||
lbCount++
|
||||
}
|
||||
}
|
||||
metrics.WriteString(fmt.Sprintf("cozy_loadbalancers_count %d\n", lbCount))
|
||||
fmt.Fprintf(&metrics, "cozy_loadbalancers_count %d\n", lbCount)
|
||||
}
|
||||
|
||||
// Collect PV metrics grouped by driver and size
|
||||
@@ -224,12 +224,12 @@ func (c *OperatorCollector) collect(ctx context.Context) {
|
||||
|
||||
for size, drivers := range pvMetrics {
|
||||
for driver, count := range drivers {
|
||||
metrics.WriteString(fmt.Sprintf(
|
||||
fmt.Fprintf(&metrics,
|
||||
"cozy_pvs_count{driver=\"%s\",size=\"%s\"} %d\n",
|
||||
driver,
|
||||
size,
|
||||
count,
|
||||
))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -244,11 +244,11 @@ func (c *OperatorCollector) collect(ctx context.Context) {
|
||||
if variant == "" {
|
||||
variant = "default"
|
||||
}
|
||||
metrics.WriteString(fmt.Sprintf(
|
||||
fmt.Fprintf(&metrics,
|
||||
"cozy_package_info{name=\"%s\",variant=\"%s\"} 1\n",
|
||||
pkg.Name,
|
||||
variant,
|
||||
))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,7 +272,7 @@ func (c *OperatorCollector) sendMetrics(clusterID, metrics string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
||||
|
||||
@@ -424,7 +424,8 @@ func (w *WrappedControllerService) ControllerUnpublishVolume(ctx context.Context
|
||||
pvc, err := w.infraClient.CoreV1().PersistentVolumeClaims(w.infraNamespace).Get(ctx, dvName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
return &csi.ControllerUnpublishVolumeResponse{}, nil
|
||||
// A failed PVC creation can still leave a hotplug entry in the VM.
|
||||
return w.ControllerService.ControllerUnpublishVolume(ctx, req)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -310,15 +310,19 @@ const (
|
||||
testVolumeName = "pvc-0"
|
||||
)
|
||||
|
||||
// teardownClient models the CAPI/CAPK teardown race: the VM is gone while its
|
||||
// VMI still reports the hot-plugged volume. The VMI drops the volume once
|
||||
// RemoveVolumeFromVMI is called, or when the garbage collector deletes a
|
||||
// VMI whose controlling VM is gone.
|
||||
// teardownClient keeps hotplug state until removal or garbage collection,
|
||||
// including when the VMI outlives its VM during CAPI/CAPK teardown.
|
||||
type teardownClient struct {
|
||||
kubevirtclient.Client
|
||||
vmiOwners []metav1.OwnerReference
|
||||
vmiRemovals int
|
||||
lookups int
|
||||
vm *kubevirtv1.VirtualMachine
|
||||
vmiOwners []metav1.OwnerReference
|
||||
detached bool
|
||||
vmiGone bool
|
||||
vmiGoneAfterCheck bool
|
||||
vmiLookups int
|
||||
vmRemovals int
|
||||
vmiRemovals int
|
||||
lookups int
|
||||
}
|
||||
|
||||
func (c *teardownClient) vmiGarbageCollected() bool {
|
||||
@@ -328,22 +332,46 @@ func (c *teardownClient) vmiGarbageCollected() bool {
|
||||
|
||||
func (c *teardownClient) GetWorkloadManagingVirtualMachine(context.Context, string, string) (*kubevirtv1.VirtualMachine, error) {
|
||||
c.lookups++
|
||||
if c.vm != nil {
|
||||
return c.vm, nil
|
||||
}
|
||||
return nil, k8serrors.NewNotFound(schema.GroupResource{Group: "kubevirt.io", Resource: "virtualmachines"}, testVMName)
|
||||
}
|
||||
|
||||
func (c *teardownClient) GetVirtualMachine(context.Context, string, string) (*kubevirtv1.VirtualMachineInstance, error) {
|
||||
c.lookups++
|
||||
c.vmiLookups++
|
||||
if c.vmiGone {
|
||||
return nil, k8serrors.NewNotFound(schema.GroupResource{Group: "kubevirt.io", Resource: "virtualmachineinstances"}, testVMName)
|
||||
}
|
||||
vmi := &kubevirtv1.VirtualMachineInstance{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: testVMName, Namespace: testInfraNamespace, OwnerReferences: c.vmiOwners},
|
||||
}
|
||||
if c.vmiRemovals == 0 {
|
||||
vmi.Status.VolumeStatus = []kubevirtv1.VolumeStatus{{Name: testVolumeName, HotplugVolume: &kubevirtv1.HotplugVolumeStatus{}}}
|
||||
if !c.detached && c.vmRemovals == 0 && c.vmiRemovals == 0 {
|
||||
vmi.Status.VolumeStatus = []kubevirtv1.VolumeStatus{{Name: testVolumeName, Phase: kubevirtv1.VolumePending, Reason: "FailedPvcNotFound", HotplugVolume: &kubevirtv1.HotplugVolumeStatus{}}}
|
||||
}
|
||||
return vmi, nil
|
||||
}
|
||||
|
||||
func (c *teardownClient) EnsureVolumeRemovedVM(context.Context, string, string, string) (bool, error) {
|
||||
return c.vm == nil || len(c.vm.Spec.Template.Spec.Volumes) == 0, nil
|
||||
}
|
||||
|
||||
func (c *teardownClient) EnsureVolumeRemovedVMI(context.Context, string, string, string) (bool, error) {
|
||||
return c.vmiRemovals > 0, nil
|
||||
removed := c.vmiGone || c.detached || c.vmRemovals > 0 || c.vmiRemovals > 0
|
||||
if c.vmiGoneAfterCheck {
|
||||
c.vmiGone = true
|
||||
}
|
||||
return removed, nil
|
||||
}
|
||||
|
||||
func (c *teardownClient) RemoveVolumeFromVM(_ context.Context, namespace, name string, opts *kubevirtv1.RemoveVolumeOptions) error {
|
||||
if namespace != testInfraNamespace || name != testVMName || opts.Name != testVolumeName {
|
||||
return errors.New("unexpected hotplug removal target")
|
||||
}
|
||||
c.vmRemovals++
|
||||
c.vm.Spec.Template.Spec.Volumes = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *teardownClient) RemoveVolumeFromVMI(context.Context, string, string, *kubevirtv1.RemoveVolumeOptions) error {
|
||||
@@ -352,7 +380,7 @@ func (c *teardownClient) RemoveVolumeFromVMI(context.Context, string, string, *k
|
||||
}
|
||||
|
||||
func (c *teardownClient) EnsureVolumeRemoved(context.Context, string, string, string, time.Duration) error {
|
||||
if c.vmiRemovals > 0 || c.vmiGarbageCollected() {
|
||||
if c.vmiGone || c.detached || c.vmRemovals > 0 || c.vmiRemovals > 0 || c.vmiGarbageCollected() {
|
||||
return nil
|
||||
}
|
||||
return errors.New("timed out waiting for the VMI to drop the volume")
|
||||
@@ -410,6 +438,58 @@ func TestControllerUnpublishVolumeHonoursVMIHotplugFallback(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerUnpublishVolumeMissingPVC(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
vmGone bool
|
||||
detached bool
|
||||
vmiGone bool
|
||||
vmiGoneAfterCheck bool
|
||||
wantVMILookups int
|
||||
wantVMCalls int
|
||||
wantVMICalls int
|
||||
}{
|
||||
{name: "pending hotplug volume", wantVMCalls: 1},
|
||||
{name: "already detached", detached: true},
|
||||
{name: "VM gone, VMI still has volume", vmGone: true, wantVMICalls: 1, wantVMILookups: 1},
|
||||
{name: "VM gone, no attached volume", vmGone: true, detached: true},
|
||||
{name: "VM and VMI gone", vmGone: true, vmiGone: true},
|
||||
{name: "VMI disappears after attachment check", vmGone: true, vmiGoneAfterCheck: true, wantVMILookups: 1},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
client := &teardownClient{detached: tt.detached, vmiGone: tt.vmiGone, vmiGoneAfterCheck: tt.vmiGoneAfterCheck}
|
||||
if !tt.vmGone {
|
||||
client.vm = &kubevirtv1.VirtualMachine{Spec: kubevirtv1.VirtualMachineSpec{Template: &kubevirtv1.VirtualMachineInstanceTemplateSpec{}}}
|
||||
if !tt.detached {
|
||||
client.vm.Spec.Template.Spec.Volumes = []kubevirtv1.Volume{{
|
||||
Name: testVolumeName,
|
||||
VolumeSource: kubevirtv1.VolumeSource{DataVolume: &kubevirtv1.DataVolumeSource{Name: testVolumeName, Hotpluggable: true}},
|
||||
}}
|
||||
}
|
||||
}
|
||||
w := &WrappedControllerService{
|
||||
ControllerService: service.NewControllerService(client, testInfraNamespace, nil, util.StorageClassEnforcement{AllowAll: true}, true),
|
||||
infraClient: fake.NewClientset(),
|
||||
infraNamespace: testInfraNamespace,
|
||||
}
|
||||
req := &csi.ControllerUnpublishVolumeRequest{VolumeId: testVolumeName, NodeId: testInfraNamespace + "/" + testVMName}
|
||||
if _, err := w.ControllerUnpublishVolume(context.Background(), req); err != nil {
|
||||
t.Fatalf("ControllerUnpublishVolume() error = %v", err)
|
||||
}
|
||||
if client.vmRemovals != tt.wantVMCalls || client.vmiRemovals != tt.wantVMICalls {
|
||||
t.Errorf("VM/VMI removevolume calls = %d/%d, want %d/%d", client.vmRemovals, client.vmiRemovals, tt.wantVMCalls, tt.wantVMICalls)
|
||||
}
|
||||
if client.vmiLookups != tt.wantVMILookups {
|
||||
t.Errorf("GetVirtualMachine calls = %d, want %d", client.vmiLookups, tt.wantVMILookups)
|
||||
}
|
||||
if client.vm != nil && len(client.vm.Spec.Template.Spec.Volumes) != 0 {
|
||||
t.Error("hotplug volume remains in VM spec")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func nfsCNP(owners ...string) *unstructured.Unstructured {
|
||||
refs := make([]interface{}, 0, len(owners))
|
||||
for _, owner := range owners {
|
||||
|
||||
@@ -13,7 +13,7 @@ set:
|
||||
s3CredentialsSecret:
|
||||
name: postgres-backup
|
||||
tests:
|
||||
- it: sets resources for native backup without inheriting the tenant 128Mi limit
|
||||
- it: sets resources for native backup without inheriting the tenant 128Mi and 250m limits
|
||||
set:
|
||||
backup.enabled: true
|
||||
asserts:
|
||||
@@ -24,15 +24,11 @@ tests:
|
||||
cpu: 100m
|
||||
memory: 256Mi
|
||||
limits:
|
||||
cpu: "1"
|
||||
memory: 1Gi
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: ObjectStore
|
||||
- notExists:
|
||||
path: spec.instanceSidecarConfiguration.resources.limits.cpu
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: ObjectStore
|
||||
- equal:
|
||||
path: spec.configuration.s3Credentials.accessKeyId.name
|
||||
value: postgres-backup
|
||||
|
||||
@@ -39,11 +39,14 @@ Service utilizes the freshworks-oss Redis Operator (a maintained fork of the arc
|
||||
|
||||
### Application-specific parameters
|
||||
|
||||
| Name | Description | Type | Value |
|
||||
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------ |
|
||||
| `authEnabled` | Enable password generation. | `bool` | `true` |
|
||||
| `minReplicasToWrite` | Redis `min-replicas-to-write`: the master refuses writes unless at least this many replicas are connected and within `minReplicasMaxLag`. Unset by default, which leaves the Redis default (disabled). With `replicas: 2` and a value of 1 the master rejects writes while its only replica is down, trading availability for a bound on the writes lost if an isolated master is failed over. Replication stays asynchronous, so this bounds the loss, it does not remove it. Must be 0 or more and below `replicas` (a master with `replicas` pods has `replicas - 1` replicas), otherwise every write is refused; the chart refuses such a value. | `*int` | `null` |
|
||||
| `minReplicasMaxLag` | Redis `min-replicas-max-lag`, in seconds: a replica whose last acknowledgement is older than this does not count towards `minReplicasToWrite`. Unset by default, which leaves the Redis default (10). Has no effect unless `minReplicasToWrite` is set. Must be at least 1 when `minReplicasToWrite` is set: Redis treats 0 as disabling the check, so the chart refuses it. | `*int` | `null` |
|
||||
| Name | Description | Type | Value |
|
||||
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ------------ |
|
||||
| `authEnabled` | Enable password generation. | `bool` | `true` |
|
||||
| `minReplicasToWrite` | Redis `min-replicas-to-write`: the master refuses writes unless at least this many replicas are connected and within `minReplicasMaxLag`. Unset by default, which leaves the Redis default (disabled). With `replicas: 2` and a value of 1 the master rejects writes while its only replica is down, trading availability for a bound on the writes lost if an isolated master is failed over. Replication stays asynchronous, so this bounds the loss, it does not remove it. Must be 0 or more and below `replicas` (a master with `replicas` pods has `replicas - 1` replicas), otherwise every write is refused; the chart refuses such a value. | `*int` | `null` |
|
||||
| `minReplicasMaxLag` | Redis `min-replicas-max-lag`, in seconds: a replica whose last acknowledgement is older than this does not count towards `minReplicasToWrite`. Unset by default, which leaves the Redis default (10). Has no effect unless `minReplicasToWrite` is set. Must be at least 1 when `minReplicasToWrite` is set: Redis treats 0 as disabling the check, so the chart refuses it. | `*int` | `null` |
|
||||
| `maxmemory` | Redis `maxmemory`: the dataset size at which Redis applies `maxmemoryPolicy`. Unset by default, which renders `maxmemory 0` (no limit, the Redis default), so a value set by hand with `CONFIG SET` is reset on the next reconcile. Must be at most 90% of the memory request of each Redis pod (from `resources` or `resourcesPreset`): the operator silently drops a larger value, so the chart refuses it. Leave headroom below that for replication and snapshot buffers. | `quantity` | `""` |
|
||||
| `maxmemoryPolicy` | Redis `maxmemory-policy`: what Redis does once `maxmemory` is reached. `noeviction` refuses writes; `allkeys-lru` evicts the least recently used keys, which suits an instance used purely as a cache. Has no effect while `maxmemory` is unset. | `string` | `noeviction` |
|
||||
| `snapshots` | Write RDB snapshots to the persistent volume (the Redis `save` directive, with the Redis default schedule). Set to `false` for an instance used purely as a cache: `save ""` is rendered and the volume is kept, so no further scheduled snapshot is written while replicas still have room for the full-sync RDB they write to disk. An RDB file already on the volume is not removed, and Redis loads it on restart, so turning snapshots off does not by itself empty the dataset. Always off when `size` is empty. | `bool` | `true` |
|
||||
|
||||
|
||||
### Sentinel parameters
|
||||
|
||||
@@ -116,8 +116,41 @@ spec:
|
||||
- loglevel notice
|
||||
{{- if not .Values.size }}
|
||||
- appendonly no
|
||||
{{- end }}
|
||||
{{- /* The operator applies these lines with CONFIG SET on every reconcile and never
|
||||
resets a line that disappears, so each one is always rendered with an explicit
|
||||
value: switching back to the default must reach the running pods too. The
|
||||
schedule is the Redis 7 and 8 default. */}}
|
||||
{{- if and .Values.size .Values.snapshots }}
|
||||
- save 3600 1 300 100 60 10000
|
||||
{{- else }}
|
||||
- save ""
|
||||
{{- end }}
|
||||
{{- $maxmemory := 0 }}
|
||||
{{- if not (kindIs "invalid" .Values.maxmemory) }}
|
||||
{{- $maxmemoryBytes := include "cozy-lib.resources.toFloat" (.Values.maxmemory | toString) | float64 }}
|
||||
{{- /* Checked before the int64 cast, which would truncate a sub-byte quantity such
|
||||
as 512m (0.512 bytes) or -512m to 0, and Redis reads 0 as no limit. */}}
|
||||
{{- if lt $maxmemoryBytes 0.0 }}
|
||||
{{- fail (printf "maxmemory (%v) must not be negative" .Values.maxmemory) }}
|
||||
{{- end }}
|
||||
{{- if ne (floor $maxmemoryBytes) $maxmemoryBytes }}
|
||||
{{- fail (printf "maxmemory (%v) must be a whole number of bytes, e.g. 512Mi rather than 512m (millibytes)" .Values.maxmemory) }}
|
||||
{{- end }}
|
||||
{{- $maxmemory = $maxmemoryBytes | int64 }}
|
||||
{{- $redisResources := include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | fromYaml }}
|
||||
{{- $memoryRequest := include "cozy-lib.resources.toFloat" (dig "requests" "memory" "0" $redisResources | toString) | float64 }}
|
||||
{{- /* The operator compares maxmemory with 90% of the pod's memory request
|
||||
(reservedPodMemoryPercent) and silently skips the line when it is larger. */}}
|
||||
{{- $allowed := mulf $memoryRequest 0.9 | int64 }}
|
||||
{{- if gt $maxmemory $allowed }}
|
||||
{{- fail (printf "maxmemory (%v, %d bytes) must be at most 90%% of the Redis memory request (%d bytes): the operator ignores a larger value" .Values.maxmemory $maxmemory $allowed) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- /* The operator sends these in list order, and lowering maxmemory below the
|
||||
dataset evicts at once, so the policy has to be in place first. */}}
|
||||
- maxmemory-policy {{ .Values.maxmemoryPolicy | default "noeviction" }}
|
||||
- maxmemory {{ $maxmemory }}
|
||||
{{- /* A master with `replicas` pods has replicas-1 replicas, so a requirement of
|
||||
`replicas` or more can never be met and the master would refuse every write. */}}
|
||||
{{- if and (not (kindIs "invalid" .Values.minReplicasToWrite)) (ge (.Values.minReplicasToWrite | int) (.Values.replicas | int)) }}
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
suite: Redis cache tuning (maxmemory, eviction, snapshots)
|
||||
|
||||
# The operator applies customConfig with CONFIG SET and never resets a line
|
||||
# that disappears, so every directive here is rendered with an explicit value
|
||||
# even at its default: reverting a setting has to reach the running pods.
|
||||
|
||||
release:
|
||||
name: my-redis
|
||||
namespace: tenant-test
|
||||
|
||||
set:
|
||||
_cluster:
|
||||
cluster-domain: cluster.local
|
||||
_namespace:
|
||||
host: example.com
|
||||
|
||||
templates:
|
||||
- templates/redisfailover.yaml
|
||||
|
||||
tests:
|
||||
- it: "renders the Redis defaults explicitly"
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: RedisFailover
|
||||
asserts:
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: save 3600 1 300 100 60 10000
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: maxmemory 0
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: maxmemory-policy noeviction
|
||||
- notContains:
|
||||
path: spec.redis.customConfig
|
||||
content: appendonly no
|
||||
|
||||
- it: "disables snapshots while keeping the volume"
|
||||
set:
|
||||
snapshots: false
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: RedisFailover
|
||||
asserts:
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: save ""
|
||||
- notContains:
|
||||
path: spec.redis.customConfig
|
||||
content: save 3600 1 300 100 60 10000
|
||||
- equal:
|
||||
path: spec.redis.storage.persistentVolumeClaim.spec.resources.requests.storage
|
||||
value: 1Gi
|
||||
|
||||
- it: "keeps snapshots off without a volume even when enabled"
|
||||
set:
|
||||
size: null
|
||||
snapshots: true
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: RedisFailover
|
||||
asserts:
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: save ""
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: appendonly no
|
||||
- notExists:
|
||||
path: spec.redis.storage
|
||||
|
||||
- it: "renders maxmemory in bytes with the chosen policy"
|
||||
set:
|
||||
resourcesPreset: t1.large
|
||||
maxmemory: 1Gi
|
||||
maxmemoryPolicy: allkeys-lru
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: RedisFailover
|
||||
asserts:
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: maxmemory 1073741824
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: maxmemory-policy allkeys-lru
|
||||
|
||||
- it: "sizes the maxmemory ceiling from explicit resources"
|
||||
set:
|
||||
resources:
|
||||
memory: 1Gi
|
||||
maxmemory: 900Mi
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: RedisFailover
|
||||
asserts:
|
||||
- contains:
|
||||
path: spec.redis.customConfig
|
||||
content: maxmemory 943718400
|
||||
|
||||
# The operator compares maxmemory with 90% of the pod's memory request and
|
||||
# silently skips a larger line, which would leave Redis unbounded.
|
||||
- it: "refuses maxmemory above 90% of the memory request"
|
||||
set:
|
||||
resources:
|
||||
memory: 1Gi
|
||||
maxmemory: 1000Mi
|
||||
asserts:
|
||||
- failedTemplate:
|
||||
errorPattern: must be at most 90% of the Redis memory request
|
||||
|
||||
- it: "refuses a negative maxmemory"
|
||||
set:
|
||||
maxmemory: -1Mi
|
||||
asserts:
|
||||
- failedTemplate:
|
||||
errorPattern: must not be negative
|
||||
|
||||
# A sub-byte quantity would truncate to maxmemory 0, which Redis reads as no
|
||||
# limit, so the bound the tenant asked for would silently disappear.
|
||||
- it: "refuses a maxmemory that is not a whole number of bytes"
|
||||
set:
|
||||
maxmemory: 512m
|
||||
asserts:
|
||||
- failedTemplate:
|
||||
errorPattern: must be a whole number of bytes
|
||||
|
||||
- it: "refuses a negative sub-byte maxmemory"
|
||||
set:
|
||||
maxmemory: -512m
|
||||
asserts:
|
||||
- failedTemplate:
|
||||
errorPattern: must not be negative
|
||||
|
||||
- it: "applies the eviction policy before the memory bound"
|
||||
set:
|
||||
resourcesPreset: t1.large
|
||||
maxmemory: 1Gi
|
||||
maxmemoryPolicy: allkeys-lru
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: RedisFailover
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.redis.customConfig[3]
|
||||
value: maxmemory-policy allkeys-lru
|
||||
- equal:
|
||||
path: spec.redis.customConfig[4]
|
||||
value: maxmemory 1073741824
|
||||
@@ -1,7 +1,7 @@
|
||||
suite: Redis min-replicas write safety
|
||||
|
||||
# Both settings are opt-in: unset keeps the Redis defaults, so existing
|
||||
# instances render the same customConfig as before.
|
||||
# Both settings are opt-in: unset keeps the Redis defaults, so neither
|
||||
# directive is rendered.
|
||||
|
||||
release:
|
||||
name: my-redis
|
||||
@@ -30,7 +30,7 @@ tests:
|
||||
pattern: ^tcp-keepalive 0$
|
||||
- lengthEqual:
|
||||
path: spec.redis.customConfig
|
||||
count: 2
|
||||
count: 5
|
||||
|
||||
- it: "renders min-replicas-to-write when set"
|
||||
set:
|
||||
|
||||
@@ -171,6 +171,39 @@
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
},
|
||||
"maxmemory": {
|
||||
"description": "Redis `maxmemory`: the dataset size at which Redis applies `maxmemoryPolicy`. Unset by default, which renders `maxmemory 0` (no limit, the Redis default), so a value set by hand with `CONFIG SET` is reset on the next reconcile. Must be at most 90% of the memory request of each Redis pod (from `resources` or `resourcesPreset`): the operator silently drops a larger value, so the chart refuses it. Leave headroom below that for replication and snapshot buffers.",
|
||||
"pattern": "^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer"
|
||||
},
|
||||
{
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"x-kubernetes-int-or-string": true
|
||||
},
|
||||
"maxmemoryPolicy": {
|
||||
"description": "Redis `maxmemory-policy`: what Redis does once `maxmemory` is reached. `noeviction` refuses writes; `allkeys-lru` evicts the least recently used keys, which suits an instance used purely as a cache. Has no effect while `maxmemory` is unset.",
|
||||
"type": "string",
|
||||
"default": "noeviction",
|
||||
"enum": [
|
||||
"noeviction",
|
||||
"allkeys-lru",
|
||||
"allkeys-lfu",
|
||||
"allkeys-random",
|
||||
"volatile-lru",
|
||||
"volatile-lfu",
|
||||
"volatile-random",
|
||||
"volatile-ttl"
|
||||
]
|
||||
},
|
||||
"snapshots": {
|
||||
"description": "Write RDB snapshots to the persistent volume (the Redis `save` directive, with the Redis default schedule). Set to `false` for an instance used purely as a cache: `save \"\"` is rendered and the volume is kept, so no further scheduled snapshot is written while replicas still have room for the full-sync RDB they write to disk. An RDB file already on the volume is not removed, and Redis loads it on restart, so turning snapshots off does not by itself empty the dataset. Always off when `size` is empty.",
|
||||
"type": "boolean",
|
||||
"default": true
|
||||
},
|
||||
"sentinel": {
|
||||
"description": "Sentinel configuration. When `sentinel.resources` is empty and `sentinel.resourcesPreset` is unset, the Sentinel pods are sized like the Redis pods (`resources` and `resourcesPreset`). An empty `sentinel.resources` map counts as unset, as an empty top-level `resources` does.",
|
||||
"type": "object",
|
||||
|
||||
@@ -119,6 +119,24 @@ authEnabled: true
|
||||
## @param {*int} [minReplicasMaxLag] - Redis `min-replicas-max-lag`, in seconds: a replica whose last acknowledgement is older than this does not count towards `minReplicasToWrite`. Unset by default, which leaves the Redis default (10). Has no effect unless `minReplicasToWrite` is set. Must be at least 1 when `minReplicasToWrite` is set: Redis treats 0 as disabling the check, so the chart refuses it.
|
||||
## @minimum 0
|
||||
|
||||
## @param {quantity} [maxmemory] - Redis `maxmemory`: the dataset size at which Redis applies `maxmemoryPolicy`. Unset by default, which renders `maxmemory 0` (no limit, the Redis default), so a value set by hand with `CONFIG SET` is reset on the next reconcile. Must be at most 90% of the memory request of each Redis pod (from `resources` or `resourcesPreset`): the operator silently drops a larger value, so the chart refuses it. Leave headroom below that for replication and snapshot buffers.
|
||||
|
||||
## @enum {string} MaxmemoryPolicy - Values accepted by the Redis `maxmemory-policy` directive.
|
||||
## @value noeviction
|
||||
## @value allkeys-lru
|
||||
## @value allkeys-lfu
|
||||
## @value allkeys-random
|
||||
## @value volatile-lru
|
||||
## @value volatile-lfu
|
||||
## @value volatile-random
|
||||
## @value volatile-ttl
|
||||
|
||||
## @param {MaxmemoryPolicy} maxmemoryPolicy="noeviction" - Redis `maxmemory-policy`: what Redis does once `maxmemory` is reached. `noeviction` refuses writes; `allkeys-lru` evicts the least recently used keys, which suits an instance used purely as a cache. Has no effect while `maxmemory` is unset.
|
||||
maxmemoryPolicy: "noeviction"
|
||||
|
||||
## @param {bool} snapshots - Write RDB snapshots to the persistent volume (the Redis `save` directive, with the Redis default schedule). Set to `false` for an instance used purely as a cache: `save ""` is rendered and the volume is kept, so no further scheduled snapshot is written while replicas still have room for the full-sync RDB they write to disk. An RDB file already on the volume is not removed, and Redis loads it on restart, so turning snapshots off does not by itself empty the dataset. Always off when `size` is empty.
|
||||
snapshots: true
|
||||
|
||||
##
|
||||
## @section Sentinel parameters
|
||||
##
|
||||
|
||||
@@ -146,6 +146,26 @@
|
||||
{{- fail (printf "source.image %q: golden image %q lives in StorageClass %q, and a clone into StorageClass %q of the same provisioner gets no host-assisted fallback from CDI. LINSTOR creates the clone next to the source data, and StorageClass %q does not allow remote volume access from every node, so the clone may never bind. Set storageClass to %q, or ask the platform administrator to set cloneStrategyOverride to copy in the kubevirt-cdi package." .Values.source.image.name $desiredSource.pvc.name $goldenSC $targetSC $targetSC $goldenSC) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- /* CDI v1.66.1 refuses to clone a Block source PVC into a smaller
|
||||
request before it creates the claim (validateClone in
|
||||
pkg/controller/datavolume/pvc-clone-controller.go), and the
|
||||
DataVolume then waits forever. It raises the request to the target
|
||||
StorageProfile's minimumSupportedPvcSize before comparing. A
|
||||
Filesystem source is not compared, as CDI sizes that clone from
|
||||
the image inside it or with a filesystem overhead the chart does
|
||||
not reproduce. */}}
|
||||
{{- $goldenSize := dig "spec" "resources" "requests" "storage" "" ($goldenPVC | default dict) | toString }}
|
||||
{{- if and .Values.storage $goldenSize (eq (dig "spec" "volumeMode" "" ($goldenPVC | default dict) | toString) "Block") }}
|
||||
{{- $goldenBytes := include "cozy-lib.resources.toFloat" $goldenSize | float64 }}
|
||||
{{- $diskBytes := include "cozy-lib.resources.toFloat" (.Values.storage | toString) | float64 }}
|
||||
{{- if and (lt $diskBytes $goldenBytes) $targetSC }}
|
||||
{{- $minimum := dig "metadata" "annotations" "cdi.kubevirt.io/minimumSupportedPvcSize" "" (lookup "cdi.kubevirt.io/v1beta1" "StorageProfile" "" $targetSC | default dict) | toString }}
|
||||
{{- $diskBytes = maxf $diskBytes (include "cozy-lib.resources.toFloat" $minimum | float64) }}
|
||||
{{- end }}
|
||||
{{- if lt $diskBytes $goldenBytes }}
|
||||
{{- fail (printf "storage %s is smaller than golden image %q (%s), so CDI refuses to clone it and the disk would stay pending forever. Set storage to %s or more." (.Values.storage | toString) $desiredSource.pvc.name $goldenSize $goldenSize) }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- else if hasKey .Values.source "http" }}
|
||||
{{- $desiredSource = dict "http" (dict "url" (required "A valid .Values.source.http.url entry required!" .Values.source.http.url)) }}
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
suite: vm-disk refuses a disk smaller than the golden image it clones
|
||||
release:
|
||||
name: test-vm-disk
|
||||
namespace: tenant-test
|
||||
templates:
|
||||
- templates/dv.yaml
|
||||
tests:
|
||||
- it: refuses a new disk at the chart default size from a larger Block golden
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
kubernetesProvider:
|
||||
scheme: &scheme
|
||||
"cdi.kubevirt.io/v1beta1/DataVolume":
|
||||
gvr: {group: cdi.kubevirt.io, version: v1beta1, resource: datavolumes}
|
||||
namespaced: true
|
||||
"cdi.kubevirt.io/v1beta1/StorageProfile":
|
||||
gvr: {group: cdi.kubevirt.io, version: v1beta1, resource: storageprofiles}
|
||||
namespaced: false
|
||||
"v1/Namespace":
|
||||
gvr: {group: "", version: v1, resource: namespaces}
|
||||
namespaced: false
|
||||
"v1/PersistentVolumeClaim":
|
||||
gvr: {group: "", version: v1, resource: persistentvolumeclaims}
|
||||
namespaced: true
|
||||
"storage.k8s.io/v1/StorageClass":
|
||||
gvr: {group: storage.k8s.io, version: v1, resource: storageclasses}
|
||||
namespaced: false
|
||||
objects:
|
||||
- &namespace
|
||||
kind: Namespace
|
||||
apiVersion: v1
|
||||
metadata: {name: tenant-test}
|
||||
- &goldenPVC
|
||||
kind: PersistentVolumeClaim
|
||||
apiVersion: v1
|
||||
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
|
||||
spec:
|
||||
storageClassName: replicated
|
||||
volumeMode: Block
|
||||
resources: {requests: {storage: 20Gi}}
|
||||
asserts:
|
||||
- failedTemplate:
|
||||
errorPattern: 'storage 5Gi is smaller than golden image "vm-default-images-ubuntu" \(20Gi\), so CDI refuses to clone it and the disk would stay pending forever\. Set storage to 20Gi or more\.'
|
||||
|
||||
- it: clones into a disk the size of the golden
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
storage: 20Gi
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.storage.resources.requests.storage
|
||||
value: 20Gi
|
||||
|
||||
- it: clones into a disk larger than the golden
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
storage: 25Gi
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.storage.resources.requests.storage
|
||||
value: 25Gi
|
||||
|
||||
- it: compares bytes, so 21G is smaller than a 20Gi golden
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
storage: 21G
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
asserts:
|
||||
- failedTemplate:
|
||||
errorPattern: 'storage 21G is smaller than golden image "vm-default-images-ubuntu" \(20Gi\)'
|
||||
|
||||
- it: compares bytes, so 20480Mi is the size of a 20Gi golden
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
storage: 20480Mi
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.storage.resources.requests.storage
|
||||
value: 20480Mi
|
||||
|
||||
- it: clones when the target StorageProfile's minimum PVC size covers the golden
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
- kind: StorageProfile
|
||||
apiVersion: cdi.kubevirt.io/v1beta1
|
||||
metadata:
|
||||
name: replicated
|
||||
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 20Gi}
|
||||
asserts:
|
||||
- notFailedTemplate: {}
|
||||
|
||||
- it: refuses when the target StorageProfile's minimum PVC size is still below the golden
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
- kind: StorageProfile
|
||||
apiVersion: cdi.kubevirt.io/v1beta1
|
||||
metadata:
|
||||
name: replicated
|
||||
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 10Gi}
|
||||
asserts:
|
||||
- failedTemplate:
|
||||
errorPattern: 'storage 5Gi is smaller than golden image "vm-default-images-ubuntu" \(20Gi\)'
|
||||
|
||||
- it: reads the minimum PVC size of the default class for an empty storageClass
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
storageClass: ""
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
- kind: StorageClass
|
||||
apiVersion: storage.k8s.io/v1
|
||||
metadata:
|
||||
name: replicated
|
||||
annotations: {storageclass.kubernetes.io/is-default-class: "true"}
|
||||
provisioner: linstor.csi.linbit.com
|
||||
- kind: StorageProfile
|
||||
apiVersion: cdi.kubevirt.io/v1beta1
|
||||
metadata:
|
||||
name: replicated
|
||||
annotations: {cdi.kubevirt.io/minimumSupportedPvcSize: 20Gi}
|
||||
asserts:
|
||||
- notFailedTemplate: {}
|
||||
|
||||
- it: does not compare sizes for a Filesystem golden
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- kind: PersistentVolumeClaim
|
||||
apiVersion: v1
|
||||
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
|
||||
spec:
|
||||
storageClassName: replicated
|
||||
volumeMode: Filesystem
|
||||
resources: {requests: {storage: 20Gi}}
|
||||
asserts:
|
||||
- notFailedTemplate: {}
|
||||
|
||||
- it: renders without a cluster to look the golden up in
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.storage.resources.requests.storage
|
||||
value: 5Gi
|
||||
|
||||
- it: clones a golden whose PVC does not exist yet
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- kind: DataVolume
|
||||
apiVersion: cdi.kubevirt.io/v1beta1
|
||||
metadata: {name: vm-default-images-ubuntu, namespace: cozy-public}
|
||||
status: {phase: ImportScheduled}
|
||||
asserts:
|
||||
- notFailedTemplate: {}
|
||||
|
||||
- it: leaves a disk without a size to CDI, which takes the golden's
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
storage: null
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
asserts:
|
||||
- notFailedTemplate: {}
|
||||
|
||||
- it: leaves an existing disk alone
|
||||
set:
|
||||
source: {image: {name: ubuntu}}
|
||||
storage: 25Gi
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
- kind: DataVolume
|
||||
apiVersion: cdi.kubevirt.io/v1beta1
|
||||
metadata:
|
||||
name: test-vm-disk
|
||||
namespace: tenant-test
|
||||
annotations:
|
||||
vm-disk.cozystack.io/source: '{"image":{"name":"ubuntu"}}'
|
||||
vm-disk.cozystack.io/storage-class: replicated
|
||||
spec:
|
||||
source: {pvc: {name: vm-default-images-ubuntu, namespace: cozy-public}}
|
||||
storage: {resources: {requests: {storage: 10Gi}}, storageClassName: replicated}
|
||||
asserts:
|
||||
- notFailedTemplate: {}
|
||||
|
||||
- it: does not compare sizes for an http source
|
||||
set:
|
||||
source: {http: {url: "https://example.com/disk.img"}}
|
||||
storage: 1Gi
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
asserts:
|
||||
- notFailedTemplate: {}
|
||||
|
||||
- it: does not compare sizes for an upload source
|
||||
set:
|
||||
source: {upload: {}}
|
||||
storage: 1Gi
|
||||
kubernetesProvider:
|
||||
scheme: *scheme
|
||||
objects:
|
||||
- *namespace
|
||||
- *goldenPVC
|
||||
asserts:
|
||||
- notFailedTemplate: {}
|
||||
@@ -34,14 +34,14 @@ coordinates).
|
||||
|
||||
### Common parameters
|
||||
|
||||
| Name | Description | Type | Value |
|
||||
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | -------- |
|
||||
| `version` | etcd version (X.Y.Z) to run, set on the EtcdCluster's spec.version; the operator pulls etcd from its own configured image. | `string` | `3.6.11` |
|
||||
| `size` | Persistent Volume size. | `quantity` | `4Gi` |
|
||||
| `storageClass` | StorageClass used to store the data. | `string` | `""` |
|
||||
| `replicas` | Number of etcd replicas. | `int` | `3` |
|
||||
| `resources` | Resource configuration for etcd. | `object` | `{}` |
|
||||
| `resources.cpu` | Number of CPU cores allocated. | `quantity` | `1000m` |
|
||||
| `resources.memory` | Amount of memory allocated. | `quantity` | `512Mi` |
|
||||
| `affinity` | Affinity rules set verbatim on the EtcdCluster's spec.affinity, replacing the chart default. When left empty the chart applies a soft (preferred) podAntiAffinity that spreads members across nodes by kubernetes.io/hostname on a best-effort basis, so a single node failure is less likely to take out more than one member. The default is intentionally soft: a hard rule would leave a member Pending when there are fewer schedulable nodes than replicas, or when node-local storage pins a member to a node. Set a hard requiredDuringSchedulingIgnoredDuringExecution rule here to forbid co-location outright (only on clusters with enough nodes and non-node-pinned storage). Note that the operator applies affinity only when it creates or replaces a member Pod and does not reschedule running members, so on a cluster that already has co-located members the rule takes effect only as each member is replaced, not retroactively. | `object` | `{}` |
|
||||
| Name | Description | Type | Value |
|
||||
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | -------- |
|
||||
| `version` | etcd version (X.Y.Z) to run, set on the EtcdCluster's spec.version; the operator pulls etcd from its own configured image. | `string` | `3.6.11` |
|
||||
| `size` | Persistent Volume size. | `quantity` | `4Gi` |
|
||||
| `storageClass` | StorageClass used to store the data. | `string` | `""` |
|
||||
| `replicas` | Number of etcd replicas. | `int` | `3` |
|
||||
| `resources` | Resource configuration for etcd. | `object` | `{}` |
|
||||
| `resources.cpu` | Number of CPU cores allocated. | `quantity` | `1000m` |
|
||||
| `resources.memory` | Amount of memory allocated. | `quantity` | `512Mi` |
|
||||
| `affinity` | Affinity rules set verbatim on the EtcdCluster's spec.affinity, replacing the chart default. When left empty the chart applies a soft (preferred) podAntiAffinity by kubernetes.io/hostname on top of the topologySpreadConstraints, which by default require an even spread of members over nodes (whenUnsatisfiable DoNotSchedule, maxSkew 1) unless the platform sets scheduling.globalAppTopologySpreadConstraints. The anti-affinity stays soft because a required rule forbids two members on one node even when every node already has one, so an etcd with more members than schedulable nodes could not start all of them. Set a requiredDuringSchedulingIgnoredDuringExecution rule here only on clusters with at least as many schedulable nodes as members. With node-local storage a recreated member Pod can only return to its own node, so a required rule keeps it Pending while that node holds another member, and the default spread does the same once some other node holds none. The operator copies affinity into each EtcdMember when it creates the member and builds every Pod of that member from that copy, so a change does not reschedule running members and takes effect only as members are replaced by new ones. | `object` | `{}` |
|
||||
|
||||
@@ -69,7 +69,8 @@ spec:
|
||||
topologySpreadConstraints:
|
||||
- maxSkew: 1
|
||||
topologyKey: "kubernetes.io/hostname"
|
||||
whenUnsatisfiable: ScheduleAnyway
|
||||
whenUnsatisfiable: DoNotSchedule
|
||||
nodeTaintsPolicy: Ignore
|
||||
labelSelector:
|
||||
matchLabels:
|
||||
app.kubernetes.io/instance: etcd
|
||||
@@ -78,11 +79,15 @@ spec:
|
||||
affinity:
|
||||
{{- toYaml . | nindent 4 }}
|
||||
{{- else }}
|
||||
# Soft anti-affinity: spread etcd members across nodes on a best-effort
|
||||
# basis so a single node failure is unlikely to take out more than one
|
||||
# member. Preferred (not required) because a hard rule would leave a member
|
||||
# Pending when replicas exceed schedulable nodes, or when node-local storage
|
||||
# pins a member to a node. Override via .Values.affinity for a hard rule.
|
||||
# Soft anti-affinity, a preference on top of the spread constraint above,
|
||||
# which by default is the hard rule that keeps two members off one node
|
||||
# while another node has none. Preferred, not required, because a required
|
||||
# rule also forbids two members on one node when every node already has
|
||||
# one, so an etcd with more members than schedulable nodes could not start
|
||||
# all of them. With node-local storage a recreated member Pod can only
|
||||
# return to its own node, and the default spread keeps it Pending there
|
||||
# while that node holds another member and some other node holds none.
|
||||
# Override via .Values.affinity for a hard rule.
|
||||
affinity:
|
||||
podAntiAffinity:
|
||||
preferredDuringSchedulingIgnoredDuringExecution:
|
||||
|
||||
@@ -29,11 +29,11 @@ spec:
|
||||
drop: ["ALL"]
|
||||
resources:
|
||||
requests:
|
||||
cpu: 200m
|
||||
memory: 256Mi
|
||||
cpu: 10m
|
||||
memory: 32Mi
|
||||
limits:
|
||||
cpu: 500m
|
||||
memory: 512Mi
|
||||
cpu: 100m
|
||||
memory: 64Mi
|
||||
args:
|
||||
# The v1alpha2 operator names the headless Service after the
|
||||
# cluster ({{ $.Release.Name }}) and does not use StatefulSet
|
||||
|
||||
@@ -41,3 +41,16 @@ tests:
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: EtcdCluster
|
||||
|
||||
- it: asks the tenant quota only for what the defrag client uses
|
||||
template: templates/etcd-defrag.yaml
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.jobTemplate.spec.template.spec.containers[0].resources
|
||||
value:
|
||||
requests:
|
||||
cpu: 10m
|
||||
memory: 32Mi
|
||||
limits:
|
||||
cpu: 100m
|
||||
memory: 64Mi
|
||||
@@ -8,7 +8,7 @@ templates:
|
||||
- templates/etcd-cluster.yaml
|
||||
|
||||
tests:
|
||||
- it: uses a soft per-host spread by default
|
||||
- it: uses a strict per-host spread by default
|
||||
set:
|
||||
_cluster: {}
|
||||
documentSelector:
|
||||
@@ -16,17 +16,41 @@ tests:
|
||||
value: EtcdCluster
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.topologySpreadConstraints[0].maxSkew
|
||||
value: 1
|
||||
path: spec.topologySpreadConstraints[0]
|
||||
value:
|
||||
maxSkew: 1
|
||||
topologyKey: kubernetes.io/hostname
|
||||
whenUnsatisfiable: DoNotSchedule
|
||||
nodeTaintsPolicy: Ignore
|
||||
labelSelector:
|
||||
matchLabels:
|
||||
app.kubernetes.io/instance: etcd
|
||||
- lengthEqual:
|
||||
path: spec.topologySpreadConstraints
|
||||
count: 1
|
||||
|
||||
- it: renders a soft cluster-wide spread policy as written
|
||||
set:
|
||||
_cluster:
|
||||
scheduling:
|
||||
globalAppTopologySpreadConstraints: |
|
||||
topologySpreadConstraints:
|
||||
- maxSkew: 1
|
||||
topologyKey: kubernetes.io/hostname
|
||||
whenUnsatisfiable: ScheduleAnyway
|
||||
documentSelector:
|
||||
path: kind
|
||||
value: EtcdCluster
|
||||
asserts:
|
||||
- equal:
|
||||
path: spec.topologySpreadConstraints[0].topologyKey
|
||||
value: kubernetes.io/hostname
|
||||
- equal:
|
||||
path: spec.topologySpreadConstraints[0].whenUnsatisfiable
|
||||
value: ScheduleAnyway
|
||||
- equal:
|
||||
path: spec.topologySpreadConstraints[0].labelSelector.matchLabels["app.kubernetes.io/instance"]
|
||||
value: etcd
|
||||
path: spec.topologySpreadConstraints[0]
|
||||
value:
|
||||
maxSkew: 1
|
||||
topologyKey: kubernetes.io/hostname
|
||||
whenUnsatisfiable: ScheduleAnyway
|
||||
labelSelector:
|
||||
matchLabels:
|
||||
app.kubernetes.io/instance: etcd
|
||||
- lengthEqual:
|
||||
path: spec.topologySpreadConstraints
|
||||
count: 1
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
}
|
||||
},
|
||||
"affinity": {
|
||||
"description": "Affinity rules set verbatim on the EtcdCluster's spec.affinity, replacing the chart default. When left empty the chart applies a soft (preferred) podAntiAffinity that spreads members across nodes by kubernetes.io/hostname on a best-effort basis, so a single node failure is less likely to take out more than one member. The default is intentionally soft: a hard rule would leave a member Pending when there are fewer schedulable nodes than replicas, or when node-local storage pins a member to a node. Set a hard requiredDuringSchedulingIgnoredDuringExecution rule here to forbid co-location outright (only on clusters with enough nodes and non-node-pinned storage). Note that the operator applies affinity only when it creates or replaces a member Pod and does not reschedule running members, so on a cluster that already has co-located members the rule takes effect only as each member is replaced, not retroactively.",
|
||||
"description": "Affinity rules set verbatim on the EtcdCluster's spec.affinity, replacing the chart default. When left empty the chart applies a soft (preferred) podAntiAffinity by kubernetes.io/hostname on top of the topologySpreadConstraints, which by default require an even spread of members over nodes (whenUnsatisfiable DoNotSchedule, maxSkew 1) unless the platform sets scheduling.globalAppTopologySpreadConstraints. The anti-affinity stays soft because a required rule forbids two members on one node even when every node already has one, so an etcd with more members than schedulable nodes could not start all of them. Set a requiredDuringSchedulingIgnoredDuringExecution rule here only on clusters with at least as many schedulable nodes as members. With node-local storage a recreated member Pod can only return to its own node, so a required rule keeps it Pending while that node holds another member, and the default spread does the same once some other node holds none. The operator copies affinity into each EtcdMember when it creates the member and builds every Pod of that member from that copy, so a change does not reschedule running members and takes effect only as members are replaced by new ones.",
|
||||
"type": "object",
|
||||
"default": {},
|
||||
"x-kubernetes-preserve-unknown-fields": true
|
||||
|
||||
@@ -23,5 +23,5 @@ resources:
|
||||
cpu: 1000m
|
||||
memory: 512Mi
|
||||
|
||||
## @param {object} [affinity] - Affinity rules set verbatim on the EtcdCluster's spec.affinity, replacing the chart default. When left empty the chart applies a soft (preferred) podAntiAffinity that spreads members across nodes by kubernetes.io/hostname on a best-effort basis, so a single node failure is less likely to take out more than one member. The default is intentionally soft: a hard rule would leave a member Pending when there are fewer schedulable nodes than replicas, or when node-local storage pins a member to a node. Set a hard requiredDuringSchedulingIgnoredDuringExecution rule here to forbid co-location outright (only on clusters with enough nodes and non-node-pinned storage). Note that the operator applies affinity only when it creates or replaces a member Pod and does not reschedule running members, so on a cluster that already has co-located members the rule takes effect only as each member is replaced, not retroactively.
|
||||
## @param {object} [affinity] - Affinity rules set verbatim on the EtcdCluster's spec.affinity, replacing the chart default. When left empty the chart applies a soft (preferred) podAntiAffinity by kubernetes.io/hostname on top of the topologySpreadConstraints, which by default require an even spread of members over nodes (whenUnsatisfiable DoNotSchedule, maxSkew 1) unless the platform sets scheduling.globalAppTopologySpreadConstraints. The anti-affinity stays soft because a required rule forbids two members on one node even when every node already has one, so an etcd with more members than schedulable nodes could not start all of them. Set a requiredDuringSchedulingIgnoredDuringExecution rule here only on clusters with at least as many schedulable nodes as members. With node-local storage a recreated member Pod can only return to its own node, so a required rule keeps it Pending while that node holds another member, and the default spread does the same once some other node holds none. The operator copies affinity into each EtcdMember when it creates the member and builds every Pod of that member from that copy, so a change does not reschedule running members and takes effect only as members are replaced by new ones.
|
||||
affinity: {}
|
||||
@@ -13,10 +13,12 @@ LimitRange, which is cozy-keycloak and any tenant that leaves resourceQuotas
|
||||
empty, gave the sidecar no requests and no limit at all, so nothing reserved
|
||||
memory for it and nothing bounded it. The same request and limit settle both:
|
||||
256Mi holds the measured working set and 1Gi is four times the measurement.
|
||||
No CPU limit is set, matching the entityOperator precedent in
|
||||
packages/apps/kafka: a throttled sidecar stalls WAL archiving instead of
|
||||
failing it. Inside a tenant the limit counts against the ResourceQuota
|
||||
limits.memory budget, 1Gi per instance pod.
|
||||
The CPU limit is explicit for the same reason: left unset, that LimitRange
|
||||
gives the sidecar its 250m default, so gzip compression of full backups and
|
||||
WAL segments runs on a quarter of a core (a 1.6 GB database took 30 minutes
|
||||
to back up). One core keeps the sidecar from starving the instance while
|
||||
removing that ceiling. Inside a tenant both limits count against the
|
||||
ResourceQuota budget, one core and 1Gi per instance pod.
|
||||
|
||||
Checksum: since botocore ~1.36 (early 2025) the default
|
||||
RequestChecksumCalculation is when_supported, which attaches a flexible checksum
|
||||
@@ -39,6 +41,7 @@ instanceSidecarConfiguration:
|
||||
cpu: 100m
|
||||
memory: 256Mi
|
||||
limits:
|
||||
cpu: "1"
|
||||
memory: 1Gi
|
||||
env:
|
||||
- name: AWS_REQUEST_CHECKSUM_CALCULATION
|
||||
|
||||
@@ -32,6 +32,8 @@ Implications:
|
||||
- **RJSF** (`@rjsf/core` + `validator-ajv8`) for application config forms.
|
||||
- **Vitest + jsdom + @testing-library/react** for tests.
|
||||
- **Monaco** for YAML view, **noVNC** for the VM console tab.
|
||||
- **uPlot** for the Metrics tab charts, lazy-loaded with that tab.
|
||||
- Metrics and logs come from the observability proxy, not kube-apiserver: `lib/observability/proxy.ts` reads its native Prometheus JSON and VictoriaLogs NDJSON through `K8sClient.raw`, under the paths nginx routes to it ahead of `/apis`.
|
||||
|
||||
## Dev loop
|
||||
|
||||
@@ -79,6 +81,7 @@ the apiserver checks that user's RBAC.
|
||||
to a chunked-encoding WATCH against the same `resourceVersion`. Don't
|
||||
poll. Don't add `refetchInterval`. If you need a one-shot, pass
|
||||
`{ watch: false }`.
|
||||
The one exception is the metrics panels in `components/observability`: the observability proxy serves the Prometheus query API, which has no watch, so they refresh on a fixed interval instead. Log lines stream through the tail endpoint and are not polled.
|
||||
4. **Tenant scoping.** Most resources live in `tenant-<name>` namespaces.
|
||||
Pull the active tenant from `useTenantContext()` — never read the
|
||||
namespace from a URL param or guess it.
|
||||
|
||||
@@ -31,7 +31,8 @@
|
||||
"react": "^19.2.4",
|
||||
"react-dom": "^19.2.4",
|
||||
"react-router": "^7.6.1",
|
||||
"simple-icons": "^16.22.0"
|
||||
"simple-icons": "^16.22.0",
|
||||
"uplot": "^1.6.32"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@tailwindcss/vite": "^4.2.2",
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
import { describe, it, expect, vi, afterEach } from "vitest"
|
||||
import { K8sClient } from "@cozystack/k8s-client"
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
describe("K8sClient request headers", () => {
|
||||
it("lets a method's own content type replace the JSON default", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
const client = new K8sClient({ getToken: async () => "t0k" })
|
||||
|
||||
await client.patch("apps", "v1", "deployments", "web", { spec: {} }, "default", "merge")
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Content-Type")).toBe("application/merge-patch+json")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it("sends JSON by default", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
|
||||
await new K8sClient().getApiGroups()
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
expect(new Headers(init.headers).get("Content-Type")).toBe("application/json")
|
||||
})
|
||||
})
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
import { describe, it, expect, vi, afterEach } from "vitest"
|
||||
import { K8sClient } from "@cozystack/k8s-client"
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe("K8sClient.raw", () => {
|
||||
it("returns the response untouched, including a non-2xx one", async () => {
|
||||
const res = new Response("not found", { status: 404 })
|
||||
vi.stubGlobal("fetch", vi.fn(async () => res))
|
||||
const client = new K8sClient()
|
||||
|
||||
await expect(client.raw("/x")).resolves.toBe(res)
|
||||
})
|
||||
|
||||
it("prefixes the base URL and attaches the bearer token", async () => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
const client = new K8sClient({ baseUrl: "/base", getToken: async () => "t0k" })
|
||||
|
||||
await client.raw("/apis/x", { headers: { Accept: "application/json" } })
|
||||
|
||||
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
expect(url).toBe("/base/apis/x")
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Accept")).toBe("application/json")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it.each([
|
||||
["a Headers instance", new Headers({ Accept: "application/x-ndjson" })],
|
||||
["a tuple list", [["Accept", "application/x-ndjson"]] as [string, string][]],
|
||||
])("keeps caller headers passed as %s", async (_, given) => {
|
||||
const fetchMock = vi.fn(async () => new Response("{}"))
|
||||
vi.stubGlobal("fetch", fetchMock)
|
||||
const client = new K8sClient({ getToken: async () => "t0k" })
|
||||
|
||||
await client.raw("/x", { headers: given })
|
||||
|
||||
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]
|
||||
const headers = new Headers(init.headers)
|
||||
expect(headers.get("Accept")).toBe("application/x-ndjson")
|
||||
expect(headers.get("Authorization")).toBe("Bearer t0k")
|
||||
})
|
||||
|
||||
it("routes a 401 through the client's unauthorized handler", async () => {
|
||||
const onUnauthorized = vi.fn()
|
||||
vi.stubGlobal("fetch", vi.fn(async () => new Response("", { status: 401 })))
|
||||
const client = new K8sClient({ onUnauthorized })
|
||||
|
||||
const res = await client.raw("/x")
|
||||
|
||||
expect(res.status).toBe(401)
|
||||
expect(onUnauthorized).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("does not swallow a network failure", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn(async () => { throw new TypeError("offline") }))
|
||||
const client = new K8sClient()
|
||||
|
||||
await expect(client.raw("/x")).rejects.toThrow("offline")
|
||||
})
|
||||
})
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { render, screen } from "@testing-library/react"
|
||||
import { EmptyState } from "./EmptyState.tsx"
|
||||
|
||||
describe("EmptyState", () => {
|
||||
it("announces its message as a status", () => {
|
||||
render(<EmptyState title="No data for this range." hint="Pick a longer range." />)
|
||||
|
||||
expect(screen.getByRole("status")).toHaveTextContent("No data for this range.Pick a longer range.")
|
||||
})
|
||||
|
||||
it("marks itself busy while something is loading", () => {
|
||||
render(<EmptyState busy title="Loading…" />)
|
||||
|
||||
expect(screen.getByRole("status")).toHaveAttribute("aria-busy", "true")
|
||||
})
|
||||
})
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
import type { ReactNode } from "react"
|
||||
import { cn, Spinner } from "@cozystack/ui"
|
||||
|
||||
interface EmptyStateProps {
|
||||
title: ReactNode
|
||||
hint?: ReactNode
|
||||
action?: ReactNode
|
||||
busy?: boolean
|
||||
tone?: "light" | "dark"
|
||||
className?: string
|
||||
}
|
||||
|
||||
export function EmptyState({ title, hint, action, busy, tone = "light", className }: EmptyStateProps) {
|
||||
const dark = tone === "dark"
|
||||
return (
|
||||
<div
|
||||
role="status"
|
||||
aria-busy={busy || undefined}
|
||||
className={cn("flex h-full flex-col items-center justify-center gap-1.5 px-4 py-8 text-center", className)}
|
||||
>
|
||||
{busy && <Spinner className={cn("mb-1 size-5", dark && "text-slate-500")} />}
|
||||
<p className={cn("text-sm", dark ? "text-slate-300" : "text-slate-600")}>{title}</p>
|
||||
{hint && <p className={cn("text-xs", dark ? "text-slate-500" : "text-slate-400")}>{hint}</p>}
|
||||
{action && <div className="mt-2">{action}</div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { fireEvent, render, screen } from "@testing-library/react"
|
||||
import { LogLines } from "./LogLines.tsx"
|
||||
|
||||
const entry = (n: number) => ({ _time: `2026-11-04T10:00:0${n}Z`, _msg: `line ${n}`, _stream_id: "s" })
|
||||
|
||||
function sized(list: HTMLElement, scrollHeight: number) {
|
||||
Object.defineProperty(list, "scrollHeight", { configurable: true, value: scrollHeight })
|
||||
Object.defineProperty(list, "clientHeight", { configurable: true, value: 100 })
|
||||
}
|
||||
|
||||
describe("LogLines", () => {
|
||||
it("keeps the console frame and shows the placeholder inside it when there are no lines", () => {
|
||||
render(<LogLines entries={[]} placeholder="Waiting for new log lines…" />)
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(frame).toHaveTextContent("Waiting for new log lines…")
|
||||
expect(screen.queryByRole("list")).toBeNull()
|
||||
})
|
||||
|
||||
it("puts the lines in the same frame once they arrive", () => {
|
||||
const { rerender } = render(<LogLines entries={[]} placeholder="Waiting for new log lines…" />)
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
|
||||
rerender(<LogLines entries={[entry(1)]} placeholder="Waiting for new log lines…" />)
|
||||
|
||||
expect(screen.getByRole("region", { name: "Log lines" })).toBe(frame)
|
||||
expect(frame).toContainElement(screen.getByRole("list"))
|
||||
expect(frame).not.toHaveTextContent("Waiting")
|
||||
})
|
||||
|
||||
it("shows a line without _time with no timestamp and no empty source", () => {
|
||||
render(<LogLines placeholder="" entries={[{ _msg: "visible message" }]} />)
|
||||
|
||||
expect(screen.getByRole("listitem")).toHaveTextContent(/^visible message$/)
|
||||
})
|
||||
|
||||
it("shows the remaining fields of a row without _msg", () => {
|
||||
render(<LogLines placeholder="" entries={[{ kubernetes_pod_name: "db-0", rows: "42" }]} />)
|
||||
|
||||
expect(screen.getByRole("listitem")).toHaveTextContent(/^db-0\/ rows=42$/)
|
||||
})
|
||||
|
||||
it("keeps following the newest line while the reader is at the bottom", () => {
|
||||
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
|
||||
const list = screen.getByRole("list")
|
||||
sized(list, 500)
|
||||
|
||||
rerender(<LogLines placeholder="" entries={[entry(1), entry(2)]} />)
|
||||
|
||||
expect(list.scrollTop).toBe(500)
|
||||
})
|
||||
|
||||
it("leaves the position alone once the reader scrolls up", () => {
|
||||
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
|
||||
const list = screen.getByRole("list")
|
||||
sized(list, 500)
|
||||
list.scrollTop = 120
|
||||
fireEvent.scroll(list)
|
||||
|
||||
rerender(<LogLines placeholder="" entries={[entry(1), entry(2)]} />)
|
||||
|
||||
expect(list.scrollTop).toBe(120)
|
||||
})
|
||||
|
||||
it("follows again once the list is cleared for a new run", () => {
|
||||
const { rerender } = render(<LogLines placeholder="" entries={[entry(1)]} />)
|
||||
const first = screen.getByRole("list")
|
||||
sized(first, 500)
|
||||
first.scrollTop = 120
|
||||
fireEvent.scroll(first)
|
||||
|
||||
rerender(<LogLines placeholder="" entries={[]} />)
|
||||
rerender(<LogLines placeholder="" entries={[entry(2)]} />)
|
||||
const second = screen.getByRole("list")
|
||||
sized(second, 300)
|
||||
rerender(<LogLines placeholder="" entries={[entry(2), entry(3)]} />)
|
||||
|
||||
expect(second.scrollTop).toBe(300)
|
||||
})
|
||||
})
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
import { useEffect, useRef, type ReactNode } from "react"
|
||||
import { CONTAINER_FIELD, POD_FIELD } from "../../lib/observability/logs.ts"
|
||||
import type { LogEntry } from "../../lib/observability/proxy.ts"
|
||||
|
||||
const BOTTOM_SLACK_PX = 24
|
||||
const SHOWN_APART = new Set(["_time", "_msg", "_stream_id", POD_FIELD, CONTAINER_FIELD])
|
||||
|
||||
function source(e: LogEntry): string | null {
|
||||
const pod = e[POD_FIELD]
|
||||
const container = e[CONTAINER_FIELD]
|
||||
return pod || container ? `${pod ?? ""}/${container ?? ""}` : null
|
||||
}
|
||||
|
||||
// A `fields` or `stats` pipe can leave a row without _msg; its other fields are then the content.
|
||||
function message(e: LogEntry): string {
|
||||
if (e._msg !== undefined) return e._msg
|
||||
return Object.entries(e)
|
||||
.filter(([k, v]) => v !== undefined && !SHOWN_APART.has(k))
|
||||
.map(([k, v]) => `${k}=${v}`)
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
export function LogLines({ entries, placeholder }: { entries: LogEntry[]; placeholder: ReactNode }) {
|
||||
const ref = useRef<HTMLOListElement>(null)
|
||||
const pinned = useRef(true)
|
||||
|
||||
useEffect(() => {
|
||||
if (entries.length === 0) pinned.current = true
|
||||
else if (ref.current && pinned.current) ref.current.scrollTop = ref.current.scrollHeight
|
||||
}, [entries])
|
||||
|
||||
return (
|
||||
<section
|
||||
aria-label="Log lines"
|
||||
className="h-[60vh] min-h-72 overflow-hidden rounded-lg bg-slate-950 font-mono text-xs leading-5 text-slate-100 shadow-xs"
|
||||
>
|
||||
{entries.length === 0 ? (
|
||||
placeholder
|
||||
) : (
|
||||
<ol
|
||||
ref={ref}
|
||||
onScroll={(e) => {
|
||||
const el = e.currentTarget
|
||||
pinned.current = el.scrollHeight - el.scrollTop - el.clientHeight <= BOTTOM_SLACK_PX
|
||||
}}
|
||||
className="h-full overflow-auto p-3"
|
||||
>
|
||||
{entries.map((e, i) => {
|
||||
const from = source(e)
|
||||
return (
|
||||
<li key={`${e._stream_id}-${e._time}-${i}`} className="whitespace-pre-wrap break-all">
|
||||
{e._time !== undefined && <><span className="text-slate-400">{new Date(e._time).toLocaleTimeString()}</span>{" "}</>}
|
||||
{from !== null && <><span className="text-sky-300">{from}</span>{" "}</>}
|
||||
{message(e)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ol>
|
||||
)}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
+416
@@ -0,0 +1,416 @@
|
||||
import { describe, it, expect, vi } from "vitest"
|
||||
import { render, screen, waitFor, within } from "@testing-library/react"
|
||||
import userEvent from "@testing-library/user-event"
|
||||
import { K8sClient, K8sProvider } from "@cozystack/k8s-client"
|
||||
import { renderWithK8sProvider } from "../../test-utils/render.tsx"
|
||||
import { LogsView } from "./LogsView.tsx"
|
||||
|
||||
const app = { namespace: "tenant-a", plural: "postgreses", name: "db" }
|
||||
const ROOT = "/apis/apps.cozystack.io/v1alpha1/namespaces/tenant-a/postgreses/db/log/"
|
||||
|
||||
const line = (time: string, msg: string, pod = "db-0") =>
|
||||
JSON.stringify({ _time: time, _msg: msg, _stream_id: "s", kubernetes_pod_name: pod, kubernetes_container_name: "postgres" })
|
||||
|
||||
function clientWith(handler: (endpoint: string, params: URLSearchParams) => Response | Promise<Response>) {
|
||||
const client = new K8sClient()
|
||||
const raw = vi.spyOn(client, "raw").mockImplementation(async (path) => {
|
||||
const url = new URL(path, "http://x")
|
||||
return handler(url.pathname, url.searchParams)
|
||||
})
|
||||
return { client, raw }
|
||||
}
|
||||
|
||||
const values = (...v: string[]) => Response.json({ values: v.map((value) => ({ value, hits: 1 })) })
|
||||
|
||||
function defaultHandler(endpoint: string, params?: URLSearchParams): Response | Promise<Response> {
|
||||
if (endpoint.endsWith("stream_field_values")) {
|
||||
return params?.get("field") === "kubernetes_pod_name" ? values("db-0", "db-1") : values("postgres")
|
||||
}
|
||||
if (endpoint.endsWith("query")) return new Response(`${line("2026-11-04T10:00:01Z", "ready to accept connections")}\n`)
|
||||
return new Promise(() => {})
|
||||
}
|
||||
|
||||
// The provider's own QueryClient keeps the previous result while a key changes; it is
|
||||
// shared across tests, so each test below uses namespaces no other test reads.
|
||||
function renderWithProductionDefaults(client: K8sClient, namespace: string) {
|
||||
const view = (ns: string) => (
|
||||
<K8sProvider client={client}>
|
||||
<LogsView scope={{ namespace: ns }} />
|
||||
</K8sProvider>
|
||||
)
|
||||
const result = render(view(namespace))
|
||||
return { switchTo: (ns: string) => result.rerender(view(ns)) }
|
||||
}
|
||||
|
||||
describe("LogsView with the production query defaults", () => {
|
||||
it("drops the previous tenant's lines and pods while the next tenant loads", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.includes("/tenant-switch-b/") ? new Promise(() => {}) : defaultHandler(endpoint, params),
|
||||
)
|
||||
const { switchTo } = renderWithProductionDefaults(client, "tenant-switch-a")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
await screen.findByRole("option", { name: "db-1" })
|
||||
await userEvent.keyboard("{Escape}")
|
||||
|
||||
switchTo("tenant-switch-b")
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("Loading log lines…")).toBeInTheDocument()
|
||||
expect(screen.queryByText("ready to accept connections")).toBeNull()
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
expect((await screen.findAllByRole("option")).map((o) => o.textContent)).toEqual(["All"])
|
||||
})
|
||||
|
||||
it("starts the next tenant with no filter, pod or container of the previous one", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
const { switchTo } = renderWithProductionDefaults(client, "tenant-reset-a")
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "error{Enter}")
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
await userEvent.click(await screen.findByRole("option", { name: "db-1" }))
|
||||
await waitFor(() =>
|
||||
expect(raw.mock.calls.some(([p]) => new URL(p, "http://x").searchParams.get("query") === '{kubernetes_pod_name="db-1"} (error)')).toBe(true),
|
||||
)
|
||||
|
||||
switchTo("tenant-reset-b")
|
||||
|
||||
await waitFor(() => expect(raw.mock.calls.some(([p]) => p.includes("/tenant-reset-b/") && p.includes("select/logsql/query?"))).toBe(true))
|
||||
const first = raw.mock.calls.find(([p]) => p.includes("/tenant-reset-b/") && p.includes("select/logsql/query?"))!
|
||||
expect(new URL(first[0], "http://x").searchParams.get("query")).toBe("*")
|
||||
expect(screen.getByRole("combobox", { name: "Pod" })).toHaveTextContent("All")
|
||||
expect(screen.getByRole("combobox", { name: "Container" })).toHaveTextContent("All")
|
||||
expect(screen.getByLabelText("LogsQL filter")).toHaveValue("")
|
||||
})
|
||||
|
||||
it("keeps the current lines while a new filter in the same tenant loads", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && params.get("query") === "slow" ? new Promise(() => {}) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-filter-a")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "slow{Enter}")
|
||||
|
||||
expect(screen.getByText("ready to accept connections")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("says it is loading, not that nothing matched, while a longer range replaces an empty one", async () => {
|
||||
const { client } = clientWith((endpoint, params) => {
|
||||
if (!endpoint.endsWith("query")) return defaultHandler(endpoint, params)
|
||||
return Date.parse(params.get("start") ?? "") < Date.now() - 2 * 3600_000
|
||||
? new Promise(() => {})
|
||||
: new Response("")
|
||||
})
|
||||
renderWithProductionDefaults(client, "tenant-range-a")
|
||||
const frame = () => screen.getByRole("region", { name: "Log lines" })
|
||||
expect(await within(frame()).findByText("No log lines for this range and filter.")).toBeInTheDocument()
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "24h" }))
|
||||
|
||||
await waitFor(() => expect(within(frame()).getByRole("status")).toHaveAttribute("aria-busy", "true"))
|
||||
expect(within(frame()).queryByText("No log lines for this range and filter.")).toBeNull()
|
||||
})
|
||||
|
||||
it("marks the lines of the previous range as standing in while a longer range loads", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && Date.parse(params.get("start") ?? "") < Date.now() - 2 * 3600_000
|
||||
? new Promise(() => {})
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-range-b")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
expect(screen.queryByText(/previous lines/)).toBeNull()
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "24h" }))
|
||||
|
||||
expect(await screen.findByText(/previous lines/)).toHaveAttribute("aria-busy", "true")
|
||||
expect(screen.getByText("ready to accept connections")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("drops the standing-in note once the longer range answers", async () => {
|
||||
let answer: (r: Response) => void = () => {}
|
||||
const longer = new Promise<Response>((resolve) => { answer = resolve })
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && Date.parse(params.get("start") ?? "") < Date.now() - 2 * 3600_000
|
||||
? longer
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-range-c")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole("button", { name: "24h" }))
|
||||
expect(await screen.findByText(/previous lines/)).toBeInTheDocument()
|
||||
|
||||
answer(new Response(`${line("2026-11-03T12:00:00Z", "checkpoint complete")}\n`))
|
||||
|
||||
expect(await screen.findByText("checkpoint complete")).toBeInTheDocument()
|
||||
expect(screen.queryByText(/previous lines/)).toBeNull()
|
||||
expect(screen.queryByText("ready to accept connections")).toBeNull()
|
||||
})
|
||||
|
||||
it("shows no standing-in note while following", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("tail")
|
||||
? new Response(`${line("2026-11-04T10:00:02Z", "live line")}\n`)
|
||||
: endpoint.endsWith("query") && params.get("query")?.includes("error")
|
||||
? new Promise(() => {})
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithProductionDefaults(client, "tenant-follow-a")
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "error{Enter}")
|
||||
expect(await screen.findByText(/previous lines/)).toBeInTheDocument()
|
||||
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
|
||||
|
||||
expect(await screen.findByText("live line")).toBeInTheDocument()
|
||||
expect(screen.queryByText(/previous lines/)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe("LogsView", () => {
|
||||
it("shows recent lines from the application root", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
const call = raw.mock.calls.find(([p]) => p.includes("select/logsql/query?"))!
|
||||
expect(call[0].startsWith(`${ROOT}select/logsql/query?`)).toBe(true)
|
||||
const params = new URL(call[0], "http://x").searchParams
|
||||
expect(params.get("query")).toBe("*")
|
||||
expect(params.get("limit")).toBe("500")
|
||||
})
|
||||
|
||||
it("narrows to the picked pod with a stream filter", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
await userEvent.click(await screen.findByRole("option", { name: "db-1" }))
|
||||
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
raw.mock.calls.some(([p]) => new URL(p, "http://x").searchParams.get("query") === '{kubernetes_pod_name="db-1"} (*)'),
|
||||
).toBe(true),
|
||||
)
|
||||
})
|
||||
|
||||
it("offers All next to every pod and starts on it", async () => {
|
||||
const { client } = clientWith(defaultHandler)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
const pod = screen.getByRole("combobox", { name: "Pod" })
|
||||
expect(pod).toHaveTextContent("All")
|
||||
|
||||
await userEvent.click(pod)
|
||||
|
||||
expect(await screen.findByRole("option", { name: "db-1" })).toBeInTheDocument()
|
||||
expect(screen.getAllByRole("option").map((o) => o.textContent)).toEqual(["All", "db-0", "db-1"])
|
||||
})
|
||||
|
||||
it("keeps the log console and says so inside it when nothing matches", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Response("") : defaultHandler(endpoint, params),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(await within(frame).findByText("No log lines for this range and filter.")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("suggests a longer range only while one exists", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Response("") : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
const frame = () => screen.getByRole("region", { name: "Log lines" })
|
||||
expect(await within(frame()).findByText("Try a longer range or a broader filter.")).toBeInTheDocument()
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "24h" }))
|
||||
|
||||
await waitFor(() => expect(within(frame()).getByText("Try a broader filter.")).toBeInTheDocument())
|
||||
expect(within(frame()).queryByText(/longer range/)).toBeNull()
|
||||
})
|
||||
|
||||
it("shows loading inside the log console", () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Promise(() => {}) : defaultHandler(endpoint, params),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByRole("status")).toHaveAttribute("aria-busy", "true")
|
||||
})
|
||||
|
||||
it("says it is waiting while Follow has no line yet", async () => {
|
||||
const { client } = clientWith(defaultHandler)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("Waiting for new log lines…")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("applies the LogsQL filter on submit", async () => {
|
||||
const { client, raw } = clientWith(defaultHandler)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "error{Enter}")
|
||||
|
||||
await waitFor(() =>
|
||||
expect(raw.mock.calls.some(([p]) => p.includes("select/logsql/query?") && p.includes("query=error"))).toBe(true),
|
||||
)
|
||||
})
|
||||
|
||||
it("follows the live tail when Follow is pressed", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("tail") ? new Response(`${line("2026-11-04T10:00:05Z", "checkpoint complete")}\n`) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
|
||||
|
||||
expect(await screen.findByText("checkpoint complete")).toBeInTheDocument()
|
||||
expect(screen.getByRole("button", { name: "Follow" })).toHaveAttribute("aria-pressed", "true")
|
||||
})
|
||||
|
||||
it("says the tail stopped when the proxy refuses the query", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("tail") ? new Response("pipe not allowed in tail", { status: 400 }) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("button", { name: "Follow" }))
|
||||
|
||||
expect(await screen.findByText(/Stopped/)).toBeInTheDocument()
|
||||
expect(screen.getByText("pipe not allowed in tail")).toBeInTheDocument()
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("Follow stopped before any line arrived.")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("renders a cluster without the proxy as not available", async () => {
|
||||
const { client } = clientWith(() => new Response("404 page not found", { status: 404 }))
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
expect(await screen.findByText("Logs are not available on this cluster.")).toBeInTheDocument()
|
||||
expect(screen.queryByLabelText("LogsQL filter")).toBeNull()
|
||||
})
|
||||
|
||||
it("renders the tenant logs page as not available when kube-apiserver answers instead of the proxy", async () => {
|
||||
const { client } = clientWith(() =>
|
||||
Response.json({ kind: "Status", apiVersion: "v1", status: "Failure", reason: "Forbidden", code: 403 }, { status: 403 }),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={{ namespace: "tenant-a" }} />, { client })
|
||||
|
||||
expect(await screen.findByText("Logs are not available on this cluster.")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("keeps the controls and explains a rejected query", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Response("cannot parse query", { status: 400 }) : defaultHandler(endpoint, params),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
expect(await screen.findByText("cannot parse query")).toBeInTheDocument()
|
||||
expect(screen.getByLabelText("LogsQL filter")).toBeInTheDocument()
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(within(frame).getByText("No log lines to show.")).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it("offers All once when some lines carry no pod name", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("stream_field_values") && params.get("field") === "kubernetes_pod_name"
|
||||
? values("", "db-0")
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(screen.getByRole("combobox", { name: "Pod" }))
|
||||
|
||||
await screen.findByRole("option", { name: "db-0" })
|
||||
expect(screen.getAllByRole("option").map((o) => o.textContent)).toEqual(["All", "db-0"])
|
||||
})
|
||||
|
||||
it("shows rows of a query whose pipe removed _time", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") ? new Response('{"_msg":"visible message"}\n') : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "* | fields _msg{Enter}")
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
expect(await within(frame).findByText("visible message")).toBeInTheDocument()
|
||||
expect(within(frame).queryByText(/No log lines/)).toBeNull()
|
||||
})
|
||||
|
||||
it("keeps the view when only the pod list is refused", async () => {
|
||||
const status = JSON.stringify({ kind: "Status", apiVersion: "v1", status: "Failure", code: 404 })
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("stream_field_values") ? new Response(status, { status: 404 }) : defaultHandler(endpoint, params),
|
||||
)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
expect(screen.queryByText("Logs are not available on this cluster.")).toBeNull()
|
||||
})
|
||||
|
||||
it("retries a failed history query on request", async () => {
|
||||
let calls = 0
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query") && calls++ === 0 ? new Response("upstream down", { status: 502 }) : defaultHandler(endpoint, params),
|
||||
)
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(await screen.findByRole("button", { name: "Retry" }))
|
||||
|
||||
expect(await screen.findByText("ready to accept connections")).toBeInTheDocument()
|
||||
expect(screen.queryByRole("button", { name: "Retry" })).toBeNull()
|
||||
})
|
||||
|
||||
it("says it is loading while a retry runs", async () => {
|
||||
let calls = 0
|
||||
const { client } = clientWith((endpoint, params) => {
|
||||
if (!endpoint.endsWith("query")) return defaultHandler(endpoint, params)
|
||||
return calls++ === 0 ? new Response("upstream down", { status: 502 }) : new Promise(() => {})
|
||||
})
|
||||
renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
|
||||
await userEvent.click(await screen.findByRole("button", { name: "Retry" }))
|
||||
|
||||
const frame = screen.getByRole("region", { name: "Log lines" })
|
||||
await waitFor(() => expect(within(frame).getByRole("status")).toHaveAttribute("aria-busy", "true"))
|
||||
expect(within(frame).queryByRole("button", { name: "Retry" })).toBeNull()
|
||||
})
|
||||
|
||||
it("opens a new history result at its newest line, not the old scroll position", async () => {
|
||||
const { client } = clientWith((endpoint, params) =>
|
||||
endpoint.endsWith("query")
|
||||
? new Response(`${line("2026-11-04T10:00:01Z", `result for ${params.get("query")}`)}\n`)
|
||||
: defaultHandler(endpoint, params),
|
||||
)
|
||||
const { queryClient } = renderWithK8sProvider(<LogsView scope={app} />, { client })
|
||||
queryClient.setDefaultOptions({ queries: { ...queryClient.getDefaultOptions().queries, placeholderData: (prev: unknown) => prev } })
|
||||
await screen.findByText("result for *")
|
||||
const before = screen.getByRole("list")
|
||||
|
||||
await userEvent.type(screen.getByLabelText("LogsQL filter"), "error{Enter}")
|
||||
|
||||
await screen.findByText("result for error")
|
||||
expect(screen.getByRole("list")).not.toBe(before)
|
||||
})
|
||||
|
||||
it("names the source pod and container of each line", async () => {
|
||||
const { client } = clientWith(defaultHandler)
|
||||
|
||||
renderWithK8sProvider(<LogsView scope={{ namespace: "tenant-a" }} />, { client })
|
||||
|
||||
expect(await screen.findByText("db-0/postgres")).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
import { useState } from "react"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { useK8sClient } from "@cozystack/k8s-client"
|
||||
import { Button } from "@cozystack/ui"
|
||||
import { Input } from "../ui/input.tsx"
|
||||
import { SelectItem, SelectLabel, SelectPopup, SelectRoot, SelectTrigger, SelectValue } from "../ui/select.tsx"
|
||||
import {
|
||||
CONTAINER_FIELD,
|
||||
LOG_RANGES,
|
||||
POD_FIELD,
|
||||
buildLogsQuery,
|
||||
type LogRangeKey,
|
||||
} from "../../lib/observability/logs.ts"
|
||||
import {
|
||||
fieldValues,
|
||||
isScopeWide,
|
||||
logsRoot,
|
||||
queryLogs,
|
||||
type ObservabilityScope,
|
||||
} from "../../lib/observability/proxy.ts"
|
||||
import { useLogTail } from "../../lib/observability/use-log-tail.ts"
|
||||
import { ProxyNotice } from "./ProxyNotice.tsx"
|
||||
import { RangePicker } from "./RangePicker.tsx"
|
||||
import { LogLines } from "./LogLines.tsx"
|
||||
import { EmptyState } from "./EmptyState.tsx"
|
||||
|
||||
const HISTORY_LIMIT = 500
|
||||
const FOLLOW_CAP = 1000
|
||||
const TAIL_STATE_LABEL = { connecting: "Connecting…", live: "Live", reconnecting: "Reconnecting…", stopped: "Stopped" }
|
||||
|
||||
function windowFor(range: LogRangeKey) {
|
||||
const end = Date.now()
|
||||
return { start: new Date(end - LOG_RANGES[range] * 1000).toISOString(), end: new Date(end).toISOString() }
|
||||
}
|
||||
|
||||
const ALL = ""
|
||||
|
||||
function Picker({ label, value, options, onChange }: { label: string; value: string; options: string[]; onChange: (v: string) => void }) {
|
||||
const items = [{ value: ALL, label: "All" }, ...options.filter((o) => o !== ALL).map((o) => ({ value: o, label: o }))]
|
||||
return (
|
||||
<SelectRoot items={items} value={value} onValueChange={(v) => onChange(v ?? ALL)}>
|
||||
<div className="flex min-w-0 items-center gap-1.5">
|
||||
<SelectLabel>{label}</SelectLabel>
|
||||
<SelectTrigger className="w-40" title={value || undefined}>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
</div>
|
||||
<SelectPopup>
|
||||
{items.map((item) => (
|
||||
<SelectItem key={item.value} value={item.value}>
|
||||
{item.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectPopup>
|
||||
</SelectRoot>
|
||||
)
|
||||
}
|
||||
|
||||
const RANGE_KEYS = Object.keys(LOG_RANGES) as LogRangeKey[]
|
||||
const LONGEST_RANGE = RANGE_KEYS[RANGE_KEYS.length - 1]
|
||||
|
||||
function placeholderFor({ follow, stopped, loading, failed, longestRange, retry }: { follow: boolean; stopped: boolean; loading: boolean; failed: boolean; longestRange: boolean; retry: () => void }) {
|
||||
if (follow) {
|
||||
return stopped ? (
|
||||
<EmptyState tone="dark" title="Follow stopped before any line arrived." />
|
||||
) : (
|
||||
<EmptyState tone="dark" busy title="Waiting for new log lines…" hint="Lines written from now on appear here." />
|
||||
)
|
||||
}
|
||||
if (loading) return <EmptyState tone="dark" busy title="Loading log lines…" />
|
||||
if (failed) {
|
||||
return (
|
||||
<EmptyState
|
||||
tone="dark"
|
||||
title="No log lines to show."
|
||||
action={<Button variant="outline" size="sm" onClick={retry}>Retry</Button>}
|
||||
/>
|
||||
)
|
||||
}
|
||||
return <EmptyState tone="dark" title="No log lines for this range and filter." hint={longestRange ? "Try a broader filter." : "Try a longer range or a broader filter."} />
|
||||
}
|
||||
|
||||
// Filter, pod, container and the lines already read belong to one scope; a new tenant or application starts clean.
|
||||
export function LogsView({ scope }: { scope: ObservabilityScope }) {
|
||||
const root = logsRoot(scope)
|
||||
return <ScopedLogsView key={root} root={root} />
|
||||
}
|
||||
|
||||
function ScopedLogsView({ root }: { root: string }) {
|
||||
const client = useK8sClient()
|
||||
const [range, setRange] = useState<LogRangeKey>("1h")
|
||||
const [draft, setDraft] = useState("")
|
||||
const [filter, setFilter] = useState("")
|
||||
const [pod, setPod] = useState("")
|
||||
const [container, setContainer] = useState("")
|
||||
const [followRun, setFollowRun] = useState<number | null>(null)
|
||||
const follow = followRun !== null
|
||||
const query = buildLogsQuery({ filter, pod, container })
|
||||
|
||||
const pickerQuery = (field: string, narrowTo?: string) => ({
|
||||
queryKey: ["observability", "logs-fields", root, field, range, narrowTo ?? ""],
|
||||
queryFn: ({ signal }: { signal: AbortSignal }) =>
|
||||
fieldValues(client, root, {
|
||||
field,
|
||||
stream: true,
|
||||
query: buildLogsQuery({ filter: "", pod: narrowTo }),
|
||||
...windowFor(range),
|
||||
}, signal),
|
||||
retry: false,
|
||||
})
|
||||
const pods = useQuery(pickerQuery(POD_FIELD))
|
||||
const containers = useQuery(pickerQuery(CONTAINER_FIELD, pod || undefined))
|
||||
|
||||
const history = useQuery({
|
||||
queryKey: ["observability", "logs", root, query, range],
|
||||
queryFn: ({ signal }) => queryLogs(client, root, { query, limit: HISTORY_LIMIT, ...windowFor(range) }, signal),
|
||||
enabled: !follow,
|
||||
retry: false,
|
||||
})
|
||||
const tail = useLogTail(root, query, followRun, { cap: FOLLOW_CAP })
|
||||
|
||||
// The pod list is optional: refusing it alone must not hide lines the query can still read.
|
||||
const wide = [history.error, tail.error].find(isScopeWide)
|
||||
if (wide) {
|
||||
return (
|
||||
<div className="p-6">
|
||||
<ProxyNotice what="Logs" error={wide} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const error = follow ? tail.error : history.error
|
||||
const entries = follow ? tail.entries : (history.data ?? [])
|
||||
|
||||
return (
|
||||
<div className="space-y-3 p-6">
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<form
|
||||
className="flex min-w-64 flex-1"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault()
|
||||
setFilter(draft)
|
||||
}}
|
||||
>
|
||||
<Input
|
||||
aria-label="LogsQL filter"
|
||||
value={draft}
|
||||
onValueChange={setDraft}
|
||||
placeholder='LogsQL filter, e.g. error or "connection refused"'
|
||||
className="font-mono"
|
||||
/>
|
||||
</form>
|
||||
<Picker label="Pod" value={pod} options={pods.data ?? []} onChange={(v) => { setPod(v); setContainer("") }} />
|
||||
<Picker label="Container" value={container} options={containers.data ?? []} onChange={setContainer} />
|
||||
{!follow && <RangePicker options={RANGE_KEYS} value={range} onChange={setRange} />}
|
||||
<Button variant={follow ? "default" : "outline"} size="sm" aria-pressed={follow} onClick={() => setFollowRun(follow ? null : Date.now())}>
|
||||
Follow
|
||||
</Button>
|
||||
</div>
|
||||
{follow && (
|
||||
<p className="text-xs text-slate-500">
|
||||
{TAIL_STATE_LABEL[tail.state]}
|
||||
{` · newest ${FOLLOW_CAP} lines kept`}
|
||||
</p>
|
||||
)}
|
||||
{!follow && history.isPlaceholderData && entries.length > 0 && (
|
||||
<p role="status" aria-busy="true" className="text-xs text-slate-500">
|
||||
Showing the previous lines until the new range or filter loads…
|
||||
</p>
|
||||
)}
|
||||
{error ? <ProxyNotice what="Logs" error={error} /> : null}
|
||||
<LogLines
|
||||
// Previous results stay as placeholder data while a new query loads, so only a new key resets the scroll pin.
|
||||
key={followRun ?? `history\n${query}\n${range}`}
|
||||
entries={entries}
|
||||
placeholder={placeholderFor({ follow, stopped: tail.state === "stopped", loading: history.isLoading || history.isPlaceholderData, failed: Boolean(error), longestRange: range === LONGEST_RANGE, retry: () => void history.refetch() })}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Loaded 100 of 200 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user