Skip to content

Commit

Permalink
Merge pull request #427 from steiler/containerd
Browse files Browse the repository at this point in the history
containerd
  • Loading branch information
hellt committed Jun 16, 2021
2 parents 6c4d97e + 91bcd27 commit 417a7b9
Show file tree
Hide file tree
Showing 31 changed files with 1,158 additions and 118 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ jobs:
smoke-tests:
runs-on: ubuntu-20.04
strategy:
matrix:
runtime: ["docker", "containerd"]
needs:
- staticcheck
- unit-test
Expand All @@ -103,7 +106,7 @@ jobs:
pip install -r tests/requirements.txt
- name: Run smoke tests
run: |
bash ./tests/rf-run.sh ./tests/01-smoke
bash ./tests/rf-run.sh ${{ matrix.runtime }} ./tests/01-smoke
# upload test reports as a zip file
- uses: actions/upload-artifact@v2
if: always()
Expand Down
15 changes: 12 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,44 @@ build-containerlab:

smoke-tests:
stage: smoke-tests
parallel:
matrix:
- RUNTIME: [docker, containerd]
tags:
- containerlab
script:
- source ~/venvs/rf/bin/activate
- bash ./tests/rf-run.sh ./tests/01-smoke
- bash ./tests/rf-run.sh $RUNTIME ./tests/01-smoke
artifacts:
when: always
paths:
- "./tests/out/*.html"

srl-tests:
stage: integration-tests
parallel:
matrix:
- RUNTIME: [docker, containerd]
tags:
- containerlab
script:
- source ~/venvs/rf/bin/activate
- bash ./tests/rf-run.sh ./tests/02-basic-srl
- bash ./tests/rf-run.sh $RUNTIME ./tests/02-basic-srl
artifacts:
when: always
paths:
- "./tests/out/*.html"

ceos-tests:
stage: integration-tests
parallel:
matrix:
- RUNTIME: [docker, containerd]
tags:
- containerlab
script:
- source ~/venvs/rf/bin/activate
- bash ./tests/rf-run.sh ./tests/03-basic-ceos
- bash ./tests/rf-run.sh $RUNTIME ./tests/02-basic-srl
artifacts:
when: always
paths:
Expand Down
6 changes: 1 addition & 5 deletions clab/ceos.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net"
"path"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -41,13 +40,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
25 changes: 18 additions & 7 deletions clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package clab
import (
"context"
"fmt"
"os"
"path"
"strings"
"sync"
Expand Down Expand Up @@ -58,15 +59,29 @@ func WithTimeout(dur time.Duration) ClabOption {

func WithRuntime(name string, d bool, dur time.Duration, gracefulShutdown bool) ClabOption {
return func(c *CLab) {
// define runtime name.
// order of preference: cli flag -> env var -> default value of docker
envN := os.Getenv("CLAB_RUNTIME")
switch {
case name != "":
case envN != "":
name = envN
default:
name = runtime.DockerRuntime
}

if rInit, ok := runtime.ContainerRuntimes[name]; ok {
c.Runtime = rInit()
c.Runtime.Init(
err := c.Runtime.Init(
runtime.WithConfig(&runtime.RuntimeConfig{
Timeout: dur,
Debug: d,
}),
runtime.WithMgmtNet(c.Config.Mgmt),
)
if err != nil {
log.Fatalf("failed to init the container runtime: %s", err)
}
return
}
log.Fatalf("unknown container runtime %q", name)
Expand Down Expand Up @@ -337,13 +352,9 @@ func (c *CLab) DeleteNodes(ctx context.Context, workers uint, containers []types
log.Debugf("Worker %d terminating...", i)
return
}
name := cont.ID
if len(cont.Names) > 0 {
name = strings.TrimLeft(cont.Names[0], "/")
}
err := c.Runtime.DeleteContainer(ctx, name)
err := c.Runtime.DeleteContainer(ctx, cont)
if err != nil {
log.Errorf("could not remove container '%s': %v", name, err)
log.Errorf("could not remove container '%s': %v", cont.ID, err)
}
case <-ctx.Done():
return
Expand Down
3 changes: 1 addition & 2 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,7 @@ func (c *CLab) VerifyContainersUniqueness(ctx context.Context) error {
nctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()

var labels []string
containers, err := c.Runtime.ListContainers(nctx, labels)
containers, err := c.Runtime.ListContainers(nctx, nil)
if err != nil {
return fmt.Errorf("could not list containers: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions clab/srl.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func initSRLNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, env
log.Fatalf("wrong node type. '%s' doesn't exist. should be any of %s", node.NodeType, strings.Join(keys, ", "))
}

// initialize specifc container information
node.Cmd = "sudo sr_linux"
// the addition touch is needed to support non docker runtimes
node.Cmd = "sudo bash -c 'touch /.dockerenv && /opt/srlinux/bin/sr_linux'"

kindEnv := map[string]string{"SRLINUX": "1"}
node.Env = mergeStringMaps(kindEnv, envs)
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var deployCmd = &cobra.Command{
}
log.Debug("containers created, retrieving state and IP addresses...")

labels = append(labels, "containerlab="+c.Config.Name)
labels := []*types.GenericFilter{{FilterType: "label", Match: c.Config.Name, Field: "containerlab", Operator: "="}}
containers, err := c.Runtime.ListContainers(ctx, labels)
if err != nil {
return fmt.Errorf("could not list containers: %v", err)
Expand Down
10 changes: 6 additions & 4 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var destroyCmd = &cobra.Command{
}
c := clab.NewContainerLab(opts...)
// list all containerlab containers
containers, err := c.Runtime.ListContainers(ctx, []string{"containerlab"})
containers, err := c.Runtime.ListContainers(ctx, []*types.GenericFilter{{FilterType: "label", Field: "containerlab", Operator: "exists"}})
if err != nil {
return fmt.Errorf("could not list containers: %v", err)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func deleteEntriesFromHostsFile(containers []types.GenericContainer, bridgeName
if bridgeName == "" {
return fmt.Errorf("missing bridge name")
}
f, err := os.OpenFile("/etc/hosts", os.O_RDWR, 0644)
f, err := os.OpenFile("/etc/hosts", os.O_RDWR, 0644) // skipcq: GSC-G302
if err != nil {
return err
}
Expand Down Expand Up @@ -171,7 +171,9 @@ func deleteEntriesFromHostsFile(containers []types.GenericContainer, bridgeName
}

func destroyLab(ctx context.Context, c *clab.CLab) (err error) {
containers, err := c.Runtime.ListContainers(ctx, []string{fmt.Sprintf("containerlab=%s", c.Config.Name)})

labels := []*types.GenericFilter{{FilterType: "label", Match: c.Config.Name, Field: "containerlab", Operator: "="}}
containers, err := c.Runtime.ListContainers(ctx, labels)
if err != nil {
return fmt.Errorf("could not list containers: %v", err)
}
Expand Down Expand Up @@ -207,7 +209,7 @@ func destroyLab(ctx context.Context, c *clab.CLab) (err error) {
}

// delete lab management network
log.Infof("Deleting docker network '%s'...", c.Config.Mgmt.Network)
log.Infof("Deleting network '%s'...", c.Config.Mgmt.Network)
if err = c.Runtime.DeleteNet(ctx); err != nil {
// do not log error message if deletion error simply says that such network doesn't exist
if err.Error() != fmt.Sprintf("Error: No such network: %s", c.Config.Mgmt.Network) {
Expand Down
8 changes: 3 additions & 5 deletions cmd/disableTxOffload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd

import (
"context"
"strconv"

"github.com/containernetworking/plugins/pkg/ns"
log "github.com/sirupsen/logrus"
Expand All @@ -33,13 +32,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
6 changes: 4 additions & 2 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/types"
)

var labels []string
Expand Down Expand Up @@ -44,8 +45,9 @@ var execCmd = &cobra.Command{
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labels = append(labels, "containerlab="+name)
containers, err := c.Runtime.ListContainers(ctx, labels)
filters := []*types.GenericFilter{{FilterType: "label", Match: name, Field: "containerlab", Operator: "="}}
filters = append(filters, types.FilterFromLabelStrings(labels)...)
containers, err := c.Runtime.ListContainers(ctx, filters)
if err != nil {
log.Fatalf("could not list containers: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ var graphCmd = &cobra.Command{
// if offline mode is not enforced, list containers matching lab name
if !offline {
var err error
containers, err = c.Runtime.ListContainers(ctx, []string{fmt.Sprintf("containerlab=%s", c.Config.Name)})
labels := []*types.GenericFilter{{FilterType: "label", Match: c.Config.Name, Field: "containerlab", Operator: "="}}
containers, err = c.Runtime.ListContainers(ctx, labels)
if err != nil {
log.Errorf("could not list containers: %v", err)
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ var inspectCmd = &cobra.Command{
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var glabels []*types.GenericFilter
if all {
labels = append(labels, "containerlab")
glabels = []*types.GenericFilter{{FilterType: "label", Field: "containerlab", Operator: "exists"}}
} else {
labels = append(labels, "containerlab="+name)
if name != "" {
glabels = []*types.GenericFilter{{FilterType: "label", Match: name, Field: "containerlab", Operator: "="}}
} else if topo != "" {
glabels = []*types.GenericFilter{{FilterType: "label", Match: c.Config.Name, Field: "containerlab", Operator: "="}}
}
}
containers, err := c.Runtime.ListContainers(ctx, labels)
containers, err := c.Runtime.ListContainers(ctx, glabels)
if err != nil {
log.Fatalf("could not list containers: %v", err)
}
Expand Down Expand Up @@ -128,9 +133,8 @@ func printContainerInspect(c *clab.CLab, containers []types.GenericContainer, br
IPv4Address: getContainerIPv4(cont, bridgeName),
IPv6Address: getContainerIPv6(cont, bridgeName),
}
if len(cont.ID) > 11 {
cdet.ContainerID = cont.ID[:12]
}
cdet.ContainerID = cont.ShortID

if len(cont.Names) > 0 {
cdet.Name = strings.TrimLeft(cont.Names[0], "/")
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/runtime"
)

var debug bool
Expand Down Expand Up @@ -52,8 +51,7 @@ func init() {
_ = rootCmd.MarkPersistentFlagFilename("topo", "*.yaml", "*.yml")
rootCmd.PersistentFlags().StringVarP(&name, "name", "n", "", "lab name")
rootCmd.PersistentFlags().DurationVarP(&timeout, "timeout", "", 30*time.Second, "timeout for docker requests, e.g: 30s, 1m, 2m30s")
rootCmd.PersistentFlags().StringVarP(&rt, "runtime", "r", runtime.DockerRuntime, "container runtime")

rootCmd.PersistentFlags().StringVarP(&rt, "runtime", "r", "", "container runtime")
}

// returns an error if topo path is not provided
Expand Down
3 changes: 2 additions & 1 deletion cmd/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Refer to the https://containerlab.srlinux.dev/cmd/save/ documentation to see the
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

containers, err := c.Runtime.ListContainers(ctx, []string{"containerlab=" + c.Config.Name})
labels := []*types.GenericFilter{{FilterType: "label", Match: c.Config.Name, Field: "containerlab", Operator: "="}}
containers, err := c.Runtime.ListContainers(ctx, labels)
if err != nil {
return fmt.Errorf("could not list containers: %v", err)
}
Expand Down
13 changes: 8 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@ require (
github.com/Juniper/go-netconf v0.1.1
github.com/awalterschulze/gographviz v2.0.1+incompatible
github.com/cloudflare/cfssl v1.4.1
github.com/containerd/containerd v1.5.0 // indirect
github.com/containerd/containerd v1.5.2
github.com/containernetworking/cni v0.8.1
github.com/containernetworking/plugins v0.9.1
github.com/digitalocean/go-openvswitch v0.0.0-20201214180534-ce0f183468d8
github.com/docker/docker v20.10.6+incompatible
github.com/docker/go-connections v0.4.0
github.com/google/go-cmp v0.5.4
github.com/docker/go-units v0.4.0
github.com/google/go-cmp v0.5.6
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.2.0
github.com/hashicorp/go-version v1.2.1
github.com/jsimonetti/rtnetlink v0.0.0-20210226120601-1b79e63a70a0
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/sirupsen/logrus v1.7.0
github.com/opencontainers/runtime-spec v1.0.3-0.20210303205135-43e4633e40c1
github.com/pkg/errors v0.9.1 // 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
github.com/vishvananda/netlink v1.1.1-0.20210330154013-f5de75959ad5
github.com/ziutek/telnet v0.0.0-20180329124119-c3b780dc415b // indirect
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
gopkg.in/yaml.v2 v2.4.0
)

Loading

0 comments on commit 417a7b9

Please sign in to comment.