istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/config/kube/gateway/gatewayclass_test.go (about)

     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  	"fmt"
    19  	"testing"
    20  	"time"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	gateway "sigs.k8s.io/gateway-api/apis/v1beta1"
    24  
    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"
    30  )
    31  
    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)
    38  	go cc.Run(stop)
    39  	createClass := func(name, controller string) {
    40  		gc := &gateway.GatewayClass{
    41  			ObjectMeta: metav1.ObjectMeta{
    42  				Name: name,
    43  			},
    44  			Spec: gateway.GatewayClassSpec{
    45  				ControllerName: gateway.GatewayController(controller),
    46  			},
    47  		}
    48  		classes.CreateOrUpdate(gc)
    49  	}
    50  	deleteClass := func(name string) {
    51  		classes.Delete(name, "")
    52  	}
    53  	expectClass := func(name, controller string) {
    54  		t.Helper()
    55  		retry.UntilSuccessOrFail(t, func() error {
    56  			gc := classes.Get(name, "")
    57  			if controller == "" {
    58  				if gc == nil { // Expect none, got none
    59  					return nil
    60  				}
    61  				return fmt.Errorf("expected no class, got %v", gc.Spec.ControllerName)
    62  			}
    63  			if gc == nil {
    64  				return fmt.Errorf("expected class %v, got none", controller)
    65  			}
    66  			if gateway.GatewayController(controller) != gc.Spec.ControllerName {
    67  				return fmt.Errorf("expected class %v, got %v", controller, gc.Spec.ControllerName)
    68  			}
    69  			return nil
    70  		}, retry.Timeout(time.Second*3))
    71  	}
    72  
    73  	// Class should be created initially
    74  	expectClass(features.GatewayAPIDefaultGatewayClass, features.ManagedGatewayController)
    75  
    76  	// Once we delete it, it should be added back
    77  	deleteClass(features.GatewayAPIDefaultGatewayClass)
    78  	expectClass(features.GatewayAPIDefaultGatewayClass, features.ManagedGatewayController)
    79  
    80  	// Overwrite the class, controller should not reconcile it back
    81  	createClass(features.GatewayAPIDefaultGatewayClass, "different-controller")
    82  	expectClass(features.GatewayAPIDefaultGatewayClass, "different-controller")
    83  
    84  	// Once we delete it, it should be added back
    85  	deleteClass(features.GatewayAPIDefaultGatewayClass)
    86  	expectClass(features.GatewayAPIDefaultGatewayClass, features.ManagedGatewayController)
    87  
    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", "")
    93  }