Skip to content

Commit

Permalink
PR review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
networkop committed Jul 6, 2021
1 parent 107db63 commit 960513e
Show file tree
Hide file tree
Showing 31 changed files with 309 additions and 291 deletions.
10 changes: 4 additions & 6 deletions clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ func NewContainerLab(opts ...ClabOption) (*CLab, error) {
o(c)
}

if err := c.parseTopology(); err != nil {
return nil, err
}
err := c.parseTopology()

return c, nil
return c, err
}

// initMgmtNetwork sets management network config
Expand Down Expand Up @@ -279,7 +277,7 @@ func (c *CLab) DeleteNodes(ctx context.Context, workers uint, deleteCandidates m
}
err := n.Delete(ctx)
if err != nil {
log.Errorf("could not remove container %q: %v", n.GetName(), err)
log.Errorf("could not remove container %q: %v", n.Config().LongName, err)
}
case <-ctx.Done():
return
Expand Down Expand Up @@ -311,7 +309,7 @@ func (c *CLab) ListContainers(ctx context.Context, labels []*types.GenericFilter

func (c *CLab) GetNodeRuntime(query string) (runtime.ContainerRuntime, error) {
for _, node := range c.Nodes {
if node.GetName() == query {
if node.Config().LongName == query {
return node.GetRuntime(), nil
}
}
Expand Down
8 changes: 4 additions & 4 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *CLab) NewNode(nodeName string, nodeDef *types.NodeDefinition, idx int)
n := nodeInitializer()
// Init

err = n.Init(nodeCfg, nodes.WithRuntime(c.GlobalRuntime()), nodes.WithMgmtNet(c.Config.Mgmt))
err = n.Init(nodeCfg, nodes.WithRuntime(c.globalRuntime, c.Runtimes), nodes.WithMgmtNet(c.Config.Mgmt))
if err != nil {
log.Errorf("failed to initialize node %q: %v", nodeCfg.ShortName, err)
return fmt.Errorf("failed to initialize node %q: %v", nodeCfg.ShortName, err)
Expand Down Expand Up @@ -172,9 +172,9 @@ func (c *CLab) createNodeCfg(nodeName string, nodeDef *types.NodeDefinition, idx
Publish: c.Config.Topology.GetNodePublish(nodeName),
Sysctls: make(map[string]string),
Endpoints: make([]*types.Endpoint, 0),
Sandbox: nodeDef.Sandbox,
Kernel: nodeDef.Kernel,
Runtime: nodeDef.Runtime,
Sandbox: nodeDef.GetNodeSandbox(),
Kernel: nodeDef.GetNodeKernel(),
Runtime: nodeDef.GetNodeRuntime(),
}

log.Debugf("node config: %+v", nodeCfg)
Expand Down
4 changes: 2 additions & 2 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/srl-labs/containerlab/cert"
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/runtime/ignite"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)
Expand Down Expand Up @@ -116,7 +116,7 @@ var deployCmd = &cobra.Command{

// Serializing ignite workers due to busy device error
for _, node := range c.Nodes {
if node.GetRuntime().GetName() == ignite.RuntimeName {
if node.GetRuntime().GetName() == runtime.IgniteRuntime {
nodeWorkers = 1
break
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/runtime/ignite"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/types"
)

Expand Down Expand Up @@ -183,8 +183,11 @@ func destroyLab(ctx context.Context, c *clab.CLab) (err error) {
}

// Serializing ignite workers due to busy device error
if rt == ignite.RuntimeName {
maxWorkers = 1
for _, node := range c.Nodes {
if node.GetRuntime().GetName() == runtime.IgniteRuntime {
maxWorkers = 1
break
}
}

log.Infof("Destroying lab: %s", c.Config.Name)
Expand Down
4 changes: 3 additions & 1 deletion cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var graphCmd = &cobra.Command{
Long: "generate topology graph based on the topology definition file and running containers\nreference: https://containerlab.srlinux.dev/cmd/graph/",

RunE: func(cmd *cobra.Command, args []string) error {
var err error

opts := []clab.ClabOption{
clab.WithDebug(debug),
clab.WithTimeout(timeout),
Expand Down Expand Up @@ -82,7 +84,7 @@ var graphCmd = &cobra.Command{
// if offline mode is not enforced, list containers matching lab name
if !offline {
labels := []*types.GenericFilter{{FilterType: "label", Match: c.Config.Name, Field: "containerlab", Operator: "="}}
containers, err := c.ListContainers(ctx, labels)
containers, err = c.ListContainers(ctx, labels)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.0.0
github.com/vishvananda/netlink v1.1.1-0.20210330154013-f5de75959ad5
github.com/weaveworks/ignite v0.9.1-0.20210628093711-07ebb19ff227
github.com/weaveworks/ignite v0.9.1-0.20210705155449-2dbcdd663727
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
gopkg.in/yaml.v2 v2.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,8 @@ github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmF
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae h1:4hwBBUfQCFe3Cym0ZtKyq7L16eZUtYKs+BaHDN6mAns=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/weaveworks/ignite v0.9.1-0.20210628093711-07ebb19ff227 h1:vD0YcUStbI2ijP4cmsc+wviFU5xboc1BM/bT24pOLYs=
github.com/weaveworks/ignite v0.9.1-0.20210628093711-07ebb19ff227/go.mod h1:Uz7DEE4rYUpN2/L9lDxjYui9IktTU6YKXThSvmP83sU=
github.com/weaveworks/ignite v0.9.1-0.20210705155449-2dbcdd663727 h1:3ghRZuxJng0xZdPNkDOk2P41gxgRr5yp0G6e+mWwKNI=
github.com/weaveworks/ignite v0.9.1-0.20210705155449-2dbcdd663727/go.mod h1:Uz7DEE4rYUpN2/L9lDxjYui9IktTU6YKXThSvmP83sU=
github.com/weaveworks/libgitops v0.0.0-20200611103311-2c871bbbbf0c h1:sjuQNsahOaafCY6l5xagnTFwFW6Y6Wm0nnn7U1WvGmA=
github.com/weaveworks/libgitops v0.0.0-20200611103311-2c871bbbbf0c/go.mod h1:1oFuIJ/fBC3gpqsa+xtgG2Febt2ru9aWn2WEMYlIqso=
github.com/weppos/publicsuffix-go v0.4.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k=
Expand Down
16 changes: 9 additions & 7 deletions nodes/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ func (s *bridge) Deploy(ctx context.Context) error { retur
func (s *bridge) PostDeploy(ctx context.Context, ns map[string]nodes.Node) error {
return nil
}
func (s *bridge) WithMgmtNet(*types.MgmtNet) {}
func (s *bridge) WithRuntime(r runtime.ContainerRuntime) { s.runtime = r }
func (s *bridge) GetRuntime() runtime.ContainerRuntime { return s.runtime }
func (s *bridge) WithMgmtNet(*types.MgmtNet) {}
func (s *bridge) WithRuntime(globalRuntime string, allRuntimes map[string]runtime.ContainerRuntime) {
s.runtime = allRuntimes[globalRuntime]
}
func (s *bridge) GetRuntime() runtime.ContainerRuntime { return s.runtime }

func (s *bridge) GetContainer(ctx context.Context) (*types.GenericContainer, error) {
return nil, nil
Expand All @@ -48,12 +50,12 @@ func (s *bridge) SaveConfig(ctx context.Context) error {
return nil
}

func (s *bridge) GetImages() []string {
return []string{}
func (s *bridge) GetImages() map[string]string {
images := make(map[string]string)
images[nodes.ImageKey] = s.cfg.Image
return images
}

func (s *bridge) Delete(ctx context.Context) error {
return nil
}

func (s *bridge) GetName() string { return s.cfg.LongName }
23 changes: 11 additions & 12 deletions nodes/ceos/ceos.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,19 @@ func (s *ceos) PreDeploy(configName, labCADir, labCARoot string) error {

func (s *ceos) Deploy(ctx context.Context) error {
_, err := s.runtime.CreateContainer(ctx, s.cfg)
if err != nil {
return err
}
return nil
return err
}

func (s *ceos) PostDeploy(ctx context.Context, ns map[string]nodes.Node) error {
log.Debugf("Running postdeploy actions for Arista cEOS '%s' node", s.cfg.ShortName)
return ceosPostDeploy(ctx, s.runtime, s.cfg)
}

func (s *ceos) WithMgmtNet(*types.MgmtNet) {}
func (s *ceos) WithRuntime(r runtime.ContainerRuntime) { s.runtime = r }
func (s *ceos) GetRuntime() runtime.ContainerRuntime { return s.runtime }
func (s *ceos) WithMgmtNet(*types.MgmtNet) {}
func (s *ceos) WithRuntime(globalRuntime string, allRuntimes map[string]runtime.ContainerRuntime) {
s.runtime = allRuntimes[globalRuntime]
}
func (s *ceos) GetRuntime() runtime.ContainerRuntime { return s.runtime }

func (s *ceos) SaveConfig(ctx context.Context) error {
_, stderr, err := s.runtime.Exec(ctx, s.cfg.LongName, saveCmd)
Expand Down Expand Up @@ -179,12 +178,12 @@ func ceosPostDeploy(ctx context.Context, r runtime.ContainerRuntime, nodeCfg *ty
return utils.LinkContainerNS(nodeCfg.NSPath, nodeCfg.LongName)
}

func (s *ceos) GetImages() []string {
return []string{s.cfg.Image}
func (s *ceos) GetImages() map[string]string {
images := make(map[string]string)
images[nodes.ImageKey] = s.cfg.Image
return images
}

func (s *ceos) Delete(ctx context.Context) error {
return s.runtime.DeleteContainer(ctx, s.GetName())
return s.runtime.DeleteContainer(ctx, s.Config().LongName)
}

func (s *ceos) GetName() string { return s.cfg.LongName }
23 changes: 11 additions & 12 deletions nodes/crpd/crpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ func (s *crpd) PreDeploy(configName, labCADir, labCARoot string) error {

func (s *crpd) Deploy(ctx context.Context) error {
_, err := s.runtime.CreateContainer(ctx, s.cfg)
if err != nil {
return err
}
return nil
return err
}

func (s *crpd) PostDeploy(ctx context.Context, ns map[string]nodes.Node) error {
Expand All @@ -87,20 +84,22 @@ func (s *crpd) PostDeploy(ctx context.Context, ns map[string]nodes.Node) error {
return err
}

func (s *crpd) GetImages() []string {
return []string{s.cfg.Image}
func (s *crpd) GetImages() map[string]string {
images := make(map[string]string)
images[nodes.ImageKey] = s.cfg.Image
return images
}

func (s *crpd) WithMgmtNet(*types.MgmtNet) {}
func (s *crpd) WithRuntime(r runtime.ContainerRuntime) { s.runtime = r }
func (s *crpd) GetRuntime() runtime.ContainerRuntime { return s.runtime }
func (s *crpd) WithMgmtNet(*types.MgmtNet) {}
func (s *crpd) WithRuntime(globalRuntime string, allRuntimes map[string]runtime.ContainerRuntime) {
s.runtime = allRuntimes[globalRuntime]
}
func (s *crpd) GetRuntime() runtime.ContainerRuntime { return s.runtime }

func (s *crpd) Delete(ctx context.Context) error {
return s.runtime.DeleteContainer(ctx, s.GetName())
return s.runtime.DeleteContainer(ctx, s.Config().LongName)
}

func (s *crpd) GetName() string { return s.cfg.LongName }

func (s *crpd) SaveConfig(ctx context.Context) error {
stdout, stderr, err := s.runtime.Exec(ctx, s.cfg.LongName, saveCmd)
if err != nil {
Expand Down
33 changes: 20 additions & 13 deletions nodes/cvx/cvx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/runtime/ignite"
"github.com/srl-labs/containerlab/types"
"github.com/weaveworks/ignite/pkg/operations"
)
Expand Down Expand Up @@ -72,28 +71,37 @@ func (c *cvx) PostDeploy(ctx context.Context, ns map[string]nodes.Node) error {
return <-c.vmChans.SpawnFinished
}

func (c *cvx) GetImages() []string {
return []string{c.cfg.Image, c.cfg.Kernel, c.cfg.Sandbox}
func (c *cvx) GetImages() map[string]string {
images := make(map[string]string)
images[nodes.ImageKey] = c.cfg.Image
images[nodes.KernelKey] = c.cfg.Kernel
images[nodes.SandboxKey] = c.cfg.Sandbox
return images
}

func (c *cvx) WithMgmtNet(*types.MgmtNet) {}
func (c *cvx) WithRuntime(r runtime.ContainerRuntime) {
func (c *cvx) WithRuntime(globalRuntime string, allRuntimes map[string]runtime.ContainerRuntime) {

// fallback to the global runtime if overridden
if c.Config().Runtime != "" {
c.runtime = r
c.runtime = allRuntimes[globalRuntime]
return
}

// By default, running in ignite runtime
if rInit, ok := runtime.ContainerRuntimes[ignite.RuntimeName]; ok {
if igniteRuntime, ok := allRuntimes[runtime.IgniteRuntime]; ok {
c.runtime = igniteRuntime
return
}

if rInit, ok := runtime.ContainerRuntimes[runtime.IgniteRuntime]; ok {

c.runtime = rInit()

defaultConfig := allRuntimes[globalRuntime].Config()

err := c.runtime.Init(
runtime.WithConfig(&runtime.RuntimeConfig{
Timeout: r.GetTimeout(),
Debug: r.GetDebug(),
GracefulShutdown: r.GetGracefulShutdown(),
}),
runtime.WithConfig(&defaultConfig),
)
if err != nil {
log.Fatalf("failed to init the container runtime: %s", err)
Expand All @@ -103,10 +111,9 @@ func (c *cvx) WithRuntime(r runtime.ContainerRuntime) {
}

func (c *cvx) Delete(ctx context.Context) error {
return c.runtime.DeleteContainer(ctx, c.GetName())
return c.runtime.DeleteContainer(ctx, c.Config().LongName)
}

func (c *cvx) GetName() string { return c.cfg.LongName }
func (s *cvx) GetRuntime() runtime.ContainerRuntime { return s.runtime }

func (c *cvx) SaveConfig(ctx context.Context) error {
Expand Down
16 changes: 9 additions & 7 deletions nodes/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ func (s *host) PostDeploy(ctx context.Context, ns map[string]nodes.Node) error {
return nil
}

func (s *host) GetImages() []string {
return []string{s.cfg.Image}
func (s *host) GetImages() map[string]string {
images := make(map[string]string)
images[nodes.ImageKey] = s.cfg.Image
return images
}

func (s *host) WithMgmtNet(*types.MgmtNet) {}
func (s *host) WithRuntime(r runtime.ContainerRuntime) { s.runtime = r }
func (s *host) GetRuntime() runtime.ContainerRuntime { return s.runtime }
func (s *host) WithMgmtNet(*types.MgmtNet) {}
func (s *host) WithRuntime(globalRuntime string, allRuntimes map[string]runtime.ContainerRuntime) {
s.runtime = allRuntimes[globalRuntime]
}
func (s *host) GetRuntime() runtime.ContainerRuntime { return s.runtime }

func (s *host) GetContainer(ctx context.Context) (*types.GenericContainer, error) {
return nil, nil
Expand All @@ -55,8 +59,6 @@ func (s *host) Delete(ctx context.Context) error {
return nil
}

func (s *host) GetName() string { return s.cfg.LongName }

func (s *host) SaveConfig(ctx context.Context) error {
return nil
}
23 changes: 11 additions & 12 deletions nodes/linux/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,30 @@ func (l *linux) PreDeploy(configName, labCADir, labCARoot string) error { return

func (l *linux) Deploy(ctx context.Context) error {
_, err := l.runtime.CreateContainer(ctx, l.cfg)
if err != nil {
return err
}
return nil
return err
}

func (l *linux) PostDeploy(ctx context.Context, ns map[string]nodes.Node) error {
log.Debugf("Running postdeploy actions for Linux '%s' node", l.cfg.ShortName)
return nil
}

func (s *linux) GetImages() []string {
return []string{s.cfg.Image}
func (s *linux) GetImages() map[string]string {
images := make(map[string]string)
images[nodes.ImageKey] = s.cfg.Image
return images
}

func (l *linux) WithMgmtNet(*types.MgmtNet) {}
func (l *linux) WithRuntime(r runtime.ContainerRuntime) { l.runtime = r }
func (s *linux) GetRuntime() runtime.ContainerRuntime { return s.runtime }
func (l *linux) WithMgmtNet(*types.MgmtNet) {}
func (l *linux) WithRuntime(globalRuntime string, allRuntimes map[string]runtime.ContainerRuntime) {
l.runtime = allRuntimes[globalRuntime]
}
func (s *linux) GetRuntime() runtime.ContainerRuntime { return s.runtime }

func (l *linux) Delete(ctx context.Context) error {
return l.runtime.DeleteContainer(ctx, l.GetName())
return l.runtime.DeleteContainer(ctx, l.Config().LongName)
}

func (l *linux) GetName() string { return l.cfg.LongName }

func (s *linux) SaveConfig(ctx context.Context) error {
return nil
}
Loading

0 comments on commit 960513e

Please sign in to comment.