Skip to content

Commit

Permalink
containers starting now
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed May 27, 2021
1 parent 11f43d9 commit a519147
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
39 changes: 33 additions & 6 deletions runtime/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions utils/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a519147

Please sign in to comment.