diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index cf5a5cf03b..5cae49d13f 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -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 diff --git a/pkg/kubelet/kubelet_test.go b/pkg/kubelet/kubelet_test.go index 4e833d41d2..1aa23903a4 100644 --- a/pkg/kubelet/kubelet_test.go +++ b/pkg/kubelet/kubelet_test.go @@ -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) diff --git a/pkg/kubelet/runtime.go b/pkg/kubelet/runtime.go index 30c30ce7a7..bf574f923e 100644 --- a/pkg/kubelet/runtime.go +++ b/pkg/kubelet/runtime.go @@ -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, } }