1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852 |
- /*
- 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 kubectl
- import (
- "bytes"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "reflect"
- "testing"
- "time"
- corev1 "k8s.io/api/core/v1"
- apiequality "k8s.io/apimachinery/pkg/api/equality"
- "k8s.io/apimachinery/pkg/api/errors"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/runtime/schema"
- "k8s.io/apimachinery/pkg/util/diff"
- "k8s.io/apimachinery/pkg/util/intstr"
- "k8s.io/apimachinery/pkg/util/sets"
- "k8s.io/client-go/kubernetes"
- "k8s.io/client-go/kubernetes/fake"
- restclient "k8s.io/client-go/rest"
- manualfake "k8s.io/client-go/rest/fake"
- testcore "k8s.io/client-go/testing"
- "k8s.io/kubernetes/pkg/kubectl/scheme"
- "k8s.io/kubernetes/pkg/kubectl/util"
- )
- func oldRc(replicas int, original int) *corev1.ReplicationController {
- t := replicas
- replicasCopy := int32(t)
- return &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo-v1",
- UID: "7764ae47-9092-11e4-8393-42010af018ff",
- Annotations: map[string]string{
- originalReplicasAnnotation: fmt.Sprintf("%d", original),
- },
- },
- Spec: corev1.ReplicationControllerSpec{
- Replicas: &replicasCopy,
- Selector: map[string]string{"version": "v1"},
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo-v1",
- Labels: map[string]string{"version": "v1"},
- },
- },
- },
- Status: corev1.ReplicationControllerStatus{
- Replicas: int32(replicas),
- },
- }
- }
- func newRc(replicas int, desired int) *corev1.ReplicationController {
- rc := oldRc(replicas, replicas)
- rc.Spec.Template = &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Name: "foo-v2",
- Labels: map[string]string{"version": "v2"},
- },
- }
- rc.Spec.Selector = map[string]string{"version": "v2"}
- rc.ObjectMeta = metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo-v2",
- Annotations: map[string]string{
- desiredReplicasAnnotation: fmt.Sprintf("%d", desired),
- sourceIDAnnotation: "foo-v1:7764ae47-9092-11e4-8393-42010af018ff",
- },
- }
- return rc
- }
- // TestUpdate performs complex scenario testing for rolling updates. It
- // provides fine grained control over the states for each update interval to
- // allow the expression of as many edge cases as possible.
- func TestUpdate(t *testing.T) {
- // up represents a simulated scale up event and expectation
- type up struct {
- // to is the expected replica count for a scale-up
- to int
- }
- // down represents a simulated scale down event and expectation
- type down struct {
- // oldReady is the number of oldRc replicas which will be seen
- // as ready during the scale down attempt
- oldReady int
- // newReady is the number of newRc replicas which will be seen
- // as ready during the scale up attempt
- newReady int
- // to is the expected replica count for the scale down
- to int
- // noop and to are mutually exclusive; if noop is true, that means for
- // this down event, no scaling attempt should be made (for example, if
- // by scaling down, the readiness minimum would be crossed.)
- noop bool
- }
- tests := []struct {
- name string
- // oldRc is the "from" deployment
- oldRc *corev1.ReplicationController
- // newRc is the "to" deployment
- newRc *corev1.ReplicationController
- // whether newRc existed (false means it was created)
- newRcExists bool
- maxUnavail intstr.IntOrString
- maxSurge intstr.IntOrString
- // expected is the sequence of up/down events that will be simulated and
- // verified
- expected []interface{}
- // output is the expected textual output written
- output string
- }{
- {
- name: "10->10 30/0 fast readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("30%"),
- maxSurge: intstr.FromString("0%"),
- expected: []interface{}{
- down{oldReady: 10, newReady: 0, to: 7},
- up{3},
- down{oldReady: 7, newReady: 3, to: 4},
- up{6},
- down{oldReady: 4, newReady: 6, to: 1},
- up{9},
- down{oldReady: 1, newReady: 9, to: 0},
- up{10},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 7 pods available, don't exceed 10 pods)
- Scaling foo-v1 down to 7
- Scaling foo-v2 up to 3
- Scaling foo-v1 down to 4
- Scaling foo-v2 up to 6
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 9
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 10
- `,
- },
- {
- name: "10->10 30/0 delayed readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("30%"),
- maxSurge: intstr.FromString("0%"),
- expected: []interface{}{
- down{oldReady: 10, newReady: 0, to: 7},
- up{3},
- down{oldReady: 7, newReady: 0, noop: true},
- down{oldReady: 7, newReady: 1, to: 6},
- up{4},
- down{oldReady: 6, newReady: 4, to: 3},
- up{7},
- down{oldReady: 3, newReady: 7, to: 0},
- up{10},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 7 pods available, don't exceed 10 pods)
- Scaling foo-v1 down to 7
- Scaling foo-v2 up to 3
- Scaling foo-v1 down to 6
- Scaling foo-v2 up to 4
- Scaling foo-v1 down to 3
- Scaling foo-v2 up to 7
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 10
- `,
- }, {
- name: "10->10 30/0 fast readiness, continuation",
- oldRc: oldRc(7, 10),
- newRc: newRc(3, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("30%"),
- maxSurge: intstr.FromString("0%"),
- expected: []interface{}{
- down{oldReady: 7, newReady: 3, to: 4},
- up{6},
- down{oldReady: 4, newReady: 6, to: 1},
- up{9},
- down{oldReady: 1, newReady: 9, to: 0},
- up{10},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 3 to 10, scaling down foo-v1 from 7 to 0 (keep 7 pods available, don't exceed 10 pods)
- Scaling foo-v1 down to 4
- Scaling foo-v2 up to 6
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 9
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 10
- `,
- }, {
- name: "10->10 30/0 fast readiness, continued after restart which prevented first scale-up",
- oldRc: oldRc(7, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("30%"),
- maxSurge: intstr.FromString("0%"),
- expected: []interface{}{
- down{oldReady: 7, newReady: 0, noop: true},
- up{3},
- down{oldReady: 7, newReady: 3, to: 4},
- up{6},
- down{oldReady: 4, newReady: 6, to: 1},
- up{9},
- down{oldReady: 1, newReady: 9, to: 0},
- up{10},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 7 to 0 (keep 7 pods available, don't exceed 10 pods)
- Scaling foo-v2 up to 3
- Scaling foo-v1 down to 4
- Scaling foo-v2 up to 6
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 9
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 10
- `,
- }, {
- name: "10->10 0/30 fast readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("0%"),
- maxSurge: intstr.FromString("30%"),
- expected: []interface{}{
- up{3},
- down{oldReady: 10, newReady: 3, to: 7},
- up{6},
- down{oldReady: 7, newReady: 6, to: 4},
- up{9},
- down{oldReady: 4, newReady: 9, to: 1},
- up{10},
- down{oldReady: 1, newReady: 10, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 10 pods available, don't exceed 13 pods)
- Scaling foo-v2 up to 3
- Scaling foo-v1 down to 7
- Scaling foo-v2 up to 6
- Scaling foo-v1 down to 4
- Scaling foo-v2 up to 9
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 10
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->10 0/30 delayed readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("0%"),
- maxSurge: intstr.FromString("30%"),
- expected: []interface{}{
- up{3},
- down{oldReady: 10, newReady: 0, noop: true},
- down{oldReady: 10, newReady: 1, to: 9},
- up{4},
- down{oldReady: 9, newReady: 3, to: 7},
- up{6},
- down{oldReady: 7, newReady: 6, to: 4},
- up{9},
- down{oldReady: 4, newReady: 9, to: 1},
- up{10},
- down{oldReady: 1, newReady: 9, noop: true},
- down{oldReady: 1, newReady: 10, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 10 pods available, don't exceed 13 pods)
- Scaling foo-v2 up to 3
- Scaling foo-v1 down to 9
- Scaling foo-v2 up to 4
- Scaling foo-v1 down to 7
- Scaling foo-v2 up to 6
- Scaling foo-v1 down to 4
- Scaling foo-v2 up to 9
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 10
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->10 10/20 fast readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("10%"),
- maxSurge: intstr.FromString("20%"),
- expected: []interface{}{
- up{2},
- down{oldReady: 10, newReady: 2, to: 7},
- up{5},
- down{oldReady: 7, newReady: 5, to: 4},
- up{8},
- down{oldReady: 4, newReady: 8, to: 1},
- up{10},
- down{oldReady: 1, newReady: 10, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 9 pods available, don't exceed 12 pods)
- Scaling foo-v2 up to 2
- Scaling foo-v1 down to 7
- Scaling foo-v2 up to 5
- Scaling foo-v1 down to 4
- Scaling foo-v2 up to 8
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 10
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->10 10/20 delayed readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("10%"),
- maxSurge: intstr.FromString("20%"),
- expected: []interface{}{
- up{2},
- down{oldReady: 10, newReady: 2, to: 7},
- up{5},
- down{oldReady: 7, newReady: 4, to: 5},
- up{7},
- down{oldReady: 5, newReady: 4, noop: true},
- down{oldReady: 5, newReady: 7, to: 2},
- up{10},
- down{oldReady: 2, newReady: 9, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 9 pods available, don't exceed 12 pods)
- Scaling foo-v2 up to 2
- Scaling foo-v1 down to 7
- Scaling foo-v2 up to 5
- Scaling foo-v1 down to 5
- Scaling foo-v2 up to 7
- Scaling foo-v1 down to 2
- Scaling foo-v2 up to 10
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->10 10/20 fast readiness continued after restart which prevented first scale-down",
- oldRc: oldRc(10, 10),
- newRc: newRc(2, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("10%"),
- maxSurge: intstr.FromString("20%"),
- expected: []interface{}{
- down{oldReady: 10, newReady: 2, to: 7},
- up{5},
- down{oldReady: 7, newReady: 5, to: 4},
- up{8},
- down{oldReady: 4, newReady: 8, to: 1},
- up{10},
- down{oldReady: 1, newReady: 10, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 2 to 10, scaling down foo-v1 from 10 to 0 (keep 9 pods available, don't exceed 12 pods)
- Scaling foo-v1 down to 7
- Scaling foo-v2 up to 5
- Scaling foo-v1 down to 4
- Scaling foo-v2 up to 8
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 10
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->10 0/100 fast readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("0%"),
- maxSurge: intstr.FromString("100%"),
- expected: []interface{}{
- up{10},
- down{oldReady: 10, newReady: 10, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 10 pods available, don't exceed 20 pods)
- Scaling foo-v2 up to 10
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->10 0/100 delayed readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("0%"),
- maxSurge: intstr.FromString("100%"),
- expected: []interface{}{
- up{10},
- down{oldReady: 10, newReady: 0, noop: true},
- down{oldReady: 10, newReady: 2, to: 8},
- down{oldReady: 8, newReady: 7, to: 3},
- down{oldReady: 3, newReady: 10, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 10 pods available, don't exceed 20 pods)
- Scaling foo-v2 up to 10
- Scaling foo-v1 down to 8
- Scaling foo-v1 down to 3
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->10 100/0 fast readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("100%"),
- maxSurge: intstr.FromString("0%"),
- expected: []interface{}{
- down{oldReady: 10, newReady: 0, to: 0},
- up{10},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 10, scaling down foo-v1 from 10 to 0 (keep 0 pods available, don't exceed 10 pods)
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 10
- `,
- }, {
- name: "1->1 25/25 maintain minimum availability",
- oldRc: oldRc(1, 1),
- newRc: newRc(0, 1),
- newRcExists: false,
- maxUnavail: intstr.FromString("25%"),
- maxSurge: intstr.FromString("25%"),
- expected: []interface{}{
- up{1},
- down{oldReady: 1, newReady: 0, noop: true},
- down{oldReady: 1, newReady: 1, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 1, scaling down foo-v1 from 1 to 0 (keep 1 pods available, don't exceed 2 pods)
- Scaling foo-v2 up to 1
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "1->1 0/10 delayed readiness",
- oldRc: oldRc(1, 1),
- newRc: newRc(0, 1),
- newRcExists: false,
- maxUnavail: intstr.FromString("0%"),
- maxSurge: intstr.FromString("10%"),
- expected: []interface{}{
- up{1},
- down{oldReady: 1, newReady: 0, noop: true},
- down{oldReady: 1, newReady: 1, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 1, scaling down foo-v1 from 1 to 0 (keep 1 pods available, don't exceed 2 pods)
- Scaling foo-v2 up to 1
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "1->1 10/10 delayed readiness",
- oldRc: oldRc(1, 1),
- newRc: newRc(0, 1),
- newRcExists: false,
- maxUnavail: intstr.FromString("10%"),
- maxSurge: intstr.FromString("10%"),
- expected: []interface{}{
- up{1},
- down{oldReady: 1, newReady: 0, noop: true},
- down{oldReady: 1, newReady: 1, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 1, scaling down foo-v1 from 1 to 0 (keep 1 pods available, don't exceed 2 pods)
- Scaling foo-v2 up to 1
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "3->3 1/1 fast readiness (absolute values)",
- oldRc: oldRc(3, 3),
- newRc: newRc(0, 3),
- newRcExists: false,
- maxUnavail: intstr.FromInt(0),
- maxSurge: intstr.FromInt(1),
- expected: []interface{}{
- up{1},
- down{oldReady: 3, newReady: 1, to: 2},
- up{2},
- down{oldReady: 2, newReady: 2, to: 1},
- up{3},
- down{oldReady: 1, newReady: 3, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 3, scaling down foo-v1 from 3 to 0 (keep 3 pods available, don't exceed 4 pods)
- Scaling foo-v2 up to 1
- Scaling foo-v1 down to 2
- Scaling foo-v2 up to 2
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 3
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->10 0/20 fast readiness, continued after restart which resulted in partial first scale-up",
- oldRc: oldRc(6, 10),
- newRc: newRc(5, 10),
- newRcExists: false,
- maxUnavail: intstr.FromString("0%"),
- maxSurge: intstr.FromString("20%"),
- expected: []interface{}{
- up{6},
- down{oldReady: 6, newReady: 6, to: 4},
- up{8},
- down{oldReady: 4, newReady: 8, to: 2},
- up{10},
- down{oldReady: 1, newReady: 10, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 5 to 10, scaling down foo-v1 from 6 to 0 (keep 10 pods available, don't exceed 12 pods)
- Scaling foo-v2 up to 6
- Scaling foo-v1 down to 4
- Scaling foo-v2 up to 8
- Scaling foo-v1 down to 2
- Scaling foo-v2 up to 10
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "10->20 0/300 fast readiness",
- oldRc: oldRc(10, 10),
- newRc: newRc(0, 20),
- newRcExists: false,
- maxUnavail: intstr.FromString("0%"),
- maxSurge: intstr.FromString("300%"),
- expected: []interface{}{
- up{20},
- down{oldReady: 10, newReady: 20, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 20, scaling down foo-v1 from 10 to 0 (keep 20 pods available, don't exceed 80 pods)
- Scaling foo-v2 up to 20
- Scaling foo-v1 down to 0
- `,
- }, {
- name: "1->1 0/1 scale down unavailable rc to a ready rc (rollback)",
- oldRc: oldRc(1, 1),
- newRc: newRc(1, 1),
- newRcExists: true,
- maxUnavail: intstr.FromInt(0),
- maxSurge: intstr.FromInt(1),
- expected: []interface{}{
- up{1},
- down{oldReady: 0, newReady: 1, to: 0},
- },
- output: `Continuing update with existing controller foo-v2.
- Scaling up foo-v2 from 1 to 1, scaling down foo-v1 from 1 to 0 (keep 1 pods available, don't exceed 2 pods)
- Scaling foo-v1 down to 0
- `,
- },
- {
- name: "3->0 1/1 desired 0 (absolute values)",
- oldRc: oldRc(3, 3),
- newRc: newRc(0, 0),
- newRcExists: true,
- maxUnavail: intstr.FromInt(1),
- maxSurge: intstr.FromInt(1),
- expected: []interface{}{
- down{oldReady: 3, newReady: 0, to: 0},
- },
- output: `Continuing update with existing controller foo-v2.
- Scaling up foo-v2 from 0 to 0, scaling down foo-v1 from 3 to 0 (keep 0 pods available, don't exceed 1 pods)
- Scaling foo-v1 down to 0
- `,
- },
- {
- name: "3->0 10/10 desired 0 (percentages)",
- oldRc: oldRc(3, 3),
- newRc: newRc(0, 0),
- newRcExists: true,
- maxUnavail: intstr.FromString("10%"),
- maxSurge: intstr.FromString("10%"),
- expected: []interface{}{
- down{oldReady: 3, newReady: 0, to: 0},
- },
- output: `Continuing update with existing controller foo-v2.
- Scaling up foo-v2 from 0 to 0, scaling down foo-v1 from 3 to 0 (keep 0 pods available, don't exceed 0 pods)
- Scaling foo-v1 down to 0
- `,
- },
- {
- name: "3->0 10/10 desired 0 (create new RC)",
- oldRc: oldRc(3, 3),
- newRc: newRc(0, 0),
- newRcExists: false,
- maxUnavail: intstr.FromString("10%"),
- maxSurge: intstr.FromString("10%"),
- expected: []interface{}{
- down{oldReady: 3, newReady: 0, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 0, scaling down foo-v1 from 3 to 0 (keep 0 pods available, don't exceed 0 pods)
- Scaling foo-v1 down to 0
- `,
- },
- {
- name: "0->0 1/1 desired 0 (absolute values)",
- oldRc: oldRc(0, 0),
- newRc: newRc(0, 0),
- newRcExists: true,
- maxUnavail: intstr.FromInt(1),
- maxSurge: intstr.FromInt(1),
- expected: []interface{}{
- down{oldReady: 0, newReady: 0, to: 0},
- },
- output: `Continuing update with existing controller foo-v2.
- Scaling up foo-v2 from 0 to 0, scaling down foo-v1 from 0 to 0 (keep 0 pods available, don't exceed 1 pods)
- `,
- }, {
- name: "30->2 50%/0",
- oldRc: oldRc(30, 30),
- newRc: newRc(0, 2),
- newRcExists: false,
- maxUnavail: intstr.FromString("50%"),
- maxSurge: intstr.FromInt(0),
- expected: []interface{}{
- down{oldReady: 30, newReady: 0, to: 1},
- up{1},
- down{oldReady: 1, newReady: 2, to: 0},
- up{2},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 2, scaling down foo-v1 from 30 to 0 (keep 1 pods available, don't exceed 2 pods)
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 1
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 2
- `,
- },
- {
- name: "2->2 1/0 blocked oldRc",
- oldRc: oldRc(2, 2),
- newRc: newRc(0, 2),
- newRcExists: false,
- maxUnavail: intstr.FromInt(1),
- maxSurge: intstr.FromInt(0),
- expected: []interface{}{
- down{oldReady: 1, newReady: 0, to: 1},
- up{1},
- down{oldReady: 1, newReady: 1, to: 0},
- up{2},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 2, scaling down foo-v1 from 2 to 0 (keep 1 pods available, don't exceed 2 pods)
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 1
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 2
- `,
- },
- {
- name: "1->1 1/0 allow maxUnavailability",
- oldRc: oldRc(1, 1),
- newRc: newRc(0, 1),
- newRcExists: false,
- maxUnavail: intstr.FromString("1%"),
- maxSurge: intstr.FromInt(0),
- expected: []interface{}{
- down{oldReady: 1, newReady: 0, to: 0},
- up{1},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 1, scaling down foo-v1 from 1 to 0 (keep 0 pods available, don't exceed 1 pods)
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 1
- `,
- },
- {
- name: "1->2 25/25 complex asymmetric deployment",
- oldRc: oldRc(1, 1),
- newRc: newRc(0, 2),
- newRcExists: false,
- maxUnavail: intstr.FromString("25%"),
- maxSurge: intstr.FromString("25%"),
- expected: []interface{}{
- up{2},
- down{oldReady: 1, newReady: 2, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 2, scaling down foo-v1 from 1 to 0 (keep 2 pods available, don't exceed 3 pods)
- Scaling foo-v2 up to 2
- Scaling foo-v1 down to 0
- `,
- },
- {
- name: "2->2 25/1 maxSurge trumps maxUnavailable",
- oldRc: oldRc(2, 2),
- newRc: newRc(0, 2),
- newRcExists: false,
- maxUnavail: intstr.FromString("25%"),
- maxSurge: intstr.FromString("1%"),
- expected: []interface{}{
- up{1},
- down{oldReady: 2, newReady: 1, to: 1},
- up{2},
- down{oldReady: 1, newReady: 2, to: 0},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 2, scaling down foo-v1 from 2 to 0 (keep 2 pods available, don't exceed 3 pods)
- Scaling foo-v2 up to 1
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 2
- Scaling foo-v1 down to 0
- `,
- },
- {
- name: "2->2 25/0 maxUnavailable resolves to zero, then one",
- oldRc: oldRc(2, 2),
- newRc: newRc(0, 2),
- newRcExists: false,
- maxUnavail: intstr.FromString("25%"),
- maxSurge: intstr.FromString("0%"),
- expected: []interface{}{
- down{oldReady: 2, newReady: 0, to: 1},
- up{1},
- down{oldReady: 1, newReady: 1, to: 0},
- up{2},
- },
- output: `Created foo-v2
- Scaling up foo-v2 from 0 to 2, scaling down foo-v1 from 2 to 0 (keep 1 pods available, don't exceed 2 pods)
- Scaling foo-v1 down to 1
- Scaling foo-v2 up to 1
- Scaling foo-v1 down to 0
- Scaling foo-v2 up to 2
- `,
- },
- }
- for i, tt := range tests {
- // Extract expectations into some makeshift FIFOs so they can be returned
- // in the correct order from the right places. This lets scale downs be
- // expressed a single event even though the data is used from multiple
- // interface calls.
- t.Run(tt.name, func(t *testing.T) {
- oldReady := []int{}
- newReady := []int{}
- upTo := []int{}
- downTo := []int{}
- for _, event := range tt.expected {
- switch e := event.(type) {
- case down:
- oldReady = append(oldReady, e.oldReady)
- newReady = append(newReady, e.newReady)
- if !e.noop {
- downTo = append(downTo, e.to)
- }
- case up:
- upTo = append(upTo, e.to)
- }
- }
- // Make a way to get the next item from our FIFOs. Returns -1 if the array
- // is empty.
- next := func(s *[]int) int {
- slice := *s
- v := -1
- if len(slice) > 0 {
- v = slice[0]
- if len(slice) > 1 {
- *s = slice[1:]
- } else {
- *s = []int{}
- }
- }
- return v
- }
- t.Logf("running test %d (%s) (up: %v, down: %v, oldReady: %v, newReady: %v)", i, tt.name, upTo, downTo, oldReady, newReady)
- updater := &RollingUpdater{
- ns: "default",
- scaleAndWait: func(rc *corev1.ReplicationController, retry *RetryParams, wait *RetryParams) (*corev1.ReplicationController, error) {
- // Return a scale up or scale down expectation depending on the rc,
- // and throw errors if there is no expectation expressed for this
- // call.
- expected := -1
- switch {
- case rc == tt.newRc:
- t.Logf("scaling up %s to %d", rc.Name, rc.Spec.Replicas)
- expected = next(&upTo)
- case rc == tt.oldRc:
- t.Logf("scaling down %s to %d", rc.Name, rc.Spec.Replicas)
- expected = next(&downTo)
- }
- if expected == -1 {
- t.Fatalf("unexpected scale of %s to %d", rc.Name, rc.Spec.Replicas)
- } else if e, a := expected, int(*rc.Spec.Replicas); e != a {
- t.Fatalf("expected scale of %s to %d, got %d", rc.Name, e, a)
- }
- // Simulate the scale.
- rc.Status.Replicas = *rc.Spec.Replicas
- return rc, nil
- },
- getOrCreateTargetController: func(controller *corev1.ReplicationController, sourceID string) (*corev1.ReplicationController, bool, error) {
- // Simulate a create vs. update of an existing controller.
- return tt.newRc, tt.newRcExists, nil
- },
- cleanup: func(oldRc, newRc *corev1.ReplicationController, config *RollingUpdaterConfig) error {
- return nil
- },
- }
- // Set up a mock readiness check which handles the test assertions.
- updater.getReadyPods = func(oldRc, newRc *corev1.ReplicationController, minReadySecondsDeadline int32) (int32, int32, error) {
- // Return simulated readiness, and throw an error if this call has no
- // expectations defined.
- oldReady := next(&oldReady)
- newReady := next(&newReady)
- if oldReady == -1 || newReady == -1 {
- t.Fatalf("unexpected getReadyPods call for:\noldRc: %#v\nnewRc: %#v", oldRc, newRc)
- }
- return int32(oldReady), int32(newReady), nil
- }
- var buffer bytes.Buffer
- config := &RollingUpdaterConfig{
- Out: &buffer,
- OldRc: tt.oldRc,
- NewRc: tt.newRc,
- UpdatePeriod: 0,
- Interval: time.Millisecond,
- Timeout: time.Millisecond,
- CleanupPolicy: DeleteRollingUpdateCleanupPolicy,
- MaxUnavailable: tt.maxUnavail,
- MaxSurge: tt.maxSurge,
- }
- err := updater.Update(config)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if buffer.String() != tt.output {
- t.Errorf("Bad output. expected:\n%s\ngot:\n%s", tt.output, buffer.String())
- }
- })
- }
- }
- // TestUpdate_progressTimeout ensures that an update which isn't making any
- // progress will eventually time out with a specified error.
- func TestUpdate_progressTimeout(t *testing.T) {
- oldRc := oldRc(2, 2)
- newRc := newRc(0, 2)
- updater := &RollingUpdater{
- ns: "default",
- scaleAndWait: func(rc *corev1.ReplicationController, retry *RetryParams, wait *RetryParams) (*corev1.ReplicationController, error) {
- // Do nothing.
- return rc, nil
- },
- getOrCreateTargetController: func(controller *corev1.ReplicationController, sourceID string) (*corev1.ReplicationController, bool, error) {
- return newRc, false, nil
- },
- cleanup: func(oldRc, newRc *corev1.ReplicationController, config *RollingUpdaterConfig) error {
- return nil
- },
- }
- updater.getReadyPods = func(oldRc, newRc *corev1.ReplicationController, minReadySeconds int32) (int32, int32, error) {
- // Coerce a timeout by pods never becoming ready.
- return 0, 0, nil
- }
- var buffer bytes.Buffer
- config := &RollingUpdaterConfig{
- Out: &buffer,
- OldRc: oldRc,
- NewRc: newRc,
- UpdatePeriod: 0,
- Interval: time.Millisecond,
- Timeout: time.Millisecond,
- CleanupPolicy: DeleteRollingUpdateCleanupPolicy,
- MaxUnavailable: intstr.FromInt(0),
- MaxSurge: intstr.FromInt(1),
- }
- err := updater.Update(config)
- if err == nil {
- t.Fatalf("expected an error")
- }
- if e, a := "timed out waiting for any update progress to be made", err.Error(); e != a {
- t.Fatalf("expected error message: %s, got: %s", e, a)
- }
- }
- func TestUpdate_assignOriginalAnnotation(t *testing.T) {
- oldRc := oldRc(1, 1)
- delete(oldRc.Annotations, originalReplicasAnnotation)
- newRc := newRc(1, 1)
- fake := fake.NewSimpleClientset(oldRc)
- updater := &RollingUpdater{
- rcClient: fake.CoreV1(),
- podClient: fake.CoreV1(),
- ns: "default",
- scaleAndWait: func(rc *corev1.ReplicationController, retry *RetryParams, wait *RetryParams) (*corev1.ReplicationController, error) {
- return rc, nil
- },
- getOrCreateTargetController: func(controller *corev1.ReplicationController, sourceID string) (*corev1.ReplicationController, bool, error) {
- return newRc, false, nil
- },
- cleanup: func(oldRc, newRc *corev1.ReplicationController, config *RollingUpdaterConfig) error {
- return nil
- },
- getReadyPods: func(oldRc, newRc *corev1.ReplicationController, minReadySeconds int32) (int32, int32, error) {
- return 1, 1, nil
- },
- }
- var buffer bytes.Buffer
- config := &RollingUpdaterConfig{
- Out: &buffer,
- OldRc: oldRc,
- NewRc: newRc,
- UpdatePeriod: 0,
- Interval: time.Millisecond,
- Timeout: time.Millisecond,
- CleanupPolicy: DeleteRollingUpdateCleanupPolicy,
- MaxUnavailable: intstr.FromString("100%"),
- }
- err := updater.Update(config)
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- updateAction := fake.Actions()[1].(testcore.UpdateAction)
- if updateAction.GetResource().GroupResource() != corev1.Resource("replicationcontrollers") {
- t.Fatalf("expected rc to be updated: %#v", updateAction)
- }
- if e, a := "1", updateAction.GetObject().(*corev1.ReplicationController).Annotations[originalReplicasAnnotation]; e != a {
- t.Fatalf("expected annotation value %s, got %s", e, a)
- }
- }
- func TestRollingUpdater_multipleContainersInPod(t *testing.T) {
- tests := []struct {
- name string
- oldRc *corev1.ReplicationController
- newRc *corev1.ReplicationController
- container string
- image string
- deploymentKey string
- }{
- {
- name: "test1",
- oldRc: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo",
- },
- Spec: corev1.ReplicationControllerSpec{
- Selector: map[string]string{
- "dk": "old",
- },
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "dk": "old",
- },
- },
- Spec: corev1.PodSpec{
- Containers: []corev1.Container{
- {
- Name: "container1",
- Image: "image1",
- },
- {
- Name: "container2",
- Image: "image2",
- },
- },
- },
- },
- },
- },
- newRc: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo",
- },
- Spec: corev1.ReplicationControllerSpec{
- Selector: map[string]string{
- "dk": "old",
- },
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "dk": "old",
- },
- },
- Spec: corev1.PodSpec{
- Containers: []corev1.Container{
- {
- Name: "container1",
- Image: "newimage",
- },
- {
- Name: "container2",
- Image: "image2",
- },
- },
- },
- },
- },
- },
- container: "container1",
- image: "newimage",
- deploymentKey: "dk",
- },
- {
- name: "test2",
- oldRc: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "bar",
- },
- Spec: corev1.ReplicationControllerSpec{
- Selector: map[string]string{
- "dk": "old",
- },
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "dk": "old",
- },
- },
- Spec: corev1.PodSpec{
- Containers: []corev1.Container{
- {
- Name: "container1",
- Image: "image1",
- },
- },
- },
- },
- },
- },
- newRc: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "bar",
- },
- Spec: corev1.ReplicationControllerSpec{
- Selector: map[string]string{
- "dk": "old",
- },
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "dk": "old",
- },
- },
- Spec: corev1.PodSpec{
- Containers: []corev1.Container{
- {
- Name: "container1",
- Image: "newimage",
- },
- },
- },
- },
- },
- },
- container: "container1",
- image: "newimage",
- deploymentKey: "dk",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- fake := fake.NewSimpleClientset(tt.oldRc)
- codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
- deploymentHash, err := util.HashObject(tt.newRc, codec)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- tt.newRc.Spec.Selector[tt.deploymentKey] = deploymentHash
- tt.newRc.Spec.Template.Labels[tt.deploymentKey] = deploymentHash
- tt.newRc.Name = fmt.Sprintf("%s-%s", tt.newRc.Name, deploymentHash)
- config := &NewControllerConfig{
- Namespace: metav1.NamespaceDefault,
- OldName: tt.oldRc.ObjectMeta.Name,
- NewName: tt.newRc.ObjectMeta.Name,
- Image: tt.image,
- Container: tt.container,
- DeploymentKey: tt.deploymentKey,
- }
- updatedRc, err := CreateNewControllerFromCurrentController(fake.CoreV1(), codec, config)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if !reflect.DeepEqual(updatedRc, tt.newRc) {
- t.Errorf("expected:\n%#v\ngot:\n%#v\n", tt.newRc, updatedRc)
- }
- })
- }
- }
- // TestRollingUpdater_cleanupWithClients ensures that the cleanup policy is
- // correctly implemented.
- func TestRollingUpdater_cleanupWithClients(t *testing.T) {
- rc := oldRc(2, 2)
- rcExisting := newRc(1, 3)
- tests := []struct {
- name string
- policy RollingUpdaterCleanupPolicy
- responses []runtime.Object
- expected []string
- }{
- {
- name: "preserve",
- policy: PreserveRollingUpdateCleanupPolicy,
- responses: []runtime.Object{rcExisting},
- expected: []string{
- "get",
- "update",
- "get",
- "get",
- },
- },
- {
- name: "delete",
- policy: DeleteRollingUpdateCleanupPolicy,
- responses: []runtime.Object{rcExisting},
- expected: []string{
- "get",
- "update",
- "get",
- "get",
- "delete",
- },
- },
- //{
- // This cases is separated to a standalone
- // TestRollingUpdater_cleanupWithClients_Rename. We have to do this
- // because the unversioned fake client is unable to delete objects.
- // TODO: uncomment this case when the unversioned fake client uses
- // pkg/client/testing/core.
- // {
- // name: "rename",
- // policy: RenameRollingUpdateCleanupPolicy,
- // responses: []runtime.Object{rcExisting},
- // expected: []string{
- // "get",
- // "update",
- // "get",
- // "get",
- // "delete",
- // "create",
- // "delete",
- // },
- // },
- //},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- objs := []runtime.Object{rc}
- objs = append(objs, tt.responses...)
- fake := fake.NewSimpleClientset(objs...)
- updater := &RollingUpdater{
- ns: "default",
- rcClient: fake.CoreV1(),
- podClient: fake.CoreV1(),
- }
- config := &RollingUpdaterConfig{
- Out: ioutil.Discard,
- OldRc: rc,
- NewRc: rcExisting,
- UpdatePeriod: 0,
- Interval: time.Millisecond,
- Timeout: time.Millisecond,
- CleanupPolicy: tt.policy,
- }
- err := updater.cleanupWithClients(rc, rcExisting, config)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if len(fake.Actions()) != len(tt.expected) {
- t.Fatalf("%s: unexpected actions: %v, expected %v", tt.name, fake.Actions(), tt.expected)
- }
- for j, action := range fake.Actions() {
- if e, a := tt.expected[j], action.GetVerb(); e != a {
- t.Errorf("%s: unexpected action: expected %s, got %s", tt.name, e, a)
- }
- }
- })
- }
- }
- // TestRollingUpdater_cleanupWithClients_Rename tests the rename cleanup policy. It's separated to
- // a standalone test because the unversioned fake client is unable to delete
- // objects.
- // TODO: move this test back to TestRollingUpdater_cleanupWithClients
- // when the fake client uses pkg/client/testing/core in the future.
- func TestRollingUpdater_cleanupWithClients_Rename(t *testing.T) {
- rc := oldRc(2, 2)
- rcExisting := newRc(1, 3)
- expectedActions := []string{"delete", "get", "create"}
- fake := fake.NewSimpleClientset()
- fake.AddReactor("*", "*", func(action testcore.Action) (handled bool, ret runtime.Object, err error) {
- switch action.(type) {
- case testcore.CreateAction:
- return true, nil, nil
- case testcore.GetAction:
- return true, nil, errors.NewNotFound(schema.GroupResource{}, "")
- case testcore.DeleteAction:
- return true, nil, nil
- }
- return false, nil, nil
- })
- err := Rename(fake.CoreV1(), rcExisting, rc.Name)
- if err != nil {
- t.Fatal(err)
- }
- for j, action := range fake.Actions() {
- if e, a := expectedActions[j], action.GetVerb(); e != a {
- t.Errorf("unexpected action: expected %s, got %s", e, a)
- }
- }
- }
- func TestFindSourceController(t *testing.T) {
- ctrl1 := corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo",
- Annotations: map[string]string{
- sourceIDAnnotation: "bar:1234",
- },
- },
- }
- ctrl2 := corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "bar",
- Annotations: map[string]string{
- sourceIDAnnotation: "foo:12345",
- },
- },
- }
- ctrl3 := corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "baz",
- Annotations: map[string]string{
- sourceIDAnnotation: "baz:45667",
- },
- },
- }
- tests := []struct {
- list *corev1.ReplicationControllerList
- expectedController *corev1.ReplicationController
- name string
- expectError bool
- }{
- {
- list: &corev1.ReplicationControllerList{},
- expectError: true,
- },
- {
- list: &corev1.ReplicationControllerList{
- Items: []corev1.ReplicationController{ctrl1},
- },
- name: "foo",
- expectError: true,
- },
- {
- list: &corev1.ReplicationControllerList{
- Items: []corev1.ReplicationController{ctrl1},
- },
- name: "bar",
- expectedController: &ctrl1,
- },
- {
- list: &corev1.ReplicationControllerList{
- Items: []corev1.ReplicationController{ctrl1, ctrl2},
- },
- name: "bar",
- expectedController: &ctrl1,
- },
- {
- list: &corev1.ReplicationControllerList{
- Items: []corev1.ReplicationController{ctrl1, ctrl2},
- },
- name: "foo",
- expectedController: &ctrl2,
- },
- {
- list: &corev1.ReplicationControllerList{
- Items: []corev1.ReplicationController{ctrl1, ctrl2, ctrl3},
- },
- name: "baz",
- expectedController: &ctrl3,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- fakeClient := fake.NewSimpleClientset(tt.list)
- ctrl, err := FindSourceController(fakeClient.CoreV1(), "default", tt.name)
- if tt.expectError && err == nil {
- t.Errorf("unexpected non-error")
- }
- if !tt.expectError && err != nil {
- t.Errorf("unexpected error")
- }
- if !reflect.DeepEqual(ctrl, tt.expectedController) {
- t.Errorf("expected:\n%v\ngot:\n%v\n", tt.expectedController, ctrl)
- }
- })
- }
- }
- func TestUpdateExistingReplicationController(t *testing.T) {
- tests := []struct {
- rc *corev1.ReplicationController
- name string
- deploymentKey string
- deploymentValue string
- expectedRc *corev1.ReplicationController
- expectErr bool
- }{
- {
- rc: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo",
- },
- Spec: corev1.ReplicationControllerSpec{
- Template: &corev1.PodTemplateSpec{},
- },
- },
- name: "foo",
- deploymentKey: "dk",
- deploymentValue: "some-hash",
- expectedRc: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo",
- Annotations: map[string]string{
- "kubectl.kubernetes.io/next-controller-id": "foo",
- },
- },
- Spec: corev1.ReplicationControllerSpec{
- Selector: map[string]string{
- "dk": "some-hash",
- },
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "dk": "some-hash",
- },
- },
- },
- },
- },
- },
- {
- rc: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo",
- },
- Spec: corev1.ReplicationControllerSpec{
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "dk": "some-other-hash",
- },
- },
- },
- Selector: map[string]string{
- "dk": "some-other-hash",
- },
- },
- },
- name: "foo",
- deploymentKey: "dk",
- deploymentValue: "some-hash",
- expectedRc: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: "foo",
- Annotations: map[string]string{
- "kubectl.kubernetes.io/next-controller-id": "foo",
- },
- },
- Spec: corev1.ReplicationControllerSpec{
- Selector: map[string]string{
- "dk": "some-other-hash",
- },
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "dk": "some-other-hash",
- },
- },
- },
- },
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- buffer := &bytes.Buffer{}
- fakeClient := fake.NewSimpleClientset(tt.expectedRc)
- rc, err := UpdateExistingReplicationController(fakeClient.CoreV1(), fakeClient.CoreV1(), tt.rc, "default", tt.name, tt.deploymentKey, tt.deploymentValue, buffer)
- if !reflect.DeepEqual(rc, tt.expectedRc) {
- t.Errorf("expected:\n%#v\ngot:\n%#v\n", tt.expectedRc, rc)
- }
- if tt.expectErr && err == nil {
- t.Errorf("unexpected non-error")
- }
- if !tt.expectErr && err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- })
- }
- }
- func TestUpdateRcWithRetries(t *testing.T) {
- codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
- one := int32(1)
- grace := int64(30)
- enableServiceLinks := corev1.DefaultEnableServiceLinks
- rc := &corev1.ReplicationController{
- TypeMeta: metav1.TypeMeta{
- APIVersion: "v1",
- Kind: "ReplicationController",
- },
- ObjectMeta: metav1.ObjectMeta{Name: "rc",
- Labels: map[string]string{
- "foo": "bar",
- },
- },
- Spec: corev1.ReplicationControllerSpec{
- Replicas: &one,
- Selector: map[string]string{
- "foo": "bar",
- },
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "foo": "bar",
- },
- },
- Spec: corev1.PodSpec{
- RestartPolicy: corev1.RestartPolicyAlways,
- DNSPolicy: corev1.DNSClusterFirst,
- TerminationGracePeriodSeconds: &grace,
- SecurityContext: &corev1.PodSecurityContext{},
- EnableServiceLinks: &enableServiceLinks,
- },
- },
- },
- }
- rc.Spec.Template.Spec.SchedulerName = "default-scheduler"
- // Test end to end updating of the rc with retries. Essentially make sure the update handler
- // sees the right updates, failures in update/get are handled properly, and that the updated
- // rc with new resource version is returned to the caller. Without any of these rollingupdate
- // will fail cryptically.
- newRc := *rc
- newRc.ResourceVersion = "2"
- newRc.Spec.Selector["baz"] = "foobar"
- header := http.Header{}
- header.Set("Content-Type", runtime.ContentTypeJSON)
- updates := []*http.Response{
- {StatusCode: 409, Header: header, Body: objBody(codec, &corev1.ReplicationController{})}, // conflict
- {StatusCode: 409, Header: header, Body: objBody(codec, &corev1.ReplicationController{})}, // conflict
- {StatusCode: 200, Header: header, Body: objBody(codec, &newRc)},
- }
- gets := []*http.Response{
- {StatusCode: 500, Header: header, Body: objBody(codec, &corev1.ReplicationController{})},
- {StatusCode: 200, Header: header, Body: objBody(codec, rc)},
- }
- fakeClient := &manualfake.RESTClient{
- GroupVersion: corev1.SchemeGroupVersion,
- NegotiatedSerializer: scheme.Codecs,
- Client: manualfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == "/api/v1/namespaces/default/replicationcontrollers/rc" && m == "PUT":
- update := updates[0]
- updates = updates[1:]
- // We should always get an update with a valid rc even when the get fails. The rc should always
- // contain the update.
- if c, ok := readOrDie(t, req, codec).(*corev1.ReplicationController); !ok || !apiequality.Semantic.DeepEqual(rc, c) {
- t.Errorf("Unexpected update body, got %+v expected %+v", c, rc)
- t.Error(diff.ObjectDiff(rc, c))
- } else if sel, ok := c.Spec.Selector["baz"]; !ok || sel != "foobar" {
- t.Errorf("Expected selector label update, got %+v", c.Spec.Selector)
- } else {
- delete(c.Spec.Selector, "baz")
- }
- return update, nil
- case p == "/api/v1/namespaces/default/replicationcontrollers/rc" && m == "GET":
- get := gets[0]
- gets = gets[1:]
- return get, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- clientConfig := &restclient.Config{
- APIPath: "/api",
- ContentConfig: restclient.ContentConfig{
- NegotiatedSerializer: scheme.Codecs,
- GroupVersion: &corev1.SchemeGroupVersion,
- },
- }
- restClient, _ := restclient.RESTClientFor(clientConfig)
- restClient.Client = fakeClient.Client
- clientset := kubernetes.New(restClient)
- if rc, err := updateRcWithRetries(
- clientset.CoreV1(), "default", rc, func(c *corev1.ReplicationController) {
- c.Spec.Selector["baz"] = "foobar"
- }); err != nil {
- t.Errorf("unexpected error: %v", err)
- } else if sel, ok := rc.Spec.Selector["baz"]; !ok || sel != "foobar" || rc.ResourceVersion != "2" {
- t.Errorf("Expected updated rc, got %+v", rc)
- }
- if len(updates) != 0 || len(gets) != 0 {
- t.Errorf("Remaining updates %#v gets %#v", updates, gets)
- }
- }
- func readOrDie(t *testing.T, req *http.Request, codec runtime.Codec) runtime.Object {
- data, err := ioutil.ReadAll(req.Body)
- if err != nil {
- t.Errorf("Error reading: %v", err)
- t.FailNow()
- }
- codec2 := scheme.Codecs.UniversalDecoder(scheme.Scheme.PrioritizedVersionsAllGroups()...)
- obj, err := runtime.Decode(codec2, data)
- if err != nil {
- t.Log(string(data))
- t.Errorf("error decoding: %v", err)
- t.FailNow()
- }
- return obj
- }
- func objBody(codec runtime.Codec, obj runtime.Object) io.ReadCloser {
- return ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, obj))))
- }
- func TestAddDeploymentHash(t *testing.T) {
- buf := &bytes.Buffer{}
- codec := scheme.Codecs.LegacyCodec(scheme.Scheme.PrioritizedVersionsAllGroups()...)
- rc := &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{Name: "rc"},
- Spec: corev1.ReplicationControllerSpec{
- Selector: map[string]string{
- "foo": "bar",
- },
- Template: &corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: map[string]string{
- "foo": "bar",
- },
- },
- },
- },
- }
- podList := &corev1.PodList{
- Items: []corev1.Pod{
- {ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
- {ObjectMeta: metav1.ObjectMeta{Name: "bar"}},
- {ObjectMeta: metav1.ObjectMeta{Name: "baz"}},
- },
- }
- seen := sets.String{}
- updatedRc := false
- fakeClient := &manualfake.RESTClient{
- GroupVersion: corev1.SchemeGroupVersion,
- NegotiatedSerializer: scheme.Codecs,
- Client: manualfake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- header := http.Header{}
- header.Set("Content-Type", runtime.ContentTypeJSON)
- switch p, m := req.URL.Path, req.Method; {
- case p == "/api/v1/namespaces/default/pods" && m == "GET":
- if req.URL.RawQuery != "labelSelector=foo%3Dbar" {
- t.Errorf("Unexpected query string: %s", req.URL.RawQuery)
- }
- return &http.Response{StatusCode: 200, Header: header, Body: objBody(codec, podList)}, nil
- case p == "/api/v1/namespaces/default/pods/foo" && m == "PUT":
- seen.Insert("foo")
- obj := readOrDie(t, req, codec)
- podList.Items[0] = *(obj.(*corev1.Pod))
- return &http.Response{StatusCode: 200, Header: header, Body: objBody(codec, &podList.Items[0])}, nil
- case p == "/api/v1/namespaces/default/pods/bar" && m == "PUT":
- seen.Insert("bar")
- obj := readOrDie(t, req, codec)
- podList.Items[1] = *(obj.(*corev1.Pod))
- return &http.Response{StatusCode: 200, Header: header, Body: objBody(codec, &podList.Items[1])}, nil
- case p == "/api/v1/namespaces/default/pods/baz" && m == "PUT":
- seen.Insert("baz")
- obj := readOrDie(t, req, codec)
- podList.Items[2] = *(obj.(*corev1.Pod))
- return &http.Response{StatusCode: 200, Header: header, Body: objBody(codec, &podList.Items[2])}, nil
- case p == "/api/v1/namespaces/default/replicationcontrollers/rc" && m == "PUT":
- updatedRc = true
- return &http.Response{StatusCode: 200, Header: header, Body: objBody(codec, rc)}, nil
- default:
- t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
- return nil, nil
- }
- }),
- }
- clientConfig := &restclient.Config{
- APIPath: "/api",
- ContentConfig: restclient.ContentConfig{
- NegotiatedSerializer: scheme.Codecs,
- GroupVersion: &corev1.SchemeGroupVersion,
- },
- }
- restClient, _ := restclient.RESTClientFor(clientConfig)
- restClient.Client = fakeClient.Client
- clientset := kubernetes.New(restClient)
- if _, err := AddDeploymentKeyToReplicationController(rc, clientset.CoreV1(), clientset.CoreV1(), "dk", "hash", metav1.NamespaceDefault, buf); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- for _, pod := range podList.Items {
- if !seen.Has(pod.Name) {
- t.Errorf("Missing update for pod: %s", pod.Name)
- }
- }
- if !updatedRc {
- t.Errorf("Failed to update replication controller with new labels")
- }
- }
- func TestRollingUpdater_readyPods(t *testing.T) {
- count := 0
- now := metav1.Date(2016, time.April, 1, 1, 0, 0, 0, time.UTC)
- mkpod := func(owner *corev1.ReplicationController, ready bool, readyTime metav1.Time) *corev1.Pod {
- count = count + 1
- labels := map[string]string{}
- for k, v := range owner.Spec.Selector {
- labels[k] = v
- }
- status := corev1.ConditionTrue
- if !ready {
- status = corev1.ConditionFalse
- }
- return &corev1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: metav1.NamespaceDefault,
- Name: fmt.Sprintf("pod-%d", count),
- Labels: labels,
- },
- Status: corev1.PodStatus{
- Conditions: []corev1.PodCondition{
- {
- Type: corev1.PodReady,
- Status: status,
- LastTransitionTime: readyTime,
- },
- },
- },
- }
- }
- tests := []struct {
- name string
- oldRc *corev1.ReplicationController
- newRc *corev1.ReplicationController
- // expectated old/new ready counts
- oldReady int32
- newReady int32
- // pods owned by the rcs; indicate whether they're ready
- oldPods []bool
- newPods []bool
- // deletions - should be less then the size of the respective slice above
- // e.g. len(oldPods) > oldPodDeletions && len(newPods) > newPodDeletions
- oldPodDeletions int
- newPodDeletions int
- // specify additional time to wait for deployment to wait on top of the
- // pod ready time
- minReadySeconds int32
- podReadyTimeFn func() metav1.Time
- nowFn func() metav1.Time
- }{
- {
- name: "test1",
- oldRc: oldRc(4, 4),
- newRc: newRc(4, 4),
- oldReady: 4,
- newReady: 2,
- oldPods: []bool{
- true,
- true,
- true,
- true,
- },
- newPods: []bool{
- true,
- false,
- true,
- false,
- },
- },
- {
- name: "test2",
- oldRc: oldRc(4, 4),
- newRc: newRc(4, 4),
- oldReady: 0,
- newReady: 1,
- oldPods: []bool{
- false,
- },
- newPods: []bool{
- true,
- },
- },
- {
- name: "test3",
- oldRc: oldRc(4, 4),
- newRc: newRc(4, 4),
- oldReady: 1,
- newReady: 0,
- oldPods: []bool{
- true,
- },
- newPods: []bool{
- false,
- },
- },
- {
- name: "test4",
- oldRc: oldRc(4, 4),
- newRc: newRc(4, 4),
- oldReady: 0,
- newReady: 0,
- oldPods: []bool{
- true,
- },
- newPods: []bool{
- true,
- },
- minReadySeconds: 5,
- nowFn: func() metav1.Time { return now },
- },
- {
- name: "test5",
- oldRc: oldRc(4, 4),
- newRc: newRc(4, 4),
- oldReady: 1,
- newReady: 1,
- oldPods: []bool{
- true,
- },
- newPods: []bool{
- true,
- },
- minReadySeconds: 5,
- nowFn: func() metav1.Time { return metav1.Time{Time: now.Add(time.Duration(6 * time.Second))} },
- podReadyTimeFn: func() metav1.Time { return now },
- },
- {
- name: "test6",
- oldRc: oldRc(4, 4),
- newRc: newRc(4, 4),
- oldReady: 2,
- newReady: 0,
- oldPods: []bool{
- // All old pods are ready
- true, true, true, true,
- },
- // Two of them have been marked for deletion though
- oldPodDeletions: 2,
- },
- }
- for i, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- t.Logf("evaluating test %d", i)
- if tt.nowFn == nil {
- tt.nowFn = func() metav1.Time { return now }
- }
- if tt.podReadyTimeFn == nil {
- tt.podReadyTimeFn = tt.nowFn
- }
- // Populate the fake client with pods associated with their owners.
- pods := []runtime.Object{}
- for _, ready := range tt.oldPods {
- pod := mkpod(tt.oldRc, ready, tt.podReadyTimeFn())
- if tt.oldPodDeletions > 0 {
- now := metav1.Now()
- pod.DeletionTimestamp = &now
- tt.oldPodDeletions--
- }
- pods = append(pods, pod)
- }
- for _, ready := range tt.newPods {
- pod := mkpod(tt.newRc, ready, tt.podReadyTimeFn())
- if tt.newPodDeletions > 0 {
- now := metav1.Now()
- pod.DeletionTimestamp = &now
- tt.newPodDeletions--
- }
- pods = append(pods, pod)
- }
- client := fake.NewSimpleClientset(pods...)
- updater := &RollingUpdater{
- ns: "default",
- rcClient: client.CoreV1(),
- podClient: client.CoreV1(),
- nowFn: tt.nowFn,
- }
- oldReady, newReady, err := updater.readyPods(tt.oldRc, tt.newRc, tt.minReadySeconds)
- if err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if e, a := tt.oldReady, oldReady; e != a {
- t.Errorf("expected old ready %d, got %d", e, a)
- }
- if e, a := tt.newReady, newReady; e != a {
- t.Errorf("expected new ready %d, got %d", e, a)
- }
- })
- }
- }
|