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

fix podman network inspect index check #12756

Merged
merged 2 commits into from
Oct 25, 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
17 changes: 9 additions & 8 deletions pkg/drivers/kic/oci/network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ func dockerNetworkInspect(name string) (netInfo, error) {
return info, nil
}

var podmanInspectGetter = func(name string) (*RunResult, error) {
cmd := exec.Command(Podman, "network", "inspect", name, "--format", `{{range .plugins}}{{if eq .type "bridge"}}{{(index (index .ipam.ranges 0) 0).subnet}},{{(index (index .ipam.ranges 0) 0).gateway}}{{end}}{{end}}`)
return runCmd(cmd)
}

func podmanNetworkInspect(name string) (netInfo, error) {
var info = netInfo{name: name}
cmd := exec.Command(Podman, "network", "inspect", name, "--format", `{{range .plugins}}{{if eq .type "bridge"}}{{(index (index .ipam.ranges 0) 0).subnet}},{{(index (index .ipam.ranges 0) 0).gateway}}{{end}}{{end}}`)
rr, err := runCmd(cmd)
rr, err := podmanInspectGetter(name)
if err != nil {
logDockerNetworkInspect(Podman, name)
if strings.Contains(rr.Output(), "no such network") {
Expand All @@ -225,18 +229,15 @@ func podmanNetworkInspect(name string) (netInfo, error) {
return info, err
}

output := rr.Stdout.String()
output := strings.TrimSpace(rr.Stdout.String())
if output == "" {
return info, fmt.Errorf("no bridge network found for %s", name)
}

// results looks like 172.17.0.0/16,172.17.0.1,1500
vals := strings.Split(strings.TrimSpace(output), ",")
if len(vals) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can still check for length zero, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed that because if the second argument of Split is non-empty, then it will always return a len of 1 or greater so the check will never be true.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah yeah i see what you're saying, we're checking for output empty above, could we check for strings.TrimSpace(output) == "" just as a precaution?

Copy link
Member Author

@spowelljr spowelljr Oct 21, 2021

Choose a reason for hiding this comment

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

Yup, will update

Done

return info, fmt.Errorf("empty list network inspect: %q", rr.Output())
}
vals := strings.Split(output, ",")

if len(vals) > 0 {
if len(vals) >= 2 {
info.gateway = net.ParseIP(vals[1])
}

Expand Down
67 changes: 64 additions & 3 deletions pkg/drivers/kic/oci/network_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,76 @@ func TestDockerInspect(t *testing.T) {
}

if netInfo.mtu != tc.mtu {
t.Errorf("Expected not to have MTU as %v but got %v", tc.mtu, netInfo.mtu)
t.Errorf("Expected MTU to be %v but got %v", tc.mtu, netInfo.mtu)
}

if !netInfo.gateway.Equal(net.ParseIP(tc.gateway)) {
t.Errorf("Expected not to have gateway as %v but got %v", tc.gateway, netInfo.gateway)
t.Errorf("Expected gateway to be %v but got %v", tc.gateway, netInfo.gateway)
}

if !netInfo.subnet.IP.Equal(net.ParseIP(tc.subnetIP)) {
t.Errorf("Expected not to have subnet as %v but got %v", tc.subnetIP, netInfo.gateway)
t.Errorf("Expected subnet to be %v but got %v", tc.subnetIP, netInfo.subnet.IP)
}
})
}
}

var podmanResponse string
var podmanInspectGetterMock = func(name string) (*RunResult, error) {
var responseInBytes bytes.Buffer
responseInBytes.WriteString(podmanResponse)
response := &RunResult{Stdout: responseInBytes}

return response, nil
}

func TestPodmanInspect(t *testing.T) {
var emptyGateway net.IP
gateway := net.ParseIP("172.17.0.1")
_, subnetIP, err := net.ParseCIDR("172.17.0.0/16")
if err != nil {
t.Fatalf("failed to parse CIDR: %v", err)
}

var tests = []struct {
name string
podmanInspectResponse string
gateway net.IP
subnetIP string
}{
{
name: "WithGateway",
podmanInspectResponse: "172.17.0.0/16,172.17.0.1",
gateway: gateway,
subnetIP: subnetIP.String(),
},
{
name: "WithoutGateway",
podmanInspectResponse: "172.17.0.0/16",
gateway: emptyGateway,
subnetIP: subnetIP.String(),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
podmanInspectResponse := tc.podmanInspectResponse

// setting up mock funcs
podmanResponse = podmanInspectResponse
podmanInspectGetter = podmanInspectGetterMock

netInfo, err := podmanNetworkInspect("m2")
if err != nil {
t.Errorf("Expected not to have error but got %v", err)
}

if !netInfo.gateway.Equal(tc.gateway) {
t.Errorf("Expected gateway to be %v but got %v", tc.gateway, netInfo.gateway)
}

if netInfo.subnet.String() != tc.subnetIP {
t.Errorf("Expected subnet to be %v but got %v", tc.subnetIP, netInfo.subnet)
}
})
}
Expand Down