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.
22
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23
gateway "sigs.k8s.io/gateway-api/apis/v1beta1"
25
"istio.io/istio/pilot/pkg/features"
26
"istio.io/istio/pkg/kube"
27
"istio.io/istio/pkg/kube/kclient/clienttest"
28
"istio.io/istio/pkg/test"
29
"istio.io/istio/pkg/test/util/retry"
32
func TestClassController(t *testing.T) {
33
client := kube.NewFakeClient()
34
cc := NewClassController(client)
35
classes := clienttest.Wrap(t, cc.classes)
36
stop := test.NewStop(t)
37
client.RunAndWait(stop)
39
createClass := func(name, controller string) {
40
gc := &gateway.GatewayClass{
41
ObjectMeta: metav1.ObjectMeta{
44
Spec: gateway.GatewayClassSpec{
45
ControllerName: gateway.GatewayController(controller),
48
classes.CreateOrUpdate(gc)
50
deleteClass := func(name string) {
51
classes.Delete(name, "")
53
expectClass := func(name, controller string) {
55
retry.UntilSuccessOrFail(t, func() error {
56
gc := classes.Get(name, "")
58
if gc == nil { // Expect none, got none
61
return fmt.Errorf("expected no class, got %v", gc.Spec.ControllerName)
64
return fmt.Errorf("expected class %v, got none", controller)
66
if gateway.GatewayController(controller) != gc.Spec.ControllerName {
67
return fmt.Errorf("expected class %v, got %v", controller, gc.Spec.ControllerName)
70
}, retry.Timeout(time.Second*3))
73
// Class should be created initially
74
expectClass(features.GatewayAPIDefaultGatewayClass, features.ManagedGatewayController)
76
// Once we delete it, it should be added back
77
deleteClass(features.GatewayAPIDefaultGatewayClass)
78
expectClass(features.GatewayAPIDefaultGatewayClass, features.ManagedGatewayController)
80
// Overwrite the class, controller should not reconcile it back
81
createClass(features.GatewayAPIDefaultGatewayClass, "different-controller")
82
expectClass(features.GatewayAPIDefaultGatewayClass, "different-controller")
84
// Once we delete it, it should be added back
85
deleteClass(features.GatewayAPIDefaultGatewayClass)
86
expectClass(features.GatewayAPIDefaultGatewayClass, features.ManagedGatewayController)
88
// Create an unrelated GatewayClass, we should not do anything to it
89
createClass("something-else", "different-controller")
90
expectClass("something-else", "different-controller")
91
deleteClass("something-else")
92
expectClass("something-else", "")