2016-09-19 07:21:14 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-12-07 16:38:36 +00:00
|
|
|
"errors"
|
2016-09-19 07:21:14 +00:00
|
|
|
"fmt"
|
|
|
|
|
2016-09-20 08:27:11 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-09-26 23:55:12 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2016-10-17 18:28:08 +00:00
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
2016-09-20 14:15:22 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2016-09-19 07:21:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type sandbox struct {
|
2016-10-24 12:01:01 +00:00
|
|
|
id string
|
|
|
|
name string
|
|
|
|
logDir string
|
|
|
|
labels fields.Set
|
|
|
|
annotations map[string]string
|
|
|
|
infraContainer *oci.Container
|
|
|
|
containers oci.Store
|
|
|
|
processLabel string
|
|
|
|
mountLabel string
|
|
|
|
metadata *pb.PodSandboxMetadata
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 22:29:41 +00:00
|
|
|
const (
|
2016-09-22 23:02:46 +00:00
|
|
|
podDefaultNamespace = "default"
|
2016-09-20 22:29:41 +00:00
|
|
|
)
|
|
|
|
|
2016-12-07 16:38:36 +00:00
|
|
|
var (
|
|
|
|
errSandboxIDEmpty = errors.New("PodSandboxId should not be empty")
|
|
|
|
)
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
func (s *sandbox) addContainer(c *oci.Container) {
|
|
|
|
s.containers.Add(c.Name(), c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) getContainer(name string) *oci.Container {
|
|
|
|
return s.containers.Get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) removeContainer(c *oci.Container) {
|
|
|
|
s.containers.Delete(c.Name())
|
|
|
|
}
|
|
|
|
|
2016-09-30 22:23:38 +00:00
|
|
|
func (s *Server) generatePodIDandName(name string, namespace string, attempt uint32) (string, string, error) {
|
2016-09-20 08:27:11 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
id = stringid.GenerateNonCryptoID()
|
|
|
|
)
|
2016-09-22 23:02:46 +00:00
|
|
|
if namespace == "" {
|
|
|
|
namespace = podDefaultNamespace
|
|
|
|
}
|
2016-09-30 22:23:38 +00:00
|
|
|
|
|
|
|
if name, err = s.reservePodName(id, fmt.Sprintf("%s-%s-%v", namespace, name, attempt)); err != nil {
|
2016-09-20 08:27:11 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return id, name, err
|
|
|
|
}
|
|
|
|
|
2016-10-06 22:32:01 +00:00
|
|
|
type podSandboxRequest interface {
|
|
|
|
GetPodSandboxId() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getPodSandboxFromRequest(req podSandboxRequest) (*sandbox, error) {
|
|
|
|
sbID := req.GetPodSandboxId()
|
|
|
|
if sbID == "" {
|
2016-12-07 16:38:36 +00:00
|
|
|
return nil, errSandboxIDEmpty
|
2016-10-06 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return sb, nil
|
|
|
|
}
|