From 175e6922545688e7ccc95cdef382ac51995c95c8 Mon Sep 17 00:00:00 2001 From: Shishir Mahajan Date: Mon, 11 May 2020 16:07:51 -0700 Subject: [PATCH] Create OCI container. --- containerd/containerd.go | 12 +++++++++++- containerd/driver.go | 12 ++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/containerd/containerd.go b/containerd/containerd.go index edce8aa..7e99abe 100644 --- a/containerd/containerd.go +++ b/containerd/containerd.go @@ -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)), + ) +} diff --git a/containerd/driver.go b/containerd/driver.go index f8d55f9..216f26c 100644 --- a/containerd/driver.go +++ b/containerd/driver.go @@ -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,