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

fixing #456 #458

Merged
merged 3 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion runtime/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {

func (c *ContainerdRuntime) Init(opts ...runtime.RuntimeOption) error {
var err error
log.Info("Runtime: containerd")
log.Debug("Runtime: containerd")
c.client, err = containerd.New("/run/containerd/containerd.sock")
if err != nil {
return err
Expand Down
24 changes: 19 additions & 5 deletions runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type DockerRuntime struct {

func (c *DockerRuntime) Init(opts ...runtime.RuntimeOption) error {
var err error
log.Info("Runtime: Docker")
log.Debug("Runtime: Docker")
c.Client, err = dockerC.NewClientWithOpts(dockerC.FromEnv, dockerC.WithAPIVersionNegotiation())
if err != nil {
return err
Expand Down Expand Up @@ -360,17 +360,31 @@ func (c *DockerRuntime) ListContainers(ctx context.Context, gfilters []*types.Ge
}
var nr []dockerTypes.NetworkResource
if c.Mgmt.Network == "" {
netFilter := filters.NewArgs()
netFilter.Add("label", "containerlab")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can create a filter that will capture both networks that have a containerlab label OR network which have bridge name

That way we will avoid getting the networks that could be created by users/other_systems and which are not needed for our purposes.

It is potentially an optimization, which is not necessary, but seems like nice to have.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be an AND operation if you add name=bridge
To do an OR, 2 queries need to be performed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, here is what I did

	if c.Mgmt.Network == "" {
		nctx, cancel := context.WithTimeout(ctx, c.timeout)
		defer cancel()
		// fetch containerlab created networks
		f := filters.NewArgs()
		f.Add("label", "containerlab")
		nr, err = c.Client.NetworkList(nctx, dockerTypes.NetworkListOptions{
			Filters: f,
		})

		if err != nil {
			return nil, err
		}

		// fetch default bridge network
		f = filters.NewArgs()
		f.Add("name", "bridge")
		bridgenet, err := c.Client.NetworkList(nctx, dockerTypes.NetworkListOptions{
			Filters: f,
		})

		if err != nil {
			return nil, err
		}

		nr = append(nr, bridgenet...)

not sure which is better though =)

nctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()

// fetch containerlab created networks
f := filters.NewArgs()
f.Add("label", "containerlab")
nr, err = c.Client.NetworkList(nctx, dockerTypes.NetworkListOptions{
Filters: netFilter,
Filters: f,
})

if err != nil {
return nil, err
}

// fetch default bridge network
f = filters.NewArgs()
f.Add("name", "bridge")
bridgenet, err := c.Client.NetworkList(nctx, dockerTypes.NetworkListOptions{
Filters: f,
})

if err != nil {
return nil, err
}

nr = append(nr, bridgenet...)
}
return c.produceGenericContainerList(ctrs, nr)
}
Expand Down
15 changes: 15 additions & 0 deletions tests/01-smoke/05-docker-bridge.clab.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2020 Nokia
# Licensed under the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause

name: 05-docker-bridge
# use the default docker network named "bridge"
mgmt:
network: bridge

topology:
nodes:
l1:
kind: linux
image: alpine:3
cmd: ash -c "sleep 9999"
34 changes: 34 additions & 0 deletions tests/01-smoke/05-docker-bridge.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
*** Settings ***
Library OperatingSystem
Library String
Suite Teardown Cleanup

*** Variables ***
${lab-name} 05-docker-bridge
${lab-file} 05-docker-bridge.clab.yml
${runtime} docker

*** Test Cases ***
Deploy ${lab-name} lab
${rc} ${output} = Run And Return Rc And Output
... sudo containerlab --runtime ${runtime} deploy -t ${CURDIR}/${lab-file}
Log ${output}
Should Be Equal As Integers ${rc} 0

Ensure inspect outputs IP addresses
${rc} ${output} = Run And Return Rc And Output
... sudo containerlab --runtime ${runtime} inspect -n ${lab-name}
Log ${output}
Should Be Equal As Integers ${rc} 0
${line} = String.Get Line ${output} -2
Log ${line}
@{data} = Split String ${line} |
Log ${data}
# verify ipv4 address
${ipv4} = String.Strip String ${data}[8]
Should Match Regexp ${ipv4} ^[\\d\\.]+/\\d{1,2}$

*** Keywords ***
Cleanup
${rc} ${output} = Run And Return Rc And Output sudo containerlab --runtime ${runtime} destroy -t ${CURDIR}/${lab-file} --cleanup
Log ${output}