Create OCI container.

This commit is contained in:
Shishir Mahajan 2020-05-11 16:07:51 -07:00
parent d0ef91a0d9
commit 175e692254
No known key found for this signature in database
GPG Key ID: D41782E7688DEC4A
2 changed files with 21 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package containerd
import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/oci"
)
func (d *Driver) isContainerdRunning() (bool, error) {
@ -12,6 +13,15 @@ func (d *Driver) getContainerdVersion() (containerd.Version, error) {
return d.client.Version(d.ctxContainerd)
}
func (d *Driver) pullOCIImage(imageName string) (containerd.Image, error) {
func (d *Driver) pullImage(imageName string) (containerd.Image, error) {
return d.client.Pull(d.ctxContainerd, imageName, containerd.WithPullUnpack)
}
func (d *Driver) createContainer(image containerd.Image, containerName, containerSnapshotName string) (containerd.Container, error) {
return d.client.NewContainer(
d.ctxContainerd,
containerName,
containerd.WithNewSnapshot(containerSnapshotName, image),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)
}

View File

@ -284,13 +284,21 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
// https://github.com/moby/moby/blob/master/pkg/namesgenerator/names-generator.go
containerName := namesgenerator.GetRandomName(1)
image, err := d.pullOCIImage(driverConfig.Image)
image, err := d.pullImage(driverConfig.Image)
if err != nil {
return nil, nil, fmt.Errorf("Error in pulling OCI image: %v", err)
return nil, nil, fmt.Errorf("Error in pulling image: %v", err)
}
d.logger.Info("Successfully pulled %s image\n", image.Name())
containerSnapshotName := fmt.Sprintf("%s-snapshot", containerName)
container, err := d.createContainer(image, containerName, containerSnapshotName)
if err != nil {
return nil, nil, fmt.Errorf("Error in creating container: %v", err)
}
d.logger.Info("Successfully created container with name: %s, ID: %s and snapshot with ID: %s", containerName, container.ID(), containerSnapshotName)
h := &taskHandle{
containerName: containerName,
taskConfig: cfg,