diff --git a/runtime/containerd/containerd.go b/runtime/containerd/containerd.go index af0bdb7e9..28681de1b 100644 --- a/runtime/containerd/containerd.go +++ b/runtime/containerd/containerd.go @@ -9,8 +9,10 @@ import ( "time" "github.com/containerd/containerd" + "github.com/containerd/containerd/cio" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/namespaces" + "github.com/containerd/containerd/oci" "github.com/docker/go-units" log "github.com/sirupsen/logrus" "github.com/srl-labs/containerlab/types" @@ -82,28 +84,51 @@ func (c *ContainerdRuntime) PullImageIfRequired(ctx context.Context, imagename s func (c *ContainerdRuntime) CreateContainer(ctx context.Context, node *types.Node) error { ctx = namespaces.WithNamespace(ctx, containerdNamespace) - _, err := c.client.NewContainer( + img, err := c.client.GetImage(ctx, node.Image) + if err != nil { + return err + } + + cont, err := c.client.NewContainer( ctx, node.ShortName, + containerd.WithNewSnapshot(node.ShortName+"nginx-server-snapshot", img), + containerd.WithNewSpec(oci.WithImageConfig(img)), containerd.WithAdditionalContainerLabels(node.Labels), - containerd.WithImageName(node.Image)) + ) if err != nil { return err } + _ = cont + log.Debugf("Container '%s' created", node.ShortName) + log.Debugf("Start container: %s", node.LongName) - return nil + err = c.StartContainer(ctx, node.ShortName) + if err != nil { + return err + } + + log.Debugf("Container started: %s", node.LongName) + + node.NSPath, err = c.GetNSPath(ctx, node.ShortName) + if err != nil { + return err + } + return utils.LinkContainerNS(node.NSPath, node.LongName) } + func (c *ContainerdRuntime) StartContainer(ctx context.Context, containername string) error { - task, err := c.getContainerTask(ctx, containername) + container, err := c.client.LoadContainer(ctx, containername) if err != nil { return err } - err = task.Start(ctx) + task, err := container.NewTask(ctx, cio.NullIO) if err != nil { log.Fatalf("Failed to start container %s", containername) return err } - return nil + err = task.Start(ctx) + return err } func (c *ContainerdRuntime) StopContainer(ctx context.Context, containername string, dur *time.Duration) error { task, err := c.getContainerTask(ctx, containername) @@ -257,6 +282,7 @@ func (c *ContainerdRuntime) ExecNotWait(context.Context, string, []string) error return nil } func (c *ContainerdRuntime) DeleteContainer(ctx context.Context, container *types.GenericContainer) error { + log.Debug("deleting container %s", container.ID) ctx = namespaces.WithNamespace(ctx, containerdNamespace) cont, err := c.client.LoadContainer(ctx, container.ID) if err != nil { @@ -270,5 +296,6 @@ func (c *ContainerdRuntime) DeleteContainer(ctx context.Context, container *type if err := cont.Delete(ctx, delOpts...); err != nil { return err } + log.Debug("successfully deleted container %s", container.ID) return nil } diff --git a/types/types.go b/types/types.go index b923d1da2..eda279c1c 100644 --- a/types/types.go +++ b/types/types.go @@ -132,10 +132,10 @@ type GenericMgmtIPs struct { type GenericFilter struct { // defined by now "label" FilterType string - // defines e.g. the label name + // defines e.g. the label name for FilterType "label" Field string - // = | != + // = | != | exists Operator string - // match + // match value Match string } diff --git a/utils/containers.go b/utils/containers.go index 6e5e597c7..3891aa37e 100644 --- a/utils/containers.go +++ b/utils/containers.go @@ -30,6 +30,7 @@ func GetCanonicalImageName(imageName string) string { tag := "latest" colonSplit := strings.Split(imageName, ":") if len(colonSplit) == 2 { + canonicalImageName = colonSplit[0] tag = colonSplit[1] } return canonicalImageName + ":" + tag