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

populate bridgeName if inspect cmd is run with --all or --name #433

Merged
merged 2 commits into from
May 28, 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
1 change: 1 addition & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
filters: |
code:
- 'clab/**'
- 'runtime/**'
- 'cmd/**'
- 'tests/**'
- '.github/workflows/cicd.yml'
Expand Down
35 changes: 29 additions & 6 deletions runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,28 +346,41 @@ func (c *DockerRuntime) StartContainer(ctx context.Context, id string) error {

// ListContainers lists all containers with labels []string
func (c *DockerRuntime) ListContainers(ctx context.Context, labels []string) ([]types.GenericContainer, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
nctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
filter := filters.NewArgs()
for _, l := range labels {
filter.Add("label", l)
}
ctrs, err := c.Client.ContainerList(ctx, dockerTypes.ContainerListOptions{
ctrs, err := c.Client.ContainerList(nctx, dockerTypes.ContainerListOptions{
All: true,
Filters: filter,
})
if err != nil {
return nil, err
}
var nr []dockerTypes.NetworkResource
if c.Mgmt.Network == "" {
netFilter := filters.NewArgs()
netFilter.Add("label", "containerlab")
nctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()

return c.produceGenericContainerList(ctrs)
nr, err = c.Client.NetworkList(nctx, dockerTypes.NetworkListOptions{
Filters: netFilter,
})
if err != nil {
return nil, err
}
}
return c.produceGenericContainerList(ctrs, nr)
}

// Transform docker-specific to generic container format
func (c *DockerRuntime) produceGenericContainerList(input []dockerTypes.Container) ([]types.GenericContainer, error) {
func (c *DockerRuntime) produceGenericContainerList(inputContainers []dockerTypes.Container, inputNetworkRessources []dockerTypes.NetworkResource) ([]types.GenericContainer, error) {
var result []types.GenericContainer

for _, i := range input {
for _, i := range inputContainers {
ctr := types.GenericContainer{
Names: i.Names,
ID: i.ID,
Expand All @@ -379,7 +392,17 @@ func (c *DockerRuntime) produceGenericContainerList(input []dockerTypes.Containe
Set: false,
},
}
if ifcfg, ok := i.NetworkSettings.Networks[c.Mgmt.Network]; ok {
bridgeName := c.Mgmt.Network
// if bridgeName is "", try to find a network created by clab that the container is connected to
if bridgeName == "" && inputNetworkRessources != nil {
for _, nr := range inputNetworkRessources {
if _, ok := i.NetworkSettings.Networks[nr.Name]; ok {
bridgeName = nr.Name
break
}
}
}
if ifcfg, ok := i.NetworkSettings.Networks[bridgeName]; ok {
ctr.NetworkSettings.IPv4addr = ifcfg.IPAddress
ctr.NetworkSettings.IPv4pLen = ifcfg.IPPrefixLen
ctr.NetworkSettings.IPv6addr = ifcfg.GlobalIPv6Address
Expand Down
17 changes: 17 additions & 0 deletions tests/01-smoke/01-basic-flow.robot
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*** Settings ***
Library OperatingSystem
Library String
Suite Teardown Run sudo containerlab destroy -t ${CURDIR}/01-linux-nodes.clab.yml --cleanup

*** Variables ***
Expand Down Expand Up @@ -43,6 +44,22 @@ Verify links in node l2
Should Be Equal As Integers ${rc} 0
Should Contain ${output} state UP

Ensure "inspect all" outputs IP addresses
${rc} ${output} = Run And Return Rc And Output
... sudo containerlab inspect --all
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}[10]
Should Match Regexp ${ipv4} ^[\\d\\.]+/\\d{1,2}$
# verify ipv6 address
${ipv6} = String.Strip String ${data}[11]
Should Match Regexp ${ipv6} ^[\\d:]+/\\d{1,2}$

Destroy ${lab-name} lab
${rc} ${output} = Run And Return Rc And Output
... sudo containerlab destroy -t ${CURDIR}/01-linux-nodes.clab.yml --cleanup
Expand Down