Skip to content

Commit

Permalink
Fabric-boot should fail if can't pre-cache artifacts
Browse files Browse the repository at this point in the history
Fixes #655
  • Loading branch information
Frostman committed Oct 22, 2024
1 parent 28a919a commit a42e549
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
20 changes: 9 additions & 11 deletions pkg/boot/server/onie.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,19 @@ import (

var ErrNotFound = errors.New("not found")

func (svc *service) preCacheBackground(ctx context.Context) {
func (svc *service) preCacheBackground(ctx context.Context) error {
l := slog.With("background", "cacher")

l.Info("Starting pre-caching")

for nosType, nosVersion := range svc.cfg.NOSVersions {
repo, ok := svc.cfg.NOSRepos[nosType]
if !ok {
// return fmt.Errorf("NOS repo not found: %s", nosType) //nolint:goerr113
l.Warn("NOS repo not found", "nosType", nosType)
return fmt.Errorf("NOS repo not found: %s", nosType) //nolint:goerr113
}

if _, err := svc.getCachedOrDownload(ctx, repo, nosVersion, true); err != nil {
// return fmt.Errorf("pre-caching NOS %s %s: %w", nosType, nosVersion, err)
l.Warn("Failed to pre-cache NOS", "nosType", nosType, "nosVersion", nosVersion, "error", err.Error())
return fmt.Errorf("pre-caching NOS %s %s: %w", nosType, nosVersion, err)
}
}

Expand All @@ -64,13 +62,11 @@ func (svc *service) preCacheBackground(ctx context.Context) {
for platform, version := range svc.cfg.ONIEPlatformVersions {
repo, ok := svc.cfg.ONIERepos[platform]
if !ok {
// return fmt.Errorf("ONIE repo not found: %s", platform) //nolint:goerr113
l.Warn("ONIE repo not found", "platform", platform)
return fmt.Errorf("ONIE repo not found: %s", platform) //nolint:goerr113
}

if _, err := svc.getCachedOrDownload(ctx, repo, version, true); err != nil {
// return fmt.Errorf("pre-caching ONIE %s %s: %w", platform, version, err)
l.Warn("Failed to pre-cache ONIE", "platform", platform, "version", version, "error", err.Error())
return fmt.Errorf("pre-caching ONIE %s %s: %w", platform, version, err)
}
}

Expand All @@ -89,7 +85,7 @@ retry:
for {
select {
case <-ctx.Done():
return
return fmt.Errorf("context done: %w", ctx.Err())
case event, ok := <-w.ResultChan():
if !ok || event.Object == nil {
l.Warn("Watch channel closed, retrying")
Expand Down Expand Up @@ -118,12 +114,14 @@ retry:
}

if _, err := svc.getCachedOrDownload(ctx, agentRepo, agentVersion, true); err != nil {
l.Warn("Failed to pre-cache agent", "repo", agentRepo, "version", agentVersion, "error", err.Error())
return fmt.Errorf("pre-caching agent %s %s: %w", agentRepo, agentVersion, err)
}
}
}
}
}

return fmt.Errorf("context done (pre-cache finished): %w", ctx.Err())
}

func (svc *service) handleONIE(w http.ResponseWriter, r *http.Request) {
Expand Down
8 changes: 7 additions & 1 deletion pkg/boot/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package server
import (
"context"
"fmt"
"log/slog"
"net/http"
"net/netip"
"os"
Expand Down Expand Up @@ -112,7 +113,12 @@ func Run(ctx context.Context) error {
sf: &singleflight.Group{},
}

go svc.preCacheBackground(ctx)
go func() {
if err := svc.preCacheBackground(ctx); err != nil {
slog.Error("Failed to pre-cache", "error", err)
os.Exit(1)
}
}()

r := chi.NewRouter()

Expand Down

0 comments on commit a42e549

Please sign in to comment.