Simplify sandbox connection code

PiperOrigin-RevId: 503203944
This commit is contained in:
Fabricio Voznika
2023-01-19 10:38:50 -08:00
committed by gVisor bot
parent 2a56495dfa
commit e906d1936c
11 changed files with 98 additions and 287 deletions
-1
View File
@@ -130,7 +130,6 @@ const (
const (
UsageCollect = "Usage.Collect"
UsageUsageFD = "Usage.UsageFD"
UsageReduce = "Usage.Reduce"
)
// Metrics related commands (see metrics.go).
-1
View File
@@ -66,7 +66,6 @@ func (c *Checkpoint) SetFlags(f *flag.FlagSet) {
// Execute implements subcommands.Command.Execute.
func (c *Checkpoint) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
if f.NArg() != 1 {
f.Usage()
return subcommands.ExitUsageError
+2 -2
View File
@@ -105,7 +105,7 @@ func (d *Debug) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcomm
id := f.Arg(0)
var err error
c, err = container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{})
c, err = container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true})
if err != nil {
return util.Errorf("loading container %q: %v", f.Arg(0), err)
}
@@ -115,7 +115,7 @@ func (d *Debug) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcomm
return subcommands.ExitUsageError
}
// Go over all sandboxes and find the one that matches PID.
ids, err := container.List(conf.RootDir)
ids, err := container.ListSandboxes(conf.RootDir)
if err != nil {
return util.Errorf("listing containers: %v", err)
}
+7 -13
View File
@@ -77,23 +77,17 @@ func (l *List) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcomma
}
func (l *List) execute(rootDir string, out io.Writer) error {
ids, err := container.List(rootDir)
var ids []container.FullID
var err error
if l.sandbox {
ids, err = container.ListSandboxes(rootDir)
} else {
ids, err = container.List(rootDir)
}
if err != nil {
return err
}
if l.sandbox {
sandboxes := make(map[string]struct{})
for _, id := range ids {
sandboxes[id.SandboxID] = struct{}{}
}
// Reset ids to list only sandboxes.
ids = nil
for id := range sandboxes {
ids = append(ids, container.FullID{SandboxID: id, ContainerID: id})
}
}
if l.quiet {
for _, id := range ids {
fmt.Fprintln(out, id.ContainerID)
+1 -1
View File
@@ -61,7 +61,7 @@ func (ps *PS) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcomm
id := f.Arg(0)
conf := args[0].(*config.Config)
c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{})
c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true})
if err != nil {
util.Fatalf("loading sandbox: %v", err)
}
+1 -1
View File
@@ -66,7 +66,7 @@ func (r *ReadControl) Execute(_ context.Context, f *flag.FlagSet, args ...any) s
id := f.Arg(0)
conf := args[0].(*config.Config)
c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{})
c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true})
if err != nil {
util.Fatalf("loading sandbox: %v", err)
}
+3 -2
View File
@@ -43,7 +43,8 @@ func (*Usage) Synopsis() string {
// Usage implements subcommands.Command.Usage.
func (*Usage) Usage() string {
return `usage [flags] <container id> - print memory usages to standard output.`
return `usage [flags] <container id> - print memory usages to standard output.
`
}
// SetFlags implements subcommands.Command.SetFlags.
@@ -62,7 +63,7 @@ func (u *Usage) Execute(_ context.Context, f *flag.FlagSet, args ...any) subcomm
id := f.Arg(0)
conf := args[0].(*config.Config)
cont, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{})
cont, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true})
if err != nil {
util.Fatalf("loading container: %v", err)
}
+1 -1
View File
@@ -66,7 +66,7 @@ func (r *WriteControl) Execute(_ context.Context, f *flag.FlagSet, args ...any)
id := f.Arg(0)
conf := args[0].(*config.Config)
c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{})
c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{SkipCheck: true})
if err != nil {
util.Fatalf("loading sandbox: %v", err)
}
-30
View File
@@ -2604,36 +2604,6 @@ func TestUsageFD(t *testing.T) {
}
}
// TestReduce checks that reduce call succeeds.
func TestReduce(t *testing.T) {
spec, conf := sleepSpecConf(t)
_, bundleDir, cleanup, err := testutil.SetupContainer(spec, conf)
if err != nil {
t.Fatalf("error setting up container: %v", err)
}
defer cleanup()
args := Args{
ID: testutil.RandomContainerID(),
Spec: spec,
BundleDir: bundleDir,
}
cont, err := New(conf, args)
if err != nil {
t.Fatalf("Creating container: %v", err)
}
defer cont.Destroy()
if err := cont.Start(conf); err != nil {
t.Fatalf("starting container: %v", err)
}
if err := cont.Sandbox.Reduce(false); err != nil {
t.Fatalf("error reduce from container: %v", err)
}
}
// TestProfile checks that profiling options generate profiles.
func TestProfile(t *testing.T) {
// Perform a non-trivial amount of work so we actually capture
+20
View File
@@ -113,6 +113,26 @@ func List(rootDir string) ([]FullID, error) {
return listMatch(rootDir, FullID{})
}
// ListSandboxes returns all sandbox ids in the given root directory.
func ListSandboxes(rootDir string) ([]FullID, error) {
log.Debugf("List containers %q", rootDir)
ids, err := List(rootDir)
if err != nil {
return nil, err
}
sandboxes := make(map[string]struct{}, len(ids))
for _, id := range ids {
sandboxes[id.SandboxID] = struct{}{}
}
// Reset ids to list only sandboxes.
ids = nil
for id := range sandboxes {
ids = append(ids, FullID{SandboxID: id, ContainerID: id})
}
return ids, nil
}
// listMatch returns all container ids that match the provided id.
func listMatch(rootDir string, id FullID) ([]FullID, error) {
id.SandboxID += "*"
+63 -235
View File
File diff suppressed because it is too large Load Diff