Keep live envs whose name only corrected branches collide on, cap forge metadata reads
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful

This commit is contained in:
2026-11-08 21:41:31 +11:00
parent 1bb175c92c
commit 77259d507d
4 changed files with 77 additions and 12 deletions
+10 -9
View File
@@ -236,10 +236,11 @@ func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) erro
type envRef struct{ branch, sha string }
func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envRef, []error) {
// Environments maps env name -> branch; skipped holds names no branch won because only corrected branches claimed them.
func (d *Deployer) Environments(ctx context.Context, s *Source) (envs map[string]envRef, skipped map[string]bool, errs []error) {
all, err := d.branches(ctx, d.srcMirror(s))
if err != nil {
return nil, []error{err}
return nil, nil, []error{err}
}
names := make([]string, 0, len(all))
for b := range all {
@@ -248,7 +249,6 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envR
sort.Strings(names)
claims := map[string][]string{}
exact := map[string]string{}
var errs []error
for _, b := range names {
if s.Ignored(b) {
continue
@@ -263,7 +263,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envR
}
claims[name] = append(claims[name], b)
}
envs := map[string]envRef{}
envs, skipped = map[string]envRef{}, map[string]bool{}
for _, name := range slices.Sorted(maps.Keys(claims)) {
bs := claims[name]
win, isExact := exact[name]
@@ -272,6 +272,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envR
}
if win == "" {
errs = append(errs, fmt.Errorf("branches %s all correct to environment %s, skipped", strings.Join(bs, ", "), name))
skipped[name] = true
continue
}
for _, b := range bs {
@@ -284,14 +285,14 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envR
}
envs[name] = envRef{win, all[win]}
}
return envs, errs
return envs, skipped, errs
}
func (d *Deployer) deploySource(ctx context.Context, s *Source, want, found map[string]bool, modules bool) error {
if err := d.syncMirror(ctx, d.srcMirror(s), s.Remote, nil, envRefspecs); err != nil {
return err
}
envs, errs := d.Environments(ctx, s)
envs, skipped, errs := d.Environments(ctx, s)
if envs == nil {
return errors.Join(errs...)
}
@@ -322,7 +323,7 @@ func (d *Deployer) deploySource(ctx context.Context, s *Source, want, found map[
}
wg.Wait()
if full {
errs = append(errs, d.purge(s, envs))
errs = append(errs, d.purge(s, envs, skipped))
}
return errors.Join(errs...)
}
@@ -655,13 +656,13 @@ func Swap(target, link string) error {
return os.Rename(tmp, link)
}
func (d *Deployer) purge(s *Source, envs map[string]envRef) error {
func (d *Deployer) purge(s *Source, envs map[string]envRef, skipped map[string]bool) error {
entries, err := os.ReadDir(s.Basedir)
if err != nil {
return err
}
for _, e := range entries {
if _, ok := envs[e.Name()]; ok {
if _, ok := envs[e.Name()]; ok || skipped[e.Name()] {
continue
}
link := filepath.Join(s.Basedir, e.Name())
+27
View File
@@ -1154,3 +1154,30 @@ func TestCorrectedBranchCollisionSkipsAll(t *testing.T) {
t.Fatal("collision must not block other envs")
}
}
func TestCorrectedBranchCollisionKeepsLiveEnv(t *testing.T) {
f := newFixture(t)
commit(t, f.control, map[string]string{"manifests/site.pp": "node default {}\n"})
gitT(t, f.control, "checkout", "-q", "-b", "feature-x")
sha := commit(t, f.control, map[string]string{"manifests/site.pp": "node fx {}\n"})
gitT(t, f.control, "checkout", "-q", "main")
f.mustDeploy(false)
live := f.env("feature_x")
if !strings.HasPrefix(filepath.Base(live), sha+"-") {
t.Fatalf("setup: feature_x -> %q, want sha %s", live, sha)
}
gitT(t, f.control, "branch", "feature.x")
for range 3 {
err := f.deploy(false)
if err == nil || !strings.Contains(err.Error(), "feature-x") || !strings.Contains(err.Error(), "feature.x") {
t.Fatalf("want collision error naming both branches, got %v", err)
}
if got := f.env("feature_x"); got != live {
t.Fatalf("feature_x -> %q after colliding push, want live %q", got, live)
}
f.clock = f.clock.Add(11 * time.Minute)
}
if !exists(live) || f.env("main") == "" {
t.Fatal("live feature_x worktree must survive GC past grace")
}
}
+6 -3
View File
@@ -5,6 +5,7 @@ import (
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
@@ -18,6 +19,8 @@ const (
defaultForge = "https://forgeapi.puppet.com"
maxForgeDownload = 100 << 20
maxForgeExtracted = 512 << 20
maxForgeMeta = 1 << 20
maxVersion = 64
)
var httpClient = &http.Client{Timeout: 5 * time.Minute}
@@ -61,10 +64,10 @@ func forgeLatest(ctx context.Context, base, slug string) (string, error) {
Version string `json:"version"`
} `json:"current_release"`
}
if err := json.NewDecoder(body).Decode(&r); err != nil {
if err := json.NewDecoder(&capReader{r: body, n: maxForgeMeta}).Decode(&r); err != nil {
return "", fmt.Errorf("%s: %w", slug, err)
}
if !versionStr.MatchString(r.CurrentRelease.Version) {
if v := r.CurrentRelease.Version; len(v) > maxVersion || !versionStr.MatchString(v) {
return "", fmt.Errorf("%s: forge returned no usable current_release", slug)
}
return r.CurrentRelease.Version, nil
@@ -102,7 +105,7 @@ func (c *capReader) Read(p []byte) (int, error) {
}
n, err := c.r.Read(p)
if c.n -= int64(n); c.n < 0 {
return 0, fmt.Errorf("archive exceeds size cap")
return 0, errors.New("size cap exceeded")
}
return n, err
}
+34
View File
@@ -4,7 +4,11 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
@@ -116,3 +120,33 @@ func TestCapReaderErrorsAtCap(t *testing.T) {
t.Fatalf("under cap: %q %v", b, err)
}
}
func forgeServer(t *testing.T, version string, pad int) string {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, `{"current_release":{"version":"%s%s"}}`, version, strings.Repeat("a", pad))
}))
t.Cleanup(srv.Close)
return srv.URL
}
func TestForgeLatestCapped(t *testing.T) {
v, err := forgeLatest(context.Background(), forgeServer(t, "", maxForgeMeta+1), "acme-x")
if err == nil || !strings.Contains(err.Error(), "size cap") {
t.Fatalf("want size cap error, got version len %d, err %v", len(v), err)
}
}
func TestForgeLatestBoundsVersion(t *testing.T) {
for version, ok := range map[string]bool{
"1.2.3": true,
strings.Repeat("1", 64): true,
strings.Repeat("1", 65): false,
"": false,
"1.0 ; rm": false,
} {
v, err := forgeLatest(context.Background(), forgeServer(t, version, 0), "acme-x")
if ok != (err == nil) || (ok && v != version) {
t.Errorf("version %q: got %q, %v", version, v, err)
}
}
}