Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/atelet/worker_sandbox_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ type workerSandboxSyncer struct {
// probeCPUIdentity is hardware.ProbeCPUIdentity. Overridable in tests.
probeCPUIdentity func() (vendor, model string, err error)
// runCPUFeatures is runRunscCPUFeatures. Overridable in tests.
runCPUFeatures func(ctx context.Context, runscPath string) ([]string, error)
runCPUFeatures func(ctx context.Context, runscPath string) (string, error)
// mu guards hostCPU, cpuFeatures and verifiedBundles across the
// syncWorkers in run: hostCPU is the probed host CPU identity, nil until
// a probe succeeds; cpuFeatures is the encoded cpu_features of each
Expand Down
25 changes: 10 additions & 15 deletions cmd/atelet/worker_sandbox_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
Expand Down Expand Up @@ -51,16 +50,12 @@ const (
testCPUModel = "6:143"
)

// testCPUFeatures are the first two CPU feature names for runtime.GOARCH,
// which encode to bit 0 | bit 1 = "3" on both amd64 and arm64.
func testCPUFeatures() []string {
switch runtime.GOARCH {
case "arm64":
return []string{"fp", "asimd"}
default:
return []string{"pni", "pclmulqdq"}
}
}
// testCPUFeaturesBlock is what the faked runsc prints for `cpu-features
// --bitmask`: bits 0 and 1, which encode to testCPUFeaturesMask.
const (
testCPUFeaturesBlock = "0x00000003"
testCPUFeaturesMask = "3"
)

// fakeAssetFetcher answers ensureSandboxAssets from fn. It creates dstDir as
// the real fetcher does, but the paths it returns are not under it: the
Expand Down Expand Up @@ -94,12 +89,12 @@ func newTestAssetFetcher() sandboxAssetFetcher {
}

// newScriptAssetFetcher returns a sandboxAssetFetcher that backs every asset
// with an executable script printing testCPUFeatures(), for a syncer that
// with an executable script printing testCPUFeaturesBlock, for a syncer that
// runs the real runsc probe.
func newScriptAssetFetcher(t *testing.T) sandboxAssetFetcher {
t.Helper()
script := filepath.Join(t.TempDir(), "runsc")
body := "#!/bin/sh\necho " + strings.Join(testCPUFeatures(), ",") + "\n"
body := "#!/bin/sh\necho " + testCPUFeaturesBlock + "\n"
if err := os.WriteFile(script, []byte(body), 0o755); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -145,7 +140,7 @@ func resolvedPoolOf(class atev1.SandboxClass, name, config string) *atev1.Worker

// newTestSyncer serves objs from synced caches and keeps worker directories
// under a temp dir. Its probes are faked: the host CPU is
// testCPUVendor/testCPUModel and every runsc reports testCPUFeatures();
// testCPUVendor/testCPUModel and every runsc reports testCPUFeaturesMask;
// TestRunRunscCPUFeatures covers the real probe.
func newTestSyncer(t *testing.T, objs ...any) *workerSandboxSyncer {
t.Helper()
Expand Down Expand Up @@ -181,7 +176,7 @@ func newTestSyncer(t *testing.T, objs ...any) *workerSandboxSyncer {
t.Cleanup(s.queue.ShutDown)
s.scratchDir = t.TempDir() // buildFile stages assets there
s.probeCPUIdentity = func() (string, string, error) { return testCPUVendor, testCPUModel, nil }
s.runCPUFeatures = func(context.Context, string) ([]string, error) { return testCPUFeatures(), nil }
s.runCPUFeatures = func(context.Context, string) (string, error) { return testCPUFeaturesMask, nil }
// Tests that check the jitter set it.
s.maxJitter = 0
return s
Expand Down
59 changes: 32 additions & 27 deletions cmd/atelet/worker_sandbox_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ import (
// - the pod has no worker pool label
// - the config's default version is missing or has no assets for this
// node's architecture
// - the default version's runsc runs but fails, prints no features, or
// prints one this atelet does not know; the binary is content-addressed,
// so running it again gives the same answer
// - the default version's runsc runs but fails, prints nothing, or prints
// what does not encode; the binary is content-addressed, so running it
// again gives the same answer
//
// The same failures on a restorable version leave it out of the file instead.
var errPermanent = errors.New("permanent failure")
Expand Down Expand Up @@ -414,11 +414,11 @@ func (s *workerSandboxSyncer) ensureMicroVMAssets(ctx context.Context, uid strin
}

// gvisorCPUFeatures returns the encoded cpu_features of v's runsc binary. It
// stages the version like ensureMicroVMAssets, and runs `<runsc>
// cpu-features` the first time this atelet process sees the binary's digest,
// on a runsc linked into scratch for the probe alone, never on the one under
// the worker's directory. The memo is per digest, so a bundle that adds an
// asset beside a known runsc is staged for that asset without running runsc
// stages the version like ensureMicroVMAssets, and probes it with
// runRunscCPUFeatures the first time this atelet process sees the binary's
// digest, on a runsc linked into scratch for the probe alone, never on the one
// under the worker's directory. The memo is per digest, so a bundle that adds
// an asset beside a known runsc is staged for that asset without running runsc
// again.
func (s *workerSandboxSyncer) gvisorCPUFeatures(ctx context.Context, uid string, v hashedVersion) (string, error) {
digest := v.rec.Assets[gvisorAssetName].SHA256
Expand All @@ -442,13 +442,10 @@ func (s *workerSandboxSyncer) gvisorCPUFeatures(ctx context.Context, uid string,
}
defer done()
if !probed {
names, err := s.runCPUFeatures(ctx, runscPathFor(paths))
mask, err = s.runCPUFeatures(ctx, runscPathFor(paths))
if err != nil {
return "", err
}
if mask, err = hardware.EncodeCPUFeatures(runtime.GOARCH, names); err != nil {
return "", fmt.Errorf("%w: %w", errPermanent, err)
}
}
}
s.mu.Lock()
Expand All @@ -472,19 +469,23 @@ func (s *workerSandboxSyncer) gvisorCPUFeatures(ctx context.Context, uid string,
// joins one still in flight.
const probeFetchTimeout = 10 * time.Minute

// cpuFeaturesTimeout bounds one `runsc cpu-features` run, which takes
// milliseconds. A runsc that hangs would hold a sync worker. A var so tests
// can shorten it.
// cpuFeaturesTimeout bounds one `runsc cpu-features --bitmask` run, which
// takes milliseconds. A runsc that hangs would hold a sync worker. A var so
// tests can shorten it.
var cpuFeaturesTimeout = 30 * time.Second

// runRunscCPUFeatures executes `<runscPath> cpu-features` and returns the
// comma-separated feature names it prints. The binary is content-addressed,
// so one that runs and fails, or prints nothing, would do so again and fails
// permanently; one that cannot be started, or is cut off by ctx, is retried.
func runRunscCPUFeatures(ctx context.Context, runscPath string) ([]string, error) {
// runRunscCPUFeatures executes `<runscPath> cpu-features --bitmask` and
// returns the cpu_features value its output encodes to. The binary is
// content-addressed, so one that runs and fails, or prints nothing or what
// does not encode, would do so again and fails permanently; one that cannot
// be started, or is cut off by ctx, is retried. runsc has the flag from
// gVisor release 20261026.0, which ateworker-gvisor needs anyway (see
// manifests/ate-install/sandboxconfig-gvisor.yaml), so an older runsc fails
// here with "flag provided but not defined".
func runRunscCPUFeatures(ctx context.Context, runscPath string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, cpuFeaturesTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, runscPath, "cpu-features")
cmd := exec.CommandContext(ctx, runscPath, "cpu-features", "--bitmask")
cmd.Env = []string{} // the probe needs nothing from atelet's environment
out, err := cmd.Output()
var exitErr *exec.ExitError
Expand All @@ -493,20 +494,24 @@ func runRunscCPUFeatures(ctx context.Context, runscPath string) ([]string, error
case ctx.Err() != nil:
// Before the ExitError case: a process killed at the deadline exits
// with one too.
return nil, fmt.Errorf("while running %s cpu-features: %w", runscPath, ctx.Err())
return "", fmt.Errorf("while running %s cpu-features --bitmask: %w", runscPath, ctx.Err())
case errors.As(err, &exitErr):
if stderr := strings.TrimSpace(string(exitErr.Stderr)); stderr != "" {
return nil, fmt.Errorf("%w: %s cpu-features: %w (%s)", errPermanent, runscPath, err, stderr)
return "", fmt.Errorf("%w: %s cpu-features --bitmask: %w (%s)", errPermanent, runscPath, err, stderr)
}
return nil, fmt.Errorf("%w: %s cpu-features: %w", errPermanent, runscPath, err)
return "", fmt.Errorf("%w: %s cpu-features --bitmask: %w", errPermanent, runscPath, err)
default:
return nil, fmt.Errorf("while starting %s cpu-features: %w", runscPath, err)
return "", fmt.Errorf("while starting %s cpu-features --bitmask: %w", runscPath, err)
}
printed := strings.TrimSpace(string(out))
if printed == "" {
return nil, fmt.Errorf("%w: %s cpu-features printed no features", errPermanent, runscPath)
return "", fmt.Errorf("%w: %s cpu-features --bitmask printed nothing", errPermanent, runscPath)
}
mask, err := hardware.EncodeCPUFeatureMask(printed)
if err != nil {
return "", fmt.Errorf("%w: %s cpu-features --bitmask: %w", errPermanent, runscPath, err)
}
return strings.Split(printed, ","), nil
return mask, nil
}

// bundleHash is the sandbox_bundle_hash of a version: a sha256 over one
Expand Down
77 changes: 39 additions & 38 deletions cmd/atelet/worker_sandbox_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ func TestWorkerSandboxResolverResolve(t *testing.T) {
}

// wantRuntime is the runtime buildFile builds from multiVersionConfig's
// version with the given name when runsc reports testCPUFeatures() ("3").
// version with the given name when the probe reports testCPUFeaturesMask.
func wantRuntime(name string) sandboxruntimes.Attributes {
return wantRuntimeWithFeatures(name, "3")
return wantRuntimeWithFeatures(name, testCPUFeaturesMask)
}

func wantRuntimeWithFeatures(name, cpuFeatures string) sandboxruntimes.Attributes {
Expand Down Expand Up @@ -723,8 +723,8 @@ func TestSyncPodAssetCacheAndEviction(t *testing.T) {
s.probeCPUIdentity = func() (string, string, error) {
return "", "", errors.New("re-probed memoized host CPU identity")
}
s.runCPUFeatures = func(context.Context, string) ([]string, error) {
return nil, errors.New("re-ran runsc cpu-features for a memoized digest")
s.runCPUFeatures = func(context.Context, string) (string, error) {
return "", errors.New("re-ran runsc cpu-features for a memoized digest")
}
if _, err := s.syncPod(ctx, workerPod("pod-a", poolName)); err != nil {
t.Fatalf("syncPod(pod-a) after bucket recovery: %v", err)
Expand Down Expand Up @@ -781,9 +781,9 @@ func TestSyncPodAssetCacheAndEviction(t *testing.T) {
probed = append(probed, "host")
return testCPUVendor, testCPUModel, nil
}
s.runCPUFeatures = func(_ context.Context, runscPath string) ([]string, error) {
s.runCPUFeatures = func(_ context.Context, runscPath string) (string, error) {
probed = append(probed, runscPath)
return testCPUFeatures(), nil
return testCPUFeaturesMask, nil
}
if _, err := s.syncPod(ctx, workerPod("pod-a", poolName)); err != nil {
t.Fatalf("syncPod(pod-a) in a new process: %v", err)
Expand Down Expand Up @@ -876,7 +876,7 @@ func TestSyncPodPinsDefaultRuntime(t *testing.T) {
// A build that fails on the default's probe writes nothing and leaves
// the previous default's directory as it was.
s = newTestSyncer(t, pod, resolvedPool("pool", "gvisor-release"), multiVersionConfig())
s.runCPUFeatures = func(context.Context, string) ([]string, error) { return nil, errors.New("runsc failed") }
s.runCPUFeatures = func(context.Context, string) (string, error) { return "", errors.New("runsc failed") }
previous := s.defaultRuntimeDir("pod-a", "sha256:previous")
if err := os.MkdirAll(previous, 0o700); err != nil {
t.Fatal(err)
Expand All @@ -896,8 +896,6 @@ func TestSyncPodPinsDefaultRuntime(t *testing.T) {
}

func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) {
feats := testCPUFeatures()

t.Run("a version with an earlier version's binaries in another bucket is neither fetched nor probed", func(t *testing.T) {
// bundleHash leaves URLs out, so v3 is v1's runtime. The syncer must
// see that from the spec alone, before touching v3's bucket.
Expand Down Expand Up @@ -946,20 +944,20 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) {
},
}
execCalls := make(map[string]int)
s.runCPUFeatures = func(_ context.Context, runscPath string) ([]string, error) {
s.runCPUFeatures = func(_ context.Context, runscPath string) (string, error) {
execCalls[runscPath]++
if runscPath == "/cache/sha-v2/runsc" {
return feats, nil // bits 0 and 1 -> "3"
return testCPUFeaturesMask, nil // bits 0 and 1
}
return feats[:1], nil // bit 0 only -> "1"
return "1", nil // bit 0 only
}

got, err := planAndBuild(s, workerPod("pod-a", "pool"))
if err != nil {
t.Fatalf("first planAndBuild() error = %v", err)
}
want := gvisorFile(
wantRuntimeWithFeatures("v2", "3"),
wantRuntimeWithFeatures("v2", testCPUFeaturesMask),
wantRuntimeWithFeatures("v1", "1"),
sandboxruntimes.Attributes{
hardware.AttrArchitecture: runtime.GOARCH,
Expand Down Expand Up @@ -1008,8 +1006,8 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) {
}

s = newTestSyncer(t, resolvedPool("pool", "gvisor-release"), multiVersionConfig())
s.runCPUFeatures = func(context.Context, string) ([]string, error) {
return nil, errors.New("runsc failed")
s.runCPUFeatures = func(context.Context, string) (string, error) {
return "", errors.New("runsc failed")
}
if _, err := planAndBuild(s, workerPod("pod-a", "pool")); err == nil || errors.Is(err, errPermanent) {
t.Errorf("planAndBuild() on runCPUFeatures error = %v, want non-permanent error", err)
Expand All @@ -1027,7 +1025,7 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) {
return map[string]string{gvisorAssetName: "sha-v2"}, nil
},
}
s.runCPUFeatures = func(context.Context, string) ([]string, error) { return feats, nil }
s.runCPUFeatures = func(context.Context, string) (string, error) { return testCPUFeaturesMask, nil }
got, err := planAndBuild(s, workerPod("pod-a", "pool"))
if !errors.Is(err, errIncomplete) || errors.Is(err, errPermanent) {
t.Errorf("planAndBuild() on a restorable version's fetch error = %v, want errIncomplete and not errPermanent", err)
Expand All @@ -1038,13 +1036,14 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) {
}
})

t.Run("unknown CPU feature on default version is permanent, on non-default is skipped", func(t *testing.T) {
t.Run("permanent probe failure on default version fails the build, on non-default is skipped", func(t *testing.T) {
// What runRunscCPUFeatures returns for a runsc that prints what does
// not encode.
probeErr := fmt.Errorf("%w: runsc cpu-features --bitmask: not a bitmask", errPermanent)
s := newTestSyncer(t, resolvedPool("pool", "gvisor-release"), multiVersionConfig())
s.runCPUFeatures = func(context.Context, string) ([]string, error) {
return []string{"not_a_known_feature"}, nil
}
s.runCPUFeatures = func(context.Context, string) (string, error) { return "", probeErr }
if _, err := planAndBuild(s, workerPod("pod-a", "pool")); !errors.Is(err, errPermanent) {
t.Errorf("planAndBuild() with unknown feature on default = %v, want errPermanent", err)
t.Errorf("planAndBuild() with the default's probe failing permanently = %v, want errPermanent", err)
}

s = newTestSyncer(t, resolvedPool("pool", "gvisor-release"), multiVersionConfig())
Expand All @@ -1053,15 +1052,15 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) {
return map[string]string{gvisorAssetName: rec.Assets[gvisorAssetName].SHA256}, nil
},
}
s.runCPUFeatures = func(_ context.Context, runscPath string) ([]string, error) {
s.runCPUFeatures = func(_ context.Context, runscPath string) (string, error) {
if runscPath == "sha-v2" {
return feats, nil
return testCPUFeaturesMask, nil
}
return []string{"future_unknown_feature"}, nil
return "", probeErr
}
got, err := planAndBuild(s, workerPod("pod-a", "pool"))
if err != nil {
t.Fatalf("planAndBuild() with unknown feature on non-default error = %v", err)
t.Fatalf("planAndBuild() with a restorable version's probe failing permanently error = %v", err)
}
want := gvisorFile(wantRuntime("v2"))
if diff := cmp.Diff(want, got); diff != "" {
Expand All @@ -1081,24 +1080,24 @@ func TestRunRunscCPUFeatures(t *testing.T) {
}
return p
}

got, err := runRunscCPUFeatures(ctx, script("ok", "echo pni,avx2\n"))
if err != nil {
t.Fatalf("runRunscCPUFeatures() error = %v", err)
}
if diff := cmp.Diff([]string{"pni", "avx2"}, got); diff != "" {
t.Errorf("runRunscCPUFeatures() mismatch (-want +got):\n%s", diff)
got, err := runRunscCPUFeatures(ctx, script("bitmask", "echo "+testCPUFeaturesBlock+"\n"))
if err != nil || got != testCPUFeaturesMask {
t.Errorf("runRunscCPUFeatures() = (%q, %v), want (%q, nil)", got, err, testCPUFeaturesMask)
}

// A binary that runs and fails, or prints nothing, will do so again:
// permanent, with its stderr in the error.
_, err = runRunscCPUFeatures(ctx, script("fail", "echo boom >&2\nexit 1\n"))
if !errors.Is(err, errPermanent) || !strings.Contains(err.Error(), "boom") {
t.Errorf("runRunscCPUFeatures() on a failing binary = %v, want permanent error quoting boom", err)
// A binary that runs and fails, prints nothing, or prints what does not
// encode will do so again: permanent, with its stderr in the error. A
// runsc from before --bitmask is the first kind.
_, err = runRunscCPUFeatures(ctx, script("old", "echo 'flag provided but not defined: -bitmask' >&2\nexit 128\n"))
if !errors.Is(err, errPermanent) || !strings.Contains(err.Error(), "not defined: -bitmask") {
t.Errorf("runRunscCPUFeatures() on a runsc without --bitmask = %v, want permanent error quoting its stderr", err)
}
if _, err := runRunscCPUFeatures(ctx, script("empty", "exit 0\n")); !errors.Is(err, errPermanent) {
t.Errorf("runRunscCPUFeatures() on empty output = %v, want permanent error", err)
}
if _, err := runRunscCPUFeatures(ctx, script("names", "echo fpu,sse2\n")); !errors.Is(err, errPermanent) {
t.Errorf("runRunscCPUFeatures() on names instead of blocks = %v, want permanent error", err)
}

// A binary that cannot be started, or is cut off, is retried.
if _, err := runRunscCPUFeatures(ctx, filepath.Join(dir, "missing")); err == nil || errors.Is(err, errPermanent) {
Expand Down Expand Up @@ -1149,7 +1148,9 @@ func TestSyncPodAdvertisesWhatItWrote(t *testing.T) {
s.mu.Lock()
s.cpuFeatures = map[string]string{}
s.mu.Unlock()
s.runCPUFeatures = func(context.Context, string) ([]string, error) { return []string{"not_a_known_feature"}, nil }
s.runCPUFeatures = func(context.Context, string) (string, error) {
return "", fmt.Errorf("%w: runsc cpu-features --bitmask: not a bitmask", errPermanent)
}
if _, err := s.syncPod(ctx, pod); !errors.Is(err, errPermanent) {
t.Fatalf("syncPod with the default's probe failing = %v, want errPermanent", err)
}
Expand Down
Loading
Loading