Merge pull request #9883 from abel-von/modify-sandbox-client

sandbox: modify sandbox client
This commit is contained in:
Maksym Pavlenko 2024-05-07 04:16:57 +00:00 committed by GitHub
commit cd7825da08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -34,8 +34,8 @@ import (
type Sandbox interface { type Sandbox interface {
// ID is a sandbox identifier // ID is a sandbox identifier
ID() string ID() string
// PID returns sandbox's process PID or error if its not yet started. // Metadata returns metadata of the sandbox
PID() (uint32, error) Metadata() api.Sandbox
// NewContainer creates new container that will belong to this sandbox // NewContainer creates new container that will belong to this sandbox
NewContainer(ctx context.Context, id string, opts ...NewContainerOpts) (Container, error) NewContainer(ctx context.Context, id string, opts ...NewContainerOpts) (Container, error)
// Labels returns the labels set on the sandbox // Labels returns the labels set on the sandbox
@ -51,7 +51,6 @@ type Sandbox interface {
} }
type sandboxClient struct { type sandboxClient struct {
pid *uint32
client *Client client *Client
metadata api.Sandbox metadata api.Sandbox
} }
@ -60,12 +59,8 @@ func (s *sandboxClient) ID() string {
return s.metadata.ID return s.metadata.ID
} }
func (s *sandboxClient) PID() (uint32, error) { func (s *sandboxClient) Metadata() api.Sandbox {
if s.pid == nil { return s.metadata
return 0, fmt.Errorf("sandbox not started")
}
return *s.pid, nil
} }
func (s *sandboxClient) NewContainer(ctx context.Context, id string, opts ...NewContainerOpts) (Container, error) { func (s *sandboxClient) NewContainer(ctx context.Context, id string, opts ...NewContainerOpts) (Container, error) {
@ -82,12 +77,11 @@ func (s *sandboxClient) Labels(ctx context.Context) (map[string]string, error) {
} }
func (s *sandboxClient) Start(ctx context.Context) error { func (s *sandboxClient) Start(ctx context.Context) error {
resp, err := s.client.SandboxController(s.metadata.Sandboxer).Start(ctx, s.ID()) _, err := s.client.SandboxController(s.metadata.Sandboxer).Start(ctx, s.ID())
if err != nil { if err != nil {
return err return err
} }
s.pid = &resp.Pid
return nil return nil
} }
@ -154,7 +148,6 @@ func (c *Client) NewSandbox(ctx context.Context, sandboxID string, opts ...NewSa
} }
return &sandboxClient{ return &sandboxClient{
pid: nil, // Not yet started
client: c, client: c,
metadata: metadata, metadata: metadata,
}, nil }, nil
@ -167,13 +160,7 @@ func (c *Client) LoadSandbox(ctx context.Context, id string) (Sandbox, error) {
return nil, err return nil, err
} }
status, err := c.SandboxController(sandbox.Sandboxer).Status(ctx, id, false)
if err != nil {
return nil, fmt.Errorf("failed to load sandbox %s, status request failed: %w", id, err)
}
return &sandboxClient{ return &sandboxClient{
pid: &status.Pid,
client: c, client: c,
metadata: sandbox, metadata: sandbox,
}, nil }, nil