Skip to content

Commit

Permalink
removed subshelling for link creation (#209)
Browse files Browse the repository at this point in the history
* removed subshelling for link creation

* optimized netns symlink creation

* removed debug messages

* addressed review comments

* combined createveth functions

* improved logging messages
  • Loading branch information
hellt committed Jan 5, 2021
1 parent 5c4e0c8 commit 78ac07f
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 146 deletions.
8 changes: 7 additions & 1 deletion clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
dockerNetIPv4Addr = "172.20.20.0/24"
dockerNetIPv6Addr = "2001:172:20:20::/80"
dockerNetMTU = "1450"
srlDefaultType = "ixr6"
srlDefaultType = "ixr6"
)

// supported kinds
Expand Down Expand Up @@ -119,12 +119,14 @@ type Node struct {
TLSCert string
TLSKey string
TLSAnchor string
NSPath string // network namespace path for this node
}

// Link is a struct that contains the information of a link between 2 containers
type Link struct {
A *Endpoint
B *Endpoint
MTU int
Labels map[string]string
}

Expand Down Expand Up @@ -470,6 +472,10 @@ func (c *cLab) NewLink(l LinkConfig) *Link {
link := new(Link)
link.Labels = l.Labels

if link.MTU <= 0 {
link.MTU = 1500
}

for i, d := range l.Endpoints {
// i indicates the number and d presents the string, which need to be
// split in node and endpoint name
Expand Down
16 changes: 8 additions & 8 deletions clab/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ const sysctlBase = "/proc/sys"

// CreateBridge creates a docker bridge
func (c *cLab) CreateBridge(ctx context.Context) (err error) {
log.Info("Creating docker bridge")
log.Debugf("Creating docker bridge: Name: %s, IPv4Subnet: '%s', IPv6Subnet: '%s", c.Config.Mgmt.Network, c.Config.Mgmt.IPv4Subnet, c.Config.Mgmt.IPv6Subnet)
log.Infof("Creating docker network: Name='%s', IPv4Subnet='%s', IPv6Subnet='%s'", c.Config.Mgmt.Network, c.Config.Mgmt.IPv4Subnet, c.Config.Mgmt.IPv6Subnet)

enableIPv6 := false
var ipamConfig []network.IPAMConfig
Expand Down Expand Up @@ -144,7 +143,7 @@ func (c *cLab) DeleteBridge(ctx context.Context) (err error) {

// CreateContainer creates a docker container
func (c *cLab) CreateContainer(ctx context.Context, node *Node) (err error) {
log.Infof("Create container: %s", node.ShortName)
log.Infof("Creating container: %s", node.ShortName)

err = c.PullImageIfRequired(ctx, node.Image)
if err != nil {
Expand Down Expand Up @@ -212,7 +211,8 @@ func (c *cLab) CreateContainer(ctx context.Context, node *Node) (err error) {
if err != nil {
return err
}
return linkContainerNS(cJSON.State.Pid, node.LongName)
node.NSPath = "/proc/" + strconv.Itoa(cJSON.State.Pid) + "/ns/net"
return linkContainerNS(node.NSPath, node.LongName)
}

func (c *cLab) PullImageIfRequired(ctx context.Context, imageName string) error {
Expand Down Expand Up @@ -345,13 +345,14 @@ func (c *cLab) DeleteContainer(ctx context.Context, name string) error {
var err error
force := !c.gracefulShutdown
if c.gracefulShutdown {
log.Infof("Stopping container: %s", name)
err = c.DockerClient.ContainerStop(ctx, name, &c.timeout)
if err != nil {
log.Errorf("could not stop container '%s': %v", name, err)
force = true
}
}
log.Infof("Removing container: %s", name)
log.Debugf("Removing container: %s", name)
err = c.DockerClient.ContainerRemove(ctx, name, types.ContainerRemoveOptions{Force: force})
if err != nil {
return err
Expand All @@ -362,11 +363,10 @@ func (c *cLab) DeleteContainer(ctx context.Context, name string) error {

// linkContainerNS creates a symlink for containers network namespace
// so that it can be managed by iproute2 utility
func linkContainerNS(pid int, containerName string) error {
func linkContainerNS(nspath string, containerName string) error {
CreateDirectory("/run/netns/", 0755)
src := "/proc/" + strconv.Itoa(pid) + "/ns/net"
dst := "/run/netns/" + containerName
err := os.Symlink(src, dst)
err := os.Symlink(nspath, dst)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion clab/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (c *cLab) CreateNodeDirStructure(node *Node) (err error) {

switch node.Kind {
case "srl":
log.Infof("Create directory structure for SRL container: %s", node.ShortName)
log.Debugf("Creating directory structure for SRL container: %s", node.ShortName)
var src string
var dst string

Expand Down
Loading

0 comments on commit 78ac07f

Please sign in to comment.