Commit 6c09689c authored by James DeFelice's avatar James DeFelice

add functional Option type to Kubelet, add SetNodeStatus Option, migrate node…

add functional Option type to Kubelet, add SetNodeStatus Option, migrate node statue update funcs to slice of callbacks
parent b36e046f
...@@ -753,6 +753,7 @@ type KubeletConfig struct { ...@@ -753,6 +753,7 @@ type KubeletConfig struct {
NodeIP net.IP NodeIP net.IP
ContainerRuntimeOptions []kubecontainer.Option ContainerRuntimeOptions []kubecontainer.Option
HairpinMode string HairpinMode string
Options []kubelet.Option
} }
func CreateAndInitKubelet(kc *KubeletConfig) (k KubeletBootstrap, pc *config.PodConfig, err error) { func CreateAndInitKubelet(kc *KubeletConfig) (k KubeletBootstrap, pc *config.PodConfig, err error) {
...@@ -839,6 +840,7 @@ func CreateAndInitKubelet(kc *KubeletConfig) (k KubeletBootstrap, pc *config.Pod ...@@ -839,6 +840,7 @@ func CreateAndInitKubelet(kc *KubeletConfig) (k KubeletBootstrap, pc *config.Pod
kc.VolumeStatsAggPeriod, kc.VolumeStatsAggPeriod,
kc.ContainerRuntimeOptions, kc.ContainerRuntimeOptions,
kc.HairpinMode, kc.HairpinMode,
kc.Options,
) )
if err != nil { if err != nil {
......
...@@ -146,6 +146,9 @@ type SyncHandler interface { ...@@ -146,6 +146,9 @@ type SyncHandler interface {
type SourcesReadyFn func(sourcesSeen sets.String) bool type SourcesReadyFn func(sourcesSeen sets.String) bool
// Option is a functional option type for Kubelet
type Option func(*Kubelet)
// New instantiates a new Kubelet object along with all the required internal modules. // New instantiates a new Kubelet object along with all the required internal modules.
// No initialization of Kubelet and its modules should happen here. // No initialization of Kubelet and its modules should happen here.
func NewMainKubelet( func NewMainKubelet(
...@@ -206,6 +209,7 @@ func NewMainKubelet( ...@@ -206,6 +209,7 @@ func NewMainKubelet(
volumeStatsAggPeriod time.Duration, volumeStatsAggPeriod time.Duration,
containerRuntimeOptions []kubecontainer.Option, containerRuntimeOptions []kubecontainer.Option,
hairpinMode string, hairpinMode string,
kubeOptions []Option,
) (*Kubelet, error) { ) (*Kubelet, error) {
if rootDirectory == "" { if rootDirectory == "" {
return nil, fmt.Errorf("invalid root directory %q", rootDirectory) return nil, fmt.Errorf("invalid root directory %q", rootDirectory)
...@@ -458,6 +462,12 @@ func NewMainKubelet( ...@@ -458,6 +462,12 @@ func NewMainKubelet(
klet.backOff = util.NewBackOff(backOffPeriod, MaxContainerBackOff) klet.backOff = util.NewBackOff(backOffPeriod, MaxContainerBackOff)
klet.podKillingCh = make(chan *kubecontainer.PodPair, podKillingChannelCapacity) klet.podKillingCh = make(chan *kubecontainer.PodPair, podKillingChannelCapacity)
klet.sourcesSeen = sets.NewString() klet.sourcesSeen = sets.NewString()
klet.setNodeStatusFuncs = klet.defaultNodeStatusFuncs()
// apply functional Option's
for _, opt := range kubeOptions {
opt(klet)
}
return klet, nil return klet, nil
} }
...@@ -690,6 +700,9 @@ type Kubelet struct { ...@@ -690,6 +700,9 @@ type Kubelet struct {
// (make cbr0 promiscuous), "hairpin-veth" (set the hairpin flag on veth interfaces) // (make cbr0 promiscuous), "hairpin-veth" (set the hairpin flag on veth interfaces)
// or "none" (do nothing). // or "none" (do nothing).
hairpinMode componentconfig.HairpinMode hairpinMode componentconfig.HairpinMode
// handlers called during the tryUpdateNodeStatus cycle
setNodeStatusFuncs []func(*api.Node) error
} }
// Validate given node IP belongs to the current host // Validate given node IP belongs to the current host
...@@ -3002,16 +3015,39 @@ func (kl *Kubelet) recordNodeSchdulableEvent(node *api.Node) { ...@@ -3002,16 +3015,39 @@ func (kl *Kubelet) recordNodeSchdulableEvent(node *api.Node) {
// TODO(madhusudancs): Simplify the logic for setting node conditions and // TODO(madhusudancs): Simplify the logic for setting node conditions and
// refactor the node status condtion code out to a different file. // refactor the node status condtion code out to a different file.
func (kl *Kubelet) setNodeStatus(node *api.Node) error { func (kl *Kubelet) setNodeStatus(node *api.Node) error {
if err := kl.setNodeAddress(node); err != nil { for _, f := range kl.setNodeStatusFuncs {
return err if err := f(node); err != nil {
return err
}
} }
kl.setNodeStatusInfo(node)
kl.setNodeOODCondition(node)
kl.setNodeReadyCondition(node)
kl.recordNodeSchdulableEvent(node)
return nil return nil
} }
// defaultNodeStatusFuncs is a factory that generates the default set of setNodeStatus funcs
func (kl *Kubelet) defaultNodeStatusFuncs() []func(*api.Node) error {
// initial set of node status update handlers, can be modified by Option's
withoutError := func(f func(*api.Node)) func(*api.Node) error {
return func(n *api.Node) error {
f(n)
return nil
}
}
return []func(*api.Node) error{
kl.setNodeAddress,
withoutError(kl.setNodeStatusInfo),
withoutError(kl.setNodeOODCondition),
withoutError(kl.setNodeReadyCondition),
withoutError(kl.recordNodeSchdulableEvent),
}
}
// SetNodeStatus returns a functional Option that adds the given node status update handler to the Kubelet
func SetNodeStatus(f func(*api.Node) error) Option {
return func(k *Kubelet) {
k.setNodeStatusFuncs = append(k.setNodeStatusFuncs, f)
}
}
// FIXME: Why not combine this with container runtime health check? // FIXME: Why not combine this with container runtime health check?
func (kl *Kubelet) isContainerRuntimeVersionCompatible() error { func (kl *Kubelet) isContainerRuntimeVersionCompatible() error {
switch kl.GetRuntime().Type() { switch kl.GetRuntime().Type() {
......
...@@ -191,6 +191,7 @@ func newTestKubelet(t *testing.T) *TestKubelet { ...@@ -191,6 +191,7 @@ func newTestKubelet(t *testing.T) *TestKubelet {
// Relist period does not affect the tests. // Relist period does not affect the tests.
kubelet.pleg = pleg.NewGenericPLEG(fakeRuntime, 100, time.Hour, nil) kubelet.pleg = pleg.NewGenericPLEG(fakeRuntime, 100, time.Hour, nil)
kubelet.clock = fakeClock kubelet.clock = fakeClock
kubelet.setNodeStatusFuncs = kubelet.defaultNodeStatusFuncs()
return &TestKubelet{kubelet, fakeRuntime, mockCadvisor, fakeKubeClient, fakeMirrorClient, fakeClock} return &TestKubelet{kubelet, fakeRuntime, mockCadvisor, fakeKubeClient, fakeMirrorClient, fakeClock}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment