periodically reload the cni plugin config
Signed-off-by: Mike Danese <mikedanese@google.com>
This commit is contained in:
		@@ -17,8 +17,11 @@ limitations under the License.
 | 
			
		||||
package cni
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"sort"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/appc/cni/libcni"
 | 
			
		||||
	cnitypes "github.com/appc/cni/pkg/types"
 | 
			
		||||
@@ -27,6 +30,7 @@ import (
 | 
			
		||||
	kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/kubelet/network"
 | 
			
		||||
	utilexec "k8s.io/kubernetes/pkg/util/exec"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/util/wait"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@@ -40,7 +44,10 @@ type cniNetworkPlugin struct {
 | 
			
		||||
	network.NoopNetworkPlugin
 | 
			
		||||
 | 
			
		||||
	loNetwork *cniNetwork
 | 
			
		||||
 | 
			
		||||
	sync.RWMutex
 | 
			
		||||
	defaultNetwork *cniNetwork
 | 
			
		||||
 | 
			
		||||
	host        network.Host
 | 
			
		||||
	execer      utilexec.Interface
 | 
			
		||||
	nsenterPath string
 | 
			
		||||
@@ -53,16 +60,19 @@ type cniNetwork struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix string) []network.NetworkPlugin {
 | 
			
		||||
	configList := make([]network.NetworkPlugin, 0)
 | 
			
		||||
	network, err := getDefaultCNINetwork(pluginDir, vendorCNIDirPrefix)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return configList
 | 
			
		||||
	}
 | 
			
		||||
	return append(configList, &cniNetworkPlugin{
 | 
			
		||||
		defaultNetwork: network,
 | 
			
		||||
	plugin := &cniNetworkPlugin{
 | 
			
		||||
		defaultNetwork: nil,
 | 
			
		||||
		loNetwork:      getLoNetwork(vendorCNIDirPrefix),
 | 
			
		||||
		execer:         utilexec.New(),
 | 
			
		||||
	})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	plugin.syncNetworkConfig(pluginDir, vendorCNIDirPrefix)
 | 
			
		||||
	// sync network config from pluginDir periodically to detect network config updates
 | 
			
		||||
	go wait.Forever(func() {
 | 
			
		||||
		plugin.syncNetworkConfig(pluginDir, vendorCNIDirPrefix)
 | 
			
		||||
	}, 10*time.Second)
 | 
			
		||||
 | 
			
		||||
	return []network.NetworkPlugin{plugin}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ProbeNetworkPlugins(pluginDir string) []network.NetworkPlugin {
 | 
			
		||||
@@ -137,11 +147,42 @@ func (plugin *cniNetworkPlugin) Init(host network.Host, hairpinMode componentcon
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (plugin *cniNetworkPlugin) syncNetworkConfig(pluginDir, vendorCNIDirPrefix string) {
 | 
			
		||||
	network, err := getDefaultCNINetwork(pluginDir, vendorCNIDirPrefix)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		glog.Errorf("error updating cni config: %s", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	plugin.setDefaultNetwork(network)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (plugin *cniNetworkPlugin) getDefaultNetwork() *cniNetwork {
 | 
			
		||||
	plugin.RLock()
 | 
			
		||||
	defer plugin.RUnlock()
 | 
			
		||||
	return plugin.defaultNetwork
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (plugin *cniNetworkPlugin) setDefaultNetwork(n *cniNetwork) {
 | 
			
		||||
	plugin.Lock()
 | 
			
		||||
	defer plugin.Unlock()
 | 
			
		||||
	plugin.defaultNetwork = n
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (plugin *cniNetworkPlugin) checkInitialized() error {
 | 
			
		||||
	if plugin.getDefaultNetwork() == nil {
 | 
			
		||||
		return errors.New("cni config unintialized")
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (plugin *cniNetworkPlugin) Name() string {
 | 
			
		||||
	return CNIPluginName
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (plugin *cniNetworkPlugin) SetUpPod(namespace string, name string, id kubecontainer.ContainerID) error {
 | 
			
		||||
	if err := plugin.checkInitialized(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	netnsPath, err := plugin.host.GetRuntime().GetNetNS(id)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
 | 
			
		||||
@@ -153,7 +194,7 @@ func (plugin *cniNetworkPlugin) SetUpPod(namespace string, name string, id kubec
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = plugin.defaultNetwork.addToNetwork(name, namespace, id, netnsPath)
 | 
			
		||||
	_, err = plugin.getDefaultNetwork().addToNetwork(name, namespace, id, netnsPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		glog.Errorf("Error while adding to cni network: %s", err)
 | 
			
		||||
		return err
 | 
			
		||||
@@ -163,12 +204,15 @@ func (plugin *cniNetworkPlugin) SetUpPod(namespace string, name string, id kubec
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (plugin *cniNetworkPlugin) TearDownPod(namespace string, name string, id kubecontainer.ContainerID) error {
 | 
			
		||||
	if err := plugin.checkInitialized(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	netnsPath, err := plugin.host.GetRuntime().GetNetNS(id)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return plugin.defaultNetwork.deleteFromNetwork(name, namespace, id, netnsPath)
 | 
			
		||||
	return plugin.getDefaultNetwork().deleteFromNetwork(name, namespace, id, netnsPath)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TODO: Use the addToNetwork function to obtain the IP of the Pod. That will assume idempotent ADD call to the plugin.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user