Skip to content

Commit

Permalink
Merge pull request kubernetes#29870 from euank/automated-cherry-pick-…
Browse files Browse the repository at this point in the history
…of-#29310-upstream-release-1.3

Automatic merge from submit-queue

Automated cherry pick of kubernetes#29310

Cherry pick of kubernetes#29310 on release-1.3.
  • Loading branch information
k8s-merge-robot authored Aug 2, 2016
2 parents fb7803a + ead2180 commit 4803711
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
45 changes: 40 additions & 5 deletions pkg/kubelet/network/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
type cniNetworkPlugin struct {
network.NoopNetworkPlugin

loNetwork *cniNetwork
defaultNetwork *cniNetwork
host network.Host
execer utilexec.Interface
Expand All @@ -48,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 @@ -59,6 +60,7 @@ func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix str
}
return append(configList, &cniNetworkPlugin{
defaultNetwork: network,
loNetwork: getLoNetwork(vendorCNIDirPrefix),
execer: utilexec.New(),
})
}
Expand Down Expand Up @@ -87,16 +89,43 @@ 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 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 {
// The hardcoded config above should always be valid and unit tests will
// catch this
panic(err)
}
cninet := &libcni.CNIConfig{
Path: []string{vendorCNIDir(vendorDirPrefix, loConfig.Network.Type), DefaultCNIDir},
}
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")
Expand All @@ -118,6 +147,12 @@ func (plugin *cniNetworkPlugin) SetUpPod(namespace string, name string, id kubec
return fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
}

_, err = plugin.loNetwork.addToNetwork(name, namespace, id, netnsPath)
if err != nil {
glog.Errorf("Error while adding to cni lo network: %s", err)
return err
}

_, err = plugin.defaultNetwork.addToNetwork(name, namespace, id, netnsPath)
if err != nil {
glog.Errorf("Error while adding to cni network: %s", err)
Expand Down Expand Up @@ -160,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 @@ -178,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 4803711

Please sign in to comment.