commit
df2eebbbcd
2 changed files with 55 additions and 28 deletions
|
@ -25,12 +25,21 @@ const (
|
||||||
|
|
||||||
// CreateContainer creates a new container in specified PodSandbox
|
// CreateContainer creates a new container in specified PodSandbox
|
||||||
func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (*pb.CreateContainerResponse, error) {
|
func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (*pb.CreateContainerResponse, error) {
|
||||||
// The id of the PodSandbox
|
sbID := req.GetPodSandboxId()
|
||||||
podSandboxID := req.GetPodSandboxId()
|
if sbID == "" {
|
||||||
sb := s.getSandbox(podSandboxID)
|
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
||||||
if sb == nil {
|
|
||||||
return nil, fmt.Errorf("the pod sandbox (%s) does not exist", podSandboxID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sandboxID, err := s.podIDIndex.Get(sbID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("PodSandbox with ID starting with %s not found: %v", sbID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sb := s.getSandbox(sandboxID)
|
||||||
|
if sb == nil {
|
||||||
|
return nil, fmt.Errorf("specified sandbox not found: %s", sandboxID)
|
||||||
|
}
|
||||||
|
|
||||||
// The config of the container
|
// The config of the container
|
||||||
containerConfig := req.GetConfig()
|
containerConfig := req.GetConfig()
|
||||||
if containerConfig == nil {
|
if containerConfig == nil {
|
||||||
|
@ -45,11 +54,11 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq
|
||||||
// containerDir is the dir for the container bundle.
|
// containerDir is the dir for the container bundle.
|
||||||
containerDir := filepath.Join(s.runtime.ContainerDir(), name)
|
containerDir := filepath.Join(s.runtime.ContainerDir(), name)
|
||||||
|
|
||||||
if _, err := os.Stat(containerDir); err == nil {
|
if _, err = os.Stat(containerDir); err == nil {
|
||||||
return nil, fmt.Errorf("container (%s) already exists", containerDir)
|
return nil, fmt.Errorf("container (%s) already exists", containerDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(containerDir, 0755); err != nil {
|
if err = os.MkdirAll(containerDir, 0755); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -234,13 +234,19 @@ func (s *Server) RunPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
|
||||||
// StopPodSandbox stops the sandbox. If there are any running containers in the
|
// StopPodSandbox stops the sandbox. If there are any running containers in the
|
||||||
// sandbox, they should be force terminated.
|
// sandbox, they should be force terminated.
|
||||||
func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxRequest) (*pb.StopPodSandboxResponse, error) {
|
func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxRequest) (*pb.StopPodSandboxResponse, error) {
|
||||||
sbID := req.PodSandboxId
|
sbID := req.GetPodSandboxId()
|
||||||
if *sbID == "" {
|
if sbID == "" {
|
||||||
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
||||||
}
|
}
|
||||||
sb := s.getSandbox(*sbID)
|
|
||||||
|
sandboxID, err := s.podIDIndex.Get(sbID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("PodSandbox with ID starting with %s not found: %v", sbID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sb := s.getSandbox(sandboxID)
|
||||||
if sb == nil {
|
if sb == nil {
|
||||||
return nil, fmt.Errorf("specified sandbox not found: %s", *sbID)
|
return nil, fmt.Errorf("specified sandbox not found: %s", sandboxID)
|
||||||
}
|
}
|
||||||
|
|
||||||
podInfraContainer := sb.name + "-infra"
|
podInfraContainer := sb.name + "-infra"
|
||||||
|
@ -251,14 +257,14 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := s.netPlugin.TearDownPod(netnsPath, podNamespace, *sbID, podInfraContainer); err != nil {
|
if err := s.netPlugin.TearDownPod(netnsPath, podNamespace, sandboxID, podInfraContainer); err != nil {
|
||||||
return nil, fmt.Errorf("failed to destroy network for container %s in sandbox %s: %v", c.Name(), *sbID, err)
|
return nil, fmt.Errorf("failed to destroy network for container %s in sandbox %s: %v", c.Name(), sandboxID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cStatus := s.runtime.ContainerStatus(c)
|
cStatus := s.runtime.ContainerStatus(c)
|
||||||
if cStatus.Status != "stopped" {
|
if cStatus.Status != "stopped" {
|
||||||
if err := s.runtime.StopContainer(c); err != nil {
|
if err := s.runtime.StopContainer(c); err != nil {
|
||||||
return nil, fmt.Errorf("failed to stop container %s in sandbox %s: %v", c.Name(), *sbID, err)
|
return nil, fmt.Errorf("failed to stop container %s in sandbox %s: %v", c.Name(), sandboxID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -269,13 +275,19 @@ func (s *Server) StopPodSandbox(ctx context.Context, req *pb.StopPodSandboxReque
|
||||||
// RemovePodSandbox deletes the sandbox. If there are any running containers in the
|
// RemovePodSandbox deletes the sandbox. If there are any running containers in the
|
||||||
// sandbox, they should be force deleted.
|
// sandbox, they should be force deleted.
|
||||||
func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxRequest) (*pb.RemovePodSandboxResponse, error) {
|
func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxRequest) (*pb.RemovePodSandboxResponse, error) {
|
||||||
sbID := req.PodSandboxId
|
sbID := req.GetPodSandboxId()
|
||||||
if *sbID == "" {
|
if sbID == "" {
|
||||||
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
||||||
}
|
}
|
||||||
sb := s.getSandbox(*sbID)
|
|
||||||
|
sandboxID, err := s.podIDIndex.Get(sbID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("PodSandbox with ID starting with %s not found: %v", sbID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sb := s.getSandbox(sandboxID)
|
||||||
if sb == nil {
|
if sb == nil {
|
||||||
return nil, fmt.Errorf("specified sandbox not found: %s", *sbID)
|
return nil, fmt.Errorf("specified sandbox not found: %s", sandboxID)
|
||||||
}
|
}
|
||||||
|
|
||||||
podInfraContainer := sb.name + "-infra"
|
podInfraContainer := sb.name + "-infra"
|
||||||
|
@ -283,7 +295,7 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
|
||||||
// Delete all the containers in the sandbox
|
// Delete all the containers in the sandbox
|
||||||
for _, c := range sb.containers.List() {
|
for _, c := range sb.containers.List() {
|
||||||
if err := s.runtime.DeleteContainer(c); err != nil {
|
if err := s.runtime.DeleteContainer(c); err != nil {
|
||||||
return nil, fmt.Errorf("failed to delete container %s in sandbox %s: %v", c.Name(), *sbID, err)
|
return nil, fmt.Errorf("failed to delete container %s in sandbox %s: %v", c.Name(), sandboxID, err)
|
||||||
}
|
}
|
||||||
if podInfraContainer == c.Name() {
|
if podInfraContainer == c.Name() {
|
||||||
continue
|
continue
|
||||||
|
@ -295,9 +307,9 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove the files related to the sandbox
|
// Remove the files related to the sandbox
|
||||||
podSandboxDir := filepath.Join(s.sandboxDir, *sbID)
|
podSandboxDir := filepath.Join(s.sandboxDir, sandboxID)
|
||||||
if err := os.RemoveAll(podSandboxDir); err != nil {
|
if err := os.RemoveAll(podSandboxDir); err != nil {
|
||||||
return nil, fmt.Errorf("failed to remove sandbox %s directory: %v", *sbID, err)
|
return nil, fmt.Errorf("failed to remove sandbox %s directory: %v", sandboxID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &pb.RemovePodSandboxResponse{}, nil
|
return &pb.RemovePodSandboxResponse{}, nil
|
||||||
|
@ -305,18 +317,24 @@ func (s *Server) RemovePodSandbox(ctx context.Context, req *pb.RemovePodSandboxR
|
||||||
|
|
||||||
// PodSandboxStatus returns the Status of the PodSandbox.
|
// PodSandboxStatus returns the Status of the PodSandbox.
|
||||||
func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusRequest) (*pb.PodSandboxStatusResponse, error) {
|
func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusRequest) (*pb.PodSandboxStatusResponse, error) {
|
||||||
sbID := req.PodSandboxId
|
sbID := req.GetPodSandboxId()
|
||||||
if *sbID == "" {
|
if sbID == "" {
|
||||||
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
return nil, fmt.Errorf("PodSandboxId should not be empty")
|
||||||
}
|
}
|
||||||
sb := s.getSandbox(*sbID)
|
|
||||||
|
sandboxID, err := s.podIDIndex.Get(sbID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("PodSandbox with ID starting with %s not found: %v", sbID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sb := s.getSandbox(sandboxID)
|
||||||
if sb == nil {
|
if sb == nil {
|
||||||
return nil, fmt.Errorf("specified sandbox not found: %s", *sbID)
|
return nil, fmt.Errorf("specified sandbox not found: %s", sandboxID)
|
||||||
}
|
}
|
||||||
|
|
||||||
podInfraContainerName := sb.name + "-infra"
|
podInfraContainerName := sb.name + "-infra"
|
||||||
podInfraContainer := sb.getContainer(podInfraContainerName)
|
podInfraContainer := sb.getContainer(podInfraContainerName)
|
||||||
if err := s.runtime.UpdateStatus(podInfraContainer); err != nil {
|
if err = s.runtime.UpdateStatus(podInfraContainer); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,7 +346,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
podNamespace := ""
|
podNamespace := ""
|
||||||
ip, err := s.netPlugin.GetContainerNetworkStatus(netNsPath, podNamespace, *sbID, podInfraContainerName)
|
ip, err := s.netPlugin.GetContainerNetworkStatus(netNsPath, podNamespace, sandboxID, podInfraContainerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// ignore the error on network status
|
// ignore the error on network status
|
||||||
ip = ""
|
ip = ""
|
||||||
|
@ -341,7 +359,7 @@ func (s *Server) PodSandboxStatus(ctx context.Context, req *pb.PodSandboxStatusR
|
||||||
|
|
||||||
return &pb.PodSandboxStatusResponse{
|
return &pb.PodSandboxStatusResponse{
|
||||||
Status: &pb.PodSandboxStatus{
|
Status: &pb.PodSandboxStatus{
|
||||||
Id: sbID,
|
Id: &sbID,
|
||||||
CreatedAt: int64Ptr(created),
|
CreatedAt: int64Ptr(created),
|
||||||
Linux: &pb.LinuxPodSandboxStatus{
|
Linux: &pb.LinuxPodSandboxStatus{
|
||||||
Namespaces: &pb.Namespace{
|
Namespaces: &pb.Namespace{
|
||||||
|
|
Loading…
Reference in a new issue