Skip to content

Commit

Permalink
move topology struct and config items to types package
Browse files Browse the repository at this point in the history
  • Loading branch information
karimra committed Jun 17, 2021
1 parent 417a7b9 commit 026e4c0
Show file tree
Hide file tree
Showing 31 changed files with 784 additions and 592 deletions.
34 changes: 19 additions & 15 deletions clab/ceos.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/srl-labs/containerlab/utils"
)

func ceosPostDeploy(ctx context.Context, c *CLab, node *types.Node, lworkers uint) error {
func ceosPostDeploy(ctx context.Context, c *CLab, node *types.NodeConfig, lworkers uint) error {
// regenerate ceos config since it is now known which IP address docker assigned to this container
err := node.GenerateConfig(node.ResConfig, defaultConfigTemplates[node.Kind])
if err != nil {
Expand Down Expand Up @@ -52,16 +52,19 @@ func ceosPostDeploy(ctx context.Context, c *CLab, node *types.Node, lworkers uin
return err
}

func initCeosNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, envs map[string]string) error {
func initCeosNode(c *CLab, nodeDef *types.NodeDefinition, nodeCfg *types.NodeConfig, user string, envs map[string]string) error {
var err error

// initialize the global parameters with defaults, can be overwritten later
node.Config, err = c.configInit(&nodeCfg, node.Kind)
// node.Config, err = c.configInit(nodeCfg, node.Kind)
c.Config.Topology.GetNodeConfig(nodeCfg.ShortName)
if err != nil {
return err
}
node.Image = c.imageInitialization(&nodeCfg, node.Kind)
node.Position = c.positionInitialization(&nodeCfg, node.Kind)
// nodeCfg.Image = c.imageInitialization(nodeDef, nodeCfg.Kind)
nodeCfg.Image = c.Config.Topology.GetNodeImage(nodeCfg.ShortName)
// nodeCfg.Position = c.positionInitialization(nodeDef, nodeCfg.Kind)
nodeCfg.Position = c.Config.Topology.GetNodePosition(nodeCfg.ShortName)

// initialize specific container information

Expand All @@ -75,31 +78,32 @@ func initCeosNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, en
"INTFTYPE": "eth",
"MAPETH0": "1",
"MGMT_INTF": "eth0"}
node.Env = mergeStringMaps(kindEnv, envs)
nodeCfg.Env = utils.MergeStringMaps(kindEnv, envs)

// the node.Cmd should be aligned with the environment.
var envSb strings.Builder
envSb.WriteString("/sbin/init ")
for k, v := range node.Env {
for k, v := range nodeCfg.Env {
envSb.WriteString("systemd.setenv=" + k + "=" + v + " ")

}
node.Cmd = envSb.String()
nodeCfg.Cmd = envSb.String()

node.User = user
node.Group = c.groupInitialization(&nodeCfg, node.Kind)
node.NodeType = nodeCfg.Type
nodeCfg.User = user
// nodeCfg.Group = c.groupInitialization(nodeDef, nodeCfg.Kind)
nodeCfg.Group = c.Config.Topology.GetNodeGroup(nodeCfg.ShortName)
nodeCfg.NodeType = nodeDef.Type

node.MacAddress = genMac("00:1c:73")
nodeCfg.MacAddress = genMac("00:1c:73")

// mount config dir
cfgPath := filepath.Join(node.LabDir, "flash")
node.Binds = append(node.Binds, fmt.Sprint(cfgPath, ":/mnt/flash/"))
cfgPath := filepath.Join(nodeCfg.LabDir, "flash")
nodeCfg.Binds = append(nodeCfg.Binds, fmt.Sprint(cfgPath, ":/mnt/flash/"))

return err
}

func (c *CLab) createCEOSFiles(node *types.Node) error {
func (c *CLab) createCEOSFiles(node *types.NodeConfig) error {
// generate config directory
utils.CreateDirectory(path.Join(node.LabDir, "flash"), 0777)
cfg := path.Join(node.LabDir, "flash", "startup-config")
Expand Down
2 changes: 1 addition & 1 deletion clab/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (c *CLab) GenerateCert(ca string, caKey string, csrJSONTpl *template.Templa

// RetrieveNodeCertData reads the node private key and certificate by the well known paths
// if either of those files doesn't exist, an error is returned
func (c *CLab) RetrieveNodeCertData(n *types.Node) (*Certificates, error) {
func (c *CLab) RetrieveNodeCertData(n *types.NodeConfig) (*Certificates, error) {
var nodeCertFilesDir = path.Join(c.Dir.LabCA, n.ShortName)
var nodeCertFile = path.Join(nodeCertFilesDir, n.ShortName+".pem")
var nodeKeyFile = path.Join(nodeCertFilesDir, n.ShortName+"-key.pem")
Expand Down
15 changes: 8 additions & 7 deletions clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type CLab struct {
Config *Config
TopoFile *TopoFile
m *sync.RWMutex
Nodes map[string]*types.Node
Nodes map[string]*types.NodeConfig
Links map[int]*types.Link
Runtime runtime.ContainerRuntime
Dir *Directory
Expand Down Expand Up @@ -115,11 +115,12 @@ func WithGracefulShutdown(gracefulShutdown bool) ClabOption {
func NewContainerLab(opts ...ClabOption) *CLab {
c := &CLab{
Config: &Config{
Mgmt: new(types.MgmtNet),
Mgmt: new(types.MgmtNet),
Topology: types.NewTopology(),
},
TopoFile: new(TopoFile),
m: new(sync.RWMutex),
Nodes: make(map[string]*types.Node),
Nodes: make(map[string]*types.NodeConfig),
Links: make(map[int]*types.Link),
}

Expand Down Expand Up @@ -152,7 +153,7 @@ func (c *CLab) initMgmtNetwork() error {
return nil
}

func (c *CLab) CreateNode(ctx context.Context, node *types.Node, certs *Certificates) error {
func (c *CLab) CreateNode(ctx context.Context, node *types.NodeConfig, certs *Certificates) error {
if certs != nil {
c.m.Lock()
node.TLSCert = string(certs.Cert)
Expand All @@ -167,7 +168,7 @@ func (c *CLab) CreateNode(ctx context.Context, node *types.Node, certs *Certific
}

// ExecPostDeployTasks executes tasks that some nodes might require to boot properly after start
func (c *CLab) ExecPostDeployTasks(ctx context.Context, node *types.Node, lworkers uint) error {
func (c *CLab) ExecPostDeployTasks(ctx context.Context, node *types.NodeConfig, lworkers uint) error {
switch node.Kind {
case "ceos":
log.Debugf("Running postdeploy actions for Arista cEOS '%s' node", node.ShortName)
Expand Down Expand Up @@ -213,7 +214,7 @@ func (c *CLab) ExecPostDeployTasks(ctx context.Context, node *types.Node, lworke
func (c *CLab) CreateNodes(ctx context.Context, workers uint) {
wg := new(sync.WaitGroup)
wg.Add(int(workers))
nodesChan := make(chan *types.Node)
nodesChan := make(chan *types.NodeConfig)
// start workers
for i := uint(0); i < workers; i++ {
go func(i uint) {
Expand Down Expand Up @@ -372,7 +373,7 @@ func (c *CLab) DeleteNodes(ctx context.Context, workers uint, containers []types

}

func disableTxOffload(n *types.Node) error {
func disableTxOffload(n *types.NodeConfig) error {
// skip this if node runs in host mode
if strings.ToLower(n.NetworkMode) == "host" {
return nil
Expand Down
Loading

0 comments on commit 026e4c0

Please sign in to comment.