id and name indexes for pods

Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
Antonio Murdaca 2016-09-20 10:27:11 +02:00
parent 361fc8fde7
commit 32029aaba6
No known key found for this signature in database
GPG key ID: B2BEAD150DE936B9
4 changed files with 95 additions and 61 deletions

View file

@ -147,7 +147,7 @@ func (r *Runtime) UpdateStatus(c *Container) error {
defer c.stateLock.Unlock() defer c.stateLock.Unlock()
out, err := exec.Command(r.path, "state", c.name).Output() out, err := exec.Command(r.path, "state", c.name).Output()
if err != nil { if err != nil {
return fmt.Errorf("error getting container state for %s: %s", c.name, err) return fmt.Errorf("error getting container state for %s: %s: %v", c.name, err, out)
} }
stateReader := bytes.NewReader(out) stateReader := bytes.NewReader(out)
if err := json.NewDecoder(stateReader).Decode(&c.state); err != nil { if err := json.NewDecoder(stateReader).Decode(&c.state); err != nil {

View file

@ -1,6 +1,7 @@
package server package server
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -17,10 +18,10 @@ import (
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 // The id of the PodSandbox
podSandboxID := req.GetPodSandboxId() podSandboxID := req.GetPodSandboxId()
if !s.hasSandbox(podSandboxID) { sb := s.getSandbox(podSandboxID)
if sb == nil {
return nil, fmt.Errorf("the pod sandbox (%s) does not exist", podSandboxID) return nil, fmt.Errorf("the pod sandbox (%s) does not exist", podSandboxID)
} }
// The config of the container // The config of the container
containerConfig := req.GetConfig() containerConfig := req.GetConfig()
if containerConfig == nil { if containerConfig == nil {
@ -43,7 +44,7 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq
return nil, err return nil, err
} }
container, err := s.createSandboxContainer(name, podSandboxID, req.GetSandboxConfig(), containerDir, containerConfig) container, err := s.createSandboxContainer(name, sb, req.GetSandboxConfig(), containerDir, containerConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -63,7 +64,10 @@ func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerReq
}, nil }, nil
} }
func (s *Server) createSandboxContainer(name, podSandboxID string, SandboxConfig *pb.PodSandboxConfig, containerDir string, containerConfig *pb.ContainerConfig) (*oci.Container, error) { func (s *Server) createSandboxContainer(name string, sb *sandbox, SandboxConfig *pb.PodSandboxConfig, containerDir string, containerConfig *pb.ContainerConfig) (*oci.Container, error) {
if sb == nil {
return nil, errors.New("createSandboxContainer needs a sandbox")
}
// creates a spec Generator with the default spec. // creates a spec Generator with the default spec.
specgen := generate.New() specgen := generate.New()
@ -129,8 +133,6 @@ func (s *Server) createSandboxContainer(name, podSandboxID string, SandboxConfig
} }
} }
specgen.AddAnnotation("pod_sandbox_id", podSandboxID)
if containerConfig.GetPrivileged() { if containerConfig.GetPrivileged() {
specgen.SetupPrivileged(true) specgen.SetupPrivileged(true)
} }
@ -235,9 +237,8 @@ func (s *Server) createSandboxContainer(name, podSandboxID string, SandboxConfig
} }
} }
} }
// Join the namespace paths for the pod sandbox container. // Join the namespace paths for the pod sandbox container.
podContainerName := podSandboxID + "-infra" podContainerName := sb.name + "-infra"
podInfraContainer := s.state.containers.Get(podContainerName) podInfraContainer := s.state.containers.Get(podContainerName)
podInfraState := s.runtime.ContainerStatus(podInfraContainer) podInfraState := s.runtime.ContainerStatus(podInfraContainer)
@ -274,7 +275,7 @@ func (s *Server) createSandboxContainer(name, podSandboxID string, SandboxConfig
return nil, err return nil, err
} }
container, err := oci.NewContainer(name, containerDir, logPath, labels, podSandboxID, containerConfig.GetTty()) container, err := oci.NewContainer(name, containerDir, logPath, labels, sb.id, containerConfig.GetTty())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,11 +3,11 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/stringid"
"github.com/kubernetes-incubator/ocid/oci" "github.com/kubernetes-incubator/ocid/oci"
"github.com/kubernetes-incubator/ocid/utils" "github.com/kubernetes-incubator/ocid/utils"
pb "github.com/kubernetes/kubernetes/pkg/kubelet/api/v1alpha1/runtime" pb "github.com/kubernetes/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
@ -16,18 +16,13 @@ import (
) )
type sandbox struct { type sandbox struct {
id string
name string name string
logDir string logDir string
labels map[string]string labels map[string]string
containers oci.Store containers oci.Store
} }
type metadata struct {
LogDir string `json:"log_dir"`
ContainerName string `json:"container_name"`
Labels map[string]string `json:"labels"`
}
const ( const (
podInfraRootfs = "/var/lib/ocid/graph/vfs/pause" podInfraRootfs = "/var/lib/ocid/graph/vfs/pause"
) )
@ -44,6 +39,17 @@ func (s *sandbox) removeContainer(c *oci.Container) {
s.containers.Delete(c.Name()) s.containers.Delete(c.Name())
} }
func (s *Server) generatePodIDandName(name string) (string, string, error) {
var (
err error
id = stringid.GenerateNonCryptoID()
)
if name, err = s.reservePodName(id, name); err != nil {
return "", "", err
}
return id, name, err
}
// CreatePodSandbox creates a pod-level sandbox. // CreatePodSandbox creates a pod-level sandbox.
// The definition of PodSandbox is at https://github.com/kubernetes/kubernetes/pull/25899 // The definition of PodSandbox is at https://github.com/kubernetes/kubernetes/pull/25899
func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxRequest) (*pb.CreatePodSandboxResponse, error) { func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxRequest) (*pb.CreatePodSandboxResponse, error) {
@ -53,16 +59,20 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
return nil, fmt.Errorf("PodSandboxConfig.Name should not be empty") return nil, fmt.Errorf("PodSandboxConfig.Name should not be empty")
} }
podSandboxDir := filepath.Join(s.sandboxDir, name) var err error
if _, err := os.Stat(podSandboxDir); err == nil { id, name, err := s.generatePodIDandName(name)
if err != nil {
return nil, err
}
podSandboxDir := filepath.Join(s.sandboxDir, id)
if _, err = os.Stat(podSandboxDir); err == nil {
return nil, fmt.Errorf("pod sandbox (%s) already exists", podSandboxDir) return nil, fmt.Errorf("pod sandbox (%s) already exists", podSandboxDir)
} }
if err := os.MkdirAll(podSandboxDir, 0755); err != nil { if err = os.MkdirAll(podSandboxDir, 0755); err != nil {
return nil, err return nil, err
} }
var err error
defer func() { defer func() {
if err != nil { if err != nil {
if err2 := os.RemoveAll(podSandboxDir); err2 != nil { if err2 := os.RemoveAll(podSandboxDir); err2 != nil {
@ -88,7 +98,7 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
// process req.LogDirectory // process req.LogDirectory
logDir := req.GetConfig().GetLogDirectory() logDir := req.GetConfig().GetLogDirectory()
if logDir == "" { if logDir == "" {
logDir = fmt.Sprintf("/var/log/ocid/pods/%s", name) logDir = fmt.Sprintf("/var/log/ocid/pods/%s", id)
} }
dnsServers := req.GetConfig().GetDnsOptions().GetServers() dnsServers := req.GetConfig().GetDnsOptions().GetServers()
@ -107,7 +117,17 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
g.AddBindMount(resolvPath, "/etc/resolv.conf", "ro") g.AddBindMount(resolvPath, "/etc/resolv.conf", "ro")
labels := req.GetConfig().GetLabels() labels := req.GetConfig().GetLabels()
labelsJSON, err := json.Marshal(labels)
if err != nil {
return nil, err
}
g.AddAnnotation("ocid/labels", string(labelsJSON))
g.AddAnnotation("ocid/log_path", logDir)
g.AddAnnotation("ocid/name", name)
containerName := name + "-infra"
g.AddAnnotation("ocid/container_name", containerName)
s.addSandbox(&sandbox{ s.addSandbox(&sandbox{
id: id,
name: name, name: name,
logDir: logDir, logDir: logDir,
labels: labels, labels: labels,
@ -162,8 +182,7 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
} }
} }
containerName := name + "-infra" container, err := oci.NewContainer(containerName, podSandboxDir, podSandboxDir, labels, id, false)
container, err := oci.NewContainer(containerName, podSandboxDir, podSandboxDir, labels, name, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -182,8 +201,8 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err = s.netPlugin.SetUpPod(netnsPath, podNamespace, name, containerName); err != nil { if err = s.netPlugin.SetUpPod(netnsPath, podNamespace, id, containerName); err != nil {
return nil, fmt.Errorf("failed to create network for container %s in sandbox %s: %v", containerName, name, err) return nil, fmt.Errorf("failed to create network for container %s in sandbox %s: %v", containerName, id, err)
} }
if err = s.runtime.StartContainer(container); err != nil { if err = s.runtime.StartContainer(container); err != nil {
@ -192,24 +211,7 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
s.addContainer(container) s.addContainer(container)
meta := &metadata{ s.podIDIndex.Add(id)
LogDir: logDir,
ContainerName: containerName,
Labels: labels,
}
b, err := json.Marshal(meta)
if err != nil {
return nil, err
}
// TODO: eventually we would track all containers in this pod so on server start
// we can repopulate the structs in memory properly...
// e.g. each container can write itself in podSandboxDir
err = ioutil.WriteFile(filepath.Join(podSandboxDir, "metadata.json"), b, 0644)
if err != nil {
return nil, err
}
if err = s.runtime.UpdateStatus(container); err != nil { if err = s.runtime.UpdateStatus(container); err != nil {
return nil, err return nil, err

View file

@ -9,8 +9,11 @@ import (
"sync" "sync"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/registrar"
"github.com/docker/docker/pkg/truncindex"
"github.com/kubernetes-incubator/ocid/oci" "github.com/kubernetes-incubator/ocid/oci"
"github.com/kubernetes-incubator/ocid/utils" "github.com/kubernetes-incubator/ocid/utils"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/rajatchopra/ocicni" "github.com/rajatchopra/ocicni"
) )
@ -21,30 +24,38 @@ const (
// Server implements the RuntimeService and ImageService // Server implements the RuntimeService and ImageService
type Server struct { type Server struct {
runtime *oci.Runtime runtime *oci.Runtime
sandboxDir string sandboxDir string
stateLock sync.Mutex stateLock sync.Mutex
state *serverState state *serverState
netPlugin ocicni.CNIPlugin netPlugin ocicni.CNIPlugin
podNameIndex *registrar.Registrar
podIDIndex *truncindex.TruncIndex
} }
func (s *Server) loadSandbox(id string) error { func (s *Server) loadSandbox(id string) error {
metaJSON, err := ioutil.ReadFile(filepath.Join(s.sandboxDir, id, "metadata.json")) config, err := ioutil.ReadFile(filepath.Join(s.sandboxDir, id, "config.json"))
if err != nil { if err != nil {
return err return err
} }
var m metadata var m rspec.Spec
if err = json.Unmarshal(metaJSON, &m); err != nil { if err = json.Unmarshal(config, &m); err != nil {
return err return err
} }
labels := make(map[string]string)
if err = json.Unmarshal([]byte(m.Annotations["ocid/labels"]), &labels); err != nil {
return err
}
name := m.Annotations["ocid/name"]
s.addSandbox(&sandbox{ s.addSandbox(&sandbox{
name: id, id: id,
logDir: m.LogDir, name: name,
labels: m.Labels, logDir: m.Annotations["ocid/log_path"],
labels: labels,
containers: oci.NewMemoryStore(), containers: oci.NewMemoryStore(),
}) })
sandboxPath := filepath.Join(s.sandboxDir, id) sandboxPath := filepath.Join(s.sandboxDir, id)
scontainer, err := oci.NewContainer(m.ContainerName, sandboxPath, sandboxPath, m.Labels, id, false) scontainer, err := oci.NewContainer(m.Annotations["ocid/container_name"], sandboxPath, sandboxPath, labels, id, false)
if err != nil { if err != nil {
return err return err
} }
@ -52,6 +63,8 @@ func (s *Server) loadSandbox(id string) error {
if err = s.runtime.UpdateStatus(scontainer); err != nil { if err = s.runtime.UpdateStatus(scontainer); err != nil {
logrus.Warnf("error updating status for container %s: %v", scontainer, err) logrus.Warnf("error updating status for container %s: %v", scontainer, err)
} }
s.podIDIndex.Add(id)
s.reservePodName(id, name)
return nil return nil
} }
@ -68,6 +81,21 @@ func (s *Server) restore() error {
return nil return nil
} }
func (s *Server) reservePodName(id, name string) (string, error) {
if err := s.podNameIndex.Reserve(name, id); err != nil {
if err == registrar.ErrNameReserved {
id, err := s.podNameIndex.Get(name)
if err != nil {
logrus.Warnf("name %s already reserved for %s", name, id)
return "", err
}
return "", fmt.Errorf("conflict, name %s already reserver", name)
}
return "", fmt.Errorf("error reserving name %s", name)
}
return name, nil
}
// New creates a new Server with options provided // New creates a new Server with options provided
func New(runtimePath, sandboxDir, containerDir string) (*Server, error) { func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
// TODO: This will go away later when we have wrapper process or systemd acting as // TODO: This will go away later when we have wrapper process or systemd acting as
@ -105,6 +133,8 @@ func New(runtimePath, sandboxDir, containerDir string) (*Server, error) {
containers: containers, containers: containers,
}, },
} }
s.podIDIndex = truncindex.NewTruncIndex([]string{})
s.podNameIndex = registrar.NewRegistrar()
if err := s.restore(); err != nil { if err := s.restore(); err != nil {
logrus.Warnf("couldn't restore: %v", err) logrus.Warnf("couldn't restore: %v", err)
} }
@ -120,20 +150,20 @@ type serverState struct {
func (s *Server) addSandbox(sb *sandbox) { func (s *Server) addSandbox(sb *sandbox) {
s.stateLock.Lock() s.stateLock.Lock()
s.state.sandboxes[sb.name] = sb s.state.sandboxes[sb.id] = sb
s.stateLock.Unlock() s.stateLock.Unlock()
} }
func (s *Server) getSandbox(name string) *sandbox { func (s *Server) getSandbox(id string) *sandbox {
s.stateLock.Lock() s.stateLock.Lock()
sb := s.state.sandboxes[name] sb := s.state.sandboxes[id]
s.stateLock.Unlock() s.stateLock.Unlock()
return sb return sb
} }
func (s *Server) hasSandbox(name string) bool { func (s *Server) hasSandbox(id string) bool {
s.stateLock.Lock() s.stateLock.Lock()
_, ok := s.state.sandboxes[name] _, ok := s.state.sandboxes[id]
s.stateLock.Unlock() s.stateLock.Unlock()
return ok return ok
} }
@ -141,6 +171,7 @@ func (s *Server) hasSandbox(name string) bool {
func (s *Server) addContainer(c *oci.Container) { func (s *Server) addContainer(c *oci.Container) {
s.stateLock.Lock() s.stateLock.Lock()
sandbox := s.state.sandboxes[c.Sandbox()] sandbox := s.state.sandboxes[c.Sandbox()]
// TODO(runcom): handle !ok above!!! otherwise it panics!
sandbox.addContainer(c) sandbox.addContainer(c)
s.state.containers.Add(c.Name(), c) s.state.containers.Add(c.Name(), c)
s.stateLock.Unlock() s.stateLock.Unlock()