refactor podCIDR handling in kubelet to runtime state

pull/6/head
Vishnu kannan 2015-09-25 11:00:14 -07:00
parent 5f4570b764
commit 89c3cb2f43
3 changed files with 27 additions and 19 deletions

View File

@ -32,7 +32,6 @@ import (
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/golang/glog"
@ -278,7 +277,7 @@ func NewMainKubelet(
clusterDNS: clusterDNS,
serviceLister: serviceLister,
nodeLister: nodeLister,
runtimeState: newRuntimeState(maxWaitForContainerRuntime),
runtimeState: newRuntimeState(maxWaitForContainerRuntime, configureCBR0, podCIDR),
masterServiceNamespace: masterServiceNamespace,
streamingConnectionIdleTimeout: streamingConnectionIdleTimeout,
recorder: recorder,
@ -297,7 +296,6 @@ func NewMainKubelet(
chownRunner: chownRunner,
writer: writer,
configureCBR0: configureCBR0,
podCIDR: podCIDR,
reconcileCIDR: reconcileCIDR,
pods: pods,
syncLoopMonitor: util.AtomicValue{},
@ -499,8 +497,7 @@ type Kubelet struct {
// Last timestamp when runtime responded on ping.
// Mutex is used to protect this value.
runtimeState *runtimeState
networkConfigMutex sync.Mutex
runtimeState *runtimeState
// Volume plugins.
volumePluginMgr volume.VolumePluginMgr
@ -588,7 +585,6 @@ type Kubelet struct {
// Whether or not kubelet should take responsibility for keeping cbr0 in
// the correct state.
configureCBR0 bool
podCIDR string
reconcileCIDR bool
// Number of Pods which can be run by this Kubelet
@ -2425,9 +2421,7 @@ func (kl *Kubelet) syncNetworkStatus() {
err = fmt.Errorf("Error on adding ip table rules: %v", err)
glog.Error(err)
}
kl.networkConfigMutex.Lock()
podCIDR := kl.podCIDR
kl.networkConfigMutex.Unlock()
podCIDR := kl.runtimeState.podCIDR()
if len(podCIDR) == 0 {
err = fmt.Errorf("ConfigureCBR0 requested, but PodCIDR not set. Will not configure CBR0 right now")
glog.Warning(err)
@ -2694,11 +2688,9 @@ func (kl *Kubelet) tryUpdateNodeStatus() error {
if node == nil {
return fmt.Errorf("no node instance returned for %q", kl.nodeName)
}
kl.networkConfigMutex.Lock()
if kl.reconcileCIDR {
kl.podCIDR = node.Spec.PodCIDR
kl.runtimeState.setPodCIDR(node.Spec.PodCIDR)
}
kl.networkConfigMutex.Unlock()
if err := kl.setNodeStatus(node); err != nil {
return err

View File

@ -96,7 +96,7 @@ func newTestKubelet(t *testing.T) *TestKubelet {
kubelet.hostname = testKubeletHostname
kubelet.nodeName = testKubeletHostname
kubelet.runtimeState = newRuntimeState(maxWaitForContainerRuntime)
kubelet.runtimeState = newRuntimeState(maxWaitForContainerRuntime, false, "" /* Pod CIDR */)
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)
@ -139,8 +139,7 @@ func newTestKubelet(t *testing.T) *TestKubelet {
kubelet.livenessManager = proberesults.NewManager()
kubelet.volumeManager = newVolumeManager()
kubelet.containerManager, _ = newContainerManager(fakeContainerMgrMountInt(), mockCadvisor, "", "", "")
kubelet.runtimeState.setNetworkState(nil)
kubelet.containerManager = cm.NewStubContainerManager()
fakeClock := &util.FakeClock{Time: time.Now()}
kubelet.backOff = util.NewBackOff(time.Second, time.Minute)
kubelet.backOff.Clock = fakeClock
@ -2996,8 +2995,7 @@ func TestUpdateNodeStatusWithoutContainerRuntime(t *testing.T) {
},
},
}
kubelet.runtimeState = newRuntimeState(time.Duration(0))
kubelet.runtimeState.setNetworkState(nil)
kubelet.runtimeState = newRuntimeState(time.Duration(0), false, "" /* Pod CIDR */)
kubelet.updateRuntimeUp()
if err := kubelet.updateNodeStatus(); err != nil {
t.Errorf("unexpected error: %v", err)

View File

@ -27,6 +27,7 @@ type runtimeState struct {
lastBaseRuntimeSync time.Time
baseRuntimeSyncThreshold time.Duration
networkError error
cidr string
initError error
}
@ -42,6 +43,18 @@ func (s *runtimeState) setNetworkState(err error) {
s.networkError = err
}
func (s *runtimeState) setPodCIDR(cidr string) {
s.Lock()
defer s.Unlock()
s.cidr = cidr
}
func (s *runtimeState) podCIDR() string {
s.Lock()
defer s.Unlock()
return s.cidr
}
func (s *runtimeState) setInitError(err error) {
s.Lock()
defer s.Unlock()
@ -64,10 +77,15 @@ func (s *runtimeState) errors() []string {
return ret
}
func newRuntimeState(runtimeSyncThreshold time.Duration) *runtimeState {
func newRuntimeState(runtimeSyncThreshold time.Duration, configureNetwork bool, cidr string) *runtimeState {
var networkError error = nil
if configureNetwork {
networkError = fmt.Errorf("network state unknown")
}
return &runtimeState{
lastBaseRuntimeSync: time.Time{},
baseRuntimeSyncThreshold: runtimeSyncThreshold,
networkError: fmt.Errorf("network state unknown"),
networkError: networkError,
cidr: cidr,
}
}