Skip to content

Commit

Permalink
network/cni: Search vendorDir for lo plugin too
Browse files Browse the repository at this point in the history
Though frankly the vendor dir thing should probably be removed in a
followup PR.
  • Loading branch information
euank committed Jul 22, 2016
1 parent 8f7b5bc commit 6aed2a0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
39 changes: 25 additions & 14 deletions pkg/kubelet/network/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type cniNetworkPlugin struct {
type cniNetwork struct {
name string
NetworkConfig *libcni.NetworkConfig
CNIConfig *libcni.CNIConfig
CNIConfig libcni.CNI
}

func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix string) []network.NetworkPlugin {
Expand All @@ -60,6 +60,7 @@ func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix str
}
return append(configList, &cniNetworkPlugin{
defaultNetwork: network,
loNetwork: getLoNetwork(vendorCNIDirPrefix),
execer: utilexec.New(),
})
}
Expand Down Expand Up @@ -88,40 +89,50 @@ func getDefaultCNINetwork(pluginDir, vendorCNIDirPrefix string) (*cniNetwork, er
continue
}
// Search for vendor-specific plugins as well as default plugins in the CNI codebase.
vendorCNIDir := fmt.Sprintf(VendorCNIDirTemplate, vendorCNIDirPrefix, conf.Network.Type)
vendorDir := vendorCNIDir(vendorCNIDirPrefix, conf.Network.Type)
cninet := &libcni.CNIConfig{
Path: []string{DefaultCNIDir, vendorCNIDir},
Path: []string{DefaultCNIDir, vendorDir},
}
network := &cniNetwork{name: conf.Network.Name, NetworkConfig: conf, CNIConfig: cninet}
return network, nil
}
return nil, fmt.Errorf("No valid networks found in %s", pluginDir)
}

func (plugin *cniNetworkPlugin) Init(host network.Host, hairpinMode componentconfig.HairpinMode, nonMasqueradeCIDR string) error {
var err error
plugin.nsenterPath, err = plugin.execer.LookPath("nsenter")
if err != nil {
return err
}
func vendorCNIDir(prefix, pluginType string) string {
return fmt.Sprintf(VendorCNIDirTemplate, prefix, pluginType)
}

func getLoNetwork(vendorDirPrefix string) *cniNetwork {
loConfig, err := libcni.ConfFromBytes([]byte(`{
"cniVersion": "0.1.0",
"name": "cni-loopback",
"type": "loopback"
}`))
if err != nil {
return err
// The hardcoded config above should always be valid and unit tests will
// catch this
panic(err)
}
cninet := &libcni.CNIConfig{
Path: []string{DefaultCNIDir},
Path: []string{vendorCNIDir(vendorDirPrefix, loConfig.Network.Type), DefaultCNIDir},
}
plugin.loNetwork = &cniNetwork{
loNetwork := &cniNetwork{
name: "lo",
NetworkConfig: loConfig,
CNIConfig: cninet,
}

return loNetwork
}

func (plugin *cniNetworkPlugin) Init(host network.Host, hairpinMode componentconfig.HairpinMode, nonMasqueradeCIDR string) error {
var err error
plugin.nsenterPath, err = plugin.execer.LookPath("nsenter")
if err != nil {
return err
}

plugin.host = host
return nil
}
Expand Down Expand Up @@ -184,7 +195,7 @@ func (network *cniNetwork) addToNetwork(podName string, podNamespace string, pod
}

netconf, cninet := network.NetworkConfig, network.CNIConfig
glog.V(4).Infof("About to run with conf.Network.Type=%v, c.Path=%v", netconf.Network.Type, cninet.Path)
glog.V(4).Infof("About to run with conf.Network.Type=%v", netconf.Network.Type)
res, err := cninet.AddNetwork(netconf, rt)
if err != nil {
glog.Errorf("Error adding network: %v", err)
Expand All @@ -202,7 +213,7 @@ func (network *cniNetwork) deleteFromNetwork(podName string, podNamespace string
}

netconf, cninet := network.NetworkConfig, network.CNIConfig
glog.V(4).Infof("About to run with conf.Network.Type=%v, c.Path=%v", netconf.Network.Type, cninet.Path)
glog.V(4).Infof("About to run with conf.Network.Type=%v", netconf.Network.Type)
err = cninet.DelNetwork(netconf, rt)
if err != nil {
glog.Errorf("Error deleting network: %v", err)
Expand Down
18 changes: 18 additions & 0 deletions pkg/kubelet/network/cni/cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ import (
"fmt"
"io/ioutil"
"math/rand"
"net"
"os"
"path"
"testing"
"text/template"

clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"

cnitypes "github.com/appc/cni/pkg/types"
"github.com/stretchr/testify/mock"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/componentconfig"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
"k8s.io/kubernetes/pkg/kubelet/network"
"k8s.io/kubernetes/pkg/kubelet/network/cni/testing"
utilexec "k8s.io/kubernetes/pkg/util/exec"
utiltesting "k8s.io/kubernetes/pkg/util/testing"
)
Expand Down Expand Up @@ -160,6 +164,9 @@ func TestCNIPlugin(t *testing.T) {
},
}

mockLoCNI := &mock_cni.MockCNI{}
// TODO mock for the test plugin too

tmpDir := utiltesting.MkTmpdirOrDie("cni-test")
testNetworkConfigPath := path.Join(tmpDir, "plugins", "net", "cni")
testVendorCNIDirPrefix := tmpDir
Expand Down Expand Up @@ -189,6 +196,9 @@ func TestCNIPlugin(t *testing.T) {
t.Fatalf("Not a CNI network plugin!")
}
cniPlugin.execer = fexec
cniPlugin.loNetwork.CNIConfig = mockLoCNI

mockLoCNI.On("AddNetwork", cniPlugin.loNetwork.NetworkConfig, mock.AnythingOfType("*libcni.RuntimeConf")).Return(&cnitypes.Result{IP4: &cnitypes.IPConfig{IP: net.IPNet{IP: []byte{127, 0, 0, 1}}}}, nil)

plug, err := network.InitNetworkPlugin(plugins, "cni", NewFakeHost(nil, pods), componentconfig.HairpinNone, "10.0.0.0/8")
if err != nil {
Expand Down Expand Up @@ -231,4 +241,12 @@ func TestCNIPlugin(t *testing.T) {
if string(output) != expectedOutput {
t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output))
}

mockLoCNI.AssertExpectations(t)
}

func TestLoNetNonNil(t *testing.T) {
if conf := getLoNetwork(""); conf == nil {
t.Error("Expected non-nil lo network")
}
}

0 comments on commit 6aed2a0

Please sign in to comment.