istio

Форк
0
/
controller_test.go 
205 строк · 5.6 Кб
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 gateway
16

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

21
	. "github.com/onsi/gomega"
22
	v1 "k8s.io/api/core/v1"
23
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
	"k8s.io/apimachinery/pkg/runtime/schema"
25
	k8sv1 "sigs.k8s.io/gateway-api/apis/v1"
26
	k8s "sigs.k8s.io/gateway-api/apis/v1alpha2"
27

28
	networking "istio.io/api/networking/v1alpha3"
29
	"istio.io/istio/pilot/pkg/config/memory"
30
	"istio.io/istio/pilot/pkg/features"
31
	"istio.io/istio/pilot/pkg/model"
32
	"istio.io/istio/pilot/pkg/networking/core"
33
	"istio.io/istio/pilot/pkg/serviceregistry/kube/controller"
34
	"istio.io/istio/pilot/pkg/serviceregistry/util/xdsfake"
35
	"istio.io/istio/pkg/config"
36
	"istio.io/istio/pkg/config/constants"
37
	"istio.io/istio/pkg/config/schema/collections"
38
	"istio.io/istio/pkg/config/schema/gvk"
39
	"istio.io/istio/pkg/kube"
40
	"istio.io/istio/pkg/kube/kclient/clienttest"
41
	"istio.io/istio/pkg/test"
42
	"istio.io/istio/pkg/util/sets"
43
)
44

45
var (
46
	gatewayClassSpec = &k8s.GatewayClassSpec{
47
		ControllerName: k8sv1.GatewayController(features.ManagedGatewayController),
48
	}
49
	gatewaySpec = &k8s.GatewaySpec{
50
		GatewayClassName: "gwclass",
51
		Listeners: []k8s.Listener{
52
			{
53
				Name:          "default",
54
				Port:          9009,
55
				Protocol:      "HTTP",
56
				AllowedRoutes: &k8s.AllowedRoutes{Namespaces: &k8s.RouteNamespaces{From: func() *k8s.FromNamespaces { x := k8sv1.NamespacesFromAll; return &x }()}},
57
			},
58
		},
59
	}
60
	httpRouteSpec = &k8s.HTTPRouteSpec{
61
		CommonRouteSpec: k8s.CommonRouteSpec{ParentRefs: []k8s.ParentReference{{
62
			Name: "gwspec",
63
		}}},
64
		Hostnames: []k8s.Hostname{"test.cluster.local"},
65
	}
66

67
	expectedgw = &networking.Gateway{
68
		Servers: []*networking.Server{
69
			{
70
				Port: &networking.Port{
71
					Number:   9009,
72
					Name:     "default",
73
					Protocol: "HTTP",
74
				},
75
				Hosts: []string{"*/*"},
76
			},
77
		},
78
	}
79
)
80

81
var AlwaysReady = func(class schema.GroupVersionResource, stop <-chan struct{}) bool {
82
	return true
83
}
84

85
func TestListInvalidGroupVersionKind(t *testing.T) {
86
	g := NewWithT(t)
87
	clientSet := kube.NewFakeClient()
88
	clientSet.RunAndWait(test.NewStop(t))
89
	store := memory.NewController(memory.Make(collections.All))
90
	controller := NewController(clientSet, store, AlwaysReady, nil, controller.Options{})
91

92
	typ := config.GroupVersionKind{Kind: "wrong-kind"}
93
	c := controller.List(typ, "ns1")
94
	g.Expect(c).To(HaveLen(0))
95
}
96

97
func TestListGatewayResourceType(t *testing.T) {
98
	g := NewWithT(t)
99

100
	clientSet := kube.NewFakeClient()
101
	clientSet.RunAndWait(test.NewStop(t))
102
	store := memory.NewController(memory.Make(collections.All))
103
	controller := NewController(clientSet, store, AlwaysReady, nil, controller.Options{})
104

105
	store.Create(config.Config{
106
		Meta: config.Meta{
107
			GroupVersionKind: gvk.GatewayClass,
108
			Name:             "gwclass",
109
			Namespace:        "ns1",
110
		},
111
		Spec: gatewayClassSpec,
112
	})
113
	if _, err := store.Create(config.Config{
114
		Meta: config.Meta{
115
			GroupVersionKind: gvk.KubernetesGateway,
116
			Name:             "gwspec",
117
			Namespace:        "ns1",
118
		},
119
		Spec: gatewaySpec,
120
	}); err != nil {
121
		t.Fatal(err)
122
	}
123
	store.Create(config.Config{
124
		Meta: config.Meta{
125
			GroupVersionKind: gvk.HTTPRoute,
126
			Name:             "http-route",
127
			Namespace:        "ns1",
128
		},
129
		Spec: httpRouteSpec,
130
	})
131

132
	cg := core.NewConfigGenTest(t, core.TestOptions{})
133
	g.Expect(controller.Reconcile(cg.PushContext())).ToNot(HaveOccurred())
134
	cfg := controller.List(gvk.Gateway, "ns1")
135
	g.Expect(cfg).To(HaveLen(1))
136
	for _, c := range cfg {
137
		g.Expect(c.GroupVersionKind).To(Equal(gvk.Gateway))
138
		g.Expect(c.Name).To(Equal("gwspec" + "-" + constants.KubernetesGatewayName + "-default"))
139
		g.Expect(c.Namespace).To(Equal("ns1"))
140
		g.Expect(c.Spec).To(Equal(expectedgw))
141
	}
142
}
143

144
func TestNamespaceEvent(t *testing.T) {
145
	clientSet := kube.NewFakeClient()
146
	store := memory.NewController(memory.Make(collections.All))
147
	c := NewController(clientSet, store, AlwaysReady, nil, controller.Options{})
148
	s := xdsfake.NewFakeXDS()
149

150
	c.RegisterEventHandler(gvk.Namespace, func(_, cfg config.Config, _ model.Event) {
151
		s.ConfigUpdate(&model.PushRequest{
152
			Full:   true,
153
			Reason: model.NewReasonStats(model.NamespaceUpdate),
154
		})
155
	})
156

157
	stop := test.NewStop(t)
158
	c.Run(stop)
159
	kube.WaitForCacheSync("test", stop, c.HasSynced)
160
	c.state.ReferencedNamespaceKeys = sets.String{"allowed": struct{}{}}
161

162
	ns1 := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{
163
		Name: "ns1",
164
		Labels: map[string]string{
165
			"foo": "bar",
166
		},
167
	}}
168
	ns2 := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{
169
		Name: "ns2",
170
		Labels: map[string]string{
171
			"allowed": "true",
172
		},
173
	}}
174
	ns := clienttest.Wrap(t, c.namespaces)
175

176
	ns.Create(ns1)
177
	s.AssertEmpty(t, time.Millisecond*10)
178

179
	ns.Create(ns2)
180
	s.AssertEmpty(t, time.Millisecond*10)
181

182
	ns1.Annotations = map[string]string{"foo": "bar"}
183
	ns.Update(ns1)
184
	s.AssertEmpty(t, time.Millisecond*10)
185

186
	ns2.Annotations = map[string]string{"foo": "bar"}
187
	ns.Update(ns2)
188
	s.AssertEmpty(t, time.Millisecond*10)
189

190
	ns1.Labels["bar"] = "foo"
191
	ns.Update(ns1)
192
	s.AssertEmpty(t, time.Millisecond*10)
193

194
	ns2.Labels["foo"] = "bar"
195
	ns.Update(ns2)
196
	s.WaitOrFail(t, "xds full")
197

198
	ns1.Labels["allowed"] = "true"
199
	ns.Update(ns1)
200
	s.WaitOrFail(t, "xds full")
201

202
	ns2.Labels["allowed"] = "false"
203
	ns.Update(ns2)
204
	s.WaitOrFail(t, "xds full")
205
}
206

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

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

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

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