123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944 |
- /*
- Copyright 2014 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package node
- import (
- "testing"
- "time"
- "k8s.io/kubernetes/pkg/api"
- "k8s.io/kubernetes/pkg/api/resource"
- "k8s.io/kubernetes/pkg/api/unversioned"
- "k8s.io/kubernetes/pkg/apis/extensions"
- "k8s.io/kubernetes/pkg/client/cache"
- "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
- fakecloud "k8s.io/kubernetes/pkg/cloudprovider/providers/fake"
- "k8s.io/kubernetes/pkg/util/diff"
- "k8s.io/kubernetes/pkg/util/wait"
- )
- const (
- testNodeMonitorGracePeriod = 40 * time.Second
- testNodeStartupGracePeriod = 60 * time.Second
- testNodeMonitorPeriod = 5 * time.Second
- testRateLimiterQPS = float32(10000)
- testLargeClusterThreshold = 20
- testUnhealtyThreshold = float32(0.55)
- )
- func TestMonitorNodeStatusEvictPods(t *testing.T) {
- fakeNow := unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC)
- evictionTimeout := 10 * time.Minute
- // Because of the logic that prevents NC from evicting anything when all Nodes are NotReady
- // we need second healthy node in tests. Because of how the tests are written we need to update
- // the status of this Node.
- healthyNodeNewStatus := api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status has just been updated, and is NotReady for 10min.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 9, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- }
- table := []struct {
- fakeNodeHandler *FakeNodeHandler
- daemonSets []extensions.DaemonSet
- timeToPass time.Duration
- newNodeStatus api.NodeStatus
- secondNodeNewStatus api.NodeStatus
- expectedEvictPods bool
- description string
- }{
- // Node created recently, with no status (happens only at cluster startup).
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: fakeNow,
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- daemonSets: nil,
- timeToPass: 0,
- newNodeStatus: api.NodeStatus{},
- secondNodeNewStatus: healthyNodeNewStatus,
- expectedEvictPods: false,
- description: "Node created recently, with no status.",
- },
- // Node created long time ago, and kubelet posted NotReady for a short period of time.
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionFalse,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- daemonSets: nil,
- timeToPass: evictionTimeout,
- newNodeStatus: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionFalse,
- // Node status has just been updated, and is NotReady for 10min.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 9, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- secondNodeNewStatus: healthyNodeNewStatus,
- expectedEvictPods: false,
- description: "Node created long time ago, and kubelet posted NotReady for a short period of time.",
- },
- // Pod is ds-managed, and kubelet posted NotReady for a long period of time.
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionFalse,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- Clientset: fake.NewSimpleClientset(
- &api.PodList{
- Items: []api.Pod{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "pod0",
- Namespace: "default",
- Labels: map[string]string{"daemon": "yes"},
- },
- Spec: api.PodSpec{
- NodeName: "node0",
- },
- },
- },
- },
- ),
- },
- daemonSets: []extensions.DaemonSet{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "ds0",
- Namespace: "default",
- },
- Spec: extensions.DaemonSetSpec{
- Selector: &unversioned.LabelSelector{
- MatchLabels: map[string]string{"daemon": "yes"},
- },
- },
- },
- },
- timeToPass: time.Hour,
- newNodeStatus: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionFalse,
- // Node status has just been updated, and is NotReady for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 59, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- secondNodeNewStatus: healthyNodeNewStatus,
- expectedEvictPods: false,
- description: "Pod is ds-managed, and kubelet posted NotReady for a long period of time.",
- },
- // Node created long time ago, and kubelet posted NotReady for a long period of time.
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionFalse,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- daemonSets: nil,
- timeToPass: time.Hour,
- newNodeStatus: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionFalse,
- // Node status has just been updated, and is NotReady for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 59, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- secondNodeNewStatus: healthyNodeNewStatus,
- expectedEvictPods: true,
- description: "Node created long time ago, and kubelet posted NotReady for a long period of time.",
- },
- // Node created long time ago, node controller posted Unknown for a short period of time.
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- daemonSets: nil,
- timeToPass: evictionTimeout - testNodeMonitorGracePeriod,
- newNodeStatus: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- // Node status was updated by nodecontroller 10min ago
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- secondNodeNewStatus: healthyNodeNewStatus,
- expectedEvictPods: false,
- description: "Node created long time ago, node controller posted Unknown for a short period of time.",
- },
- // Node created long time ago, node controller posted Unknown for a long period of time.
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- daemonSets: nil,
- timeToPass: 60 * time.Minute,
- newNodeStatus: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- // Node status was updated by nodecontroller 1hr ago
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- secondNodeNewStatus: healthyNodeNewStatus,
- expectedEvictPods: true,
- description: "Node created long time ago, node controller posted Unknown for a long period of time.",
- },
- }
- for _, item := range table {
- nodeController, _ := NewNodeControllerFromClient(nil, item.fakeNodeHandler,
- evictionTimeout, testRateLimiterQPS, testRateLimiterQPS, testLargeClusterThreshold, testUnhealtyThreshold, testNodeMonitorGracePeriod,
- testNodeStartupGracePeriod, testNodeMonitorPeriod, nil, nil, 0, false)
- nodeController.now = func() unversioned.Time { return fakeNow }
- for _, ds := range item.daemonSets {
- nodeController.daemonSetStore.Add(&ds)
- }
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if item.timeToPass > 0 {
- nodeController.now = func() unversioned.Time { return unversioned.Time{Time: fakeNow.Add(item.timeToPass)} }
- item.fakeNodeHandler.Existing[0].Status = item.newNodeStatus
- item.fakeNodeHandler.Existing[1].Status = item.secondNodeNewStatus
- }
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- zones := getZones(item.fakeNodeHandler)
- for _, zone := range zones {
- nodeController.zonePodEvictor[zone].Try(func(value TimedValue) (bool, time.Duration) {
- nodeUid, _ := value.UID.(string)
- remaining, _ := deletePods(item.fakeNodeHandler, nodeController.recorder, value.Value, nodeUid, nodeController.daemonSetStore)
- if remaining {
- nodeController.zoneTerminationEvictor[zone].Add(value.Value, nodeUid)
- }
- return true, 0
- })
- nodeController.zonePodEvictor[zone].Try(func(value TimedValue) (bool, time.Duration) {
- nodeUid, _ := value.UID.(string)
- terminatePods(item.fakeNodeHandler, nodeController.recorder, value.Value, nodeUid, value.AddedAt, nodeController.maximumGracePeriod)
- return true, 0
- })
- }
- podEvicted := false
- for _, action := range item.fakeNodeHandler.Actions() {
- if action.GetVerb() == "delete" && action.GetResource().Resource == "pods" {
- podEvicted = true
- }
- }
- if item.expectedEvictPods != podEvicted {
- t.Errorf("expected pod eviction: %+v, got %+v for %+v", item.expectedEvictPods,
- podEvicted, item.description)
- }
- }
- }
- func TestMonitorNodeStatusEvictPodsWithDisruption(t *testing.T) {
- fakeNow := unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC)
- evictionTimeout := 10 * time.Minute
- timeToPass := 60 * time.Minute
- // Because of the logic that prevents NC from evicting anything when all Nodes are NotReady
- // we need second healthy node in tests. Because of how the tests are written we need to update
- // the status of this Node.
- healthyNodeNewStatus := api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 13, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- }
- unhealthyNodeNewStatus := api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- // Node status was updated by nodecontroller 1hr ago
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- }
- table := []struct {
- nodeList []*api.Node
- podList []api.Pod
- updatedNodeStatuses []api.NodeStatus
- expectedInitialStates map[string]zoneState
- expectedFollowingStates map[string]zoneState
- expectedEvictPods bool
- description string
- }{
- // NetworkDisruption: Node created long time ago, node controller posted Unknown for a long period of time on both Nodes.
- // Only zone is down - eviction shouldn't take place
- {
- nodeList: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- podList: []api.Pod{*newPod("pod0", "node0")},
- updatedNodeStatuses: []api.NodeStatus{
- unhealthyNodeNewStatus,
- unhealthyNodeNewStatus,
- },
- expectedInitialStates: map[string]zoneState{createZoneID("region1", "zone1"): stateFullDisruption},
- expectedFollowingStates: map[string]zoneState{createZoneID("region1", "zone1"): stateFullDisruption},
- expectedEvictPods: false,
- description: "Network Disruption: Only zone is down - eviction shouldn't take place.",
- },
- // NetworkDisruption: Node created long time ago, node controller posted Unknown for a long period of time on both Nodes.
- // Both zones down - eviction shouldn't take place
- {
- nodeList: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region2",
- unversioned.LabelZoneFailureDomain: "zone2",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- podList: []api.Pod{*newPod("pod0", "node0")},
- updatedNodeStatuses: []api.NodeStatus{
- unhealthyNodeNewStatus,
- unhealthyNodeNewStatus,
- },
- expectedInitialStates: map[string]zoneState{
- createZoneID("region1", "zone1"): stateFullDisruption,
- createZoneID("region2", "zone2"): stateFullDisruption,
- },
- expectedFollowingStates: map[string]zoneState{
- createZoneID("region1", "zone1"): stateFullDisruption,
- createZoneID("region2", "zone2"): stateFullDisruption,
- },
- expectedEvictPods: false,
- description: "Network Disruption: Both zones down - eviction shouldn't take place.",
- },
- // NetworkDisruption: Node created long time ago, node controller posted Unknown for a long period of time on both Nodes.
- // One zone is down - eviction should take place
- {
- nodeList: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone2",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- podList: []api.Pod{*newPod("pod0", "node0")},
- updatedNodeStatuses: []api.NodeStatus{
- unhealthyNodeNewStatus,
- healthyNodeNewStatus,
- },
- expectedInitialStates: map[string]zoneState{
- createZoneID("region1", "zone1"): stateFullDisruption,
- createZoneID("region1", "zone2"): stateNormal,
- },
- expectedFollowingStates: map[string]zoneState{
- createZoneID("region1", "zone1"): stateFullDisruption,
- createZoneID("region1", "zone2"): stateNormal,
- },
- expectedEvictPods: true,
- description: "Network Disruption: One zone is down - eviction should take place.",
- },
- // NetworkDisruption: Node created long time ago, node controller posted Unknown for a long period
- // of on first Node, eviction should stop even though -master Node is healthy.
- {
- nodeList: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node-master",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- podList: []api.Pod{*newPod("pod0", "node0")},
- updatedNodeStatuses: []api.NodeStatus{
- unhealthyNodeNewStatus,
- healthyNodeNewStatus,
- },
- expectedInitialStates: map[string]zoneState{
- createZoneID("region1", "zone1"): stateFullDisruption,
- },
- expectedFollowingStates: map[string]zoneState{
- createZoneID("region1", "zone1"): stateFullDisruption,
- },
- expectedEvictPods: false,
- description: "NetworkDisruption: eviction should stop, only -master Node is healthy",
- },
- // NetworkDisruption: Node created long time ago, node controller posted Unknown for a long period of time on both Nodes.
- // Initially both zones down, one comes back - eviction should take place
- {
- nodeList: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone2",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- podList: []api.Pod{*newPod("pod0", "node0")},
- updatedNodeStatuses: []api.NodeStatus{
- unhealthyNodeNewStatus,
- healthyNodeNewStatus,
- },
- expectedInitialStates: map[string]zoneState{
- createZoneID("region1", "zone1"): stateFullDisruption,
- createZoneID("region1", "zone2"): stateFullDisruption,
- },
- expectedFollowingStates: map[string]zoneState{
- createZoneID("region1", "zone1"): stateFullDisruption,
- createZoneID("region1", "zone2"): stateNormal,
- },
- expectedEvictPods: true,
- description: "Initially both zones down, one comes back - eviction should take place",
- },
- // NetworkDisruption: Node created long time ago, node controller posted Unknown for a long period of time on both Nodes.
- // Zone is partially disrupted - eviction should take place
- {
- nodeList: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node2",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node3",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node4",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- Labels: map[string]string{
- unversioned.LabelZoneRegion: "region1",
- unversioned.LabelZoneFailureDomain: "zone1",
- },
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- podList: []api.Pod{*newPod("pod0", "node0")},
- updatedNodeStatuses: []api.NodeStatus{
- unhealthyNodeNewStatus,
- unhealthyNodeNewStatus,
- unhealthyNodeNewStatus,
- healthyNodeNewStatus,
- healthyNodeNewStatus,
- },
- expectedInitialStates: map[string]zoneState{
- createZoneID("region1", "zone1"): statePartialDisruption,
- },
- expectedFollowingStates: map[string]zoneState{
- createZoneID("region1", "zone1"): statePartialDisruption,
- },
- expectedEvictPods: true,
- description: "Zone is partially disrupted - eviction should take place.",
- },
- }
- for _, item := range table {
- fakeNodeHandler := &FakeNodeHandler{
- Existing: item.nodeList,
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: item.podList}),
- }
- nodeController, _ := NewNodeControllerFromClient(nil, fakeNodeHandler,
- evictionTimeout, testRateLimiterQPS, testRateLimiterQPS, testLargeClusterThreshold, testUnhealtyThreshold, testNodeMonitorGracePeriod,
- testNodeStartupGracePeriod, testNodeMonitorPeriod, nil, nil, 0, false)
- nodeController.now = func() unversioned.Time { return fakeNow }
- nodeController.enterPartialDisruptionFunc = func(nodeNum int) float32 {
- return testRateLimiterQPS
- }
- nodeController.enterFullDisruptionFunc = func(nodeNum int) float32 {
- return testRateLimiterQPS
- }
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("%v: unexpected error: %v", item.description, err)
- }
- for zone, state := range item.expectedInitialStates {
- if state != nodeController.zoneStates[zone] {
- t.Errorf("%v: Unexpected zone state: %v: %v instead %v", item.description, zone, nodeController.zoneStates[zone], state)
- }
- }
- nodeController.now = func() unversioned.Time { return unversioned.Time{Time: fakeNow.Add(timeToPass)} }
- for i := range item.updatedNodeStatuses {
- fakeNodeHandler.Existing[i].Status = item.updatedNodeStatuses[i]
- }
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("%v: unexpected error: %v", item.description, err)
- }
- // Give some time for rate-limiter to reload
- time.Sleep(50 * time.Millisecond)
- for zone, state := range item.expectedFollowingStates {
- if state != nodeController.zoneStates[zone] {
- t.Errorf("%v: Unexpected zone state: %v: %v instead %v", item.description, zone, nodeController.zoneStates[zone], state)
- }
- }
- zones := getZones(fakeNodeHandler)
- for _, zone := range zones {
- nodeController.zonePodEvictor[zone].Try(func(value TimedValue) (bool, time.Duration) {
- uid, _ := value.UID.(string)
- remaining, _ := deletePods(fakeNodeHandler, nodeController.recorder, value.Value, uid, nodeController.daemonSetStore)
- if remaining {
- nodeController.zoneTerminationEvictor[zone].Add(value.Value, value.UID)
- }
- return true, 0
- })
- nodeController.zonePodEvictor[zone].Try(func(value TimedValue) (bool, time.Duration) {
- uid, _ := value.UID.(string)
- terminatePods(fakeNodeHandler, nodeController.recorder, value.Value, uid, value.AddedAt, nodeController.maximumGracePeriod)
- return true, 0
- })
- }
- podEvicted := false
- for _, action := range fakeNodeHandler.Actions() {
- if action.GetVerb() == "delete" && action.GetResource().Resource == "pods" {
- podEvicted = true
- break
- }
- }
- if item.expectedEvictPods != podEvicted {
- t.Errorf("%v: expected pod eviction: %+v, got %+v", item.description, item.expectedEvictPods, podEvicted)
- }
- }
- }
- // TestCloudProviderNoRateLimit tests that monitorNodes() immediately deletes
- // pods and the node when kubelet has not reported, and the cloudprovider says
- // the node is gone.
- func TestCloudProviderNoRateLimit(t *testing.T) {
- fnh := &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0"), *newPod("pod1", "node0")}}),
- deleteWaitChan: make(chan struct{}),
- }
- nodeController, _ := NewNodeControllerFromClient(nil, fnh, 10*time.Minute,
- testRateLimiterQPS, testRateLimiterQPS, testLargeClusterThreshold, testUnhealtyThreshold,
- testNodeMonitorGracePeriod, testNodeStartupGracePeriod,
- testNodeMonitorPeriod, nil, nil, 0, false)
- nodeController.cloud = &fakecloud.FakeCloud{}
- nodeController.now = func() unversioned.Time { return unversioned.Date(2016, 1, 1, 12, 0, 0, 0, time.UTC) }
- nodeController.nodeExistsInCloudProvider = func(nodeName string) (bool, error) {
- return false, nil
- }
- // monitorNodeStatus should allow this node to be immediately deleted
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- select {
- case <-fnh.deleteWaitChan:
- case <-time.After(wait.ForeverTestTimeout):
- t.Errorf("Timed out waiting %v for node to be deleted", wait.ForeverTestTimeout)
- }
- if len(fnh.DeletedNodes) != 1 || fnh.DeletedNodes[0].Name != "node0" {
- t.Errorf("Node was not deleted")
- }
- if nodeOnQueue := nodeController.zonePodEvictor[""].Remove("node0"); nodeOnQueue {
- t.Errorf("Node was queued for eviction. Should have been immediately deleted.")
- }
- }
- func TestMonitorNodeStatusUpdateStatus(t *testing.T) {
- fakeNow := unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC)
- table := []struct {
- fakeNodeHandler *FakeNodeHandler
- timeToPass time.Duration
- newNodeStatus api.NodeStatus
- expectedEvictPods bool
- expectedRequestCount int
- expectedNodes []*api.Node
- }{
- // Node created long time ago, without status:
- // Expect Unknown status posted from node controller.
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- expectedRequestCount: 2, // List+Update
- expectedNodes: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- Reason: "NodeStatusNeverUpdated",
- Message: "Kubelet never posted node status.",
- LastHeartbeatTime: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- LastTransitionTime: fakeNow,
- },
- {
- Type: api.NodeOutOfDisk,
- Status: api.ConditionUnknown,
- Reason: "NodeStatusNeverUpdated",
- Message: "Kubelet never posted node status.",
- LastHeartbeatTime: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- LastTransitionTime: fakeNow,
- },
- },
- },
- },
- },
- },
- // Node created recently, without status.
- // Expect no action from node controller (within startup grace period).
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: fakeNow,
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- expectedRequestCount: 1, // List
- expectedNodes: nil,
- },
- // Node created long time ago, with status updated by kubelet exceeds grace period.
- // Expect Unknown status posted from node controller.
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- {
- Type: api.NodeOutOfDisk,
- Status: api.ConditionFalse,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- expectedRequestCount: 3, // (List+)List+Update
- timeToPass: time.Hour,
- newNodeStatus: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- {
- Type: api.NodeOutOfDisk,
- Status: api.ConditionFalse,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- expectedNodes: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionUnknown,
- Reason: "NodeStatusUnknown",
- Message: "Kubelet stopped posting node status.",
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Time{Time: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC).Add(time.Hour)},
- },
- {
- Type: api.NodeOutOfDisk,
- Status: api.ConditionUnknown,
- Reason: "NodeStatusUnknown",
- Message: "Kubelet stopped posting node status.",
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Time{Time: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC).Add(time.Hour)},
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- },
- },
- },
- // Node created long time ago, with status updated recently.
- // Expect no action from node controller (within monitor grace period).
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status has just been updated.
- LastHeartbeatTime: fakeNow,
- LastTransitionTime: fakeNow,
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- expectedRequestCount: 1, // List
- expectedNodes: nil,
- },
- }
- for i, item := range table {
- nodeController, _ := NewNodeControllerFromClient(nil, item.fakeNodeHandler, 5*time.Minute,
- testRateLimiterQPS, testRateLimiterQPS, testLargeClusterThreshold, testUnhealtyThreshold,
- testNodeMonitorGracePeriod, testNodeStartupGracePeriod, testNodeMonitorPeriod, nil, nil, 0, false)
- nodeController.now = func() unversioned.Time { return fakeNow }
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if item.timeToPass > 0 {
- nodeController.now = func() unversioned.Time { return unversioned.Time{Time: fakeNow.Add(item.timeToPass)} }
- item.fakeNodeHandler.Existing[0].Status = item.newNodeStatus
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- }
- if item.expectedRequestCount != item.fakeNodeHandler.RequestCount {
- t.Errorf("expected %v call, but got %v.", item.expectedRequestCount, item.fakeNodeHandler.RequestCount)
- }
- if len(item.fakeNodeHandler.UpdatedNodes) > 0 && !api.Semantic.DeepEqual(item.expectedNodes, item.fakeNodeHandler.UpdatedNodes) {
- t.Errorf("Case[%d] unexpected nodes: %s", i, diff.ObjectDiff(item.expectedNodes[0], item.fakeNodeHandler.UpdatedNodes[0]))
- }
- if len(item.fakeNodeHandler.UpdatedNodeStatuses) > 0 && !api.Semantic.DeepEqual(item.expectedNodes, item.fakeNodeHandler.UpdatedNodeStatuses) {
- t.Errorf("Case[%d] unexpected nodes: %s", i, diff.ObjectDiff(item.expectedNodes[0], item.fakeNodeHandler.UpdatedNodeStatuses[0]))
- }
- }
- }
- func TestMonitorNodeStatusMarkPodsNotReady(t *testing.T) {
- fakeNow := unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC)
- table := []struct {
- fakeNodeHandler *FakeNodeHandler
- timeToPass time.Duration
- newNodeStatus api.NodeStatus
- expectedPodStatusUpdate bool
- }{
- // Node created recently, without status.
- // Expect no action from node controller (within startup grace period).
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: fakeNow,
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- expectedPodStatusUpdate: false,
- },
- // Node created long time ago, with status updated recently.
- // Expect no action from node controller (within monitor grace period).
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status has just been updated.
- LastHeartbeatTime: fakeNow,
- LastTransitionTime: fakeNow,
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- expectedPodStatusUpdate: false,
- },
- // Node created long time ago, with status updated by kubelet exceeds grace period.
- // Expect pods status updated and Unknown node status posted from node controller
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: "v1.2.0",
- },
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- {
- Type: api.NodeOutOfDisk,
- Status: api.ConditionFalse,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- timeToPass: 1 * time.Minute,
- newNodeStatus: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: "v1.2.0",
- },
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- {
- Type: api.NodeOutOfDisk,
- Status: api.ConditionFalse,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- expectedPodStatusUpdate: true,
- },
- // Node created long time ago, with outdated kubelet version 1.1.0 and status
- // updated by kubelet exceeds grace period. Expect no action from node controller.
- {
- fakeNodeHandler: &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: "v1.1.0",
- },
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- {
- Type: api.NodeOutOfDisk,
- Status: api.ConditionFalse,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- },
- timeToPass: 1 * time.Minute,
- newNodeStatus: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: "v1.1.0",
- },
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- {
- Type: api.NodeOutOfDisk,
- Status: api.ConditionFalse,
- // Node status hasn't been updated for 1hr.
- LastHeartbeatTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- LastTransitionTime: unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- expectedPodStatusUpdate: false,
- },
- }
- for i, item := range table {
- nodeController, _ := NewNodeControllerFromClient(nil, item.fakeNodeHandler, 5*time.Minute,
- testRateLimiterQPS, testRateLimiterQPS, testLargeClusterThreshold, testUnhealtyThreshold,
- testNodeMonitorGracePeriod, testNodeStartupGracePeriod, testNodeMonitorPeriod, nil, nil, 0, false)
- nodeController.now = func() unversioned.Time { return fakeNow }
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("Case[%d] unexpected error: %v", i, err)
- }
- if item.timeToPass > 0 {
- nodeController.now = func() unversioned.Time { return unversioned.Time{Time: fakeNow.Add(item.timeToPass)} }
- item.fakeNodeHandler.Existing[0].Status = item.newNodeStatus
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("Case[%d] unexpected error: %v", i, err)
- }
- }
- podStatusUpdated := false
- for _, action := range item.fakeNodeHandler.Actions() {
- if action.GetVerb() == "update" && action.GetResource().Resource == "pods" && action.GetSubresource() == "status" {
- podStatusUpdated = true
- }
- }
- if podStatusUpdated != item.expectedPodStatusUpdate {
- t.Errorf("Case[%d] expect pod status updated to be %v, but got %v", i, item.expectedPodStatusUpdate, podStatusUpdated)
- }
- }
- }
- func TestNodeDeletion(t *testing.T) {
- fakeNow := unversioned.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC)
- fakeNodeHandler := &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status has just been updated.
- LastHeartbeatTime: fakeNow,
- LastTransitionTime: fakeNow,
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node1",
- CreationTimestamp: unversioned.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status has just been updated.
- LastHeartbeatTime: fakeNow,
- LastTransitionTime: fakeNow,
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("10G"),
- },
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0"), *newPod("pod1", "node1")}}),
- }
- nodeController, _ := NewNodeControllerFromClient(nil, fakeNodeHandler, 5*time.Minute,
- testRateLimiterQPS, testRateLimiterQPS, testLargeClusterThreshold, testUnhealtyThreshold,
- testNodeMonitorGracePeriod, testNodeStartupGracePeriod,
- testNodeMonitorPeriod, nil, nil, 0, false)
- nodeController.now = func() unversioned.Time { return fakeNow }
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- fakeNodeHandler.Delete("node1", nil)
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- nodeController.zonePodEvictor[""].Try(func(value TimedValue) (bool, time.Duration) {
- uid, _ := value.UID.(string)
- deletePods(fakeNodeHandler, nodeController.recorder, value.Value, uid, nodeController.daemonSetStore)
- return true, 0
- })
- podEvicted := false
- for _, action := range fakeNodeHandler.Actions() {
- if action.GetVerb() == "delete" && action.GetResource().Resource == "pods" {
- podEvicted = true
- }
- }
- if !podEvicted {
- t.Error("expected pods to be evicted from the deleted node")
- }
- }
- func TestNodeEventGeneration(t *testing.T) {
- fakeNow := unversioned.Date(2016, 8, 10, 12, 0, 0, 0, time.UTC)
- fakeNodeHandler := &FakeNodeHandler{
- Existing: []*api.Node{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "node0",
- UID: "1234567890",
- CreationTimestamp: unversioned.Date(2016, 8, 10, 0, 0, 0, 0, time.UTC),
- },
- Spec: api.NodeSpec{
- ExternalID: "node0",
- },
- Status: api.NodeStatus{
- Conditions: []api.NodeCondition{
- {
- Type: api.NodeReady,
- Status: api.ConditionTrue,
- // Node status has just been updated.
- LastHeartbeatTime: fakeNow,
- LastTransitionTime: fakeNow,
- },
- },
- Capacity: api.ResourceList{
- api.ResourceName(api.ResourceRequestsCPU): resource.MustParse("10"),
- api.ResourceName(api.ResourceMemory): resource.MustParse("20G"),
- },
- },
- },
- },
- Clientset: fake.NewSimpleClientset(&api.PodList{Items: []api.Pod{*newPod("pod0", "node0")}}),
- }
- nodeController, _ := NewNodeControllerFromClient(nil, fakeNodeHandler, 5*time.Minute,
- testRateLimiterQPS, testRateLimiterQPS, testLargeClusterThreshold, testUnhealtyThreshold,
- testNodeMonitorGracePeriod, testNodeStartupGracePeriod,
- testNodeMonitorPeriod, nil, nil, 0, false)
- nodeController.now = func() unversioned.Time { return fakeNow }
- fakeRecorder := NewFakeRecorder()
- nodeController.recorder = fakeRecorder
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- fakeNodeHandler.Delete("node0", nil)
- if err := nodeController.monitorNodeStatus(); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- nodeController.zonePodEvictor[""].Try(func(value TimedValue) (bool, time.Duration) {
- nodeUid, _ := value.UID.(string)
- deletePods(fakeNodeHandler, nodeController.recorder, value.Value, nodeUid, nodeController.daemonSetStore)
- return true, 0
- })
- if len(fakeRecorder.events) != 3 {
- t.Fatalf("unexpected events: %v", fakeRecorder.events)
- }
- if fakeRecorder.events[0].Reason != "RegisteredNode" || fakeRecorder.events[1].Reason != "RemovingNode" || fakeRecorder.events[2].Reason != "DeletingAllPods" {
- t.Fatalf("unexpected events generation: %v", fakeRecorder.events)
- }
- for _, event := range fakeRecorder.events {
- involvedObject := event.InvolvedObject
- actualUID := string(involvedObject.UID)
- if actualUID != "1234567890" {
- t.Fatalf("unexpected event uid: %v", actualUID)
- }
- }
- }
- func TestCheckPod(t *testing.T) {
- tcs := []struct {
- pod api.Pod
- prune bool
- }{
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: nil},
- Spec: api.PodSpec{NodeName: "new"},
- },
- prune: false,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: nil},
- Spec: api.PodSpec{NodeName: "old"},
- },
- prune: false,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: nil},
- Spec: api.PodSpec{NodeName: ""},
- },
- prune: false,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: nil},
- Spec: api.PodSpec{NodeName: "nonexistant"},
- },
- prune: false,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: &unversioned.Time{}},
- Spec: api.PodSpec{NodeName: "new"},
- },
- prune: false,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: &unversioned.Time{}},
- Spec: api.PodSpec{NodeName: "old"},
- },
- prune: true,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: &unversioned.Time{}},
- Spec: api.PodSpec{NodeName: "older"},
- },
- prune: true,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: &unversioned.Time{}},
- Spec: api.PodSpec{NodeName: "oldest"},
- },
- prune: true,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: &unversioned.Time{}},
- Spec: api.PodSpec{NodeName: ""},
- },
- prune: true,
- },
- {
- pod: api.Pod{
- ObjectMeta: api.ObjectMeta{DeletionTimestamp: &unversioned.Time{}},
- Spec: api.PodSpec{NodeName: "nonexistant"},
- },
- prune: true,
- },
- }
- nc, _ := NewNodeControllerFromClient(nil, nil, 0, 0, 0, 0, 0, 0, 0, 0, nil, nil, 0, false)
- nc.nodeStore.Store = cache.NewStore(cache.MetaNamespaceKeyFunc)
- nc.nodeStore.Store.Add(&api.Node{
- ObjectMeta: api.ObjectMeta{
- Name: "new",
- },
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: "v1.1.0",
- },
- },
- })
- nc.nodeStore.Store.Add(&api.Node{
- ObjectMeta: api.ObjectMeta{
- Name: "old",
- },
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: "v1.0.0",
- },
- },
- })
- nc.nodeStore.Store.Add(&api.Node{
- ObjectMeta: api.ObjectMeta{
- Name: "older",
- },
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: "v0.21.4",
- },
- },
- })
- nc.nodeStore.Store.Add(&api.Node{
- ObjectMeta: api.ObjectMeta{
- Name: "oldest",
- },
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: "v0.19.3",
- },
- },
- })
- for i, tc := range tcs {
- var deleteCalls int
- nc.forcefullyDeletePod = func(_ *api.Pod) error {
- deleteCalls++
- return nil
- }
- nc.maybeDeleteTerminatingPod(&tc.pod)
- if tc.prune && deleteCalls != 1 {
- t.Errorf("[%v] expected number of delete calls to be 1 but got %v", i, deleteCalls)
- }
- if !tc.prune && deleteCalls != 0 {
- t.Errorf("[%v] expected number of delete calls to be 0 but got %v", i, deleteCalls)
- }
- }
- }
- func TestCleanupOrphanedPods(t *testing.T) {
- pods := []*api.Pod{
- newPod("a", "foo"),
- newPod("b", "bar"),
- newPod("c", "gone"),
- }
- nc, _ := NewNodeControllerFromClient(nil, nil, 0, 0, 0, 0, 0, 0, 0, 0, nil, nil, 0, false)
- nc.nodeStore.Store.Add(newNode("foo"))
- nc.nodeStore.Store.Add(newNode("bar"))
- for _, pod := range pods {
- p := pod
- nc.podStore.Indexer.Add(&p)
- }
- var deleteCalls int
- var deletedPodName string
- forcefullyDeletePodFunc := func(p *api.Pod) error {
- deleteCalls++
- deletedPodName = p.ObjectMeta.Name
- return nil
- }
- cleanupOrphanedPods(pods, nc.nodeStore.Store, forcefullyDeletePodFunc)
- if deleteCalls != 1 {
- t.Fatalf("expected one delete, got: %v", deleteCalls)
- }
- if deletedPodName != "c" {
- t.Fatalf("expected deleted pod name to be 'c', but got: %q", deletedPodName)
- }
- }
- func TestCheckNodeKubeletVersionParsing(t *testing.T) {
- tests := []struct {
- version string
- outdated bool
- }{
- {
- version: "",
- outdated: true,
- },
- {
- version: "v0.21.4",
- outdated: true,
- },
- {
- version: "v1.0.0",
- outdated: true,
- },
- {
- version: "v1.1.0",
- outdated: true,
- },
- {
- version: "v1.1.0-alpha.2.961+9d4c6846fc03b9-dirty",
- outdated: true,
- },
- {
- version: "v1.2.0",
- outdated: false,
- },
- {
- version: "v1.3.3",
- outdated: false,
- },
- {
- version: "v1.4.0-alpha.2.961+9d4c6846fc03b9-dirty",
- outdated: false,
- },
- {
- version: "v2.0.0",
- outdated: false,
- },
- }
- for _, ov := range tests {
- n := &api.Node{
- Status: api.NodeStatus{
- NodeInfo: api.NodeSystemInfo{
- KubeletVersion: ov.version,
- },
- },
- }
- isOutdated := nodeRunningOutdatedKubelet(n)
- if ov.outdated != isOutdated {
- t.Errorf("Version %v doesn't match test expectation. Expected outdated %v got %v", n.Status.NodeInfo.KubeletVersion, ov.outdated, isOutdated)
- } else {
- t.Logf("Version %v outdated %v", ov.version, isOutdated)
- }
- }
- }
|