12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143 |
- /*
- Copyright 2017 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 ipvs
- import (
- "bytes"
- "fmt"
- "net"
- "reflect"
- "strings"
- "testing"
- "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/types"
- "k8s.io/apimachinery/pkg/util/intstr"
- "k8s.io/kubernetes/pkg/proxy"
- "k8s.io/utils/exec"
- fakeexec "k8s.io/utils/exec/testing"
- "k8s.io/apimachinery/pkg/util/sets"
- netlinktest "k8s.io/kubernetes/pkg/proxy/ipvs/testing"
- utilproxy "k8s.io/kubernetes/pkg/proxy/util"
- proxyutiltest "k8s.io/kubernetes/pkg/proxy/util/testing"
- utilipset "k8s.io/kubernetes/pkg/util/ipset"
- ipsettest "k8s.io/kubernetes/pkg/util/ipset/testing"
- utiliptables "k8s.io/kubernetes/pkg/util/iptables"
- iptablestest "k8s.io/kubernetes/pkg/util/iptables/testing"
- utilipvs "k8s.io/kubernetes/pkg/util/ipvs"
- ipvstest "k8s.io/kubernetes/pkg/util/ipvs/testing"
- )
- const testHostname = "test-hostname"
- type fakeIPGetter struct {
- nodeIPs []net.IP
- }
- func (f *fakeIPGetter) NodeIPs() ([]net.IP, error) {
- return f.nodeIPs, nil
- }
- type fakeHealthChecker struct {
- services map[types.NamespacedName]uint16
- Endpoints map[types.NamespacedName]int
- }
- func newFakeHealthChecker() *fakeHealthChecker {
- return &fakeHealthChecker{
- services: map[types.NamespacedName]uint16{},
- Endpoints: map[types.NamespacedName]int{},
- }
- }
- // fakePortOpener implements portOpener.
- type fakePortOpener struct {
- openPorts []*utilproxy.LocalPort
- }
- // OpenLocalPort fakes out the listen() and bind() used by syncProxyRules
- // to lock a local port.
- func (f *fakePortOpener) OpenLocalPort(lp *utilproxy.LocalPort) (utilproxy.Closeable, error) {
- f.openPorts = append(f.openPorts, lp)
- return nil, nil
- }
- func (fake *fakeHealthChecker) SyncServices(newServices map[types.NamespacedName]uint16) error {
- fake.services = newServices
- return nil
- }
- func (fake *fakeHealthChecker) SyncEndpoints(newEndpoints map[types.NamespacedName]int) error {
- fake.Endpoints = newEndpoints
- return nil
- }
- // fakeKernelHandler implements KernelHandler.
- type fakeKernelHandler struct {
- modules []string
- }
- func (fake *fakeKernelHandler) GetModules() ([]string, error) {
- return fake.modules, nil
- }
- // fakeKernelHandler implements KernelHandler.
- type fakeIPSetVersioner struct {
- version string
- err error
- }
- func (fake *fakeIPSetVersioner) GetVersion() (string, error) {
- return fake.version, fake.err
- }
- func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset utilipset.Interface, nodeIPs []net.IP, excludeCIDRs []*net.IPNet) *Proxier {
- fcmd := fakeexec.FakeCmd{
- CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
- func() ([]byte, error) { return []byte("dummy device have been created"), nil },
- func() ([]byte, error) { return []byte(""), nil },
- },
- }
- fexec := &fakeexec.FakeExec{
- CommandScript: []fakeexec.FakeCommandAction{
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
- },
- LookPathFunc: func(cmd string) (string, error) { return cmd, nil },
- }
- // initialize ipsetList with all sets we needed
- ipsetList := make(map[string]*IPSet)
- for _, is := range ipsetInfo {
- ipsetList[is.name] = NewIPSet(ipset, is.name, is.setType, false, is.comment)
- }
- return &Proxier{
- exec: fexec,
- serviceMap: make(proxy.ServiceMap),
- serviceChanges: proxy.NewServiceChangeTracker(newServiceInfo, nil, nil),
- endpointsMap: make(proxy.EndpointsMap),
- endpointsChanges: proxy.NewEndpointChangeTracker(testHostname, nil, nil, nil),
- excludeCIDRs: excludeCIDRs,
- iptables: ipt,
- ipvs: ipvs,
- ipset: ipset,
- clusterCIDR: "10.0.0.0/24",
- strictARP: false,
- hostname: testHostname,
- portsMap: make(map[utilproxy.LocalPort]utilproxy.Closeable),
- portMapper: &fakePortOpener{[]*utilproxy.LocalPort{}},
- healthChecker: newFakeHealthChecker(),
- ipvsScheduler: DefaultScheduler,
- ipGetter: &fakeIPGetter{nodeIPs: nodeIPs},
- iptablesData: bytes.NewBuffer(nil),
- filterChainsData: bytes.NewBuffer(nil),
- natChains: bytes.NewBuffer(nil),
- natRules: bytes.NewBuffer(nil),
- filterChains: bytes.NewBuffer(nil),
- filterRules: bytes.NewBuffer(nil),
- netlinkHandle: netlinktest.NewFakeNetlinkHandle(),
- ipsetList: ipsetList,
- nodePortAddresses: make([]string, 0),
- networkInterfacer: proxyutiltest.NewFakeNetwork(),
- }
- }
- func makeNSN(namespace, name string) types.NamespacedName {
- return types.NamespacedName{Namespace: namespace, Name: name}
- }
- func makeServiceMap(proxier *Proxier, allServices ...*v1.Service) {
- for i := range allServices {
- proxier.OnServiceAdd(allServices[i])
- }
- proxier.mu.Lock()
- defer proxier.mu.Unlock()
- proxier.servicesSynced = true
- }
- func makeTestService(namespace, name string, svcFunc func(*v1.Service)) *v1.Service {
- svc := &v1.Service{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- Namespace: namespace,
- Annotations: map[string]string{},
- },
- Spec: v1.ServiceSpec{},
- Status: v1.ServiceStatus{},
- }
- svcFunc(svc)
- return svc
- }
- func makeEndpointsMap(proxier *Proxier, allEndpoints ...*v1.Endpoints) {
- for i := range allEndpoints {
- proxier.OnEndpointsAdd(allEndpoints[i])
- }
- proxier.mu.Lock()
- defer proxier.mu.Unlock()
- proxier.endpointsSynced = true
- }
- func makeTestEndpoints(namespace, name string, eptFunc func(*v1.Endpoints)) *v1.Endpoints {
- ept := &v1.Endpoints{
- ObjectMeta: metav1.ObjectMeta{
- Name: name,
- Namespace: namespace,
- },
- }
- eptFunc(ept)
- return ept
- }
- func TestCleanupLeftovers(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- svcIP := "10.20.30.41"
- svcPort := 80
- svcNodePort := 3001
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "NodePort"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- NodePort: int32(svcNodePort),
- }}
- }),
- )
- epIP := "10.180.0.1"
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: epIP,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }},
- }}
- }),
- )
- fp.syncProxyRules()
- // test cleanup left over
- if CleanupLeftovers(ipvs, ipt, ipset, true) {
- t.Errorf("Cleanup leftovers failed")
- }
- }
- func TestCanUseIPVSProxier(t *testing.T) {
- testCases := []struct {
- mods []string
- kernelErr error
- ipsetVersion string
- ipsetErr error
- ok bool
- }{
- // case 0, kernel error
- {
- mods: []string{"foo", "bar", "baz"},
- kernelErr: fmt.Errorf("oops"),
- ipsetVersion: "0.0",
- ok: false,
- },
- // case 1, ipset error
- {
- mods: []string{"foo", "bar", "baz"},
- ipsetVersion: MinIPSetCheckVersion,
- ipsetErr: fmt.Errorf("oops"),
- ok: false,
- },
- // case 2, missing required kernel modules and ipset version too low
- {
- mods: []string{"foo", "bar", "baz"},
- ipsetVersion: "1.1",
- ok: false,
- },
- // case 3, missing required ip_vs_* kernel modules
- {
- mods: []string{"ip_vs", "a", "bc", "def"},
- ipsetVersion: MinIPSetCheckVersion,
- ok: false,
- },
- // case 4, ipset version too low
- {
- mods: []string{"ip_vs", "ip_vs_rr", "ip_vs_wrr", "ip_vs_sh", "nf_conntrack_ipv4"},
- ipsetVersion: "4.3.0",
- ok: false,
- },
- // case 5
- {
- mods: []string{"ip_vs", "ip_vs_rr", "ip_vs_wrr", "ip_vs_sh", "nf_conntrack_ipv4"},
- ipsetVersion: MinIPSetCheckVersion,
- ok: true,
- },
- // case 6
- {
- mods: []string{"ip_vs", "ip_vs_rr", "ip_vs_wrr", "ip_vs_sh", "nf_conntrack_ipv4", "foo", "bar"},
- ipsetVersion: "6.19",
- ok: true,
- },
- }
- for i := range testCases {
- handle := &fakeKernelHandler{modules: testCases[i].mods}
- versioner := &fakeIPSetVersioner{version: testCases[i].ipsetVersion, err: testCases[i].ipsetErr}
- ok, _ := CanUseIPVSProxier(handle, versioner)
- if ok != testCases[i].ok {
- t.Errorf("Case [%d], expect %v, got %v", i, testCases[i].ok, ok)
- }
- }
- }
- func TestGetNodeIPs(t *testing.T) {
- testCases := []struct {
- devAddresses map[string][]string
- expectIPs []string
- }{
- // case 0
- {
- devAddresses: map[string][]string{"eth0": {"1.2.3.4"}, "lo": {"127.0.0.1"}},
- expectIPs: []string{"1.2.3.4", "127.0.0.1"},
- },
- // case 1
- {
- devAddresses: map[string][]string{"lo": {"127.0.0.1"}},
- expectIPs: []string{"127.0.0.1"},
- },
- // case 2
- {
- devAddresses: map[string][]string{},
- expectIPs: []string{},
- },
- // case 3
- {
- devAddresses: map[string][]string{"encap0": {"10.20.30.40"}, "lo": {"127.0.0.1"}, "docker0": {"172.17.0.1"}},
- expectIPs: []string{"10.20.30.40", "127.0.0.1", "172.17.0.1"},
- },
- // case 4
- {
- devAddresses: map[string][]string{"encaps9": {"10.20.30.40"}, "lo": {"127.0.0.1"}, "encap7": {"10.20.30.31"}},
- expectIPs: []string{"10.20.30.40", "127.0.0.1", "10.20.30.31"},
- },
- // case 5
- {
- devAddresses: map[string][]string{"kube-ipvs0": {"1.2.3.4"}, "lo": {"127.0.0.1"}, "encap7": {"10.20.30.31"}},
- expectIPs: []string{"127.0.0.1", "10.20.30.31"},
- },
- // case 6
- {
- devAddresses: map[string][]string{"kube-ipvs0": {"1.2.3.4", "2.3.4.5"}, "lo": {"127.0.0.1"}},
- expectIPs: []string{"127.0.0.1"},
- },
- // case 7
- {
- devAddresses: map[string][]string{"kube-ipvs0": {"1.2.3.4", "2.3.4.5"}},
- expectIPs: []string{},
- },
- // case 8
- {
- devAddresses: map[string][]string{"kube-ipvs0": {"1.2.3.4", "2.3.4.5"}, "eth5": {"3.4.5.6"}, "lo": {"127.0.0.1"}},
- expectIPs: []string{"127.0.0.1", "3.4.5.6"},
- },
- // case 9
- {
- devAddresses: map[string][]string{"ipvs0": {"1.2.3.4"}, "lo": {"127.0.0.1"}, "encap7": {"10.20.30.31"}},
- expectIPs: []string{"127.0.0.1", "10.20.30.31", "1.2.3.4"},
- },
- }
- for i := range testCases {
- fake := netlinktest.NewFakeNetlinkHandle()
- for dev, addresses := range testCases[i].devAddresses {
- fake.SetLocalAddresses(dev, addresses...)
- }
- r := realIPGetter{nl: fake}
- ips, err := r.NodeIPs()
- if err != nil {
- t.Errorf("Unexpected error: %v", err)
- }
- ipStrs := sets.NewString()
- for _, ip := range ips {
- ipStrs.Insert(ip.String())
- }
- if !ipStrs.Equal(sets.NewString(testCases[i].expectIPs...)) {
- t.Errorf("case[%d], unexpected mismatch, expected: %v, got: %v", i, testCases[i].expectIPs, ips)
- }
- }
- }
- func TestNodePortUDP(t *testing.T) {
- nodeIP := net.ParseIP("100.101.102.103")
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, []net.IP{nodeIP}, nil)
- svcIP := "10.20.30.41"
- svcPort := 80
- svcNodePort := 3001
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "NodePort"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolUDP,
- NodePort: int32(svcNodePort),
- }}
- }),
- )
- epIP := "10.180.0.1"
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: epIP,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }},
- }}
- }),
- )
- fp.nodePortAddresses = []string{"0.0.0.0/0"}
- fp.syncProxyRules()
- // Check ipvs service and destinations
- epVS := &netlinktest.ExpectedVirtualServer{
- VSNum: 2, IP: nodeIP.String(), Port: uint16(svcNodePort), Protocol: string(v1.ProtocolTCP),
- RS: []netlinktest.ExpectedRealServer{{
- IP: epIP, Port: uint16(svcPort),
- }}}
- checkIPVS(t, fp, epVS)
- // check ipSet rules
- epIPSet := netlinktest.ExpectedIPSet{
- kubeNodePortSetUDP: {{
- Port: svcNodePort,
- Protocol: strings.ToLower(string(v1.ProtocolUDP)),
- SetType: utilipset.BitmapPort,
- }},
- }
- checkIPSet(t, fp, epIPSet)
- // Check iptables chain and rules
- epIpt := netlinktest.ExpectedIptablesChain{
- string(KubeNodePortChain): {{
- JumpChain: string(KubeMarkMasqChain), MatchSet: kubeNodePortSetUDP,
- }},
- string(kubeServicesChain): {{
- JumpChain: string(KubeNodePortChain), MatchSet: "",
- }},
- }
- checkIptables(t, ipt, epIpt)
- }
- func TestNodePort(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- nodeIPv4 := net.ParseIP("100.101.102.103")
- nodeIPv6 := net.ParseIP("2001:db8::1:1")
- nodeIPs := sets.NewString(nodeIPv4.String(), nodeIPv6.String())
- fp := NewFakeProxier(ipt, ipvs, ipset, []net.IP{nodeIPv4, nodeIPv6}, nil)
- svcIP := "10.20.30.41"
- svcPort := 80
- svcNodePort := 3001
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "NodePort"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- NodePort: int32(svcNodePort),
- }}
- }),
- )
- epIPv4 := "10.180.0.1"
- epIPv6 := "1002:ab8::2:10"
- epIPs := sets.NewString(epIPv4, epIPv6)
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: epIPv4,
- }, {
- IP: epIPv6,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }},
- }}
- }),
- )
- fp.nodePortAddresses = []string{"0.0.0.0/0"}
- fp.syncProxyRules()
- // Check ipvs service and destinations
- services, err := ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Failed to get ipvs services, err: %v", err)
- }
- if len(services) != 3 {
- t.Errorf("Expect 3 ipvs services, got %d", len(services))
- }
- found := false
- for _, svc := range services {
- if nodeIPs.Has(svc.Address.String()) && svc.Port == uint16(svcNodePort) && svc.Protocol == string(v1.ProtocolTCP) {
- found = true
- destinations, err := ipvs.GetRealServers(svc)
- if err != nil {
- t.Errorf("Failed to get ipvs destinations, err: %v", err)
- }
- for _, dest := range destinations {
- if !epIPs.Has(dest.Address.String()) || dest.Port != uint16(svcPort) {
- t.Errorf("service Endpoint mismatch ipvs service destination")
- }
- }
- break
- }
- }
- if !found {
- t.Errorf("Expect node port type service, got none")
- }
- }
- func TestNodePortNoEndpoint(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- nodeIP := net.ParseIP("100.101.102.103")
- fp := NewFakeProxier(ipt, ipvs, ipset, []net.IP{nodeIP}, nil)
- svcIP := "10.20.30.41"
- svcPort := 80
- svcNodePort := 3001
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "NodePort"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- NodePort: int32(svcNodePort),
- }}
- }),
- )
- makeEndpointsMap(fp)
- fp.nodePortAddresses = []string{"0.0.0.0/0"}
- fp.syncProxyRules()
- // Check ipvs service and destinations
- services, err := ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Failed to get ipvs services, err: %v", err)
- }
- if len(services) != 2 {
- t.Errorf("Expect 2 ipvs services, got %d", len(services))
- }
- found := false
- for _, svc := range services {
- if svc.Address.Equal(nodeIP) && svc.Port == uint16(svcNodePort) && svc.Protocol == string(v1.ProtocolTCP) {
- found = true
- destinations, _ := ipvs.GetRealServers(svc)
- if len(destinations) != 0 {
- t.Errorf("Unexpected %d destinations, expect 0 destinations", len(destinations))
- }
- break
- }
- }
- if !found {
- t.Errorf("Expect node port type service, got none")
- }
- }
- func TestClusterIPNoEndpoint(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- svcIP := "10.20.30.41"
- svcPort := 80
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Namespace, func(svc *v1.Service) {
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- }}
- }),
- )
- makeEndpointsMap(fp)
- fp.syncProxyRules()
- // check ipvs service and destinations
- services, err := ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Failed to get ipvs services, err: %v", err)
- }
- if len(services) != 1 {
- t.Errorf("Expect 1 ipvs services, got %d", len(services))
- } else {
- if services[0].Address.String() != svcIP || services[0].Port != uint16(svcPort) || services[0].Protocol != string(v1.ProtocolTCP) {
- t.Errorf("Unexpected mismatch service")
- } else {
- destinations, _ := ipvs.GetRealServers(services[0])
- if len(destinations) != 0 {
- t.Errorf("Unexpected %d destinations, expect 0 destinations", len(destinations))
- }
- }
- }
- }
- func TestClusterIP(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- svcIPv4 := "10.20.30.41"
- svcPortV4 := 80
- svcPortNameV4 := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- svcIPv6 := "1002:ab8::2:1"
- svcPortV6 := 8080
- svcPortNameV6 := proxy.ServicePortName{
- NamespacedName: makeNSN("ns2", "svc2"),
- Port: "p8080",
- }
- makeServiceMap(fp,
- makeTestService(svcPortNameV4.Namespace, svcPortNameV4.Name, func(svc *v1.Service) {
- svc.Spec.ClusterIP = svcIPv4
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortNameV4.Port,
- Port: int32(svcPortV4),
- Protocol: v1.ProtocolTCP,
- }}
- }),
- makeTestService(svcPortNameV6.Namespace, svcPortNameV6.Name, func(svc *v1.Service) {
- svc.Spec.ClusterIP = svcIPv6
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortNameV6.Port,
- Port: int32(svcPortV6),
- Protocol: v1.ProtocolTCP,
- }}
- }),
- )
- epIPv4 := "10.180.0.1"
- epIPv6 := "1009:ab8::5:6"
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortNameV4.Namespace, svcPortNameV4.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: epIPv4,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortNameV4.Port,
- Port: int32(svcPortV4),
- }},
- }}
- }),
- makeTestEndpoints(svcPortNameV6.Namespace, svcPortNameV6.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: epIPv6,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortNameV6.Port,
- Port: int32(svcPortV6),
- }},
- }}
- }),
- )
- fp.syncProxyRules()
- // check ipvs service and destinations
- services, err := ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Failed to get ipvs services, err: %v", err)
- }
- if len(services) != 2 {
- t.Errorf("Expect 2 ipvs services, got %d", len(services))
- }
- for i := range services {
- // Check services
- if services[i].Address.String() == svcIPv4 {
- if services[i].Port != uint16(svcPortV4) || services[i].Protocol != string(v1.ProtocolTCP) {
- t.Errorf("Unexpected mismatch service")
- }
- // Check destinations
- destinations, _ := ipvs.GetRealServers(services[i])
- if len(destinations) != 1 {
- t.Errorf("Expected 1 destinations, got %d destinations", len(destinations))
- continue
- }
- if destinations[0].Address.String() != epIPv4 || destinations[0].Port != uint16(svcPortV4) {
- t.Errorf("Unexpected mismatch destinations")
- }
- }
- if services[i].Address.String() == svcIPv6 {
- if services[i].Port != uint16(svcPortV6) || services[i].Protocol != string(v1.ProtocolTCP) {
- t.Errorf("Unexpected mismatch service")
- }
- // Check destinations
- destinations, _ := ipvs.GetRealServers(services[i])
- if len(destinations) != 1 {
- t.Errorf("Expected 1 destinations, got %d destinations", len(destinations))
- continue
- }
- if destinations[0].Address.String() != epIPv6 || destinations[0].Port != uint16(svcPortV6) {
- t.Errorf("Unexpected mismatch destinations")
- }
- }
- }
- }
- func TestExternalIPsNoEndpoint(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- svcIP := "10.20.30.41"
- svcPort := 80
- svcExternalIPs := "50.60.70.81"
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "ClusterIP"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.ExternalIPs = []string{svcExternalIPs}
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- TargetPort: intstr.FromInt(svcPort),
- }}
- }),
- )
- makeEndpointsMap(fp)
- fp.syncProxyRules()
- // check ipvs service and destinations
- services, err := ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Failed to get ipvs services, err: %v", err)
- }
- if len(services) != 2 {
- t.Errorf("Expect 2 ipvs services, got %d", len(services))
- }
- found := false
- for _, svc := range services {
- if svc.Address.String() == svcExternalIPs && svc.Port == uint16(svcPort) && svc.Protocol == string(v1.ProtocolTCP) {
- found = true
- destinations, _ := ipvs.GetRealServers(svc)
- if len(destinations) != 0 {
- t.Errorf("Unexpected %d destinations, expect 0 destinations", len(destinations))
- }
- break
- }
- }
- if !found {
- t.Errorf("Expect external ip type service, got none")
- }
- }
- func TestExternalIPs(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- svcIP := "10.20.30.41"
- svcPort := 80
- svcExternalIPs := sets.NewString("50.60.70.81", "2012::51", "127.0.0.1")
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "ClusterIP"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.ExternalIPs = svcExternalIPs.UnsortedList()
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- TargetPort: intstr.FromInt(svcPort),
- }}
- }),
- )
- epIP := "10.180.0.1"
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: epIP,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }},
- }}
- }),
- )
- fp.syncProxyRules()
- // check ipvs service and destinations
- services, err := ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Failed to get ipvs services, err: %v", err)
- }
- if len(services) != 4 {
- t.Errorf("Expect 4 ipvs services, got %d", len(services))
- }
- found := false
- for _, svc := range services {
- if svcExternalIPs.Has(svc.Address.String()) && svc.Port == uint16(svcPort) && svc.Protocol == string(v1.ProtocolTCP) {
- found = true
- destinations, _ := ipvs.GetRealServers(svc)
- for _, dest := range destinations {
- if dest.Address.String() != epIP || dest.Port != uint16(svcPort) {
- t.Errorf("service Endpoint mismatch ipvs service destination")
- }
- }
- break
- }
- }
- if !found {
- t.Errorf("Expect external ip type service, got none")
- }
- }
- func TestLoadBalancer(t *testing.T) {
- ipt, fp := buildFakeProxier()
- svcIP := "10.20.30.41"
- svcPort := 80
- svcNodePort := 3001
- svcLBIP := "1.2.3.4"
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "LoadBalancer"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- NodePort: int32(svcNodePort),
- }}
- svc.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{{
- IP: svcLBIP,
- }}
- }),
- )
- epIP := "10.180.0.1"
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: epIP,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }},
- }}
- }),
- )
- fp.syncProxyRules()
- // Expect 2 services and 1 destination
- epVS := &netlinktest.ExpectedVirtualServer{
- VSNum: 2, IP: svcLBIP, Port: uint16(svcNodePort), Protocol: string(v1.ProtocolTCP),
- RS: []netlinktest.ExpectedRealServer{{
- IP: epIP, Port: uint16(svcPort),
- }}}
- checkIPVS(t, fp, epVS)
- // check ipSet rules
- epIPSet := netlinktest.ExpectedIPSet{
- kubeLoadBalancerSet: {{
- IP: svcLBIP,
- Port: svcPort,
- Protocol: strings.ToLower(string(v1.ProtocolTCP)),
- SetType: utilipset.HashIPPort,
- }},
- }
- checkIPSet(t, fp, epIPSet)
- // Check iptables chain and rules
- epIpt := netlinktest.ExpectedIptablesChain{
- string(kubeServicesChain): {{
- JumpChain: string(KubeLoadBalancerChain), MatchSet: kubeLoadBalancerSet,
- }},
- string(kubeLoadBalancerSet): {{
- JumpChain: string(KubeMarkMasqChain), MatchSet: "",
- }},
- }
- checkIptables(t, ipt, epIpt)
- }
- func TestOnlyLocalNodePorts(t *testing.T) {
- nodeIP := net.ParseIP("100.101.102.103")
- ipt, fp := buildFakeProxier()
- svcIP := "10.20.30.41"
- svcPort := 80
- svcNodePort := 3001
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "NodePort"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- NodePort: int32(svcNodePort),
- }}
- svc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeLocal
- }),
- )
- epIP := "10.180.0.1"
- epIP1 := "10.180.1.1"
- thisHostname := testHostname
- otherHostname := "other-hostname"
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{
- { // **local** endpoint address, should be added as RS
- Addresses: []v1.EndpointAddress{{
- IP: epIP,
- NodeName: &thisHostname,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }}},
- { // **remote** endpoint address, should not be added as RS
- Addresses: []v1.EndpointAddress{{
- IP: epIP1,
- NodeName: &otherHostname,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }},
- }}
- }),
- )
- itf := net.Interface{Index: 0, MTU: 0, Name: "eth0", HardwareAddr: nil, Flags: 0}
- addrs := []net.Addr{proxyutiltest.AddrStruct{Val: "100.101.102.103/24"}}
- itf1 := net.Interface{Index: 1, MTU: 0, Name: "eth1", HardwareAddr: nil, Flags: 0}
- addrs1 := []net.Addr{proxyutiltest.AddrStruct{Val: "2001:db8::0/64"}}
- fp.networkInterfacer.(*proxyutiltest.FakeNetwork).AddInterfaceAddr(&itf, addrs)
- fp.networkInterfacer.(*proxyutiltest.FakeNetwork).AddInterfaceAddr(&itf1, addrs1)
- fp.nodePortAddresses = []string{"100.101.102.0/24", "2001:db8::0/64"}
- fp.syncProxyRules()
- // Expect 3 services and 1 destination
- epVS := &netlinktest.ExpectedVirtualServer{
- VSNum: 3, IP: nodeIP.String(), Port: uint16(svcNodePort), Protocol: string(v1.ProtocolTCP),
- RS: []netlinktest.ExpectedRealServer{{
- IP: epIP, Port: uint16(svcPort),
- }}}
- checkIPVS(t, fp, epVS)
- // check ipSet rules
- epEntry := &utilipset.Entry{
- Port: svcNodePort,
- Protocol: strings.ToLower(string(v1.ProtocolTCP)),
- SetType: utilipset.BitmapPort,
- }
- epIPSet := netlinktest.ExpectedIPSet{
- kubeNodePortSetTCP: {epEntry},
- kubeNodePortLocalSetTCP: {epEntry},
- }
- checkIPSet(t, fp, epIPSet)
- // Check iptables chain and rules
- epIpt := netlinktest.ExpectedIptablesChain{
- string(kubeServicesChain): {{
- JumpChain: string(KubeNodePortChain), MatchSet: "",
- }},
- string(KubeNodePortChain): {{
- JumpChain: "RETURN", MatchSet: kubeNodePortLocalSetTCP,
- }, {
- JumpChain: string(KubeMarkMasqChain), MatchSet: kubeNodePortSetTCP,
- }},
- }
- checkIptables(t, ipt, epIpt)
- }
- func TestLoadBalanceSourceRanges(t *testing.T) {
- ipt, fp := buildFakeProxier()
- svcIP := "10.20.30.41"
- svcPort := 80
- svcLBIP := "1.2.3.4"
- svcLBSource := "10.0.0.0/8"
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- epIP := "10.180.0.1"
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "LoadBalancer"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- }}
- svc.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{{
- IP: svcLBIP,
- }}
- svc.Spec.LoadBalancerSourceRanges = []string{
- svcLBSource,
- }
- }),
- )
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: epIP,
- NodeName: nil,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }},
- }}
- }),
- )
- fp.syncProxyRules()
- // Check ipvs service and destinations
- epVS := &netlinktest.ExpectedVirtualServer{
- VSNum: 2, IP: svcLBIP, Port: uint16(svcPort), Protocol: string(v1.ProtocolTCP),
- RS: []netlinktest.ExpectedRealServer{{
- IP: epIP, Port: uint16(svcPort),
- }}}
- checkIPVS(t, fp, epVS)
- // Check ipset entry
- epIPSet := netlinktest.ExpectedIPSet{
- kubeLoadBalancerSet: {{
- IP: svcLBIP,
- Port: svcPort,
- Protocol: strings.ToLower(string(v1.ProtocolTCP)),
- SetType: utilipset.HashIPPort,
- }},
- kubeLoadbalancerFWSet: {{
- IP: svcLBIP,
- Port: svcPort,
- Protocol: strings.ToLower(string(v1.ProtocolTCP)),
- SetType: utilipset.HashIPPort,
- }},
- kubeLoadBalancerSourceCIDRSet: {{
- IP: svcLBIP,
- Port: svcPort,
- Protocol: strings.ToLower(string(v1.ProtocolTCP)),
- Net: svcLBSource,
- SetType: utilipset.HashIPPortNet,
- }},
- }
- checkIPSet(t, fp, epIPSet)
- // Check iptables chain and rules
- epIpt := netlinktest.ExpectedIptablesChain{
- string(kubeServicesChain): {{
- JumpChain: string(KubeLoadBalancerChain), MatchSet: kubeLoadBalancerSet,
- }},
- string(KubeLoadBalancerChain): {{
- JumpChain: string(KubeFireWallChain), MatchSet: kubeLoadbalancerFWSet,
- }, {
- JumpChain: string(KubeMarkMasqChain), MatchSet: "",
- }},
- string(KubeFireWallChain): {{
- JumpChain: "RETURN", MatchSet: kubeLoadBalancerSourceCIDRSet,
- }, {
- JumpChain: string(KubeMarkDropChain), MatchSet: "",
- }},
- }
- checkIptables(t, ipt, epIpt)
- }
- func TestAcceptIPVSTraffic(t *testing.T) {
- ipt, fp := buildFakeProxier()
- ingressIP := "1.2.3.4"
- externalIP := []string{"5.6.7.8"}
- svcInfos := []struct {
- svcType v1.ServiceType
- svcIP string
- svcName string
- epIP string
- }{
- {v1.ServiceTypeClusterIP, "10.20.30.40", "svc1", "10.180.0.1"},
- {v1.ServiceTypeLoadBalancer, "10.20.30.41", "svc2", "10.180.0.2"},
- {v1.ServiceTypeNodePort, "10.20.30.42", "svc3", "10.180.0.3"},
- }
- for _, svcInfo := range svcInfos {
- makeServiceMap(fp,
- makeTestService("ns1", svcInfo.svcName, func(svc *v1.Service) {
- svc.Spec.Type = svcInfo.svcType
- svc.Spec.ClusterIP = svcInfo.svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: "p80",
- Port: 80,
- Protocol: v1.ProtocolTCP,
- NodePort: 80,
- }}
- if svcInfo.svcType == v1.ServiceTypeLoadBalancer {
- svc.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{{
- IP: ingressIP,
- }}
- }
- if svcInfo.svcType == v1.ServiceTypeClusterIP {
- svc.Spec.ExternalIPs = externalIP
- }
- }),
- )
- makeEndpointsMap(fp,
- makeTestEndpoints("ns1", "p80", func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: svcInfo.epIP,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p80",
- Port: 80,
- }},
- }}
- }),
- )
- }
- fp.syncProxyRules()
- // Check iptables chain and rules
- epIpt := netlinktest.ExpectedIptablesChain{
- string(kubeServicesChain): {
- {JumpChain: "ACCEPT", MatchSet: kubeClusterIPSet},
- {JumpChain: "ACCEPT", MatchSet: kubeLoadBalancerSet},
- {JumpChain: "ACCEPT", MatchSet: kubeExternalIPSet},
- },
- }
- checkIptables(t, ipt, epIpt)
- }
- func TestOnlyLocalLoadBalancing(t *testing.T) {
- ipt, fp := buildFakeProxier()
- svcIP := "10.20.30.41"
- svcPort := 80
- svcNodePort := 3001
- svcLBIP := "1.2.3.4"
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "LoadBalancer"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- NodePort: int32(svcNodePort),
- }}
- svc.Status.LoadBalancer.Ingress = []v1.LoadBalancerIngress{{
- IP: svcLBIP,
- }}
- svc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeLocal
- }),
- )
- epIP := "10.180.0.1"
- epIP1 := "10.180.1.1"
- thisHostname := testHostname
- otherHostname := "other-hostname"
- makeEndpointsMap(fp,
- makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{
- { // **local** endpoint address, should be added as RS
- Addresses: []v1.EndpointAddress{{
- IP: epIP,
- NodeName: &thisHostname,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }}},
- { // **remote** endpoint address, should not be added as RS
- Addresses: []v1.EndpointAddress{{
- IP: epIP1,
- NodeName: &otherHostname,
- }},
- Ports: []v1.EndpointPort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- }},
- }}
- }),
- )
- fp.syncProxyRules()
- // Expect 2 services and 1 destination
- epVS := &netlinktest.ExpectedVirtualServer{
- VSNum: 2, IP: svcLBIP, Port: uint16(svcPort), Protocol: string(v1.ProtocolTCP),
- RS: []netlinktest.ExpectedRealServer{{
- IP: epIP, Port: uint16(svcPort),
- }}}
- checkIPVS(t, fp, epVS)
- // check ipSet rules
- epIPSet := netlinktest.ExpectedIPSet{
- kubeLoadBalancerSet: {{
- IP: svcLBIP,
- Port: svcPort,
- Protocol: strings.ToLower(string(v1.ProtocolTCP)),
- SetType: utilipset.HashIPPort,
- }},
- kubeLoadBalancerLocalSet: {{
- IP: svcLBIP,
- Port: svcPort,
- Protocol: strings.ToLower(string(v1.ProtocolTCP)),
- SetType: utilipset.HashIPPort,
- }},
- }
- checkIPSet(t, fp, epIPSet)
- // Check iptables chain and rules
- epIpt := netlinktest.ExpectedIptablesChain{
- string(kubeServicesChain): {{
- JumpChain: string(KubeLoadBalancerChain), MatchSet: kubeLoadBalancerSet,
- }},
- string(KubeLoadBalancerChain): {{
- JumpChain: "RETURN", MatchSet: kubeLoadBalancerLocalSet,
- }, {
- JumpChain: string(KubeMarkMasqChain), MatchSet: "",
- }},
- }
- checkIptables(t, ipt, epIpt)
- }
- func addTestPort(array []v1.ServicePort, name string, protocol v1.Protocol, port, nodeport int32, targetPort int) []v1.ServicePort {
- svcPort := v1.ServicePort{
- Name: name,
- Protocol: protocol,
- Port: port,
- NodePort: nodeport,
- TargetPort: intstr.FromInt(targetPort),
- }
- return append(array, svcPort)
- }
- func TestBuildServiceMapAddRemove(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- services := []*v1.Service{
- makeTestService("somewhere-else", "cluster-ip", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = "172.16.55.4"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "something", "UDP", 1234, 4321, 0)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "somethingelse", "UDP", 1235, 5321, 0)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "somesctp", "SCTP", 1236, 6321, 0)
- }),
- makeTestService("somewhere-else", "node-port", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeNodePort
- svc.Spec.ClusterIP = "172.16.55.10"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "blahblah", "UDP", 345, 678, 0)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "moreblahblah", "TCP", 344, 677, 0)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "sctpblah", "SCTP", 343, 676, 0)
- }),
- makeTestService("somewhere", "load-balancer", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeLoadBalancer
- svc.Spec.ClusterIP = "172.16.55.11"
- svc.Spec.LoadBalancerIP = "5.6.7.8"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "foobar", "UDP", 8675, 30061, 7000)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "baz", "UDP", 8676, 30062, 7001)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "sctpfoo", "SCTP", 8677, 30063, 7002)
- svc.Status.LoadBalancer = v1.LoadBalancerStatus{
- Ingress: []v1.LoadBalancerIngress{
- {IP: "10.1.2.4"},
- },
- }
- }),
- makeTestService("somewhere", "only-local-load-balancer", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeLoadBalancer
- svc.Spec.ClusterIP = "172.16.55.12"
- svc.Spec.LoadBalancerIP = "5.6.7.8"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "foobar2", "UDP", 8677, 30063, 7002)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "baz", "UDP", 8678, 30064, 7003)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "sctpbaz", "SCTP", 8679, 30065, 7004)
- svc.Status.LoadBalancer = v1.LoadBalancerStatus{
- Ingress: []v1.LoadBalancerIngress{
- {IP: "10.1.2.3"},
- },
- }
- svc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeLocal
- svc.Spec.HealthCheckNodePort = 345
- }),
- }
- for i := range services {
- fp.OnServiceAdd(services[i])
- }
- result := proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
- if len(fp.serviceMap) != 12 {
- t.Errorf("expected service map length 12, got %v", fp.serviceMap)
- }
- // The only-local-loadbalancer ones get added
- if len(result.HCServiceNodePorts) != 1 {
- t.Errorf("expected 1 healthcheck port, got %v", result.HCServiceNodePorts)
- } else {
- nsn := makeNSN("somewhere", "only-local-load-balancer")
- if port, found := result.HCServiceNodePorts[nsn]; !found || port != 345 {
- t.Errorf("expected healthcheck port [%q]=345: got %v", nsn, result.HCServiceNodePorts)
- }
- }
- if len(result.UDPStaleClusterIP) != 0 {
- // Services only added, so nothing stale yet
- t.Errorf("expected stale UDP services length 0, got %d", len(result.UDPStaleClusterIP))
- }
- // Remove some stuff
- // oneService is a modification of services[0] with removed first port.
- oneService := makeTestService("somewhere-else", "cluster-ip", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = "172.16.55.4"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "somethingelse", "UDP", 1235, 5321, 0)
- })
- fp.OnServiceUpdate(services[0], oneService)
- fp.OnServiceDelete(services[1])
- fp.OnServiceDelete(services[2])
- fp.OnServiceDelete(services[3])
- result = proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
- if len(fp.serviceMap) != 1 {
- t.Errorf("expected service map length 1, got %v", fp.serviceMap)
- }
- if len(result.HCServiceNodePorts) != 0 {
- t.Errorf("expected 0 healthcheck ports, got %v", result.HCServiceNodePorts)
- }
- // All services but one were deleted. While you'd expect only the ClusterIPs
- // from the three deleted services here, we still have the ClusterIP for
- // the not-deleted service, because one of it's ServicePorts was deleted.
- expectedStaleUDPServices := []string{"172.16.55.10", "172.16.55.4", "172.16.55.11", "172.16.55.12"}
- if len(result.UDPStaleClusterIP) != len(expectedStaleUDPServices) {
- t.Errorf("expected stale UDP services length %d, got %v", len(expectedStaleUDPServices), result.UDPStaleClusterIP.List())
- }
- for _, ip := range expectedStaleUDPServices {
- if !result.UDPStaleClusterIP.Has(ip) {
- t.Errorf("expected stale UDP service service %s", ip)
- }
- }
- }
- func TestBuildServiceMapServiceHeadless(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- makeServiceMap(fp,
- makeTestService("somewhere-else", "headless", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = v1.ClusterIPNone
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "rpc", "UDP", 1234, 0, 0)
- }),
- makeTestService("somewhere-else", "headless-without-port", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = v1.ClusterIPNone
- }),
- makeTestService("somewhere-else", "headless-sctp", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = v1.ClusterIPNone
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "sip", "SCTP", 1235, 0, 0)
- }),
- )
- // Headless service should be ignored
- result := proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
- if len(fp.serviceMap) != 0 {
- t.Errorf("expected service map length 0, got %d", len(fp.serviceMap))
- }
- // No proxied services, so no healthchecks
- if len(result.HCServiceNodePorts) != 0 {
- t.Errorf("expected healthcheck ports length 0, got %d", len(result.HCServiceNodePorts))
- }
- if len(result.UDPStaleClusterIP) != 0 {
- t.Errorf("expected stale UDP services length 0, got %d", len(result.UDPStaleClusterIP))
- }
- }
- func TestBuildServiceMapServiceTypeExternalName(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- makeServiceMap(fp,
- makeTestService("somewhere-else", "external-name", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeExternalName
- svc.Spec.ClusterIP = "172.16.55.4" // Should be ignored
- svc.Spec.ExternalName = "foo2.bar.com"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "blah", "UDP", 1235, 5321, 0)
- }),
- )
- result := proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
- if len(fp.serviceMap) != 0 {
- t.Errorf("expected service map length 0, got %v", fp.serviceMap)
- }
- // No proxied services, so no healthchecks
- if len(result.HCServiceNodePorts) != 0 {
- t.Errorf("expected healthcheck ports length 0, got %v", result.HCServiceNodePorts)
- }
- if len(result.UDPStaleClusterIP) != 0 {
- t.Errorf("expected stale UDP services length 0, got %v", result.UDPStaleClusterIP)
- }
- }
- func TestBuildServiceMapServiceUpdate(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- servicev1 := makeTestService("somewhere", "some-service", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = "172.16.55.4"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "something", "UDP", 1234, 4321, 0)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "somethingelse", "TCP", 1235, 5321, 0)
- })
- servicev2 := makeTestService("somewhere", "some-service", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeLoadBalancer
- svc.Spec.ClusterIP = "172.16.55.4"
- svc.Spec.LoadBalancerIP = "5.6.7.8"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "something", "UDP", 1234, 4321, 7002)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "somethingelse", "TCP", 1235, 5321, 7003)
- svc.Status.LoadBalancer = v1.LoadBalancerStatus{
- Ingress: []v1.LoadBalancerIngress{
- {IP: "10.1.2.3"},
- },
- }
- svc.Spec.ExternalTrafficPolicy = v1.ServiceExternalTrafficPolicyTypeLocal
- svc.Spec.HealthCheckNodePort = 345
- })
- fp.OnServiceAdd(servicev1)
- result := proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
- if len(fp.serviceMap) != 2 {
- t.Errorf("expected service map length 2, got %v", fp.serviceMap)
- }
- if len(result.HCServiceNodePorts) != 0 {
- t.Errorf("expected healthcheck ports length 0, got %v", result.HCServiceNodePorts)
- }
- if len(result.UDPStaleClusterIP) != 0 {
- // Services only added, so nothing stale yet
- t.Errorf("expected stale UDP services length 0, got %d", len(result.UDPStaleClusterIP))
- }
- // Change service to load-balancer
- fp.OnServiceUpdate(servicev1, servicev2)
- result = proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
- if len(fp.serviceMap) != 2 {
- t.Errorf("expected service map length 2, got %v", fp.serviceMap)
- }
- if len(result.HCServiceNodePorts) != 1 {
- t.Errorf("expected healthcheck ports length 1, got %v", result.HCServiceNodePorts)
- }
- if len(result.UDPStaleClusterIP) != 0 {
- t.Errorf("expected stale UDP services length 0, got %v", result.UDPStaleClusterIP.List())
- }
- // No change; make sure the service map stays the same and there are
- // no health-check changes
- fp.OnServiceUpdate(servicev2, servicev2)
- result = proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
- if len(fp.serviceMap) != 2 {
- t.Errorf("expected service map length 2, got %v", fp.serviceMap)
- }
- if len(result.HCServiceNodePorts) != 1 {
- t.Errorf("expected healthcheck ports length 1, got %v", result.HCServiceNodePorts)
- }
- if len(result.UDPStaleClusterIP) != 0 {
- t.Errorf("expected stale UDP services length 0, got %v", result.UDPStaleClusterIP.List())
- }
- // And back to ClusterIP
- fp.OnServiceUpdate(servicev2, servicev1)
- result = proxy.UpdateServiceMap(fp.serviceMap, fp.serviceChanges)
- if len(fp.serviceMap) != 2 {
- t.Errorf("expected service map length 2, got %v", fp.serviceMap)
- }
- if len(result.HCServiceNodePorts) != 0 {
- t.Errorf("expected healthcheck ports length 0, got %v", result.HCServiceNodePorts)
- }
- if len(result.UDPStaleClusterIP) != 0 {
- // Services only added, so nothing stale yet
- t.Errorf("expected stale UDP services length 0, got %d", len(result.UDPStaleClusterIP))
- }
- }
- func TestSessionAffinity(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- nodeIP := net.ParseIP("100.101.102.103")
- fp := NewFakeProxier(ipt, ipvs, ipset, []net.IP{nodeIP}, nil)
- svcIP := "10.20.30.41"
- svcPort := 80
- svcNodePort := 3001
- svcExternalIPs := "50.60.70.81"
- svcPortName := proxy.ServicePortName{
- NamespacedName: makeNSN("ns1", "svc1"),
- Port: "p80",
- }
- timeoutSeconds := v1.DefaultClientIPServiceAffinitySeconds
- makeServiceMap(fp,
- makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
- svc.Spec.Type = "NodePort"
- svc.Spec.ClusterIP = svcIP
- svc.Spec.ExternalIPs = []string{svcExternalIPs}
- svc.Spec.SessionAffinity = v1.ServiceAffinityClientIP
- svc.Spec.SessionAffinityConfig = &v1.SessionAffinityConfig{
- ClientIP: &v1.ClientIPConfig{
- TimeoutSeconds: &timeoutSeconds,
- },
- }
- svc.Spec.Ports = []v1.ServicePort{{
- Name: svcPortName.Port,
- Port: int32(svcPort),
- Protocol: v1.ProtocolTCP,
- NodePort: int32(svcNodePort),
- }}
- }),
- )
- makeEndpointsMap(fp)
- fp.syncProxyRules()
- // check ipvs service and destinations
- services, err := ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Failed to get ipvs services, err: %v", err)
- }
- for _, svc := range services {
- if svc.Timeout != uint32(v1.DefaultClientIPServiceAffinitySeconds) {
- t.Errorf("Unexpected mismatch ipvs service session affinity timeout: %d, expected: %d", svc.Timeout, v1.DefaultClientIPServiceAffinitySeconds)
- }
- }
- }
- func makeServicePortName(ns, name, port string) proxy.ServicePortName {
- return proxy.ServicePortName{
- NamespacedName: makeNSN(ns, name),
- Port: port,
- }
- }
- func Test_updateEndpointsMap(t *testing.T) {
- var nodeName = testHostname
- emptyEndpoint := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{}
- }
- unnamedPort := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }},
- Ports: []v1.EndpointPort{{
- Port: 11,
- }},
- }}
- }
- unnamedPortLocal := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Port: 11,
- }},
- }}
- }
- namedPortLocal := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }},
- }}
- }
- namedPort := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }},
- }}
- }
- namedPortRenamed := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11-2",
- Port: 11,
- }},
- }}
- }
- namedPortRenumbered := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 22,
- }},
- }}
- }
- namedPortsLocalNoLocal := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }, {
- IP: "1.1.1.2",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }, {
- Name: "p12",
- Port: 12,
- }},
- }}
- }
- multipleSubsets := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }},
- }, {
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.2",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p12",
- Port: 12,
- }},
- }}
- }
- multipleSubsetsWithLocal := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }},
- }, {
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.2",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p12",
- Port: 12,
- }},
- }}
- }
- multipleSubsetsMultiplePortsLocal := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }, {
- Name: "p12",
- Port: 12,
- }},
- }, {
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.3",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p13",
- Port: 13,
- }},
- }}
- }
- multipleSubsetsIPsPorts1 := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }, {
- IP: "1.1.1.2",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }, {
- Name: "p12",
- Port: 12,
- }},
- }, {
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.3",
- }, {
- IP: "1.1.1.4",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p13",
- Port: 13,
- }, {
- Name: "p14",
- Port: 14,
- }},
- }}
- }
- multipleSubsetsIPsPorts2 := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "2.2.2.1",
- }, {
- IP: "2.2.2.2",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p21",
- Port: 21,
- }, {
- Name: "p22",
- Port: 22,
- }},
- }}
- }
- complexBefore1 := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }},
- }}
- }
- complexBefore2 := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "2.2.2.2",
- NodeName: &nodeName,
- }, {
- IP: "2.2.2.22",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p22",
- Port: 22,
- }},
- }, {
- Addresses: []v1.EndpointAddress{{
- IP: "2.2.2.3",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p23",
- Port: 23,
- }},
- }}
- }
- complexBefore4 := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "4.4.4.4",
- NodeName: &nodeName,
- }, {
- IP: "4.4.4.5",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p44",
- Port: 44,
- }},
- }, {
- Addresses: []v1.EndpointAddress{{
- IP: "4.4.4.6",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p45",
- Port: 45,
- }},
- }}
- }
- complexAfter1 := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.1",
- }, {
- IP: "1.1.1.11",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p11",
- Port: 11,
- }},
- }, {
- Addresses: []v1.EndpointAddress{{
- IP: "1.1.1.2",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p12",
- Port: 12,
- }, {
- Name: "p122",
- Port: 122,
- }},
- }}
- }
- complexAfter3 := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "3.3.3.3",
- }},
- Ports: []v1.EndpointPort{{
- Name: "p33",
- Port: 33,
- }},
- }}
- }
- complexAfter4 := func(ept *v1.Endpoints) {
- ept.Subsets = []v1.EndpointSubset{{
- Addresses: []v1.EndpointAddress{{
- IP: "4.4.4.4",
- NodeName: &nodeName,
- }},
- Ports: []v1.EndpointPort{{
- Name: "p44",
- Port: 44,
- }},
- }}
- }
- testCases := []struct {
- // previousEndpoints and currentEndpoints are used to call appropriate
- // handlers OnEndpoints* (based on whether corresponding values are nil
- // or non-nil) and must be of equal length.
- previousEndpoints []*v1.Endpoints
- currentEndpoints []*v1.Endpoints
- oldEndpoints map[proxy.ServicePortName][]*proxy.BaseEndpointInfo
- expectedResult map[proxy.ServicePortName][]*proxy.BaseEndpointInfo
- expectedStaleEndpoints []proxy.ServiceEndpoint
- expectedStaleServiceNames map[proxy.ServicePortName]bool
- expectedHealthchecks map[types.NamespacedName]int
- }{{
- // Case[0]: nothing
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{},
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{},
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{},
- }, {
- // Case[1]: no change, unnamed port
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", unnamedPort),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", unnamedPort),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", ""): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", ""): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{},
- }, {
- // Case[2]: no change, named port, local
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPortLocal),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPortLocal),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: true},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: true},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{
- makeNSN("ns1", "ep1"): 1,
- },
- }, {
- // Case[3]: no change, multiple subsets
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", multipleSubsets),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", multipleSubsets),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.2:12", IsLocal: false},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.2:12", IsLocal: false},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{},
- }, {
- // Case[4]: no change, multiple subsets, multiple ports, local
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", multipleSubsetsMultiplePortsLocal),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", multipleSubsetsMultiplePortsLocal),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.1:12", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p13"): {
- {Endpoint: "1.1.1.3:13", IsLocal: false},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.1:12", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p13"): {
- {Endpoint: "1.1.1.3:13", IsLocal: false},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{
- makeNSN("ns1", "ep1"): 1,
- },
- }, {
- // Case[5]: no change, multiple endpoints, subsets, IPs, and ports
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", multipleSubsetsIPsPorts1),
- makeTestEndpoints("ns2", "ep2", multipleSubsetsIPsPorts2),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", multipleSubsetsIPsPorts1),
- makeTestEndpoints("ns2", "ep2", multipleSubsetsIPsPorts2),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- {Endpoint: "1.1.1.2:11", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.1:12", IsLocal: false},
- {Endpoint: "1.1.1.2:12", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p13"): {
- {Endpoint: "1.1.1.3:13", IsLocal: false},
- {Endpoint: "1.1.1.4:13", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p14"): {
- {Endpoint: "1.1.1.3:14", IsLocal: false},
- {Endpoint: "1.1.1.4:14", IsLocal: true},
- },
- makeServicePortName("ns2", "ep2", "p21"): {
- {Endpoint: "2.2.2.1:21", IsLocal: false},
- {Endpoint: "2.2.2.2:21", IsLocal: true},
- },
- makeServicePortName("ns2", "ep2", "p22"): {
- {Endpoint: "2.2.2.1:22", IsLocal: false},
- {Endpoint: "2.2.2.2:22", IsLocal: true},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- {Endpoint: "1.1.1.2:11", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.1:12", IsLocal: false},
- {Endpoint: "1.1.1.2:12", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p13"): {
- {Endpoint: "1.1.1.3:13", IsLocal: false},
- {Endpoint: "1.1.1.4:13", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p14"): {
- {Endpoint: "1.1.1.3:14", IsLocal: false},
- {Endpoint: "1.1.1.4:14", IsLocal: true},
- },
- makeServicePortName("ns2", "ep2", "p21"): {
- {Endpoint: "2.2.2.1:21", IsLocal: false},
- {Endpoint: "2.2.2.2:21", IsLocal: true},
- },
- makeServicePortName("ns2", "ep2", "p22"): {
- {Endpoint: "2.2.2.1:22", IsLocal: false},
- {Endpoint: "2.2.2.2:22", IsLocal: true},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{
- makeNSN("ns1", "ep1"): 2,
- makeNSN("ns2", "ep2"): 1,
- },
- }, {
- // Case[6]: add an Endpoints
- previousEndpoints: []*v1.Endpoints{
- nil,
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", unnamedPortLocal),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{},
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", ""): {
- {Endpoint: "1.1.1.1:11", IsLocal: true},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{
- makeServicePortName("ns1", "ep1", ""): true,
- },
- expectedHealthchecks: map[types.NamespacedName]int{
- makeNSN("ns1", "ep1"): 1,
- },
- }, {
- // Case[7]: remove an Endpoints
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", unnamedPortLocal),
- },
- currentEndpoints: []*v1.Endpoints{
- nil,
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", ""): {
- {Endpoint: "1.1.1.1:11", IsLocal: true},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{},
- expectedStaleEndpoints: []proxy.ServiceEndpoint{{
- Endpoint: "1.1.1.1:11",
- ServicePortName: makeServicePortName("ns1", "ep1", ""),
- }},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{},
- }, {
- // Case[8]: add an IP and port
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPort),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPortsLocalNoLocal),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- {Endpoint: "1.1.1.2:11", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.1:12", IsLocal: false},
- {Endpoint: "1.1.1.2:12", IsLocal: true},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{
- makeServicePortName("ns1", "ep1", "p12"): true,
- },
- expectedHealthchecks: map[types.NamespacedName]int{
- makeNSN("ns1", "ep1"): 1,
- },
- }, {
- // Case[9]: remove an IP and port
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPortsLocalNoLocal),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPort),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- {Endpoint: "1.1.1.2:11", IsLocal: true},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.1:12", IsLocal: false},
- {Endpoint: "1.1.1.2:12", IsLocal: true},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{{
- Endpoint: "1.1.1.2:11",
- ServicePortName: makeServicePortName("ns1", "ep1", "p11"),
- }, {
- Endpoint: "1.1.1.1:12",
- ServicePortName: makeServicePortName("ns1", "ep1", "p12"),
- }, {
- Endpoint: "1.1.1.2:12",
- ServicePortName: makeServicePortName("ns1", "ep1", "p12"),
- }},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{},
- }, {
- // Case[10]: add a subset
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPort),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", multipleSubsetsWithLocal),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.2:12", IsLocal: true},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{
- makeServicePortName("ns1", "ep1", "p12"): true,
- },
- expectedHealthchecks: map[types.NamespacedName]int{
- makeNSN("ns1", "ep1"): 1,
- },
- }, {
- // Case[11]: remove a subset
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", multipleSubsets),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPort),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.2:12", IsLocal: false},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{{
- Endpoint: "1.1.1.2:12",
- ServicePortName: makeServicePortName("ns1", "ep1", "p12"),
- }},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{},
- }, {
- // Case[12]: rename a port
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPort),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPortRenamed),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11-2"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{{
- Endpoint: "1.1.1.1:11",
- ServicePortName: makeServicePortName("ns1", "ep1", "p11"),
- }},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{
- makeServicePortName("ns1", "ep1", "p11-2"): true,
- },
- expectedHealthchecks: map[types.NamespacedName]int{},
- }, {
- // Case[13]: renumber a port
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPort),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", namedPortRenumbered),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:22", IsLocal: false},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{{
- Endpoint: "1.1.1.1:11",
- ServicePortName: makeServicePortName("ns1", "ep1", "p11"),
- }},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{},
- expectedHealthchecks: map[types.NamespacedName]int{},
- }, {
- // Case[14]: complex add and remove
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", complexBefore1),
- makeTestEndpoints("ns2", "ep2", complexBefore2),
- nil,
- makeTestEndpoints("ns4", "ep4", complexBefore4),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", complexAfter1),
- nil,
- makeTestEndpoints("ns3", "ep3", complexAfter3),
- makeTestEndpoints("ns4", "ep4", complexAfter4),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- makeServicePortName("ns2", "ep2", "p22"): {
- {Endpoint: "2.2.2.2:22", IsLocal: true},
- {Endpoint: "2.2.2.22:22", IsLocal: true},
- },
- makeServicePortName("ns2", "ep2", "p23"): {
- {Endpoint: "2.2.2.3:23", IsLocal: true},
- },
- makeServicePortName("ns4", "ep4", "p44"): {
- {Endpoint: "4.4.4.4:44", IsLocal: true},
- {Endpoint: "4.4.4.5:44", IsLocal: true},
- },
- makeServicePortName("ns4", "ep4", "p45"): {
- {Endpoint: "4.4.4.6:45", IsLocal: true},
- },
- },
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", "p11"): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- {Endpoint: "1.1.1.11:11", IsLocal: false},
- },
- makeServicePortName("ns1", "ep1", "p12"): {
- {Endpoint: "1.1.1.2:12", IsLocal: false},
- },
- makeServicePortName("ns1", "ep1", "p122"): {
- {Endpoint: "1.1.1.2:122", IsLocal: false},
- },
- makeServicePortName("ns3", "ep3", "p33"): {
- {Endpoint: "3.3.3.3:33", IsLocal: false},
- },
- makeServicePortName("ns4", "ep4", "p44"): {
- {Endpoint: "4.4.4.4:44", IsLocal: true},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{{
- Endpoint: "2.2.2.2:22",
- ServicePortName: makeServicePortName("ns2", "ep2", "p22"),
- }, {
- Endpoint: "2.2.2.22:22",
- ServicePortName: makeServicePortName("ns2", "ep2", "p22"),
- }, {
- Endpoint: "2.2.2.3:23",
- ServicePortName: makeServicePortName("ns2", "ep2", "p23"),
- }, {
- Endpoint: "4.4.4.5:44",
- ServicePortName: makeServicePortName("ns4", "ep4", "p44"),
- }, {
- Endpoint: "4.4.4.6:45",
- ServicePortName: makeServicePortName("ns4", "ep4", "p45"),
- }},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{
- makeServicePortName("ns1", "ep1", "p12"): true,
- makeServicePortName("ns1", "ep1", "p122"): true,
- makeServicePortName("ns3", "ep3", "p33"): true,
- },
- expectedHealthchecks: map[types.NamespacedName]int{
- makeNSN("ns4", "ep4"): 1,
- },
- }, {
- // Case[15]: change from 0 endpoint address to 1 unnamed port
- previousEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", emptyEndpoint),
- },
- currentEndpoints: []*v1.Endpoints{
- makeTestEndpoints("ns1", "ep1", unnamedPort),
- },
- oldEndpoints: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{},
- expectedResult: map[proxy.ServicePortName][]*proxy.BaseEndpointInfo{
- makeServicePortName("ns1", "ep1", ""): {
- {Endpoint: "1.1.1.1:11", IsLocal: false},
- },
- },
- expectedStaleEndpoints: []proxy.ServiceEndpoint{},
- expectedStaleServiceNames: map[proxy.ServicePortName]bool{
- makeServicePortName("ns1", "ep1", ""): true,
- },
- expectedHealthchecks: map[types.NamespacedName]int{},
- },
- }
- for tci, tc := range testCases {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- fp.hostname = nodeName
- // First check that after adding all previous versions of endpoints,
- // the fp.oldEndpoints is as we expect.
- for i := range tc.previousEndpoints {
- if tc.previousEndpoints[i] != nil {
- fp.OnEndpointsAdd(tc.previousEndpoints[i])
- }
- }
- fp.endpointsMap.Update(fp.endpointsChanges)
- compareEndpointsMaps(t, tci, fp.endpointsMap, tc.oldEndpoints)
- // Now let's call appropriate handlers to get to state we want to be.
- if len(tc.previousEndpoints) != len(tc.currentEndpoints) {
- t.Fatalf("[%d] different lengths of previous and current endpoints", tci)
- continue
- }
- for i := range tc.previousEndpoints {
- prev, curr := tc.previousEndpoints[i], tc.currentEndpoints[i]
- switch {
- case prev == nil:
- fp.OnEndpointsAdd(curr)
- case curr == nil:
- fp.OnEndpointsDelete(prev)
- default:
- fp.OnEndpointsUpdate(prev, curr)
- }
- }
- result := fp.endpointsMap.Update(fp.endpointsChanges)
- newMap := fp.endpointsMap
- compareEndpointsMaps(t, tci, newMap, tc.expectedResult)
- if len(result.StaleEndpoints) != len(tc.expectedStaleEndpoints) {
- t.Errorf("[%d] expected %d staleEndpoints, got %d: %v", tci, len(tc.expectedStaleEndpoints), len(result.StaleEndpoints), result.StaleEndpoints)
- }
- for _, x := range tc.expectedStaleEndpoints {
- found := false
- for _, stale := range result.StaleEndpoints {
- if stale == x {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("[%d] expected staleEndpoints[%v], but didn't find it: %v", tci, x, result.StaleEndpoints)
- }
- }
- if len(result.StaleServiceNames) != len(tc.expectedStaleServiceNames) {
- t.Errorf("[%d] expected %d staleServiceNames, got %d: %v", tci, len(tc.expectedStaleServiceNames), len(result.StaleServiceNames), result.StaleServiceNames)
- }
- for svcName := range tc.expectedStaleServiceNames {
- found := false
- for _, stale := range result.StaleServiceNames {
- if stale == svcName {
- found = true
- break
- }
- }
- if !found {
- t.Errorf("[%d] expected staleServiceNames[%v], but didn't find it: %v", tci, svcName, result.StaleServiceNames)
- }
- }
- if !reflect.DeepEqual(result.HCEndpointsLocalIPSize, tc.expectedHealthchecks) {
- t.Errorf("[%d] expected healthchecks %v, got %v", tci, tc.expectedHealthchecks, result.HCEndpointsLocalIPSize)
- }
- }
- }
- func compareEndpointsMaps(t *testing.T, tci int, newMap proxy.EndpointsMap, expected map[proxy.ServicePortName][]*proxy.BaseEndpointInfo) {
- if len(newMap) != len(expected) {
- t.Errorf("[%d] expected %d results, got %d: %v", tci, len(expected), len(newMap), newMap)
- }
- for x := range expected {
- if len(newMap[x]) != len(expected[x]) {
- t.Errorf("[%d] expected %d endpoints for %v, got %d", tci, len(expected[x]), x, len(newMap[x]))
- } else {
- for i := range expected[x] {
- newEp, ok := newMap[x][i].(*proxy.BaseEndpointInfo)
- if !ok {
- t.Errorf("Failed to cast proxy.BaseEndpointInfo")
- continue
- }
- if *newEp != *(expected[x][i]) {
- t.Errorf("[%d] expected new[%v][%d] to be %v, got %v", tci, x, i, expected[x][i], newEp)
- }
- }
- }
- }
- }
- func Test_syncService(t *testing.T) {
- testCases := []struct {
- oldVirtualServer *utilipvs.VirtualServer
- svcName string
- newVirtualServer *utilipvs.VirtualServer
- bindAddr bool
- }{
- {
- // case 0, old virtual server is same as new virtual server
- oldVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolTCP),
- Port: 80,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- svcName: "foo",
- newVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolTCP),
- Port: 80,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- bindAddr: false,
- },
- {
- // case 1, old virtual server is different from new virtual server
- oldVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolTCP),
- Port: 8080,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- svcName: "bar",
- newVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolTCP),
- Port: 8080,
- Scheduler: "rr",
- Flags: utilipvs.FlagPersistent,
- },
- bindAddr: false,
- },
- {
- // case 2, old virtual server is different from new virtual server
- oldVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolTCP),
- Port: 8080,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- svcName: "bar",
- newVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolTCP),
- Port: 8080,
- Scheduler: "wlc",
- Flags: utilipvs.FlagHashed,
- },
- bindAddr: false,
- },
- {
- // case 3, old virtual server is nil, and create new virtual server
- oldVirtualServer: nil,
- svcName: "baz",
- newVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolUDP),
- Port: 53,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- bindAddr: true,
- },
- {
- // case 4, SCTP, old virtual server is same as new virtual server
- oldVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolSCTP),
- Port: 80,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- svcName: "foo",
- newVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolSCTP),
- Port: 80,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- bindAddr: false,
- },
- {
- // case 5, old virtual server is different from new virtual server
- oldVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolSCTP),
- Port: 8080,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- svcName: "bar",
- newVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolSCTP),
- Port: 8080,
- Scheduler: "rr",
- Flags: utilipvs.FlagPersistent,
- },
- bindAddr: false,
- },
- {
- // case 6, old virtual server is different from new virtual server
- oldVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolSCTP),
- Port: 8080,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- svcName: "bar",
- newVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolSCTP),
- Port: 8080,
- Scheduler: "wlc",
- Flags: utilipvs.FlagHashed,
- },
- bindAddr: false,
- },
- {
- // case 7, old virtual server is nil, and create new virtual server
- oldVirtualServer: nil,
- svcName: "baz",
- newVirtualServer: &utilipvs.VirtualServer{
- Address: net.ParseIP("1.2.3.4"),
- Protocol: string(v1.ProtocolSCTP),
- Port: 53,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- bindAddr: true,
- },
- }
- for i := range testCases {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- proxier := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- proxier.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice)
- if testCases[i].oldVirtualServer != nil {
- if err := proxier.ipvs.AddVirtualServer(testCases[i].oldVirtualServer); err != nil {
- t.Errorf("Case [%d], unexpected add IPVS virtual server error: %v", i, err)
- }
- }
- if err := proxier.syncService(testCases[i].svcName, testCases[i].newVirtualServer, testCases[i].bindAddr); err != nil {
- t.Errorf("Case [%d], unexpected sync IPVS virtual server error: %v", i, err)
- }
- // check
- list, err := proxier.ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Case [%d], unexpected list IPVS virtual server error: %v", i, err)
- }
- if len(list) != 1 {
- t.Errorf("Case [%d], expect %d virtual servers, got %d", i, 1, len(list))
- continue
- }
- if !list[0].Equal(testCases[i].newVirtualServer) {
- t.Errorf("Case [%d], unexpected mismatch, expect: %#v, got: %#v", i, testCases[i].newVirtualServer, list[0])
- }
- }
- }
- func buildFakeProxier() (*iptablestest.FakeIPTables, *Proxier) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- return ipt, NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- }
- func hasJump(rules []iptablestest.Rule, destChain, ipSet string) bool {
- for _, r := range rules {
- if r[iptablestest.Jump] == destChain {
- if ipSet == "" {
- return true
- }
- if strings.Contains(r[iptablestest.MatchSet], ipSet) {
- return true
- }
- }
- }
- return false
- }
- // checkIptabless to check expected iptables chain and rules
- func checkIptables(t *testing.T, ipt *iptablestest.FakeIPTables, epIpt netlinktest.ExpectedIptablesChain) {
- for epChain, epRules := range epIpt {
- rules := ipt.GetRules(epChain)
- for _, epRule := range epRules {
- if !hasJump(rules, epRule.JumpChain, epRule.MatchSet) {
- t.Errorf("Didn't find jump from chain %v match set %v to %v", epChain, epRule.MatchSet, epRule.JumpChain)
- }
- }
- }
- }
- // checkIPSet to check expected ipset and entries
- func checkIPSet(t *testing.T, fp *Proxier, ipSet netlinktest.ExpectedIPSet) {
- for set, entries := range ipSet {
- ents, err := fp.ipset.ListEntries(set)
- if err != nil || len(ents) != len(entries) {
- t.Errorf("Check ipset entries failed for ipset: %q, expect %d, got %d", set, len(entries), len(ents))
- continue
- }
- if len(entries) == 1 {
- if ents[0] != entries[0].String() {
- t.Errorf("Check ipset entries failed for ipset: %q", set)
- }
- }
- }
- }
- // checkIPVS to check expected ipvs service and destination
- func checkIPVS(t *testing.T, fp *Proxier, vs *netlinktest.ExpectedVirtualServer) {
- services, err := fp.ipvs.GetVirtualServers()
- if err != nil {
- t.Errorf("Failed to get ipvs services, err: %v", err)
- }
- if len(services) != vs.VSNum {
- t.Errorf("Expect %d ipvs services, got %d", vs.VSNum, len(services))
- }
- for _, svc := range services {
- if svc.Address.String() == vs.IP && svc.Port == vs.Port && svc.Protocol == vs.Protocol {
- destinations, _ := fp.ipvs.GetRealServers(svc)
- if len(destinations) != len(vs.RS) {
- t.Errorf("Expected %d destinations, got %d destinations", len(vs.RS), len(destinations))
- }
- if len(vs.RS) == 1 {
- if destinations[0].Address.String() != vs.RS[0].IP || destinations[0].Port != vs.RS[0].Port {
- t.Errorf("Unexpected mismatch destinations")
- }
- }
- }
- }
- }
- func TestCleanLegacyService(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, parseExcludedCIDRs([]string{"3.3.3.0/24", "4.4.4.0/24"}))
- // All ipvs services that were processed in the latest sync loop.
- activeServices := map[string]bool{"ipvs0": true, "ipvs1": true}
- // All ipvs services in the system.
- currentServices := map[string]*utilipvs.VirtualServer{
- // Created by kube-proxy.
- "ipvs0": {
- Address: net.ParseIP("1.1.1.1"),
- Protocol: string(v1.ProtocolUDP),
- Port: 53,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by kube-proxy.
- "ipvs1": {
- Address: net.ParseIP("2.2.2.2"),
- Protocol: string(v1.ProtocolUDP),
- Port: 54,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by an external party.
- "ipvs2": {
- Address: net.ParseIP("3.3.3.3"),
- Protocol: string(v1.ProtocolUDP),
- Port: 55,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by an external party.
- "ipvs3": {
- Address: net.ParseIP("4.4.4.4"),
- Protocol: string(v1.ProtocolUDP),
- Port: 56,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by an external party.
- "ipvs4": {
- Address: net.ParseIP("5.5.5.5"),
- Protocol: string(v1.ProtocolUDP),
- Port: 57,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by kube-proxy, but now stale.
- "ipvs5": {
- Address: net.ParseIP("6.6.6.6"),
- Protocol: string(v1.ProtocolUDP),
- Port: 58,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- }
- for v := range currentServices {
- fp.ipvs.AddVirtualServer(currentServices[v])
- }
- fp.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice)
- activeBindAddrs := map[string]bool{"1.1.1.1": true, "2.2.2.2": true, "3.3.3.3": true, "4.4.4.4": true}
- // This is ipv4-only so ipv6 addresses should be ignored
- currentBindAddrs := []string{"1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4", "5.5.5.5", "6.6.6.6", "fd80::1:2:3", "fd80::1:2:4"}
- for i := range currentBindAddrs {
- fp.netlinkHandle.EnsureAddressBind(currentBindAddrs[i], DefaultDummyDevice)
- }
- fp.cleanLegacyService(activeServices, currentServices, map[string]bool{"5.5.5.5": true, "6.6.6.6": true})
- // ipvs4 and ipvs5 should have been cleaned.
- remainingVirtualServers, _ := fp.ipvs.GetVirtualServers()
- if len(remainingVirtualServers) != 4 {
- t.Errorf("Expected number of remaining IPVS services after cleanup to be %v. Got %v", 4, len(remainingVirtualServers))
- }
- for _, vs := range remainingVirtualServers {
- // Checking that ipvs4 and ipvs5 were removed.
- if vs.Port == 57 {
- t.Errorf("Expected ipvs4 to be removed after cleanup. It still remains")
- }
- if vs.Port == 58 {
- t.Errorf("Expected ipvs5 to be removed after cleanup. It still remains")
- }
- }
- // Addresses 5.5.5.5 and 6.6.6.6 should not be bound any more, but the ipv6 addresses should remain
- remainingAddrs, _ := fp.netlinkHandle.ListBindAddress(DefaultDummyDevice)
- if len(remainingAddrs) != 6 {
- t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 6, len(remainingAddrs))
- }
- // check that address "1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4" are bound, ignore ipv6 addresses
- remainingAddrsMap := make(map[string]bool)
- for _, a := range remainingAddrs {
- if net.ParseIP(a).To4() == nil {
- continue
- }
- remainingAddrsMap[a] = true
- }
- if !reflect.DeepEqual(activeBindAddrs, remainingAddrsMap) {
- t.Errorf("Expected remainingAddrsMap %v, got %v", activeBindAddrs, remainingAddrsMap)
- }
- }
- func TestCleanLegacyRealServersExcludeCIDRs(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- gtm := NewGracefulTerminationManager(ipvs)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, parseExcludedCIDRs([]string{"4.4.4.4/32"}))
- fp.gracefuldeleteManager = gtm
- vs := &utilipvs.VirtualServer{
- Address: net.ParseIP("4.4.4.4"),
- Protocol: string(v1.ProtocolUDP),
- Port: 56,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- }
- fp.ipvs.AddVirtualServer(vs)
- rss := []*utilipvs.RealServer{
- {
- Address: net.ParseIP("10.10.10.10"),
- Port: 56,
- ActiveConn: 0,
- InactiveConn: 0,
- },
- {
- Address: net.ParseIP("11.11.11.11"),
- Port: 56,
- ActiveConn: 0,
- InactiveConn: 0,
- },
- }
- for _, rs := range rss {
- fp.ipvs.AddRealServer(vs, rs)
- }
- fp.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice)
- fp.netlinkHandle.EnsureAddressBind("4.4.4.4", DefaultDummyDevice)
- fp.cleanLegacyService(
- map[string]bool{},
- map[string]*utilipvs.VirtualServer{"ipvs0": vs},
- map[string]bool{"4.4.4.4": true},
- )
- fp.gracefuldeleteManager.tryDeleteRs()
- remainingRealServers, _ := fp.ipvs.GetRealServers(vs)
- if len(remainingRealServers) != 2 {
- t.Errorf("Expected number of remaining IPVS real servers after cleanup should be %v. Got %v", 2, len(remainingRealServers))
- }
- }
- func TestCleanLegacyService6(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, parseExcludedCIDRs([]string{"3000::/64", "4000::/64"}))
- fp.nodeIP = net.ParseIP("::1")
- // All ipvs services that were processed in the latest sync loop.
- activeServices := map[string]bool{"ipvs0": true, "ipvs1": true}
- // All ipvs services in the system.
- currentServices := map[string]*utilipvs.VirtualServer{
- // Created by kube-proxy.
- "ipvs0": {
- Address: net.ParseIP("1000::1"),
- Protocol: string(v1.ProtocolUDP),
- Port: 53,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by kube-proxy.
- "ipvs1": {
- Address: net.ParseIP("1000::2"),
- Protocol: string(v1.ProtocolUDP),
- Port: 54,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by an external party.
- "ipvs2": {
- Address: net.ParseIP("3000::1"),
- Protocol: string(v1.ProtocolUDP),
- Port: 55,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by an external party.
- "ipvs3": {
- Address: net.ParseIP("4000::1"),
- Protocol: string(v1.ProtocolUDP),
- Port: 56,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by an external party.
- "ipvs4": {
- Address: net.ParseIP("5000::1"),
- Protocol: string(v1.ProtocolUDP),
- Port: 57,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- // Created by kube-proxy, but now stale.
- "ipvs5": {
- Address: net.ParseIP("1000::6"),
- Protocol: string(v1.ProtocolUDP),
- Port: 58,
- Scheduler: "rr",
- Flags: utilipvs.FlagHashed,
- },
- }
- for v := range currentServices {
- fp.ipvs.AddVirtualServer(currentServices[v])
- }
- fp.netlinkHandle.EnsureDummyDevice(DefaultDummyDevice)
- activeBindAddrs := map[string]bool{"1000::1": true, "1000::2": true, "3000::1": true, "4000::1": true}
- // This is ipv6-only so ipv4 addresses should be ignored
- currentBindAddrs := []string{"1000::1", "1000::2", "3000::1", "4000::1", "5000::1", "1000::6", "1.1.1.1", "2.2.2.2"}
- for i := range currentBindAddrs {
- fp.netlinkHandle.EnsureAddressBind(currentBindAddrs[i], DefaultDummyDevice)
- }
- fp.cleanLegacyService(activeServices, currentServices, map[string]bool{"5000::1": true, "1000::6": true})
- // ipvs4 and ipvs5 should have been cleaned.
- remainingVirtualServers, _ := fp.ipvs.GetVirtualServers()
- if len(remainingVirtualServers) != 4 {
- t.Errorf("Expected number of remaining IPVS services after cleanup to be %v. Got %v", 4, len(remainingVirtualServers))
- }
- for _, vs := range remainingVirtualServers {
- // Checking that ipvs4 and ipvs5 were removed.
- if vs.Port == 57 {
- t.Errorf("Expected ipvs4 to be removed after cleanup. It still remains")
- }
- if vs.Port == 58 {
- t.Errorf("Expected ipvs5 to be removed after cleanup. It still remains")
- }
- }
- // Addresses 5000::1 and 1000::6 should not be bound any more, but the ipv4 addresses should remain
- remainingAddrs, _ := fp.netlinkHandle.ListBindAddress(DefaultDummyDevice)
- if len(remainingAddrs) != 6 {
- t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 6, len(remainingAddrs))
- }
- // check that address "1000::1", "1000::2", "3000::1", "4000::1" are still bound, ignore ipv4 addresses
- remainingAddrsMap := make(map[string]bool)
- for _, a := range remainingAddrs {
- if net.ParseIP(a).To4() != nil {
- continue
- }
- remainingAddrsMap[a] = true
- }
- if !reflect.DeepEqual(activeBindAddrs, remainingAddrsMap) {
- t.Errorf("Expected remainingAddrsMap %v, got %v", activeBindAddrs, remainingAddrsMap)
- }
- }
- func TestMultiPortServiceBindAddr(t *testing.T) {
- ipt := iptablestest.NewFake()
- ipvs := ipvstest.NewFake()
- ipset := ipsettest.NewFake(testIPSetVersion)
- fp := NewFakeProxier(ipt, ipvs, ipset, nil, nil)
- service1 := makeTestService("ns1", "svc1", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = "172.16.55.4"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "port1", "TCP", 1234, 0, 0)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "port2", "TCP", 1235, 0, 0)
- })
- service2 := makeTestService("ns1", "svc1", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = "172.16.55.4"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "port1", "TCP", 1234, 0, 0)
- })
- service3 := makeTestService("ns1", "svc1", func(svc *v1.Service) {
- svc.Spec.Type = v1.ServiceTypeClusterIP
- svc.Spec.ClusterIP = "172.16.55.4"
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "port1", "TCP", 1234, 0, 0)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "port2", "TCP", 1235, 0, 0)
- svc.Spec.Ports = addTestPort(svc.Spec.Ports, "port3", "UDP", 1236, 0, 0)
- })
- fp.servicesSynced = true
- fp.endpointsSynced = true
- // first, add multi-port service1
- fp.OnServiceAdd(service1)
- fp.syncProxyRules()
- remainingAddrs, _ := fp.netlinkHandle.ListBindAddress(DefaultDummyDevice)
- // should only remain address "172.16.55.4"
- if len(remainingAddrs) != 1 {
- t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 1, len(remainingAddrs))
- }
- if remainingAddrs[0] != "172.16.55.4" {
- t.Errorf("Expected remaining address should be %s, got %s", "172.16.55.4", remainingAddrs[0])
- }
- // update multi-port service1 to single-port service2
- fp.OnServiceUpdate(service1, service2)
- fp.syncProxyRules()
- remainingAddrs, _ = fp.netlinkHandle.ListBindAddress(DefaultDummyDevice)
- // should still only remain address "172.16.55.4"
- if len(remainingAddrs) != 1 {
- t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 1, len(remainingAddrs))
- } else if remainingAddrs[0] != "172.16.55.4" {
- t.Errorf("Expected remaining address should be %s, got %s", "172.16.55.4", remainingAddrs[0])
- }
- // update single-port service2 to multi-port service3
- fp.OnServiceUpdate(service2, service3)
- fp.syncProxyRules()
- remainingAddrs, _ = fp.netlinkHandle.ListBindAddress(DefaultDummyDevice)
- // should still only remain address "172.16.55.4"
- if len(remainingAddrs) != 1 {
- t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 1, len(remainingAddrs))
- } else if remainingAddrs[0] != "172.16.55.4" {
- t.Errorf("Expected remaining address should be %s, got %s", "172.16.55.4", remainingAddrs[0])
- }
- // delete multi-port service3
- fp.OnServiceDelete(service3)
- fp.syncProxyRules()
- remainingAddrs, _ = fp.netlinkHandle.ListBindAddress(DefaultDummyDevice)
- // all addresses should be unbound
- if len(remainingAddrs) != 0 {
- t.Errorf("Expected number of remaining bound addrs after cleanup to be %v. Got %v", 0, len(remainingAddrs))
- }
- }
|