From 792868c743318ad94b1ae108ac115cc2ea69d652 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Tue, 28 Jun 2016 12:40:12 -0700 Subject: [PATCH] periodically reload the cni plugin config Signed-off-by: Mike Danese --- pkg/kubelet/network/cni/cni.go | 72 +++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/pkg/kubelet/network/cni/cni.go b/pkg/kubelet/network/cni/cni.go index 0b6ba442493..13d1e91653f 100644 --- a/pkg/kubelet/network/cni/cni.go +++ b/pkg/kubelet/network/cni/cni.go @@ -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 ( @@ -39,11 +43,14 @@ const ( type cniNetworkPlugin struct { network.NoopNetworkPlugin - loNetwork *cniNetwork + loNetwork *cniNetwork + + sync.RWMutex defaultNetwork *cniNetwork - host network.Host - execer utilexec.Interface - nsenterPath string + + host network.Host + execer utilexec.Interface + nsenterPath string } type cniNetwork struct { @@ -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.