diff --git a/hypervisor/constants.go b/hypervisor/constants.go index f9dee7b8..6695109c 100644 --- a/hypervisor/constants.go +++ b/hypervisor/constants.go @@ -45,7 +45,6 @@ const ( COMMAND_STOP_POD COMMAND_SHUTDOWN COMMAND_RELEASE - COMMAND_NEWCONTAINER COMMAND_ONLINECPUMEM COMMAND_ATTACH COMMAND_DETACH @@ -116,8 +115,6 @@ func EventString(ev int) string { return "COMMAND_SHUTDOWN" case COMMAND_RELEASE: return "COMMAND_RELEASE" - case COMMAND_NEWCONTAINER: - return "COMMAND_NEWCONTAINER" case COMMAND_ATTACH: return "COMMAND_ATTACH" case COMMAND_DETACH: diff --git a/hypervisor/events.go b/hypervisor/events.go index a227ccc4..e7f22137 100644 --- a/hypervisor/events.go +++ b/hypervisor/events.go @@ -49,11 +49,6 @@ type RunPodCommand struct { type ReplacePodCommand RunPodCommand -type NewContainerCommand struct { - container *pod.UserContainer - info *ContainerInfo -} - type OnlineCpuMemCommand struct{} type ShutdownCommand struct { @@ -211,7 +206,6 @@ func (qe *NetDevRemovedEvent) Event() int { return EVENT_INTERFACE_EJECTED } func (qe *RunPodCommand) Event() int { return COMMAND_RUN_POD } func (qe *GetPodStatsCommand) Event() int { return COMMAND_GET_POD_STATS } func (qe *ReplacePodCommand) Event() int { return COMMAND_REPLACE_POD } -func (qe *NewContainerCommand) Event() int { return COMMAND_NEWCONTAINER } func (qe *OnlineCpuMemCommand) Event() int { return COMMAND_ONLINECPUMEM } func (qe *AttachCommand) Event() int { return COMMAND_ATTACH } func (qe *WindowSizeCommand) Event() int { return COMMAND_WINDOWSIZE } diff --git a/hypervisor/vm.go b/hypervisor/vm.go index 93382acd..59d0b1f0 100644 --- a/hypervisor/vm.go +++ b/hypervisor/vm.go @@ -567,12 +567,22 @@ func (vm *Vm) AddProcess(container, execId string, terminal bool, args []string, } func (vm *Vm) NewContainer(c *pod.UserContainer, info *ContainerInfo) error { - newContainerCommand := &NewContainerCommand{ - container: c, - info: info, + err := vm.GenericOperation("NewContainer", func(ctx *VmContext, result chan<- error) { + ctx.newContainer(c, info, result) + }, StateInit, StateRunning) + + if err != nil { + return fmt.Errorf("Create new container failed: %v", err) } - vm.Hub <- newContainerCommand + vm.GenericOperation("StartNewContainerStdin", func(ctx *VmContext, result chan<- error) { + // start stdin. TODO: find the correct idx if parallel multi INIT_NEWCONTAINER + idx := len(ctx.vmSpec.Containers) - 1 + c := ctx.vmSpec.Containers[idx] + ctx.ptys.startStdin(c.Process.Stdio, c.Process.Terminal) + result <- nil + }, StateInit, StateRunning) + return nil } diff --git a/hypervisor/vm_states.go b/hypervisor/vm_states.go index 030529c9..8cd983ff 100644 --- a/hypervisor/vm_states.go +++ b/hypervisor/vm_states.go @@ -85,24 +85,24 @@ func (ctx *VmContext) prepareDevice(cmd *RunPodCommand) bool { return true } -func (ctx *VmContext) prepareContainer(cmd *NewContainerCommand) *hyperstartapi.Container { +func (ctx *VmContext) prepareContainer(spec *pod.UserContainer, info *ContainerInfo) *hyperstartapi.Container { ctx.lock.Lock() idx := len(ctx.vmSpec.Containers) vmContainer := &hyperstartapi.Container{} - ctx.initContainerInfo(idx, vmContainer, cmd.container) - ctx.setContainerInfo(idx, vmContainer, cmd.info) + ctx.initContainerInfo(idx, vmContainer, spec) + ctx.setContainerInfo(idx, vmContainer, info) - vmContainer.Sysctl = cmd.container.Sysctl + vmContainer.Sysctl = spec.Sysctl vmContainer.Process.Stdio = ctx.ptys.attachId ctx.ptys.attachId++ - if !cmd.container.Tty { + if !spec.Tty { vmContainer.Process.Stderr = ctx.ptys.attachId ctx.ptys.attachId++ } - ctx.userSpec.Containers = append(ctx.userSpec.Containers, *cmd.container) + ctx.userSpec.Containers = append(ctx.userSpec.Containers, *spec) ctx.vmSpec.Containers = append(ctx.vmSpec.Containers, *vmContainer) ctx.lock.Unlock() @@ -122,12 +122,13 @@ func (ctx *VmContext) prepareContainer(cmd *NewContainerCommand) *hyperstartapi. return vmContainer } -func (ctx *VmContext) newContainer(cmd *NewContainerCommand) { - c := ctx.prepareContainer(cmd) +func (ctx *VmContext) newContainer(spec *pod.UserContainer, info *ContainerInfo, result chan<- error) { + c := ctx.prepareContainer(spec, info) glog.Infof("start sending INIT_NEWCONTAINER") ctx.vm <- &hyperstartCmd{ Code: hyperstartapi.INIT_NEWCONTAINER, Message: c, + result: result, } glog.Infof("sent INIT_NEWCONTAINER") } @@ -428,8 +429,6 @@ func stateInit(ctx *VmContext, ev VmEvent) { ctx.shutdownVM(false, "") ctx.Become(stateDestroying, StateDestroying) ctx.reportVmShutdown() - case COMMAND_NEWCONTAINER: - ctx.newContainer(ev.(*NewContainerCommand)) case COMMAND_ONLINECPUMEM: ctx.onlineCpuMem(ev.(*OnlineCpuMemCommand)) case COMMAND_WINDOWSIZE: @@ -441,17 +440,6 @@ func stateInit(ctx *VmContext, ev VmEvent) { ctx.setTimeout(60) ctx.Become(stateStarting, StateStarting) } - case COMMAND_ACK: - ack := ev.(*CommandAck) - glog.V(1).Infof("[init] got init ack to %d", ack.reply) - if ack.reply.Code == hyperstartapi.INIT_NEWCONTAINER { - glog.Infof("Get ack for new container") - - // start stdin. TODO: find the correct idx if parallel multi INIT_NEWCONTAINER - idx := len(ctx.vmSpec.Containers) - 1 - c := ctx.vmSpec.Containers[idx] - ctx.ptys.startStdin(c.Process.Stdio, c.Process.Terminal) - } default: unexpectedEventHandler(ctx, ev, "pod initiating") } @@ -547,8 +535,6 @@ func stateRunning(ctx *VmContext, ev VmEvent) { glog.Info("pod is running, got release command, let VM fly") ctx.Become(nil, StateNone) ctx.reportSuccess("", nil) - case COMMAND_NEWCONTAINER: - ctx.newContainer(ev.(*NewContainerCommand)) case COMMAND_WINDOWSIZE: cmd := ev.(*WindowSizeCommand) ctx.setWindowSize(cmd.ContainerId, cmd.ExecId, cmd.Size) @@ -556,17 +542,6 @@ func stateRunning(ctx *VmContext, ev VmEvent) { result := ev.(*PodFinished) ctx.reportPodFinished(result) ctx.exitVM(false, "", true, false) - case COMMAND_ACK: - ack := ev.(*CommandAck) - glog.V(1).Infof("[running] got init ack to %d", ack.reply) - - if ack.reply.Code == hyperstartapi.INIT_NEWCONTAINER { - glog.Infof("Get ack for new container") - // start stdin. TODO: find the correct idx if parallel multi INIT_NEWCONTAINER - idx := len(ctx.vmSpec.Containers) - 1 - c := ctx.vmSpec.Containers[idx] - ctx.ptys.startStdin(c.Process.Stdio, c.Process.Terminal) - } case COMMAND_GET_POD_STATS: ctx.reportPodStats(ev) case EVENT_INTERFACE_EJECTED: