From 67414afd116081bf8fc96ecb68c2ed3e0409ae6c Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 26 Jan 2016 22:02:59 -0600 Subject: [PATCH] Send PodCIDR to network plugins as an event --- pkg/kubelet/kubelet.go | 24 +++++++++++++++++++----- pkg/kubelet/kubelet_test.go | 6 +++--- pkg/kubelet/network/cni/cni.go | 3 +++ pkg/kubelet/network/exec/exec.go | 3 +++ pkg/kubelet/network/plugins.go | 12 ++++++++++++ pkg/kubelet/runtime.go | 2 -- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 6c11f12a3f0..0bc00a9cc16 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -398,7 +398,8 @@ func NewMainKubelet( } klet.pleg = pleg.NewGenericPLEG(klet.containerRuntime, plegChannelCapacity, plegRelistPeriod, klet.podCache) - klet.runtimeState = newRuntimeState(maxWaitForContainerRuntime, configureCBR0, podCIDR, klet.isContainerRuntimeVersionCompatible) + klet.runtimeState = newRuntimeState(maxWaitForContainerRuntime, configureCBR0, klet.isContainerRuntimeVersionCompatible) + klet.updatePodCIDR(podCIDR) // setup containerGC containerGC, err := kubecontainer.NewContainerGC(klet.containerRuntime, containerGCPolicy) @@ -2644,9 +2645,7 @@ func (kl *Kubelet) syncNetworkStatus() { glog.Infof("Flannel server handshake failed %v", err) return } - glog.Infof("Setting cidr: %v -> %v", - kl.runtimeState.podCIDR(), podCIDR) - kl.runtimeState.setPodCIDR(podCIDR) + kl.updatePodCIDR(podCIDR) } if err := ensureIPTablesMasqRule(kl.nonMasqueradeCIDR); err != nil { err = fmt.Errorf("Error on adding ip table rules: %v", err) @@ -3026,7 +3025,7 @@ func (kl *Kubelet) tryUpdateNodeStatus() error { } } } else if kl.reconcileCIDR { - kl.runtimeState.setPodCIDR(node.Spec.PodCIDR) + kl.updatePodCIDR(node.Spec.PodCIDR) } if err := kl.setNodeStatus(node); err != nil { @@ -3445,6 +3444,21 @@ func (kl *Kubelet) GetRuntime() kubecontainer.Runtime { return kl.containerRuntime } +func (kl *Kubelet) updatePodCIDR(cidr string) { + if kl.runtimeState.podCIDR() == cidr { + return + } + + glog.Infof("Setting Pod CIDR: %v -> %v", kl.runtimeState.podCIDR(), cidr) + kl.runtimeState.setPodCIDR(cidr) + + if kl.networkPlugin != nil { + details := make(map[string]interface{}) + details[network.NET_PLUGIN_EVENT_POD_CIDR_CHANGE_DETAIL_CIDR] = cidr + kl.networkPlugin.Event(network.NET_PLUGIN_EVENT_POD_CIDR_CHANGE, details) + } +} + var minRsrc = resource.MustParse("1k") var maxRsrc = resource.MustParse("1P") diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index eccb6b959d6..3a1d8aa4ac5 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -117,7 +117,7 @@ func newTestKubelet(t *testing.T) *TestKubelet { kubelet.hostname = testKubeletHostname kubelet.nodeName = testKubeletHostname - kubelet.runtimeState = newRuntimeState(maxWaitForContainerRuntime, false, "" /* Pod CIDR */, func() error { return nil }) + kubelet.runtimeState = newRuntimeState(maxWaitForContainerRuntime, false, func() error { return nil }) kubelet.networkPlugin, _ = network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil)) if tempDir, err := ioutil.TempDir("/tmp", "kubelet_test."); err != nil { t.Fatalf("can't make a temp rootdir: %v", err) @@ -2961,7 +2961,7 @@ func TestDockerRuntimeVersion(t *testing.T) { }, } - kubelet.runtimeState = newRuntimeState(maxWaitForContainerRuntime, false, "", kubelet.isContainerRuntimeVersionCompatible) + kubelet.runtimeState = newRuntimeState(maxWaitForContainerRuntime, false, kubelet.isContainerRuntimeVersionCompatible) kubelet.updateRuntimeUp() if err := kubelet.updateNodeStatus(); err != nil { t.Errorf("unexpected error: %v", err) @@ -3424,7 +3424,7 @@ func TestUpdateNodeStatusWithoutContainerRuntime(t *testing.T) { }, }, } - kubelet.runtimeState = newRuntimeState(time.Duration(0), false, "" /* Pod CIDR */, func() error { return nil }) + kubelet.runtimeState = newRuntimeState(time.Duration(0), false, func() error { return nil }) kubelet.updateRuntimeUp() if err := kubelet.updateNodeStatus(); err != nil { t.Errorf("unexpected error: %v", err) diff --git a/pkg/kubelet/network/cni/cni.go b/pkg/kubelet/network/cni/cni.go index 5a124e08bd3..6fa7615348f 100644 --- a/pkg/kubelet/network/cni/cni.go +++ b/pkg/kubelet/network/cni/cni.go @@ -97,6 +97,9 @@ func (plugin *cniNetworkPlugin) Init(host network.Host) error { return nil } +func (plugin *cniNetworkPlugin) Event(name string, details map[string]interface{}) { +} + func (plugin *cniNetworkPlugin) Name() string { return CNIPluginName } diff --git a/pkg/kubelet/network/exec/exec.go b/pkg/kubelet/network/exec/exec.go index 6f2144581b3..52d895d9962 100644 --- a/pkg/kubelet/network/exec/exec.go +++ b/pkg/kubelet/network/exec/exec.go @@ -120,6 +120,9 @@ func (plugin *execNetworkPlugin) getExecutable() string { return path.Join(plugin.execPath, execName) } +func (plugin *execNetworkPlugin) Event(name string, details map[string]interface{}) { +} + func (plugin *execNetworkPlugin) Name() string { return plugin.execName } diff --git a/pkg/kubelet/network/plugins.go b/pkg/kubelet/network/plugins.go index 80678924532..eeb2971142c 100644 --- a/pkg/kubelet/network/plugins.go +++ b/pkg/kubelet/network/plugins.go @@ -33,12 +33,21 @@ import ( const DefaultPluginName = "kubernetes.io/no-op" +// Called when the node's Pod CIDR is known when using the +// controller manager's --allocate-node-cidrs=true option +const NET_PLUGIN_EVENT_POD_CIDR_CHANGE = "pod-cidr-change" +const NET_PLUGIN_EVENT_POD_CIDR_CHANGE_DETAIL_CIDR = "pod-cidr" + // Plugin is an interface to network plugins for the kubelet type NetworkPlugin interface { // Init initializes the plugin. This will be called exactly once // before any other methods are called. Init(host Host) error + // Called on various events like: + // NET_PLUGIN_EVENT_POD_CIDR_CHANGE + Event(name string, details map[string]interface{}) + // Name returns the plugin's name. This will be used when searching // for a plugin by name, e.g. Name() string @@ -130,6 +139,9 @@ func (plugin *noopNetworkPlugin) Init(host Host) error { return nil } +func (plugin *noopNetworkPlugin) Event(name string, details map[string]interface{}) { +} + func (plugin *noopNetworkPlugin) Name() string { return DefaultPluginName } diff --git a/pkg/kubelet/runtime.go b/pkg/kubelet/runtime.go index e97e8245780..6e3c413ad41 100644 --- a/pkg/kubelet/runtime.go +++ b/pkg/kubelet/runtime.go @@ -94,7 +94,6 @@ func (s *runtimeState) errors() []string { func newRuntimeState( runtimeSyncThreshold time.Duration, configureNetwork bool, - cidr string, runtimeCompatibility func() error, ) *runtimeState { var networkError error = nil @@ -105,7 +104,6 @@ func newRuntimeState( lastBaseRuntimeSync: time.Time{}, baseRuntimeSyncThreshold: runtimeSyncThreshold, networkError: networkError, - cidr: cidr, internalError: nil, runtimeCompatibility: runtimeCompatibility, }