Skip to content

Commit

Permalink
CNI binary check
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Jun 16, 2021
1 parent fa56d64 commit 937cad9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
5 changes: 4 additions & 1 deletion clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ func WithRuntime(name string, d bool, dur time.Duration, gracefulShutdown bool)
return func(c *CLab) {
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
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
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.20210330154013-f5de75959ad5
Expand Down
24 changes: 18 additions & 6 deletions runtime/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"os"
"path"
"strconv"
"strings"
"syscall"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/docker/go-units"
"github.com/google/shlex"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/types"
Expand All @@ -37,6 +39,8 @@ const (
defaultTimeout = 30 * time.Second
)

var cniPath string

func init() {
runtime.Register(dockerRuntimeName, func() runtime.ContainerRuntime {
return &ContainerdRuntime{
Expand All @@ -45,15 +49,27 @@ func init() {
})
}

func (c *ContainerdRuntime) Init(opts ...runtime.RuntimeOption) {
func (c *ContainerdRuntime) Init(opts ...runtime.RuntimeOption) error {
var err error
var ok bool
c.client, err = containerd.New("/run/containerd/containerd.sock")
if err != nil {
log.Fatalf("failed to create containerd client: %v", err)
return err
}
if cniPath, ok = os.LookupEnv("CNI_BIN"); !ok {
cniPath = cniBin
}
binaries := []string{"tuning", "bridge", "host-local"}
for _, binary := range binaries {
binary = path.Join(cniPath, binary)
if _, err := os.Stat(binary); err != nil {
return errors.WithMessagef(err, "CNI binaries not found. [ %s ] are required.", strings.Join(binaries, ","))
}
}
for _, o := range opts {
o(c)
}
return nil
}

type ContainerdRuntime struct {
Expand Down Expand Up @@ -301,10 +317,6 @@ func (c *ContainerdRuntime) CreateContainer(ctx context.Context, node *types.Nod

func cniInit(cId, ifName string, mgmtNet *types.MgmtNet) (*libcni.CNIConfig, *libcni.NetworkConfigList, *libcni.RuntimeConf, error) {
// allow overwriting cni plugin binary path via ENV var
cniPath, ok := os.LookupEnv("CNI_BIN")
if !ok {
cniPath = cniBin
}

cnic := libcni.NewCNIConfigWithCacheDir([]string{cniPath}, cniCache, nil)

Expand Down
3 changes: 2 additions & 1 deletion runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type DockerRuntime struct {
gracefulShutdown bool
}

func (c *DockerRuntime) Init(opts ...runtime.RuntimeOption) {
func (c *DockerRuntime) Init(opts ...runtime.RuntimeOption) error {
var err error
c.Client, err = dockerC.NewClientWithOpts(dockerC.FromEnv, dockerC.WithAPIVersionNegotiation())
if err != nil {
Expand All @@ -59,6 +59,7 @@ func (c *DockerRuntime) Init(opts ...runtime.RuntimeOption) {
for _, o := range opts {
o(c)
}
return nil
}

func (c *DockerRuntime) WithConfig(cfg *runtime.RuntimeConfig) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (

type ContainerRuntime interface {
// Intializes the Container runtime struct
Init(...RuntimeOption)
Init(...RuntimeOption) error
// Adds custom configuration items to the container runtime struct
WithConfig(*RuntimeConfig)
// Set the network management details (generated by the config.go)
Expand Down

0 comments on commit 937cad9

Please sign in to comment.