Skip to content

Commit

Permalink
Merge pull request #448 from srl-labs/runtime-update
Browse files Browse the repository at this point in the history
rework runtime interface
  • Loading branch information
hellt committed Jun 15, 2021
2 parents f7f3663 + b9b4f07 commit 6c4d97e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 24 deletions.
15 changes: 13 additions & 2 deletions clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/containernetworking/plugins/pkg/ns"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/runtime"
_ "github.com/srl-labs/containerlab/runtime/all"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)
Expand Down Expand Up @@ -57,8 +58,18 @@ func WithTimeout(dur time.Duration) ClabOption {

func WithRuntime(name string, d bool, dur time.Duration, gracefulShutdown bool) ClabOption {
return func(c *CLab) {
c.Runtime = runtime.NewRuntime(name, d, dur, gracefulShutdown)
c.Runtime.SetMgmtNet(c.Config.Mgmt)
if rInit, ok := runtime.ContainerRuntimes[name]; ok {
c.Runtime = rInit()
c.Runtime.Init(
runtime.WithConfig(&runtime.RuntimeConfig{
Timeout: dur,
Debug: d,
}),
runtime.WithMgmtNet(c.Config.Mgmt),
)
return
}
log.Fatalf("unknown container runtime %q", name)
}
}

Expand Down
3 changes: 3 additions & 0 deletions runtime/all/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package all

import _ "github.com/srl-labs/containerlab/runtime/docker"
39 changes: 28 additions & 11 deletions runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@ import (
"github.com/docker/docker/pkg/stdcopy"
"github.com/google/shlex"
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

const sysctlBase = "/proc/sys"
const (
dockerRuntimeName = "docker"
sysctlBase = "/proc/sys"
defaultTimeout = 30 * time.Second
)

func init() {
runtime.Register(dockerRuntimeName, func() runtime.ContainerRuntime {
return &DockerRuntime{
Mgmt: new(types.MgmtNet),
}
})
}

type DockerRuntime struct {
Client *dockerC.Client
Expand All @@ -37,23 +50,27 @@ type DockerRuntime struct {
gracefulShutdown bool
}

func NewDockerRuntime(d bool, dur time.Duration, gracefulShutdown bool) *DockerRuntime {
c, err := dockerC.NewClientWithOpts(dockerC.FromEnv, dockerC.WithAPIVersionNegotiation())
func (c *DockerRuntime) Init(opts ...runtime.RuntimeOption) {
var err error
c.Client, err = dockerC.NewClientWithOpts(dockerC.FromEnv, dockerC.WithAPIVersionNegotiation())
if err != nil {
log.Fatalf("failed to create docker client: %v", err)
}
for _, o := range opts {
o(c)
}
}

return &DockerRuntime{
Client: c,
Mgmt: new(types.MgmtNet),
// TODO: undo this hard-coding
timeout: dur,
debug: d,
gracefulShutdown: gracefulShutdown,
func (c *DockerRuntime) WithConfig(cfg *runtime.RuntimeConfig) {
c.timeout = cfg.Timeout
c.debug = cfg.Debug
c.gracefulShutdown = cfg.GracefulShutdown
if c.timeout <= 0 {
c.timeout = defaultTimeout
}
}

func (c *DockerRuntime) SetMgmtNet(n *types.MgmtNet) {
func (c *DockerRuntime) WithMgmtNet(n *types.MgmtNet) {
c.Mgmt = n
}

Expand Down
40 changes: 29 additions & 11 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ package runtime

import (
"context"
"log"
"time"

"github.com/srl-labs/containerlab/runtime/docker"
"github.com/srl-labs/containerlab/types"
)

Expand All @@ -19,8 +17,12 @@ const (
)

type ContainerRuntime interface {
// Intializes the Container runtime struct
Init(...RuntimeOption)
// Adds custom configuration items to the container runtime struct
WithConfig(*RuntimeConfig)
// Set the network management details (generated by the config.go)
SetMgmtNet(*types.MgmtNet)
WithMgmtNet(*types.MgmtNet)
// Create container (bridge) network
CreateNet(context.Context) error
// Delete container (bridge) network
Expand All @@ -47,14 +49,30 @@ type ContainerRuntime interface {
DeleteContainer(context.Context, string) error
}

func NewRuntime(name string, d bool, dur time.Duration, gracefulShutdown bool) ContainerRuntime {
switch name {
case DockerRuntime:
return docker.NewDockerRuntime(d, dur, gracefulShutdown)
case ContainerdRuntime:
log.Fatalf("%s runtime is not implemented", ContainerdRuntime)
type Initializer func() ContainerRuntime

type RuntimeOption func(ContainerRuntime)

type RuntimeConfig struct {
Timeout time.Duration
GracefulShutdown bool
Debug bool
}

var ContainerRuntimes = map[string]Initializer{}

func Register(name string, initFn Initializer) {
ContainerRuntimes[name] = initFn
}

func WithConfig(cfg *RuntimeConfig) RuntimeOption {
return func(r ContainerRuntime) {
r.WithConfig(cfg)
}
}

log.Fatalf("Unexpected runtime name: %s", name)
return nil
func WithMgmtNet(mgmt *types.MgmtNet) RuntimeOption {
return func(r ContainerRuntime) {
r.WithMgmtNet(mgmt)
}
}

0 comments on commit 6c4d97e

Please sign in to comment.