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 docker inspect network go template for network which doesn't have MTU #10053

Merged
merged 4 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 7 additions & 2 deletions pkg/drivers/kic/oci/network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,18 @@ type networkInspect struct {
ContainerIPs []string
}

var dockerInsepctGetter = func(name string) (*RunResult, error) {
cmd := exec.Command(Docker, "network", "inspect", name, "--format", `{"Name": "{{.Name}}","Driver": "{{.Driver}}","Subnet": "{{range .IPAM.Config}}{{.Subnet}}{{end}}","Gateway": "{{range .IPAM.Config}}{{.Gateway}}{{end}}","MTU": {{if (index .Options "com.docker.network.driver.mtu")}}{{(index .Options "com.docker.network.driver.mtu")}}{{else}}0{{end}},{{$first := true}} "ContainerIPs": [{{range $k,$v := .Containers }}{{if $first}}{{$first = false}}{{else}}, {{end}}"{{$v.IPv4Address}}"{{end}}]}`)
rr, err := runCmd(cmd)
return rr, err
}

// if exists returns subnet, gateway and mtu
func dockerNetworkInspect(name string) (netInfo, error) {
var vals networkInspect
var info = netInfo{name: name}

cmd := exec.Command(Docker, "network", "inspect", name, "--format", `{"Name": "{{.Name}}","Driver": "{{.Driver}}","Subnet": "{{range .IPAM.Config}}{{.Subnet}}{{end}}","Gateway": "{{range .IPAM.Config}}{{.Gateway}}{{end}}","MTU": {{(index .Options "com.docker.network.driver.mtu")}},{{$first := true}} "ContainerIPs": [{{range $k,$v := .Containers }}{{if $first}}{{$first = false}}{{else}}, {{end}}"{{$v.IPv4Address}}"{{end}}]}`)
rr, err := runCmd(cmd)
rr, err := dockerInsepctGetter(name)
if err != nil {
logDockerNetworkInspect(Docker, name)
if strings.Contains(rr.Output(), "No such network") {
Expand Down
84 changes: 84 additions & 0 deletions pkg/drivers/kic/oci/network_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package oci

import (
"bytes"
"net"
"testing"
)

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

return response, nil
}

func TestDockerInspectWithMTU(t *testing.T) {
azhao155 marked this conversation as resolved.
Show resolved Hide resolved
azhao155 marked this conversation as resolved.
Show resolved Hide resolved
var tests = []struct {
name string
dockerInspectResponse string
gateway string
subnetIP string
mtu int
}{
{
name: "withMTU",
dockerInspectResponse: `{"Name": "m2","Driver": "bridge","Subnet": "172.19.0.0/16","Gateway": "172.19.0.1","MTU": 9216, "ContainerIPs": []}`,
gateway: "172.19.0.1",
subnetIP: "172.19.0.0",
mtu: 9216,
},
{
name: "withoutMTU",
dockerInspectResponse: `{"Name": "m2","Driver": "bridge","Subnet": "172.19.0.0/16","Gateway": "172.19.0.1","MTU": 0, "ContainerIPs": []}`,
gateway: "172.19.0.1",
subnetIP: "172.19.0.0",
mtu: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
dockerInspectResponseWithMtu := tc.dockerInspectResponse

// setting up mock funcs
dockerResponse = dockerInspectResponseWithMtu
dockerInsepctGetter = dockerInspectGetterMock

netInfo, err := dockerNetworkInspect("m2")

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

if netInfo.mtu != tc.mtu {
t.Errorf("Expected not to have MTU as %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)
}

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)
}
})
}
}