Skip to content

Commit

Permalink
populate bridgeName if inspect cmd is run with --all or --name
Browse files Browse the repository at this point in the history
  • Loading branch information
karimra committed May 28, 2021
1 parent fedc24e commit 62e901c
Showing 1 changed file with 29 additions and 6 deletions.
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

0 comments on commit 62e901c

Please sign in to comment.