Skip to content

Commit

Permalink
Show containers on detail network
Browse files Browse the repository at this point in the history
  • Loading branch information
ernesto27 committed Jul 1, 2023
1 parent 3582e7e commit 2b14c31
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 14 deletions.
46 changes: 33 additions & 13 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type MyNetwork struct {
Gateway string
Subnet string
Resource types.NetworkResource
Containers []types.ContainerJSON
Containers []MyContainer
}

type MyContainer struct {
Expand Down Expand Up @@ -145,19 +145,12 @@ func (d *Docker) ContainerList() ([]MyContainer, error) {
continue
}

var name string
if len(c.Names) > 0 {
name = c.Names[0][1:]
}

name := d.getContainerName(c.Names)
networkSettings := cJSON.NetworkSettings
networkMode := string(cJSON.HostConfig.NetworkMode)

defaultNetwork := "default"
ipAddress := networkSettings.IPAddress
if networkMode != defaultNetwork {
ipAddress = networkSettings.Networks[networkMode].IPAddress
}
ipAddress := d.getContainerIP(cJSON)

gateway := networkSettings.Gateway
if networkMode != defaultNetwork {
Expand Down Expand Up @@ -190,6 +183,13 @@ func (d *Docker) ContainerList() ([]MyContainer, error) {
return mc, nil
}

func (d *Docker) getContainerName(names []string) string {
if len(names) > 0 {
return names[0][1:]
}
return ""
}

func (d *Docker) GetContainerByName(name string) (MyContainer, error) {
for _, c := range d.Containers {
if c.Name == name {
Expand Down Expand Up @@ -340,10 +340,18 @@ func (d *Docker) NetworkList() ([]MyNetwork, error) {
fmt.Println(err)
}

var containers []types.ContainerJSON
containers := []MyContainer{}
for containerID := range network.Containers {
container, _ := d.cli.ContainerInspect(context.Background(), containerID)
containers = append(containers, container)
container, _, err := d.cli.ContainerInspectWithRaw(context.Background(), containerID, true)
if err == nil {
n := MyNetwork{
IPAddress: d.getContainerIP(container),
}
containers = append(containers, MyContainer{
Name: d.getContainerName([]string{container.Name}),
Network: n,
})
}
}

myNetwork = append(myNetwork, MyNetwork{
Expand All @@ -359,6 +367,18 @@ func (d *Docker) NetworkList() ([]MyNetwork, error) {
return myNetwork, nil
}

func (d *Docker) getContainerIP(c types.ContainerJSON) string {
networkSettings := c.NetworkSettings
networkMode := string(c.HostConfig.NetworkMode)

defaultNetwork := "default"
ipAddress := networkSettings.IPAddress
if networkMode != defaultNetwork {
ipAddress = networkSettings.Networks[networkMode].IPAddress
}
return ipAddress
}

func (d *Docker) GetNetworkByName(name string) (MyNetwork, error) {
for _, n := range d.Networks {
if n.Resource.Name == name {
Expand Down
106 changes: 106 additions & 0 deletions docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package docker

import (
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
)

func TestGetContainerIP(t *testing.T) {
type args struct {
container types.ContainerJSON
}
tests := []struct {
name string
args args
want string
}{
{
name: "should get container ip",
args: args{
container: types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
HostConfig: &container.HostConfig{
NetworkMode: "bridge",
},
},
NetworkSettings: &types.NetworkSettings{
Networks: map[string]*network.EndpointSettings{
"bridge": {
IPAddress: "172.0.0.10",
},
},
},
},
},
want: "172.0.0.10",
},
{
name: "should get empty string if network mode is default",
args: args{
container: types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
HostConfig: &container.HostConfig{
NetworkMode: "default",
},
},
NetworkSettings: &types.NetworkSettings{
Networks: map[string]*network.EndpointSettings{
"default": {
IPAddress: "",
},
},
},
},
},
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := Docker{}
if got := d.getContainerIP(tt.args.container); got != tt.want {
t.Errorf("getContainerIP() = got%v, want %v", got, tt.want)
}
})
}
}

func TestGetContainerName(t *testing.T) {
type args struct {
names []string
}

tests := []struct {
name string
args args
want string
}{
{
name: "should get container name without slash",
args: args{
names: []string{"/container_name"},
},
want: "/container_name",
},
{
name: "should get empty string args is empty",
args: args{
names: []string{},
},
want: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := Docker{}
if got := d.getContainerName(tt.args.names); got != tt.want {
t.Errorf("getContainerName() = got %v, want %v", got, tt.want)
}
})
}
}
13 changes: 12 additions & 1 deletion models/network_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@ func getContentNetwork(network docker.MyNetwork) string {
{"Gateway", network.Gateway},
})

response += "# Containers\n" + network.Containers[0].Name
if len(network.Containers) > 0 {
columns := []string{"Name", "IPv4 Address"}

rows := [][]string{}
for _, container := range network.Containers {
rows = append(rows, []string{container.Name, container.Network.IPAddress})
}

response += "\n\n"
response += utils.CreateTable("# Containers", columns, rows)

}

return response
}

0 comments on commit 2b14c31

Please sign in to comment.