Skip to content

Commit

Permalink
Merge pull request #2 from steiler/lateinitmgmtbrNode
Browse files Browse the repository at this point in the history
delay initilaisation of the mgmt-net bridge name
  • Loading branch information
steiler committed Aug 7, 2023
2 parents 3ad9578 + ebb1ee0 commit 6fdeead
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func (c *CLab) ResolveLinks() error {
// add the virtual host and mgmt-bridge nodes to the resolve nodes
specialNodes := map[string]links.Node{
"host": links.GetFakeHostLinkNode(),
"mgmt-net": links.GetFakeMgmtBrLinkNode(c.Config.Mgmt.Bridge),
"mgmt-net": links.GetFakeMgmtBrLinkNode(),
}
for _, n := range specialNodes {
resolveNodes[n.GetShortName()] = n
Expand Down
10 changes: 8 additions & 2 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/clab/dependency_manager"
"github.com/srl-labs/containerlab/clab/exec"
"github.com/srl-labs/containerlab/links"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/utils"
Expand Down Expand Up @@ -111,15 +112,15 @@ func deployFn(_ *cobra.Command, _ []string) error {

opts := []clab.ClabOption{
clab.WithTimeout(timeout),
clab.WithTopoFile(topo, varsFile),
clab.WithNodeFilter(nodeFilter),
clab.WithRuntime(rt,
&runtime.RuntimeConfig{
Debug: debug,
Timeout: timeout,
GracefulShutdown: graceful,
},
),
clab.WithTopoFile(topo, varsFile),
clab.WithNodeFilter(nodeFilter),
clab.WithDebug(debug),
}
c, err := clab.NewContainerLab(opts...)
Expand Down Expand Up @@ -154,6 +155,11 @@ func deployFn(_ *cobra.Command, _ []string) error {
}
}

err = links.SetMgmtNetUnderlayingBridge(c.Config.Mgmt.Bridge)
if err != nil {
return err
}

if err = c.CheckTopologyDefinition(ctx); err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/labels"
"github.com/srl-labs/containerlab/links"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/runtime/ignite"
"github.com/srl-labs/containerlab/types"
Expand Down Expand Up @@ -98,15 +99,15 @@ func destroyFn(_ *cobra.Command, _ []string) error {
for topo := range topos {
opts := []clab.ClabOption{
clab.WithTimeout(timeout),
clab.WithTopoFile(topo, varsFile),
clab.WithNodeFilter(nodeFilter),
clab.WithRuntime(rt,
&runtime.RuntimeConfig{
Debug: debug,
Timeout: timeout,
GracefulShutdown: graceful,
},
),
clab.WithTopoFile(topo, varsFile),
clab.WithNodeFilter(nodeFilter),
clab.WithDebug(debug),
}

Expand All @@ -120,6 +121,11 @@ func destroyFn(_ *cobra.Command, _ []string) error {
return err
}

err = links.SetMgmtNetUnderlayingBridge(nc.Config.Mgmt.Bridge)
if err != nil {
return err
}

// create management network or use existing one
// we call this to populate the nc.cfg.mgmt.bridge variable
// which is needed for the removal of the iptables rules
Expand Down
10 changes: 8 additions & 2 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/srl-labs/containerlab/clab"
"github.com/srl-labs/containerlab/clab/exec"
"github.com/srl-labs/containerlab/labels"
"github.com/srl-labs/containerlab/links"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/types"
)
Expand Down Expand Up @@ -43,22 +44,27 @@ func execFn(_ *cobra.Command, _ []string) error {

opts := []clab.ClabOption{
clab.WithTimeout(timeout),
clab.WithTopoFile(topo, varsFile),
clab.WithNodeFilter(nodeFilter),
clab.WithRuntime(rt,
&runtime.RuntimeConfig{
Debug: debug,
Timeout: timeout,
GracefulShutdown: graceful,
},
),
clab.WithTopoFile(topo, varsFile),
clab.WithNodeFilter(nodeFilter),
clab.WithDebug(debug),
}
c, err := clab.NewContainerLab(opts...)
if err != nil {
return err
}

err = links.SetMgmtNetUnderlayingBridge(c.Config.Mgmt.Bridge)
if err != nil {
return err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
10 changes: 8 additions & 2 deletions cmd/save.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/links"
"github.com/srl-labs/containerlab/nodes"
"github.com/srl-labs/containerlab/runtime"
)
Expand All @@ -29,22 +30,27 @@ Refer to the https://containerlab.dev/cmd/save/ documentation to see the exact c
}
opts := []clab.ClabOption{
clab.WithTimeout(timeout),
clab.WithTopoFile(topo, varsFile),
clab.WithNodeFilter(nodeFilter),
clab.WithRuntime(rt,
&runtime.RuntimeConfig{
Debug: debug,
Timeout: timeout,
GracefulShutdown: graceful,
},
),
clab.WithTopoFile(topo, varsFile),
clab.WithNodeFilter(nodeFilter),
clab.WithDebug(debug),
}
c, err := clab.NewContainerLab(opts...)
if err != nil {
return err
}

err = links.SetMgmtNetUnderlayingBridge(c.Config.Mgmt.Bridge)
if err != nil {
return err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
12 changes: 5 additions & 7 deletions links/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (er *EndpointRaw) Resolve(params *ResolveParams, l Link) (Endpoint, error)
case LinkEndpointTypeBridge:
e = &EndpointBridge{
EndpointGeneric: *genericEndpoint,
masterInterface: node.GetShortName(),
}
case LinkEndpointTypeHost:
e = &EndpointHost{
Expand Down Expand Up @@ -164,7 +163,6 @@ type Endpoint interface {

type EndpointBridge struct {
EndpointGeneric
masterInterface string
}

func (e *EndpointBridge) Verify(_ []Endpoint) error {
Expand All @@ -173,7 +171,7 @@ func (e *EndpointBridge) Verify(_ []Endpoint) error {
if err != nil {
errs = append(errs, err)
}
err = CheckBridgeExists(e.GetNode(), e.masterInterface)
err = CheckBridgeExists(e.GetNode())
if err != nil {
errs = append(errs, err)
}
Expand Down Expand Up @@ -254,17 +252,17 @@ func CheckEndptExists(e Endpoint) error {

// CheckBridgeExists verifies that the given bridge is present in the
// netnwork namespace referenced via the provided nspath handle
func CheckBridgeExists(n Node, brName string) error {
func CheckBridgeExists(n Node) error {
return n.ExecFunction(func(_ ns.NetNS) error {
br, err := netlink.LinkByName(brName)
br, err := netlink.LinkByName(n.GetShortName())
_, notfound := err.(netlink.LinkNotFoundError)
switch {
case notfound:
return fmt.Errorf("bridge %q referenced in topology but does not exist", brName)
return fmt.Errorf("bridge %q referenced in topology but does not exist", n.GetShortName())
case err != nil:
return err
case br.Type() != "bridge":
return fmt.Errorf("interface %s found. expected type \"bridge\", actual is %q", brName, br.Type())
return fmt.Errorf("interface %s found. expected type \"bridge\", actual is %q", n.GetShortName(), br.Type())
}
return nil
})
Expand Down
17 changes: 13 additions & 4 deletions links/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ type ResolveParams struct {
MgmtBridgeName string
}

var _fakeHostLinkNodeInstance Node
var _fakeHostLinkNodeInstance *fakeHostLinkNode

type fakeHostLinkNode struct {
GenericLinkNode
Expand Down Expand Up @@ -364,7 +364,7 @@ func GetFakeHostLinkNode() Node {
return _fakeHostLinkNodeInstance
}

var _fakeMgmtBrLinkMgmtBrInstance Node
var _fakeMgmtBrLinkMgmtBrInstance *fakeMgmtBridgeLinkNode

type fakeMgmtBridgeLinkNode struct {
GenericLinkNode
Expand All @@ -374,7 +374,7 @@ func (*fakeMgmtBridgeLinkNode) GetLinkEndpointType() LinkEndpointType {
return LinkEndpointTypeBridge
}

func GetFakeMgmtBrLinkNode(mgmtBridgeName string) Node {
func getFakeMgmtBrLinkNode() *fakeMgmtBridgeLinkNode {
if _fakeMgmtBrLinkMgmtBrInstance == nil {
currns, err := ns.GetCurrentNS()
if err != nil {
Expand All @@ -383,11 +383,20 @@ func GetFakeMgmtBrLinkNode(mgmtBridgeName string) Node {
nspath := currns.Path()
_fakeMgmtBrLinkMgmtBrInstance = &fakeMgmtBridgeLinkNode{
GenericLinkNode: GenericLinkNode{
shortname: mgmtBridgeName,
shortname: "TBD",
endpoints: []Endpoint{},
nspath: nspath,
},
}
}
return _fakeMgmtBrLinkMgmtBrInstance
}

func GetFakeMgmtBrLinkNode() Node {
return getFakeMgmtBrLinkNode()
}

func SetMgmtNetUnderlayingBridge(bridge string) error {
getFakeMgmtBrLinkNode().GenericLinkNode.shortname = bridge
return nil
}
3 changes: 1 addition & 2 deletions links/link_mgmt-net.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *LinkMgmtNetRaw) Resolve(params *ResolveParams) (Link, error) {
LinkCommonParams: r.LinkCommonParams,
}

fakeMgmtBridgeNode := GetFakeMgmtBrLinkNode(params.MgmtBridgeName)
fakeMgmtBridgeNode := GetFakeMgmtBrLinkNode()

bridgeEp := &EndpointBridge{
EndpointGeneric: EndpointGeneric{
Expand All @@ -42,7 +42,6 @@ func (r *LinkMgmtNetRaw) Resolve(params *ResolveParams) (Link, error) {
IfaceName: r.HostInterface,
Link: link,
},
masterInterface: params.MgmtBridgeName,
}

// add endpoint to fake mgmt bridge node
Expand Down

0 comments on commit 6fdeead

Please sign in to comment.