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
 | 
					package cni
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/appc/cni/libcni"
 | 
						"github.com/appc/cni/libcni"
 | 
				
			||||||
	cnitypes "github.com/appc/cni/pkg/types"
 | 
						cnitypes "github.com/appc/cni/pkg/types"
 | 
				
			||||||
@@ -27,6 +30,7 @@ import (
 | 
				
			|||||||
	kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
 | 
						kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
 | 
				
			||||||
	"k8s.io/kubernetes/pkg/kubelet/network"
 | 
						"k8s.io/kubernetes/pkg/kubelet/network"
 | 
				
			||||||
	utilexec "k8s.io/kubernetes/pkg/util/exec"
 | 
						utilexec "k8s.io/kubernetes/pkg/util/exec"
 | 
				
			||||||
 | 
						"k8s.io/kubernetes/pkg/util/wait"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@@ -39,11 +43,14 @@ const (
 | 
				
			|||||||
type cniNetworkPlugin struct {
 | 
					type cniNetworkPlugin struct {
 | 
				
			||||||
	network.NoopNetworkPlugin
 | 
						network.NoopNetworkPlugin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	loNetwork      *cniNetwork
 | 
						loNetwork *cniNetwork
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sync.RWMutex
 | 
				
			||||||
	defaultNetwork *cniNetwork
 | 
						defaultNetwork *cniNetwork
 | 
				
			||||||
	host           network.Host
 | 
					
 | 
				
			||||||
	execer         utilexec.Interface
 | 
						host        network.Host
 | 
				
			||||||
	nsenterPath    string
 | 
						execer      utilexec.Interface
 | 
				
			||||||
 | 
						nsenterPath string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type cniNetwork struct {
 | 
					type cniNetwork struct {
 | 
				
			||||||
@@ -53,16 +60,19 @@ type cniNetwork struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix string) []network.NetworkPlugin {
 | 
					func probeNetworkPluginsWithVendorCNIDirPrefix(pluginDir, vendorCNIDirPrefix string) []network.NetworkPlugin {
 | 
				
			||||||
	configList := make([]network.NetworkPlugin, 0)
 | 
						plugin := &cniNetworkPlugin{
 | 
				
			||||||
	network, err := getDefaultCNINetwork(pluginDir, vendorCNIDirPrefix)
 | 
							defaultNetwork: nil,
 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return configList
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return append(configList, &cniNetworkPlugin{
 | 
					 | 
				
			||||||
		defaultNetwork: network,
 | 
					 | 
				
			||||||
		loNetwork:      getLoNetwork(vendorCNIDirPrefix),
 | 
							loNetwork:      getLoNetwork(vendorCNIDirPrefix),
 | 
				
			||||||
		execer:         utilexec.New(),
 | 
							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 {
 | 
					func ProbeNetworkPlugins(pluginDir string) []network.NetworkPlugin {
 | 
				
			||||||
@@ -137,11 +147,42 @@ func (plugin *cniNetworkPlugin) Init(host network.Host, hairpinMode componentcon
 | 
				
			|||||||
	return nil
 | 
						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 {
 | 
					func (plugin *cniNetworkPlugin) Name() string {
 | 
				
			||||||
	return CNIPluginName
 | 
						return CNIPluginName
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (plugin *cniNetworkPlugin) SetUpPod(namespace string, name string, id kubecontainer.ContainerID) error {
 | 
					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)
 | 
						netnsPath, err := plugin.host.GetRuntime().GetNetNS(id)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
 | 
							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
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_, err = plugin.defaultNetwork.addToNetwork(name, namespace, id, netnsPath)
 | 
						_, err = plugin.getDefaultNetwork().addToNetwork(name, namespace, id, netnsPath)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		glog.Errorf("Error while adding to cni network: %s", err)
 | 
							glog.Errorf("Error while adding to cni network: %s", err)
 | 
				
			||||||
		return 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 {
 | 
					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)
 | 
						netnsPath, err := plugin.host.GetRuntime().GetNetNS(id)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
 | 
							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.
 | 
					// 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