Merge pull request #1452 from lifupan/main

shimv2: return the hypervisor's pid as the container pid
This commit is contained in:
Bin Liu
2021-03-01 15:48:01 +08:00
committed by GitHub
4 changed files with 34 additions and 14 deletions

View File

@@ -101,6 +101,12 @@ func create(ctx context.Context, s *service, r *taskAPI.CreateTaskRequest) (*con
return nil, err
}
s.sandbox = sandbox
pid, err := s.sandbox.GetHypervisorPid()
if err != nil {
return nil, err
}
s.hpid = uint32(pid)
go s.startManagementServer(ctx, ociSpec)
case vc.PodContainer:

View File

@@ -113,9 +113,12 @@ type service struct {
mu sync.Mutex
eventSendMu sync.Mutex
// pid Since this shimv2 cannot get the container processes pid from VM,
// thus for the returned values needed pid, just return this shim's
// hypervisor pid, Since this shimv2 cannot get the container processes pid from VM,
// thus for the returned values needed pid, just return the hypervisor's
// pid directly.
hpid uint32
// shim's pid
pid uint32
ctx context.Context
@@ -394,11 +397,11 @@ func (s *service) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (_ *
Terminal: r.Terminal,
},
Checkpoint: r.Checkpoint,
Pid: s.pid,
Pid: s.hpid,
})
return &taskAPI.CreateTaskResponse{
Pid: s.pid,
Pid: s.hpid,
}, nil
}
}
@@ -434,7 +437,7 @@ func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (_ *taskAP
}
s.send(&eventstypes.TaskStart{
ContainerID: c.id,
Pid: s.pid,
Pid: s.hpid,
})
} else {
//start an exec
@@ -445,12 +448,12 @@ func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (_ *taskAP
s.send(&eventstypes.TaskExecStarted{
ContainerID: c.id,
ExecID: r.ExecID,
Pid: s.pid,
Pid: s.hpid,
})
}
return &taskAPI.StartResponse{
Pid: s.pid,
Pid: s.hpid,
}, nil
}
@@ -480,7 +483,7 @@ func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (_ *task
s.send(&eventstypes.TaskDelete{
ContainerID: c.id,
Pid: s.pid,
Pid: s.hpid,
ExitStatus: c.exit,
ExitedAt: c.exitTime,
})
@@ -488,7 +491,7 @@ func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (_ *task
return &taskAPI.DeleteResponse{
ExitStatus: c.exit,
ExitedAt: c.exitTime,
Pid: s.pid,
Pid: s.hpid,
}, nil
}
//deal with the exec case
@@ -502,7 +505,7 @@ func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (_ *task
return &taskAPI.DeleteResponse{
ExitStatus: uint32(execs.exitCode),
ExitedAt: execs.exitTime,
Pid: s.pid,
Pid: s.hpid,
}, nil
}
@@ -606,7 +609,7 @@ func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (_ *taskAP
return &taskAPI.StateResponse{
ID: c.id,
Bundle: c.bundle,
Pid: s.pid,
Pid: s.hpid,
Status: c.status,
Stdin: c.stdin,
Stdout: c.stdout,
@@ -625,7 +628,7 @@ func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (_ *taskAP
return &taskAPI.StateResponse{
ID: execs.id,
Bundle: c.bundle,
Pid: s.pid,
Pid: s.hpid,
Status: execs.status,
Stdin: execs.tty.stdin,
Stdout: execs.tty.stdout,
@@ -786,7 +789,7 @@ func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (_ *taskAPI.
}()
pInfo := task.ProcessInfo{
Pid: s.pid,
Pid: s.hpid,
}
processes = append(processes, &pInfo)
@@ -867,7 +870,7 @@ func (s *service) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (_ *ta
return &taskAPI.ConnectResponse{
ShimPid: s.pid,
//Since kata cannot get the container's pid in VM, thus only return the shim's pid.
TaskPid: s.pid,
TaskPid: s.hpid,
}, nil
}

View File

@@ -72,6 +72,7 @@ type VCSandbox interface {
ListRoutes() ([]*pbTypes.Route, error)
GetOOMEvent() (string, error)
GetHypervisorPid() (int, error)
UpdateRuntimeMetrics() error
GetAgentMetrics() (string, error)

View File

@@ -248,6 +248,16 @@ func (s *Sandbox) GetNetNs() string {
return s.networkNS.NetNsPath
}
// GetHypervisorPid returns the hypervisor's pid.
func (s *Sandbox) GetHypervisorPid() (int, error) {
pids := s.hypervisor.getPids()
if len(pids) == 0 || pids[0] == 0 {
return -1, fmt.Errorf("Invalid hypervisor PID: %+v", pids)
}
return pids[0], nil
}
// GetAllContainers returns all containers.
func (s *Sandbox) GetAllContainers() []VCContainer {
ifa := make([]VCContainer, len(s.containers))