mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
refactor podCIDR handling in kubelet to runtime state
This commit is contained in:
parent
5f4570b764
commit
89c3cb2f43
@ -32,7 +32,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
@ -278,7 +277,7 @@ func NewMainKubelet(
|
|||||||
clusterDNS: clusterDNS,
|
clusterDNS: clusterDNS,
|
||||||
serviceLister: serviceLister,
|
serviceLister: serviceLister,
|
||||||
nodeLister: nodeLister,
|
nodeLister: nodeLister,
|
||||||
runtimeState: newRuntimeState(maxWaitForContainerRuntime),
|
runtimeState: newRuntimeState(maxWaitForContainerRuntime, configureCBR0, podCIDR),
|
||||||
masterServiceNamespace: masterServiceNamespace,
|
masterServiceNamespace: masterServiceNamespace,
|
||||||
streamingConnectionIdleTimeout: streamingConnectionIdleTimeout,
|
streamingConnectionIdleTimeout: streamingConnectionIdleTimeout,
|
||||||
recorder: recorder,
|
recorder: recorder,
|
||||||
@ -297,7 +296,6 @@ func NewMainKubelet(
|
|||||||
chownRunner: chownRunner,
|
chownRunner: chownRunner,
|
||||||
writer: writer,
|
writer: writer,
|
||||||
configureCBR0: configureCBR0,
|
configureCBR0: configureCBR0,
|
||||||
podCIDR: podCIDR,
|
|
||||||
reconcileCIDR: reconcileCIDR,
|
reconcileCIDR: reconcileCIDR,
|
||||||
pods: pods,
|
pods: pods,
|
||||||
syncLoopMonitor: util.AtomicValue{},
|
syncLoopMonitor: util.AtomicValue{},
|
||||||
@ -499,8 +497,7 @@ type Kubelet struct {
|
|||||||
|
|
||||||
// Last timestamp when runtime responded on ping.
|
// Last timestamp when runtime responded on ping.
|
||||||
// Mutex is used to protect this value.
|
// Mutex is used to protect this value.
|
||||||
runtimeState *runtimeState
|
runtimeState *runtimeState
|
||||||
networkConfigMutex sync.Mutex
|
|
||||||
|
|
||||||
// Volume plugins.
|
// Volume plugins.
|
||||||
volumePluginMgr volume.VolumePluginMgr
|
volumePluginMgr volume.VolumePluginMgr
|
||||||
@ -588,7 +585,6 @@ type Kubelet struct {
|
|||||||
// Whether or not kubelet should take responsibility for keeping cbr0 in
|
// Whether or not kubelet should take responsibility for keeping cbr0 in
|
||||||
// the correct state.
|
// the correct state.
|
||||||
configureCBR0 bool
|
configureCBR0 bool
|
||||||
podCIDR string
|
|
||||||
reconcileCIDR bool
|
reconcileCIDR bool
|
||||||
|
|
||||||
// Number of Pods which can be run by this Kubelet
|
// 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)
|
err = fmt.Errorf("Error on adding ip table rules: %v", err)
|
||||||
glog.Error(err)
|
glog.Error(err)
|
||||||
}
|
}
|
||||||
kl.networkConfigMutex.Lock()
|
podCIDR := kl.runtimeState.podCIDR()
|
||||||
podCIDR := kl.podCIDR
|
|
||||||
kl.networkConfigMutex.Unlock()
|
|
||||||
if len(podCIDR) == 0 {
|
if len(podCIDR) == 0 {
|
||||||
err = fmt.Errorf("ConfigureCBR0 requested, but PodCIDR not set. Will not configure CBR0 right now")
|
err = fmt.Errorf("ConfigureCBR0 requested, but PodCIDR not set. Will not configure CBR0 right now")
|
||||||
glog.Warning(err)
|
glog.Warning(err)
|
||||||
@ -2694,11 +2688,9 @@ func (kl *Kubelet) tryUpdateNodeStatus() error {
|
|||||||
if node == nil {
|
if node == nil {
|
||||||
return fmt.Errorf("no node instance returned for %q", kl.nodeName)
|
return fmt.Errorf("no node instance returned for %q", kl.nodeName)
|
||||||
}
|
}
|
||||||
kl.networkConfigMutex.Lock()
|
|
||||||
if kl.reconcileCIDR {
|
if kl.reconcileCIDR {
|
||||||
kl.podCIDR = node.Spec.PodCIDR
|
kl.runtimeState.setPodCIDR(node.Spec.PodCIDR)
|
||||||
}
|
}
|
||||||
kl.networkConfigMutex.Unlock()
|
|
||||||
|
|
||||||
if err := kl.setNodeStatus(node); err != nil {
|
if err := kl.setNodeStatus(node); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -96,7 +96,7 @@ func newTestKubelet(t *testing.T) *TestKubelet {
|
|||||||
|
|
||||||
kubelet.hostname = testKubeletHostname
|
kubelet.hostname = testKubeletHostname
|
||||||
kubelet.nodeName = testKubeletHostname
|
kubelet.nodeName = testKubeletHostname
|
||||||
kubelet.runtimeState = newRuntimeState(maxWaitForContainerRuntime)
|
kubelet.runtimeState = newRuntimeState(maxWaitForContainerRuntime, false, "" /* Pod CIDR */)
|
||||||
kubelet.networkPlugin, _ = network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
|
kubelet.networkPlugin, _ = network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
|
||||||
if tempDir, err := ioutil.TempDir("/tmp", "kubelet_test."); err != nil {
|
if tempDir, err := ioutil.TempDir("/tmp", "kubelet_test."); err != nil {
|
||||||
t.Fatalf("can't make a temp rootdir: %v", err)
|
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.livenessManager = proberesults.NewManager()
|
||||||
|
|
||||||
kubelet.volumeManager = newVolumeManager()
|
kubelet.volumeManager = newVolumeManager()
|
||||||
kubelet.containerManager, _ = newContainerManager(fakeContainerMgrMountInt(), mockCadvisor, "", "", "")
|
kubelet.containerManager = cm.NewStubContainerManager()
|
||||||
kubelet.runtimeState.setNetworkState(nil)
|
|
||||||
fakeClock := &util.FakeClock{Time: time.Now()}
|
fakeClock := &util.FakeClock{Time: time.Now()}
|
||||||
kubelet.backOff = util.NewBackOff(time.Second, time.Minute)
|
kubelet.backOff = util.NewBackOff(time.Second, time.Minute)
|
||||||
kubelet.backOff.Clock = fakeClock
|
kubelet.backOff.Clock = fakeClock
|
||||||
@ -2996,8 +2995,7 @@ func TestUpdateNodeStatusWithoutContainerRuntime(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
kubelet.runtimeState = newRuntimeState(time.Duration(0))
|
kubelet.runtimeState = newRuntimeState(time.Duration(0), false, "" /* Pod CIDR */)
|
||||||
kubelet.runtimeState.setNetworkState(nil)
|
|
||||||
kubelet.updateRuntimeUp()
|
kubelet.updateRuntimeUp()
|
||||||
if err := kubelet.updateNodeStatus(); err != nil {
|
if err := kubelet.updateNodeStatus(); err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
@ -27,6 +27,7 @@ type runtimeState struct {
|
|||||||
lastBaseRuntimeSync time.Time
|
lastBaseRuntimeSync time.Time
|
||||||
baseRuntimeSyncThreshold time.Duration
|
baseRuntimeSyncThreshold time.Duration
|
||||||
networkError error
|
networkError error
|
||||||
|
cidr string
|
||||||
initError error
|
initError error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,6 +43,18 @@ func (s *runtimeState) setNetworkState(err error) {
|
|||||||
s.networkError = err
|
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) {
|
func (s *runtimeState) setInitError(err error) {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
@ -64,10 +77,15 @@ func (s *runtimeState) errors() []string {
|
|||||||
return ret
|
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{
|
return &runtimeState{
|
||||||
lastBaseRuntimeSync: time.Time{},
|
lastBaseRuntimeSync: time.Time{},
|
||||||
baseRuntimeSyncThreshold: runtimeSyncThreshold,
|
baseRuntimeSyncThreshold: runtimeSyncThreshold,
|
||||||
networkError: fmt.Errorf("network state unknown"),
|
networkError: networkError,
|
||||||
|
cidr: cidr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user