Skip to content

Commit

Permalink
continued
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed May 27, 2021
1 parent a519147 commit d9e885f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 20 deletions.
6 changes: 1 addition & 5 deletions clab/ceos.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"path"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -37,13 +36,10 @@ func ceosPostDeploy(ctx context.Context, c *CLab, node *types.Node, lworkers uin
if err != nil {
return err
}
// since container has been restarted, we need to get its new NSPath and link netns
cont, err := c.Runtime.ContainerInspect(ctx, node.ContainerID)
node.NSPath, err = c.Runtime.GetNSPath(ctx, node.ContainerID)
if err != nil {
return err
}
log.Debugf("node %s new pid %v", node.LongName, cont.Pid)
node.NSPath = "/proc/" + strconv.Itoa(cont.Pid) + "/ns/net"
err = utils.LinkContainerNS(node.NSPath, node.LongName)
if err != nil {
return err
Expand Down
8 changes: 3 additions & 5 deletions cmd/disableTxOffload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"strconv"

"github.com/containernetworking/plugins/pkg/ns"
log "github.com/sirupsen/logrus"
Expand All @@ -29,13 +28,12 @@ var disableTxOffloadCmd = &cobra.Command{
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

cnt, err := c.Runtime.ContainerInspect(ctx, cntName)
log.Infof("getting container '%s' information", cntName)

NSPath, err := c.Runtime.GetNSPath(ctx, cntName)
if err != nil {
return err
}

log.Infof("getting container '%s' information", cntName)
NSPath := "/proc/" + strconv.Itoa(cnt.Pid) + "/ns/net"
nodeNS, err := ns.GetNS(NSPath)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func printContainerInspect(c *clab.CLab, containers []types.GenericContainer, br
}
if len(cont.ID) > 11 {
cdet.ContainerID = cont.ID[:12]
} else {
cdet.ContainerID = cont.ID
}
if len(cont.Names) > 0 {
cdet.Name = strings.TrimLeft(cont.Names[0], "/")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/morikuni/aec v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.5-0.20201029120751-42e21c7531a3
github.com/opencontainers/runtime-spec v1.0.3-0.20210303205135-43e4633e40c1 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.0.0
github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852
Expand Down
80 changes: 70 additions & 10 deletions runtime/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
"github.com/docker/go-units"
"github.com/google/shlex"
"github.com/opencontainers/runtime-spec/specs-go"
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
Expand Down Expand Up @@ -89,17 +93,40 @@ func (c *ContainerdRuntime) CreateContainer(ctx context.Context, node *types.Nod
return err
}

cont, err := c.client.NewContainer(
cmd, err := shlex.Split(node.Cmd)
if err != nil {
return err
}
// TODO: MAC address
// TODO: Network interface
// TODO: Portbinding

opts := []oci.SpecOpts{
oci.WithImageConfig(img),
oci.WithEnv(utils.ConvertEnvs(node.Env)),
oci.WithProcessArgs(cmd...),
oci.WithHostname(node.ShortName),
oci.WithUser(node.User),
WithSysctls(node.Sysctls),
oci.WithPrivileged,
}

switch node.NetworkMode {
case "host":
opts = append(opts, oci.WithHostNamespace(specs.NetworkNamespace), oci.WithHostHostsFile, oci.WithHostResolvconf)
}

_, err = c.client.NewContainer(
ctx,
node.ShortName,
containerd.WithNewSnapshot(node.ShortName+"nginx-server-snapshot", img),
containerd.WithNewSpec(oci.WithImageConfig(img)),
containerd.WithNewSnapshot(node.ShortName+"-snapshot", img),
containerd.WithNewSpec(opts...),
containerd.WithAdditionalContainerLabels(node.Labels),
)
if err != nil {
return err
}
_ = cont

log.Debugf("Container '%s' created", node.ShortName)
log.Debugf("Start container: %s", node.LongName)

Expand All @@ -114,15 +141,30 @@ func (c *ContainerdRuntime) CreateContainer(ctx context.Context, node *types.Nod
if err != nil {
return err
}
return utils.LinkContainerNS(node.NSPath, node.LongName)
return nil
}

func WithSysctls(sysctls map[string]string) oci.SpecOpts {
return func(ctx context.Context, client oci.Client, c *containers.Container, s *runtimespec.Spec) error {
if s.Linux == nil {
s.Linux = &runtimespec.Linux{}
}
if s.Linux.Sysctl == nil {
s.Linux.Sysctl = make(map[string]string)
}
for k, v := range sysctls {
s.Linux.Sysctl[k] = v
}
return nil
}
}

func (c *ContainerdRuntime) StartContainer(ctx context.Context, containername string) error {
container, err := c.client.LoadContainer(ctx, containername)
if err != nil {
return err
}
task, err := container.NewTask(ctx, cio.NullIO)
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStdio))
if err != nil {
log.Fatalf("Failed to start container %s", containername)
return err
Expand All @@ -131,14 +173,26 @@ func (c *ContainerdRuntime) StartContainer(ctx context.Context, containername st
return err
}
func (c *ContainerdRuntime) StopContainer(ctx context.Context, containername string, dur *time.Duration) error {
task, err := c.getContainerTask(ctx, containername)
ctask, err := c.getContainerTask(ctx, containername)
if err != nil {
return err
}
err = task.Kill(ctx, syscall.SIGTERM)
taskstatus, err := ctask.Status(ctx)
if err != nil {
return err
}
switch taskstatus.Status {
case "running", "paused":
err = ctask.Kill(ctx, syscall.SIGQUIT)
if err != nil {
return err
}
}
existStatus, err := ctask.Delete(ctx)
if err != nil {
return err
}
log.Debugf("Container %s stopped with exit code %d", containername, existStatus.ExitCode())
return nil
}

Expand Down Expand Up @@ -212,7 +266,7 @@ func (c *ContainerdRuntime) produceGenericContainerList(ctx context.Context, inp
return nil, err
}

ctr.Names = []string{info.Labels["name"]}
ctr.Names = []string{i.ID()}
ctr.ID = i.ID()
ctr.Image = info.Image
ctr.Labels = info.Labels
Expand Down Expand Up @@ -282,8 +336,14 @@ 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)
log.Debugf("deleting container %s", container.ID)
ctx = namespaces.WithNamespace(ctx, containerdNamespace)

err := c.StopContainer(ctx, container.ID, nil)
if err != nil {
return err
}

cont, err := c.client.LoadContainer(ctx, container.ID)
if err != nil {
return err
Expand Down

0 comments on commit d9e885f

Please sign in to comment.