Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move topology struct and config items to types package #461

Merged
merged 9 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 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,18 +52,16 @@ 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 (c *CLab) initCeosNode(nodeCfg *types.NodeConfig) error {
var err error

// initialize the global parameters with defaults, can be overwritten later
node.Config, err = c.configInit(&nodeCfg, node.Kind)
c.Config.Topology.GetNodeConfig(nodeCfg.ShortName)
hellt marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
node.Image = c.imageInitialization(&nodeCfg, node.Kind)
node.Position = c.positionInitialization(&nodeCfg, node.Kind)

// initialize specific container information
if nodeCfg.Config == "" {
nodeCfg.Config = defaultConfigTemplates[nodeCfg.Kind]
}

// defined env vars for the ceos
kindEnv := map[string]string{
Expand All @@ -74,32 +72,27 @@ func initCeosNode(c *CLab, nodeCfg NodeConfig, node *types.Node, user string, en
"SKIP_ZEROTOUCH_BARRIER_IN_SYSDBINIT": "1",
"INTFTYPE": "eth",
"MAPETH0": "1",
"MGMT_INTF": "eth0"}
node.Env = mergeStringMaps(kindEnv, envs)
"MGMT_INTF": "eth0",
}
nodeCfg.Env = utils.MergeStringMaps(kindEnv, nodeCfg.Env)

// 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()

node.User = user
node.Group = c.groupInitialization(&nodeCfg, node.Kind)
node.NodeType = nodeCfg.Type

node.MacAddress = genMac("00:1c:73")
nodeCfg.Cmd = envSb.String()
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.Sprintf("%s:/mnt/flash/", cfgPath))

return err
return nil
}

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 All @@ -111,6 +104,6 @@ func (c *CLab) createCEOSFiles(node *types.Node) error {
return err
}
m[5] = m[5] + 1
createFile(path.Join(node.LabDir, "flash", "system_mac_address"), m.String())
utils.CreateFile(path.Join(node.LabDir, "flash", "system_mac_address"), m.String())
return nil
}
8 changes: 4 additions & 4 deletions 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 Expand Up @@ -230,9 +230,9 @@ func (c *CLab) RetrieveNodeCertData(n *types.Node) (*Certificates, error) {
}

func (c *CLab) writeCertFiles(certs *Certificates, filesPrefix string) {
createFile(filesPrefix+".pem", string(certs.Cert))
createFile(filesPrefix+"-key.pem", string(certs.Key))
createFile(filesPrefix+".csr", string(certs.Csr))
utils.CreateFile(filesPrefix+".pem", string(certs.Cert))
utils.CreateFile(filesPrefix+"-key.pem", string(certs.Key))
utils.CreateFile(filesPrefix+".csr", string(certs.Csr))
}

//CreateRootCA creates RootCA key/certificate if it is needed by the topology
Expand Down
21 changes: 11 additions & 10 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,21 +214,21 @@ 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) {
defer wg.Done()
for {
select {
case node := <-nodesChan:
if node == nil {
case node, ok := <-nodesChan:
if node == nil || !ok {
log.Debugf("Worker %d terminating...", i)
return
}
log.Debugf("Worker %d received node: %+v", i, node)
if node.Kind == "bridge" || node.Kind == "ovs-bridge" {
return
continue
}

var nodeCerts *Certificates
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