Skip to content

Commit

Permalink
build dynamic interface name map (#2329)
Browse files Browse the repository at this point in the history
* build dynamic interface name map

* hide empty link deployment state

* linting
  • Loading branch information
hellt authored Dec 4, 2024
1 parent 13f951f commit 51c6366
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 44 deletions.
109 changes: 93 additions & 16 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,105 @@ import (
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/links"
"github.com/srl-labs/containerlab/nodes/ceos"
"github.com/srl-labs/containerlab/nodes/crpd"
"github.com/srl-labs/containerlab/nodes/rare"
"github.com/srl-labs/containerlab/nodes/sonic"
"github.com/srl-labs/containerlab/nodes/srl"
"github.com/srl-labs/containerlab/nodes/vr_sros"
"github.com/srl-labs/containerlab/nodes/vr_veos"
"github.com/srl-labs/containerlab/nodes/vr_vjunosevolved"
"github.com/srl-labs/containerlab/nodes/vr_vjunosswitch"
"github.com/srl-labs/containerlab/nodes/vr_vmx"
"github.com/srl-labs/containerlab/nodes/vr_vqfx"
"github.com/srl-labs/containerlab/nodes/vr_vsrx"
"github.com/srl-labs/containerlab/nodes/vr_xrv9k"
"github.com/srl-labs/containerlab/nodes/xrd"
"github.com/srl-labs/containerlab/types"
"gopkg.in/yaml.v2"
)

var interfaceFormat = map[string]string{
"srl": "e1-%d",
"ceos": "eth%d",
"crpd": "eth%d",
"sonic-vs": "eth%d",
"linux": "eth%d",
"bridge": "veth%d",
"vr-sros": "eth%d",
"vr-vmx": "ge-0/0/%d",
"vr-vsrx": "ge-0/0/%d",
"vr-vqfx": "ge-0/0/%d",
"vr-xrv9k": "Gi0/0/0/%d",
"vr-veos": "Et1/%d",
"xrd": "eth%d",
"rare": "eth%d",
func buildInterfaceFormatMap() map[string]string {
m := make(map[string]string)

// Add SR Linux kinds
for _, k := range srl.KindNames {
m[k] = "e1-%d"
}

// Add Arista cEOS kinds
for _, k := range ceos.KindNames {
m[k] = "eth%d"
}

// Add Arista vEOS kinds
for _, k := range vr_veos.KindNames {
m[k] = "Et1/%d"
}

// Add Nokia SR OS kinds
for _, k := range vr_sros.KindNames {
m[k] = "1/1/%d"
}

// Add Juniper cRPD
for _, k := range crpd.KindNames {
m[k] = "eth%d"
}

// Add Juniper VMX kinds
for _, k := range vr_vmx.KindNames {
m[k] = "ge-0/0/%d"
}

// Add Juniper vSRX kinds
for _, k := range vr_vsrx.KindNames {
m[k] = "ge-0/0/%d"
}

// Add Juniper vQFX kinds
for _, k := range vr_vqfx.KindNames {
m[k] = "ge-0/0/%d"
}

// Add Juniper vJunos-Switch/Router
for _, k := range vr_vjunosswitch.KindNames {
m[k] = "et-0/0/%d"
}

// Add Juniper vJunos-Evolved
for _, k := range vr_vjunosevolved.KindNames {
m[k] = "et-0/0/%d"
}

// Add Cisco XRv9k kinds
for _, k := range vr_xrv9k.KindNames {
m[k] = "Gi0/0/0/%d"
}

// Add Cisco XRd
for _, k := range xrd.KindNames {
m[k] = "eth%d"
}

// Add RARE FreeRtr
for _, k := range rare.KindNames {
m[k] = "eth%d"
}

// Add SONiC VS
for _, k := range sonic.KindNames {
m[k] = "eth%d"
}

m["linux"] = "eth%d"
m["bridge"] = "veth%d"

return m
}

var interfaceFormat = buildInterfaceFormatMap()

var supportedKinds = []string{
"srl", "ceos", "linux", "bridge", "sonic-vs", "crpd", "vr-sros", "vr-vmx", "vr-vsrx",
"vr-vqfx", "juniper_vjunosevolved", "juniper_vjunosrouter", "juniper_vjunosswitch", "vr-xrv9k", "vr-veos",
Expand Down Expand Up @@ -77,7 +154,7 @@ var generateCmd = &cobra.Command{
Use: "generate",
Aliases: []string{"gen"},
Short: "generate a Clos topology file, based on provided flags",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
if name == "" {
return errors.New("provide a lab name with --name flag")
}
Expand Down
2 changes: 1 addition & 1 deletion links/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type LinkCommonParams struct {
MTU int `yaml:"mtu,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
Vars map[string]interface{} `yaml:"vars,omitempty"`
DeploymentState LinkDeploymentState
DeploymentState LinkDeploymentState `yaml:",omitempty"`
}

// GetMTU returns the MTU of the link.
Expand Down
6 changes: 3 additions & 3 deletions nodes/ceos/ceos.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
)

var (
kindnames = []string{"ceos", "arista_ceos"}
KindNames = []string{"ceos", "arista_ceos"}
// defined env vars for the ceos.
ceosEnv = map[string]string{
"CEOS": "1",
Expand All @@ -52,7 +52,7 @@ var (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(ceos)
}, defaultCredentials)
}
Expand Down Expand Up @@ -288,7 +288,7 @@ func (n *ceos) ceosPostDeploy(_ context.Context) error {
func (n *ceos) CheckInterfaceName() error {
// allow eth and et interfaces
// https://regex101.com/r/umQW5Z/2
ifRe := regexp.MustCompile(`eth[1-9][\w\.]*$|et[1-9][\w\.]*$`)
ifRe := regexp.MustCompile(`eth[1-9][\w.]*$|et[1-9][\w.]*$`)
for _, e := range n.Endpoints {
if !ifRe.MatchString(e.GetIfaceName()) {
return fmt.Errorf("arista cEOS node %q has an interface named %q which doesn't match the required pattern. Interfaces should be named as ethX or etX, where X consists of alpanumerical characters", n.Cfg.ShortName, e.GetIfaceName())
Expand Down
4 changes: 2 additions & 2 deletions nodes/crpd/crpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (
)

var (
kindnames = []string{"crpd", "juniper_crpd"}
KindNames = []string{"crpd", "juniper_crpd"}
//go:embed crpd.cfg
defaultCfgTemplate string

Expand All @@ -41,7 +41,7 @@ var (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(crpd)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/rare/rare.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
"github.com/srl-labs/containerlab/utils"
)

var kindnames = []string{"rare"}
var KindNames = []string{"rare"}

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(rare)
}, nil)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/sonic/sonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"github.com/srl-labs/containerlab/utils"
)

var kindnames = []string{"sonic-vs"}
var KindNames = []string{"sonic-vs"}

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(sonic)
}, nil)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/vr_sros/vr-sros.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

var (
kindNames = []string{"nokia_sros", "vr-sros", "vr-nokia_sros"}
KindNames = []string{"nokia_sros", "vr-sros", "vr-nokia_sros"}
defaultCredentials = nodes.NewCredentials("admin", "admin")

InterfaceRegexp = regexp.MustCompile(`1/1/(?P<port>\d+)`)
Expand All @@ -56,7 +56,7 @@ type SROSTemplateData struct {

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindNames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(vrSROS)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/vr_veos/vr-veos.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
kindnames = []string{"arista_veos", "vr-veos", "vr-arista_veos"}
KindNames = []string{"arista_veos", "vr-veos", "vr-arista_veos"}
defaultCredentials = nodes.NewCredentials("admin", "admin")

InterfaceRegexp = regexp.MustCompile(`(?:Et|Ethernet)1/(?P<port>\d+)`)
Expand All @@ -35,7 +35,7 @@ const (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(vrVEOS)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/vr_vjunosevolved/vr-vjunosevolved.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
kindnames = []string{"juniper_vjunosevolved"}
KindNames = []string{"juniper_vjunosevolved"}
defaultCredentials = nodes.NewCredentials("admin", "admin@123")

InterfaceRegexp = regexp.MustCompile(`(?:et|xe|ge)-0/0/(?P<port>\d+)$`)
Expand All @@ -35,7 +35,7 @@ const (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(vrVJUNOSEVOLVED)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/vr_vjunosswitch/vr-vjunosswitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
kindnames = []string{"juniper_vjunosrouter", "juniper_vjunosswitch"}
KindNames = []string{"juniper_vjunosrouter", "juniper_vjunosswitch"}
defaultCredentials = nodes.NewCredentials("admin", "admin@123")

InterfaceRegexp = regexp.MustCompile(`(?:et|xe|ge)-0/0/(?P<port>\d+)$`)
Expand All @@ -35,7 +35,7 @@ const (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(vrVJUNOSSWITCH)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/vr_vmx/vr-vmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
kindnames = []string{"juniper_vmx", "vr-vmx", "vr-juniper_vmx"}
KindNames = []string{"juniper_vmx", "vr-vmx", "vr-juniper_vmx"}
defaultCredentials = nodes.NewCredentials("admin", "admin@123")

InterfaceRegexp = regexp.MustCompile(`(?:et|xe|ge)-0/0/(?P<port>\d+)$`)
Expand All @@ -35,7 +35,7 @@ const (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(vrVMX)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/vr_vqfx/vr-vqfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
kindnames = []string{"juniper_vqfx", "vr-vqfx", "vr-juniper_vqfx"}
KindNames = []string{"juniper_vqfx", "vr-vqfx", "vr-juniper_vqfx"}
defaultCredentials = nodes.NewCredentials("admin", "admin@123")

InterfaceRegexp = regexp.MustCompile(`(?:et|xe|ge)-0/0/(?P<port>\d+)$`)
Expand All @@ -35,7 +35,7 @@ const (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(vrVQFX)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/vr_vsrx/vr-vsrx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
kindnames = []string{"juniper_vsrx", "vr-vsrx", "vr-juniper_vsrx"}
KindNames = []string{"juniper_vsrx", "vr-vsrx", "vr-juniper_vsrx"}
defaultCredentials = nodes.NewCredentials("admin", "admin@123")

InterfaceRegexp = regexp.MustCompile(`(?:et|xe|ge)-0/0/(?P<port>\d+)$`)
Expand All @@ -35,7 +35,7 @@ const (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(vrVSRX)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/vr_xrv9k/vr-xrv9k.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var (
kindnames = []string{"cisco_xrv9k", "vr-xrv9k", "vr-cisco_xrv9k"}
KindNames = []string{"cisco_xrv9k", "vr-xrv9k", "vr-cisco_xrv9k"}
defaultCredentials = nodes.NewCredentials("clab", "clab@123")

InterfaceRegexp = regexp.MustCompile(`(?:Gi|GigabitEthernet|Te|TenGigE|TenGigabitEthernet)\s?0/0/0/(?P<port>\d+)`)
Expand All @@ -35,7 +35,7 @@ const (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(vrXRV9K)
}, defaultCredentials)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/xrd/xrd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

var (
kindnames = []string{"xrd", "cisco_xrd"}
KindNames = []string{"xrd", "cisco_xrd"}
defaultCredentials = nodes.NewCredentials("clab", "clab@123")
xrdEnv = map[string]string{
"XR_FIRST_BOOT_CONFIG": "/etc/xrd/first-boot.cfg",
Expand All @@ -49,7 +49,7 @@ const (

// Register registers the node in the NodeRegistry.
func Register(r *nodes.NodeRegistry) {
r.Register(kindnames, func() nodes.Node {
r.Register(KindNames, func() nodes.Node {
return new(xrd)
}, defaultCredentials)
}
Expand Down

0 comments on commit 51c6366

Please sign in to comment.