mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-25 23:11:57 +00:00
Merge pull request #229 from jshachm/fix-pod-to-sandbox
runtime: fix codes misunderstanding after rename pod to sandbox
This commit is contained in:
commit
fdadb041aa
@ -48,32 +48,32 @@ func CreateSandbox(sandboxConfig SandboxConfig) (VCSandbox, error) {
|
|||||||
|
|
||||||
func createSandboxFromConfig(sandboxConfig SandboxConfig) (*Sandbox, error) {
|
func createSandboxFromConfig(sandboxConfig SandboxConfig) (*Sandbox, error) {
|
||||||
// Create the sandbox.
|
// Create the sandbox.
|
||||||
p, err := createSandbox(sandboxConfig)
|
s, err := createSandbox(sandboxConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the sandbox network
|
// Create the sandbox network
|
||||||
if err := p.createNetwork(); err != nil {
|
if err := s.createNetwork(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the VM
|
// Start the VM
|
||||||
if err := p.startVM(); err != nil {
|
if err := s.startVM(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Containers
|
// Create Containers
|
||||||
if err := p.createContainers(); err != nil {
|
if err := s.createContainers(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// The sandbox is completely created now, we can store it.
|
// The sandbox is completely created now, we can store it.
|
||||||
if err := p.storeSandbox(); err != nil {
|
if err := s.storeSandbox(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteSandbox is the virtcontainers sandbox deletion entry point.
|
// DeleteSandbox is the virtcontainers sandbox deletion entry point.
|
||||||
|
@ -472,21 +472,21 @@ type Sandbox struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ID returns the sandbox identifier string.
|
// ID returns the sandbox identifier string.
|
||||||
func (p *Sandbox) ID() string {
|
func (s *Sandbox) ID() string {
|
||||||
return p.id
|
return s.id
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logger returns a logrus logger appropriate for logging Sandbox messages
|
// Logger returns a logrus logger appropriate for logging Sandbox messages
|
||||||
func (p *Sandbox) Logger() *logrus.Entry {
|
func (s *Sandbox) Logger() *logrus.Entry {
|
||||||
return virtLog.WithFields(logrus.Fields{
|
return virtLog.WithFields(logrus.Fields{
|
||||||
"subsystem": "sandbox",
|
"subsystem": "sandbox",
|
||||||
"sandbox-id": p.id,
|
"sandbox-id": s.id,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Annotations returns any annotation that a user could have stored through the sandbox.
|
// Annotations returns any annotation that a user could have stored through the sandbox.
|
||||||
func (p *Sandbox) Annotations(key string) (string, error) {
|
func (s *Sandbox) Annotations(key string) (string, error) {
|
||||||
value, exist := p.config.Annotations[key]
|
value, exist := s.config.Annotations[key]
|
||||||
if exist == false {
|
if exist == false {
|
||||||
return "", fmt.Errorf("Annotations key %s does not exist", key)
|
return "", fmt.Errorf("Annotations key %s does not exist", key)
|
||||||
}
|
}
|
||||||
@ -495,15 +495,15 @@ func (p *Sandbox) Annotations(key string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetAnnotations sets or adds an annotations
|
// SetAnnotations sets or adds an annotations
|
||||||
func (p *Sandbox) SetAnnotations(annotations map[string]string) error {
|
func (s *Sandbox) SetAnnotations(annotations map[string]string) error {
|
||||||
p.annotationsLock.Lock()
|
s.annotationsLock.Lock()
|
||||||
defer p.annotationsLock.Unlock()
|
defer s.annotationsLock.Unlock()
|
||||||
|
|
||||||
for k, v := range annotations {
|
for k, v := range annotations {
|
||||||
p.config.Annotations[k] = v
|
s.config.Annotations[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
err := p.storage.storeSandboxResource(p.id, configFileType, *(p.config))
|
err := s.storage.storeSandboxResource(s.id, configFileType, *(s.config))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -512,18 +512,18 @@ func (p *Sandbox) SetAnnotations(annotations map[string]string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAnnotations returns sandbox's annotations
|
// GetAnnotations returns sandbox's annotations
|
||||||
func (p *Sandbox) GetAnnotations() map[string]string {
|
func (s *Sandbox) GetAnnotations() map[string]string {
|
||||||
p.annotationsLock.RLock()
|
s.annotationsLock.RLock()
|
||||||
defer p.annotationsLock.RUnlock()
|
defer s.annotationsLock.RUnlock()
|
||||||
|
|
||||||
return p.config.Annotations
|
return s.config.Annotations
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllContainers returns all containers.
|
// GetAllContainers returns all containers.
|
||||||
func (p *Sandbox) GetAllContainers() []VCContainer {
|
func (s *Sandbox) GetAllContainers() []VCContainer {
|
||||||
ifa := make([]VCContainer, len(p.containers))
|
ifa := make([]VCContainer, len(s.containers))
|
||||||
|
|
||||||
for i, v := range p.containers {
|
for i, v := range s.containers {
|
||||||
ifa[i] = v
|
ifa[i] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -531,8 +531,8 @@ func (p *Sandbox) GetAllContainers() []VCContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetContainer returns the container named by the containerID.
|
// GetContainer returns the container named by the containerID.
|
||||||
func (p *Sandbox) GetContainer(containerID string) VCContainer {
|
func (s *Sandbox) GetContainer(containerID string) VCContainer {
|
||||||
for _, c := range p.containers {
|
for _, c := range s.containers {
|
||||||
if c.id == containerID {
|
if c.id == containerID {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
@ -579,38 +579,38 @@ func createSandbox(sandboxConfig SandboxConfig) (*Sandbox, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := newSandbox(sandboxConfig)
|
s, err := newSandbox(sandboxConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch sandbox network to be able to access it from the sandbox structure.
|
// Fetch sandbox network to be able to access it from the sandbox structure.
|
||||||
networkNS, err := p.storage.fetchSandboxNetwork(p.id)
|
networkNS, err := s.storage.fetchSandboxNetwork(s.id)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
p.networkNS = networkNS
|
s.networkNS = networkNS
|
||||||
}
|
}
|
||||||
|
|
||||||
// We first try to fetch the sandbox state from storage.
|
// We first try to fetch the sandbox state from storage.
|
||||||
// If it exists, this means this is a re-creation, i.e.
|
// If it exists, this means this is a re-creation, i.e.
|
||||||
// we don't need to talk to the guest's agent, but only
|
// we don't need to talk to the guest's agent, but only
|
||||||
// want to create the sandbox and its containers in memory.
|
// want to create the sandbox and its containers in memory.
|
||||||
state, err := p.storage.fetchSandboxState(p.id)
|
state, err := s.storage.fetchSandboxState(s.id)
|
||||||
if err == nil && state.State != "" {
|
if err == nil && state.State != "" {
|
||||||
p.state = state
|
s.state = state
|
||||||
return p, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Below code path is called only during create, because of earlier check.
|
// Below code path is called only during create, because of earlier check.
|
||||||
if err := p.agent.createSandbox(p); err != nil {
|
if err := s.agent.createSandbox(s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sandbox state
|
// Set sandbox state
|
||||||
if err := p.setSandboxState(StateReady); err != nil {
|
if err := s.setSandboxState(StateReady); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSandbox(sandboxConfig SandboxConfig) (*Sandbox, error) {
|
func newSandbox(sandboxConfig SandboxConfig) (*Sandbox, error) {
|
||||||
@ -627,7 +627,7 @@ func newSandbox(sandboxConfig SandboxConfig) (*Sandbox, error) {
|
|||||||
|
|
||||||
network := newNetwork(sandboxConfig.NetworkModel)
|
network := newNetwork(sandboxConfig.NetworkModel)
|
||||||
|
|
||||||
p := &Sandbox{
|
s := &Sandbox{
|
||||||
id: sandboxConfig.ID,
|
id: sandboxConfig.ID,
|
||||||
hypervisor: hypervisor,
|
hypervisor: hypervisor,
|
||||||
agent: agent,
|
agent: agent,
|
||||||
@ -642,52 +642,52 @@ func newSandbox(sandboxConfig SandboxConfig) (*Sandbox, error) {
|
|||||||
wg: &sync.WaitGroup{},
|
wg: &sync.WaitGroup{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = globalSandboxList.addSandbox(p); err != nil {
|
if err = globalSandboxList.addSandbox(s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Logger().WithError(err).WithField("sandboxid", p.id).Error("Create new sandbox failed")
|
s.Logger().WithError(err).WithField("sandboxid", s.id).Error("Create new sandbox failed")
|
||||||
globalSandboxList.removeSandbox(p.id)
|
globalSandboxList.removeSandbox(s.id)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err = p.storage.createAllResources(*p); err != nil {
|
if err = s.storage.createAllResources(*s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.storage.deleteSandboxResources(p.id, nil)
|
s.storage.deleteSandboxResources(s.id, nil)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err = p.hypervisor.init(p); err != nil {
|
if err = s.hypervisor.init(s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = p.hypervisor.createSandbox(sandboxConfig); err != nil {
|
if err = s.hypervisor.createSandbox(sandboxConfig); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
agentConfig := newAgentConfig(sandboxConfig)
|
agentConfig := newAgentConfig(sandboxConfig)
|
||||||
if err = p.agent.init(p, agentConfig); err != nil {
|
if err = s.agent.init(s, agentConfig); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// storeSandbox stores a sandbox config.
|
// storeSandbox stores a sandbox config.
|
||||||
func (p *Sandbox) storeSandbox() error {
|
func (s *Sandbox) storeSandbox() error {
|
||||||
err := p.storage.storeSandboxResource(p.id, configFileType, *(p.config))
|
err := s.storage.storeSandboxResource(s.id, configFileType, *(s.config))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, container := range p.containers {
|
for _, container := range s.containers {
|
||||||
err = p.storage.storeContainerResource(p.id, container.id, configFileType, *(container.config))
|
err = s.storage.storeContainerResource(s.id, container.id, configFileType, *(container.config))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -729,8 +729,8 @@ func fetchSandbox(sandboxID string) (sandbox *Sandbox, err error) {
|
|||||||
|
|
||||||
// findContainer returns a container from the containers list held by the
|
// findContainer returns a container from the containers list held by the
|
||||||
// sandbox structure, based on a container ID.
|
// sandbox structure, based on a container ID.
|
||||||
func (p *Sandbox) findContainer(containerID string) (*Container, error) {
|
func (s *Sandbox) findContainer(containerID string) (*Container, error) {
|
||||||
if p == nil {
|
if s == nil {
|
||||||
return nil, errNeedSandbox
|
return nil, errNeedSandbox
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -738,20 +738,20 @@ func (p *Sandbox) findContainer(containerID string) (*Container, error) {
|
|||||||
return nil, errNeedContainerID
|
return nil, errNeedContainerID
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range p.containers {
|
for _, c := range s.containers {
|
||||||
if containerID == c.id {
|
if containerID == c.id {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("Could not find the container %q from the sandbox %q containers list",
|
return nil, fmt.Errorf("Could not find the container %q from the sandbox %q containers list",
|
||||||
containerID, p.id)
|
containerID, s.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// removeContainer removes a container from the containers list held by the
|
// removeContainer removes a container from the containers list held by the
|
||||||
// sandbox structure, based on a container ID.
|
// sandbox structure, based on a container ID.
|
||||||
func (p *Sandbox) removeContainer(containerID string) error {
|
func (s *Sandbox) removeContainer(containerID string) error {
|
||||||
if p == nil {
|
if s == nil {
|
||||||
return errNeedSandbox
|
return errNeedSandbox
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -759,94 +759,94 @@ func (p *Sandbox) removeContainer(containerID string) error {
|
|||||||
return errNeedContainerID
|
return errNeedContainerID
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, c := range p.containers {
|
for idx, c := range s.containers {
|
||||||
if containerID == c.id {
|
if containerID == c.id {
|
||||||
p.containers = append(p.containers[:idx], p.containers[idx+1:]...)
|
s.containers = append(s.containers[:idx], s.containers[idx+1:]...)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("Could not remove the container %q from the sandbox %q containers list",
|
return fmt.Errorf("Could not remove the container %q from the sandbox %q containers list",
|
||||||
containerID, p.id)
|
containerID, s.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete deletes an already created sandbox.
|
// delete deletes an already created sandbox.
|
||||||
// The VM in which the sandbox is running will be shut down.
|
// The VM in which the sandbox is running will be shut down.
|
||||||
func (p *Sandbox) delete() error {
|
func (s *Sandbox) delete() error {
|
||||||
if p.state.State != StateReady &&
|
if s.state.State != StateReady &&
|
||||||
p.state.State != StatePaused &&
|
s.state.State != StatePaused &&
|
||||||
p.state.State != StateStopped {
|
s.state.State != StateStopped {
|
||||||
return fmt.Errorf("Sandbox not ready, paused or stopped, impossible to delete")
|
return fmt.Errorf("Sandbox not ready, paused or stopped, impossible to delete")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range p.containers {
|
for _, c := range s.containers {
|
||||||
if err := c.delete(); err != nil {
|
if err := c.delete(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
globalSandboxList.removeSandbox(p.id)
|
globalSandboxList.removeSandbox(s.id)
|
||||||
|
|
||||||
return p.storage.deleteSandboxResources(p.id, nil)
|
return s.storage.deleteSandboxResources(s.id, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) createNetwork() error {
|
func (s *Sandbox) createNetwork() error {
|
||||||
// Initialize the network.
|
// Initialize the network.
|
||||||
netNsPath, netNsCreated, err := p.network.init(p.config.NetworkConfig)
|
netNsPath, netNsCreated, err := s.network.init(s.config.NetworkConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute prestart hooks inside netns
|
// Execute prestart hooks inside netns
|
||||||
if err := p.network.run(netNsPath, func() error {
|
if err := s.network.run(netNsPath, func() error {
|
||||||
return p.config.Hooks.preStartHooks()
|
return s.config.Hooks.preStartHooks()
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the network
|
// Add the network
|
||||||
networkNS, err := p.network.add(*p, p.config.NetworkConfig, netNsPath, netNsCreated)
|
networkNS, err := s.network.add(*s, s.config.NetworkConfig, netNsPath, netNsCreated)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.networkNS = networkNS
|
s.networkNS = networkNS
|
||||||
|
|
||||||
// Store the network
|
// Store the network
|
||||||
return p.storage.storeSandboxNetwork(p.id, networkNS)
|
return s.storage.storeSandboxNetwork(s.id, networkNS)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) removeNetwork() error {
|
func (s *Sandbox) removeNetwork() error {
|
||||||
if p.networkNS.NetNsCreated {
|
if s.networkNS.NetNsCreated {
|
||||||
return p.network.remove(*p, p.networkNS)
|
return s.network.remove(*s, s.networkNS)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// startVM starts the VM.
|
// startVM starts the VM.
|
||||||
func (p *Sandbox) startVM() error {
|
func (s *Sandbox) startVM() error {
|
||||||
p.Logger().Info("Starting VM")
|
s.Logger().Info("Starting VM")
|
||||||
|
|
||||||
if err := p.network.run(p.networkNS.NetNsPath, func() error {
|
if err := s.network.run(s.networkNS.NetNsPath, func() error {
|
||||||
return p.hypervisor.startSandbox()
|
return s.hypervisor.startSandbox()
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.hypervisor.waitSandbox(vmStartTimeout); err != nil {
|
if err := s.hypervisor.waitSandbox(vmStartTimeout); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Logger().Info("VM started")
|
s.Logger().Info("VM started")
|
||||||
|
|
||||||
// Once startVM is done, we want to guarantee
|
// Once startVM is done, we want to guarantee
|
||||||
// that the sandbox is manageable. For that we need
|
// that the sandbox is manageable. For that we need
|
||||||
// to start the sandbox inside the VM.
|
// to start the sandbox inside the VM.
|
||||||
return p.agent.startSandbox(*p)
|
return s.agent.startSandbox(*s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) addContainer(c *Container) error {
|
func (s *Sandbox) addContainer(c *Container) error {
|
||||||
p.containers = append(p.containers, c)
|
s.containers = append(s.containers, c)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -855,14 +855,14 @@ func (p *Sandbox) addContainer(c *Container) error {
|
|||||||
// adds them to the sandbox. It does not create the containers
|
// adds them to the sandbox. It does not create the containers
|
||||||
// in the guest. This should only be used when fetching a
|
// in the guest. This should only be used when fetching a
|
||||||
// sandbox that already exists.
|
// sandbox that already exists.
|
||||||
func (p *Sandbox) newContainers() error {
|
func (s *Sandbox) newContainers() error {
|
||||||
for _, contConfig := range p.config.Containers {
|
for _, contConfig := range s.config.Containers {
|
||||||
c, err := newContainer(p, contConfig)
|
c, err := newContainer(s, contConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.addContainer(c); err != nil {
|
if err := s.addContainer(c); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -872,14 +872,14 @@ func (p *Sandbox) newContainers() error {
|
|||||||
|
|
||||||
// createContainers registers all containers to the proxy, create the
|
// createContainers registers all containers to the proxy, create the
|
||||||
// containers in the guest and starts one shim per container.
|
// containers in the guest and starts one shim per container.
|
||||||
func (p *Sandbox) createContainers() error {
|
func (s *Sandbox) createContainers() error {
|
||||||
for _, contConfig := range p.config.Containers {
|
for _, contConfig := range s.config.Containers {
|
||||||
newContainer, err := createContainer(p, contConfig)
|
newContainer, err := createContainer(s, contConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.addContainer(newContainer); err != nil {
|
if err := s.addContainer(newContainer); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -889,122 +889,122 @@ func (p *Sandbox) createContainers() error {
|
|||||||
|
|
||||||
// start starts a sandbox. The containers that are making the sandbox
|
// start starts a sandbox. The containers that are making the sandbox
|
||||||
// will be started.
|
// will be started.
|
||||||
func (p *Sandbox) start() error {
|
func (s *Sandbox) start() error {
|
||||||
if err := p.state.validTransition(p.state.State, StateRunning); err != nil {
|
if err := s.state.validTransition(s.state.State, StateRunning); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.setSandboxState(StateRunning); err != nil {
|
if err := s.setSandboxState(StateRunning); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range p.containers {
|
for _, c := range s.containers {
|
||||||
if err := c.start(); err != nil {
|
if err := c.start(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Logger().Info("Sandbox is started")
|
s.Logger().Info("Sandbox is started")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop stops a sandbox. The containers that are making the sandbox
|
// stop stops a sandbox. The containers that are making the sandbox
|
||||||
// will be destroyed.
|
// will be destroyed.
|
||||||
func (p *Sandbox) stop() error {
|
func (s *Sandbox) stop() error {
|
||||||
if err := p.state.validTransition(p.state.State, StateStopped); err != nil {
|
if err := s.state.validTransition(s.state.State, StateStopped); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range p.containers {
|
for _, c := range s.containers {
|
||||||
if err := c.stop(); err != nil {
|
if err := c.stop(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.agent.stopSandbox(*p); err != nil {
|
if err := s.agent.stopSandbox(*s); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Logger().Info("Stopping VM")
|
s.Logger().Info("Stopping VM")
|
||||||
if err := p.hypervisor.stopSandbox(); err != nil {
|
if err := s.hypervisor.stopSandbox(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.setSandboxState(StateStopped)
|
return s.setSandboxState(StateStopped)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) pause() error {
|
func (s *Sandbox) pause() error {
|
||||||
if err := p.hypervisor.pauseSandbox(); err != nil {
|
if err := s.hypervisor.pauseSandbox(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.pauseSetStates()
|
return s.pauseSetStates()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) resume() error {
|
func (s *Sandbox) resume() error {
|
||||||
if err := p.hypervisor.resumeSandbox(); err != nil {
|
if err := s.hypervisor.resumeSandbox(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.resumeSetStates()
|
return s.resumeSetStates()
|
||||||
}
|
}
|
||||||
|
|
||||||
// list lists all sandbox running on the host.
|
// list lists all sandbox running on the host.
|
||||||
func (p *Sandbox) list() ([]Sandbox, error) {
|
func (s *Sandbox) list() ([]Sandbox, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// enter runs an executable within a sandbox.
|
// enter runs an executable within a sandbox.
|
||||||
func (p *Sandbox) enter(args []string) error {
|
func (s *Sandbox) enter(args []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// setSandboxState sets both the in-memory and on-disk state of the
|
// setSandboxState sets both the in-memory and on-disk state of the
|
||||||
// sandbox.
|
// sandbox.
|
||||||
func (p *Sandbox) setSandboxState(state stateString) error {
|
func (s *Sandbox) setSandboxState(state stateString) error {
|
||||||
if state == "" {
|
if state == "" {
|
||||||
return errNeedState
|
return errNeedState
|
||||||
}
|
}
|
||||||
|
|
||||||
// update in-memory state
|
// update in-memory state
|
||||||
p.state.State = state
|
s.state.State = state
|
||||||
|
|
||||||
// update on-disk state
|
// update on-disk state
|
||||||
return p.storage.storeSandboxResource(p.id, stateFileType, p.state)
|
return s.storage.storeSandboxResource(s.id, stateFileType, s.state)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) pauseSetStates() error {
|
func (s *Sandbox) pauseSetStates() error {
|
||||||
// XXX: When a sandbox is paused, all its containers are forcibly
|
// XXX: When a sandbox is paused, all its containers are forcibly
|
||||||
// paused too.
|
// paused too.
|
||||||
if err := p.setContainersState(StatePaused); err != nil {
|
if err := s.setContainersState(StatePaused); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.setSandboxState(StatePaused)
|
return s.setSandboxState(StatePaused)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) resumeSetStates() error {
|
func (s *Sandbox) resumeSetStates() error {
|
||||||
// XXX: Resuming a paused sandbox puts all containers back into the
|
// XXX: Resuming a paused sandbox puts all containers back into the
|
||||||
// running state.
|
// running state.
|
||||||
if err := p.setContainersState(StateRunning); err != nil {
|
if err := s.setContainersState(StateRunning); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.setSandboxState(StateRunning)
|
return s.setSandboxState(StateRunning)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getAndSetSandboxBlockIndex retrieves sandbox block index and increments it for
|
// getAndSetSandboxBlockIndex retrieves sandbox block index and increments it for
|
||||||
// subsequent accesses. This index is used to maintain the index at which a
|
// subsequent accesses. This index is used to maintain the index at which a
|
||||||
// block device is assigned to a container in the sandbox.
|
// block device is assigned to a container in the sandbox.
|
||||||
func (p *Sandbox) getAndSetSandboxBlockIndex() (int, error) {
|
func (s *Sandbox) getAndSetSandboxBlockIndex() (int, error) {
|
||||||
currentIndex := p.state.BlockIndex
|
currentIndex := s.state.BlockIndex
|
||||||
|
|
||||||
// Increment so that container gets incremented block index
|
// Increment so that container gets incremented block index
|
||||||
p.state.BlockIndex++
|
s.state.BlockIndex++
|
||||||
|
|
||||||
// update on-disk state
|
// update on-disk state
|
||||||
err := p.storage.storeSandboxResource(p.id, stateFileType, p.state)
|
err := s.storage.storeSandboxResource(s.id, stateFileType, s.state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return -1, err
|
||||||
}
|
}
|
||||||
@ -1014,11 +1014,11 @@ func (p *Sandbox) getAndSetSandboxBlockIndex() (int, error) {
|
|||||||
|
|
||||||
// decrementSandboxBlockIndex decrements the current sandbox block index.
|
// decrementSandboxBlockIndex decrements the current sandbox block index.
|
||||||
// This is used to recover from failure while adding a block device.
|
// This is used to recover from failure while adding a block device.
|
||||||
func (p *Sandbox) decrementSandboxBlockIndex() error {
|
func (s *Sandbox) decrementSandboxBlockIndex() error {
|
||||||
p.state.BlockIndex--
|
s.state.BlockIndex--
|
||||||
|
|
||||||
// update on-disk state
|
// update on-disk state
|
||||||
err := p.storage.storeSandboxResource(p.id, stateFileType, p.state)
|
err := s.storage.storeSandboxResource(s.id, stateFileType, s.state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -1026,12 +1026,12 @@ func (p *Sandbox) decrementSandboxBlockIndex() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) setContainersState(state stateString) error {
|
func (s *Sandbox) setContainersState(state stateString) error {
|
||||||
if state == "" {
|
if state == "" {
|
||||||
return errNeedState
|
return errNeedState
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range p.containers {
|
for _, c := range s.containers {
|
||||||
if err := c.setContainerState(state); err != nil {
|
if err := c.setContainerState(state); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -1040,12 +1040,12 @@ func (p *Sandbox) setContainersState(state stateString) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) deleteContainerState(containerID string) error {
|
func (s *Sandbox) deleteContainerState(containerID string) error {
|
||||||
if containerID == "" {
|
if containerID == "" {
|
||||||
return errNeedContainerID
|
return errNeedContainerID
|
||||||
}
|
}
|
||||||
|
|
||||||
err := p.storage.deleteContainerResources(p.id, containerID, []sandboxResource{stateFileType})
|
err := s.storage.deleteContainerResources(s.id, containerID, []sandboxResource{stateFileType})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -1053,9 +1053,9 @@ func (p *Sandbox) deleteContainerState(containerID string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Sandbox) deleteContainersState() error {
|
func (s *Sandbox) deleteContainersState() error {
|
||||||
for _, container := range p.config.Containers {
|
for _, container := range s.config.Containers {
|
||||||
err := p.deleteContainerState(container.ID)
|
err := s.deleteContainerState(container.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user