1
// Copyright Istio Authors
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
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"
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"
46
gatewayClassSpec = &k8s.GatewayClassSpec{
47
ControllerName: k8sv1.GatewayController(features.ManagedGatewayController),
49
gatewaySpec = &k8s.GatewaySpec{
50
GatewayClassName: "gwclass",
51
Listeners: []k8s.Listener{
56
AllowedRoutes: &k8s.AllowedRoutes{Namespaces: &k8s.RouteNamespaces{From: func() *k8s.FromNamespaces { x := k8sv1.NamespacesFromAll; return &x }()}},
60
httpRouteSpec = &k8s.HTTPRouteSpec{
61
CommonRouteSpec: k8s.CommonRouteSpec{ParentRefs: []k8s.ParentReference{{
64
Hostnames: []k8s.Hostname{"test.cluster.local"},
67
expectedgw = &networking.Gateway{
68
Servers: []*networking.Server{
70
Port: &networking.Port{
75
Hosts: []string{"*/*"},
81
var AlwaysReady = func(class schema.GroupVersionResource, stop <-chan struct{}) bool {
85
func TestListInvalidGroupVersionKind(t *testing.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{})
92
typ := config.GroupVersionKind{Kind: "wrong-kind"}
93
c := controller.List(typ, "ns1")
94
g.Expect(c).To(HaveLen(0))
97
func TestListGatewayResourceType(t *testing.T) {
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{})
105
store.Create(config.Config{
107
GroupVersionKind: gvk.GatewayClass,
111
Spec: gatewayClassSpec,
113
if _, err := store.Create(config.Config{
115
GroupVersionKind: gvk.KubernetesGateway,
123
store.Create(config.Config{
125
GroupVersionKind: gvk.HTTPRoute,
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))
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()
150
c.RegisterEventHandler(gvk.Namespace, func(_, cfg config.Config, _ model.Event) {
151
s.ConfigUpdate(&model.PushRequest{
153
Reason: model.NewReasonStats(model.NamespaceUpdate),
157
stop := test.NewStop(t)
159
kube.WaitForCacheSync("test", stop, c.HasSynced)
160
c.state.ReferencedNamespaceKeys = sets.String{"allowed": struct{}{}}
162
ns1 := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{
164
Labels: map[string]string{
168
ns2 := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{
170
Labels: map[string]string{
174
ns := clienttest.Wrap(t, c.namespaces)
177
s.AssertEmpty(t, time.Millisecond*10)
180
s.AssertEmpty(t, time.Millisecond*10)
182
ns1.Annotations = map[string]string{"foo": "bar"}
184
s.AssertEmpty(t, time.Millisecond*10)
186
ns2.Annotations = map[string]string{"foo": "bar"}
188
s.AssertEmpty(t, time.Millisecond*10)
190
ns1.Labels["bar"] = "foo"
192
s.AssertEmpty(t, time.Millisecond*10)
194
ns2.Labels["foo"] = "bar"
196
s.WaitOrFail(t, "xds full")
198
ns1.Labels["allowed"] = "true"
200
s.WaitOrFail(t, "xds full")
202
ns2.Labels["allowed"] = "false"
204
s.WaitOrFail(t, "xds full")