network/cni: Search vendorDir for lo plugin too

Though frankly the vendor dir thing should probably be removed in a
followup PR.
This commit is contained in:
Euan Kemp 2016-07-19 18:13:07 -07:00
parent 8f7b5bcfd3
commit 6aed2a0b1d
2 changed files with 43 additions and 14 deletions

View File

@ -49,7 +49,7 @@ type cniNetworkPlugin struct {
type cniNetwork struct { type cniNetwork struct {
name string name string
NetworkConfig *libcni.NetworkConfig NetworkConfig *libcni.NetworkConfig
CNIConfig *libcni.CNIConfig CNIConfig libcni.CNI
} }
func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix string) []network.NetworkPlugin { func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix string) []network.NetworkPlugin {
@ -60,6 +60,7 @@ func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix str
} }
return append(configList, &cniNetworkPlugin{ return append(configList, &cniNetworkPlugin{
defaultNetwork: network, defaultNetwork: network,
loNetwork: getLoNetwork(vendorCNIDirPrefix),
execer: utilexec.New(), execer: utilexec.New(),
}) })
} }
@ -88,9 +89,9 @@ func getDefaultCNINetwork(pluginDir, vendorCNIDirPrefix string) (*cniNetwork, er
continue continue
} }
// Search for vendor-specific plugins as well as default plugins in the CNI codebase. // 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{ cninet := &libcni.CNIConfig{
Path: []string{DefaultCNIDir, vendorCNIDir}, Path: []string{DefaultCNIDir, vendorDir},
} }
network := &cniNetwork{name: conf.Network.Name, NetworkConfig: conf, CNIConfig: cninet} network := &cniNetwork{name: conf.Network.Name, NetworkConfig: conf, CNIConfig: cninet}
return network, nil return network, nil
@ -98,30 +99,40 @@ func getDefaultCNINetwork(pluginDir, vendorCNIDirPrefix string) (*cniNetwork, er
return nil, fmt.Errorf("No valid networks found in %s", pluginDir) return nil, fmt.Errorf("No valid networks found in %s", pluginDir)
} }
func (plugin *cniNetworkPlugin) Init(host network.Host, hairpinMode componentconfig.HairpinMode, nonMasqueradeCIDR string) error { func vendorCNIDir(prefix, pluginType string) string {
var err error return fmt.Sprintf(VendorCNIDirTemplate, prefix, pluginType)
plugin.nsenterPath, err = plugin.execer.LookPath("nsenter") }
if err != nil {
return err
}
func getLoNetwork(vendorDirPrefix string) *cniNetwork {
loConfig, err := libcni.ConfFromBytes([]byte(`{ loConfig, err := libcni.ConfFromBytes([]byte(`{
"cniVersion": "0.1.0", "cniVersion": "0.1.0",
"name": "cni-loopback", "name": "cni-loopback",
"type": "loopback" "type": "loopback"
}`)) }`))
if err != nil { if err != nil {
return err // The hardcoded config above should always be valid and unit tests will
// catch this
panic(err)
} }
cninet := &libcni.CNIConfig{ cninet := &libcni.CNIConfig{
Path: []string{DefaultCNIDir}, Path: []string{vendorCNIDir(vendorDirPrefix, loConfig.Network.Type), DefaultCNIDir},
} }
plugin.loNetwork = &cniNetwork{ loNetwork := &cniNetwork{
name: "lo", name: "lo",
NetworkConfig: loConfig, NetworkConfig: loConfig,
CNIConfig: cninet, 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 plugin.host = host
return nil return nil
} }
@ -184,7 +195,7 @@ func (network *cniNetwork) addToNetwork(podName string, podNamespace string, pod
} }
netconf, cninet := network.NetworkConfig, network.CNIConfig 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) res, err := cninet.AddNetwork(netconf, rt)
if err != nil { if err != nil {
glog.Errorf("Error adding network: %v", err) glog.Errorf("Error adding network: %v", err)
@ -202,7 +213,7 @@ func (network *cniNetwork) deleteFromNetwork(podName string, podNamespace string
} }
netconf, cninet := network.NetworkConfig, network.CNIConfig 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) err = cninet.DelNetwork(netconf, rt)
if err != nil { if err != nil {
glog.Errorf("Error deleting network: %v", err) glog.Errorf("Error deleting network: %v", err)

View File

@ -23,6 +23,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"net"
"os" "os"
"path" "path"
"testing" "testing"
@ -30,11 +31,14 @@ import (
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" 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/api"
"k8s.io/kubernetes/pkg/apis/componentconfig" "k8s.io/kubernetes/pkg/apis/componentconfig"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing" containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
"k8s.io/kubernetes/pkg/kubelet/network" "k8s.io/kubernetes/pkg/kubelet/network"
"k8s.io/kubernetes/pkg/kubelet/network/cni/testing"
utilexec "k8s.io/kubernetes/pkg/util/exec" utilexec "k8s.io/kubernetes/pkg/util/exec"
utiltesting "k8s.io/kubernetes/pkg/util/testing" utiltesting "k8s.io/kubernetes/pkg/util/testing"
) )
@ -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") tmpDir := utiltesting.MkTmpdirOrDie("cni-test")
testNetworkConfigPath := path.Join(tmpDir, "plugins", "net", "cni") testNetworkConfigPath := path.Join(tmpDir, "plugins", "net", "cni")
testVendorCNIDirPrefix := tmpDir testVendorCNIDirPrefix := tmpDir
@ -189,6 +196,9 @@ func TestCNIPlugin(t *testing.T) {
t.Fatalf("Not a CNI network plugin!") t.Fatalf("Not a CNI network plugin!")
} }
cniPlugin.execer = fexec 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") plug, err := network.InitNetworkPlugin(plugins, "cni", NewFakeHost(nil, pods), componentconfig.HairpinNone, "10.0.0.0/8")
if err != nil { if err != nil {
@ -231,4 +241,12 @@ func TestCNIPlugin(t *testing.T) {
if string(output) != expectedOutput { if string(output) != expectedOutput {
t.Errorf("Mismatch in expected output for setup hook. Expected '%s', got '%s'", expectedOutput, string(output)) 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")
}
} }