istio

Форк
0
/
endpointslice_test.go 
156 строк · 4.5 Кб
1
// Copyright Istio Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package controller
16

17
import (
18
	"reflect"
19
	"testing"
20
	"time"
21

22
	corev1 "k8s.io/api/core/v1"
23
	mcs "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
24

25
	"istio.io/api/label"
26
	"istio.io/istio/pilot/pkg/model"
27
	"istio.io/istio/pilot/pkg/serviceregistry/kube"
28
	"istio.io/istio/pkg/config/host"
29
	"istio.io/istio/pkg/test/util/assert"
30
)
31

32
func TestEndpointSliceFromMCSShouldBeIgnored(t *testing.T) {
33
	const (
34
		ns      = "nsa"
35
		svcName = "svc1"
36
		appName = "prod-app"
37
	)
38

39
	controller, fx := NewFakeControllerWithOptions(t, FakeControllerOptions{})
40

41
	node := generateNode("node1", map[string]string{
42
		NodeZoneLabel:              "zone1",
43
		NodeRegionLabel:            "region1",
44
		label.TopologySubzone.Name: "subzone1",
45
	})
46
	addNodes(t, controller, node)
47

48
	pod := generatePod("128.0.0.1", "pod1", ns, "svcaccount", "node1",
49
		map[string]string{"app": appName}, map[string]string{})
50
	pods := []*corev1.Pod{pod}
51
	addPods(t, controller, fx, pods...)
52

53
	createServiceWait(controller, svcName, ns, nil, nil,
54
		[]int32{8080}, map[string]string{"app": appName}, t)
55

56
	// Ensure that the service is available.
57
	hostname := kube.ServiceHostname(svcName, ns, controller.opts.DomainSuffix)
58
	svc := controller.GetService(hostname)
59
	if svc == nil {
60
		t.Fatal("failed to get service")
61
	}
62

63
	// Create an endpoint that indicates it's an MCS endpoint for the service.
64
	svc1Ips := []string{"128.0.0.1"}
65
	portNames := []string{"tcp-port"}
66
	createEndpoints(t, controller, svcName, ns, portNames, svc1Ips, nil, map[string]string{
67
		mcs.LabelServiceName: svcName,
68
	})
69
	fx.AssertEmpty(t, time.Millisecond*50)
70

71
	// Ensure that no endpoint is create
72
	endpoints := GetEndpoints(svc, controller.Endpoints)
73
	assert.Equal(t, len(endpoints), 0)
74
}
75

76
func TestEndpointSliceCache(t *testing.T) {
77
	cache := newEndpointSliceCache()
78
	hostname := host.Name("foo")
79

80
	// add a endpoint
81
	ep1 := &model.IstioEndpoint{
82
		Address:         "1.2.3.4",
83
		ServicePortName: "http",
84
	}
85
	cache.Update(hostname, "slice1", []*model.IstioEndpoint{ep1})
86
	if !testEndpointsEqual(cache.Get(hostname), []*model.IstioEndpoint{ep1}) {
87
		t.Fatalf("unexpected endpoints")
88
	}
89
	if !cache.Has(hostname) {
90
		t.Fatalf("expect to find the host name")
91
	}
92
	// add a new endpoint
93
	ep2 := &model.IstioEndpoint{
94
		Address:         "2.3.4.5",
95
		ServicePortName: "http",
96
	}
97
	cache.Update(hostname, "slice1", []*model.IstioEndpoint{ep1, ep2})
98
	if !testEndpointsEqual(cache.Get(hostname), []*model.IstioEndpoint{ep1, ep2}) {
99
		t.Fatalf("unexpected endpoints")
100
	}
101

102
	// change service port name
103
	ep1 = &model.IstioEndpoint{
104
		Address:         "1.2.3.4",
105
		ServicePortName: "http2",
106
	}
107
	ep2 = &model.IstioEndpoint{
108
		Address:         "2.3.4.5",
109
		ServicePortName: "http2",
110
	}
111
	cache.Update(hostname, "slice1", []*model.IstioEndpoint{ep1, ep2})
112
	if !testEndpointsEqual(cache.Get(hostname), []*model.IstioEndpoint{ep1, ep2}) {
113
		t.Fatalf("unexpected endpoints")
114
	}
115

116
	// add a new slice
117
	ep3 := &model.IstioEndpoint{
118
		Address:         "3.4.5.6",
119
		ServicePortName: "http2",
120
	}
121
	cache.Update(hostname, "slice2", []*model.IstioEndpoint{ep3})
122
	if !testEndpointsEqual(cache.Get(hostname), []*model.IstioEndpoint{ep1, ep2, ep3}) {
123
		t.Fatalf("unexpected endpoints")
124
	}
125

126
	// dedup when transitioning
127
	cache.Update(hostname, "slice2", []*model.IstioEndpoint{ep2, ep3})
128
	if !testEndpointsEqual(cache.Get(hostname), []*model.IstioEndpoint{ep1, ep2, ep3}) {
129
		t.Fatalf("unexpected endpoints")
130
	}
131

132
	cache.Delete(hostname, "slice1")
133
	if !testEndpointsEqual(cache.Get(hostname), []*model.IstioEndpoint{ep2, ep3}) {
134
		t.Fatalf("unexpected endpoints")
135
	}
136

137
	cache.Delete(hostname, "slice2")
138
	if cache.Get(hostname) != nil {
139
		t.Fatalf("unexpected endpoints")
140
	}
141
}
142

143
func testEndpointsEqual(a, b []*model.IstioEndpoint) bool {
144
	if len(a) != len(b) {
145
		return false
146
	}
147
	m1 := make(map[endpointKey]int)
148
	m2 := make(map[endpointKey]int)
149
	for _, i := range a {
150
		m1[endpointKey{i.Address, i.ServicePortName}]++
151
	}
152
	for _, i := range b {
153
		m2[endpointKey{i.Address, i.ServicePortName}]++
154
	}
155
	return reflect.DeepEqual(m1, m2)
156
}
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.