Add initial container implementation.
Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
@@ -17,14 +17,84 @@ limitations under the License.
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
||||
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/metadata"
|
||||
)
|
||||
|
||||
// CreateContainer creates a new container in the given PodSandbox.
|
||||
func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.CreateContainerRequest) (*runtime.CreateContainerResponse, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
func (c *criContainerdService) CreateContainer(ctx context.Context, r *runtime.CreateContainerRequest) (retRes *runtime.CreateContainerResponse, retErr error) {
|
||||
glog.V(2).Infof("CreateContainer within sandbox %q with container config %+v and sandbox config %+v",
|
||||
r.GetPodSandboxId(), r.GetConfig(), r.GetSandboxConfig())
|
||||
defer func() {
|
||||
if retErr == nil {
|
||||
glog.V(2).Infof("CreateContainer returns container id %q", retRes.GetContainerId())
|
||||
}
|
||||
}()
|
||||
|
||||
config := r.GetConfig()
|
||||
sandboxConfig := r.GetSandboxConfig()
|
||||
sandbox, err := c.getSandbox(r.GetPodSandboxId())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find sandbox id %q: %v", r.GetPodSandboxId(), err)
|
||||
}
|
||||
|
||||
// Generate unique id and name for the container and reserve the name.
|
||||
// Reserve the container name to avoid concurrent `CreateContainer` request creating
|
||||
// the same container.
|
||||
id := generateID()
|
||||
name := makeContainerName(config.GetMetadata(), sandboxConfig.GetMetadata())
|
||||
if err := c.containerNameIndex.Reserve(name, id); err != nil {
|
||||
return nil, fmt.Errorf("failed to reserve container name %q: %v", name, err)
|
||||
}
|
||||
defer func() {
|
||||
// Release the name if the function returns with an error.
|
||||
if retErr != nil {
|
||||
c.containerNameIndex.ReleaseByName(name)
|
||||
}
|
||||
}()
|
||||
|
||||
// Create initial container metadata.
|
||||
meta := metadata.ContainerMetadata{
|
||||
ID: id,
|
||||
Name: name,
|
||||
SandboxID: sandbox.ID,
|
||||
Config: config,
|
||||
}
|
||||
|
||||
// TODO(random-liu): [P0] Prepare container rootfs.
|
||||
|
||||
// TODO(random-liu): [P0] Set ImageRef in ContainerMetadata with image id.
|
||||
|
||||
// Create container root directory.
|
||||
containerRootDir := getContainerRootDir(c.rootDir, id)
|
||||
if err := c.os.MkdirAll(containerRootDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create container root directory %q: %v",
|
||||
containerRootDir, err)
|
||||
}
|
||||
defer func() {
|
||||
if retErr != nil {
|
||||
// Cleanup the container root directory.
|
||||
if err := c.os.RemoveAll(containerRootDir); err != nil {
|
||||
glog.Errorf("Failed to remove container root directory %q: %v",
|
||||
containerRootDir, err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Update container CreatedAt.
|
||||
meta.CreatedAt = time.Now().UnixNano()
|
||||
// Add container into container store.
|
||||
if err := c.containerStore.Create(meta); err != nil {
|
||||
return nil, fmt.Errorf("failed to add container metadata %+v into store: %v",
|
||||
meta, err)
|
||||
}
|
||||
|
||||
return &runtime.CreateContainerResponse{ContainerId: id}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user