istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/crd/gateway.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 crd
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"fmt"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  
    27  	"istio.io/istio/pkg/test/env"
    28  	"istio.io/istio/pkg/test/framework"
    29  	"istio.io/istio/pkg/test/framework/resource"
    30  	"istio.io/istio/pkg/test/framework/resource/config/apply"
    31  	"istio.io/istio/pkg/test/util/retry"
    32  )
    33  
    34  // SupportsGatewayAPI checks if the gateway API is supported.
    35  func SupportsGatewayAPI(t resource.Context) bool {
    36  	for _, cluster := range t.Clusters() {
    37  		if !cluster.MinKubeVersion(23) { // API uses CEL which requires 1.23
    38  			return false
    39  		}
    40  	}
    41  	return true
    42  }
    43  
    44  var errSkip = errors.New("not supported; requires CRDv1 support")
    45  
    46  func DeployGatewayAPIOrSkip(ctx framework.TestContext) {
    47  	res := DeployGatewayAPI(ctx)
    48  	if res == errSkip {
    49  		ctx.Skip(errSkip.Error())
    50  	}
    51  	if res != nil {
    52  		ctx.Fatal(res)
    53  	}
    54  }
    55  
    56  func DeployGatewayAPI(ctx resource.Context) error {
    57  	if !SupportsGatewayAPI(ctx) {
    58  		return errSkip
    59  	}
    60  	if err := ctx.ConfigIstio().
    61  		File("", filepath.Join(env.IstioSrc, "tests/integration/pilot/testdata/gateway-api-crd.yaml")).
    62  		Apply(apply.NoCleanup); err != nil {
    63  		return err
    64  	}
    65  	// Wait until our GatewayClass is ready
    66  	return retry.UntilSuccess(func() error {
    67  		for _, c := range ctx.Clusters().Configs() {
    68  			_, err := c.GatewayAPI().GatewayV1beta1().GatewayClasses().Get(context.Background(), "istio", metav1.GetOptions{})
    69  			if err != nil {
    70  				return err
    71  			}
    72  			crdl, err := c.Ext().ApiextensionsV1().CustomResourceDefinitions().List(context.Background(), metav1.ListOptions{})
    73  			if err != nil {
    74  				return err
    75  			}
    76  			for _, crd := range crdl.Items {
    77  				if !strings.HasSuffix(crd.Name, "gateway.networking.k8s.io") {
    78  					continue
    79  				}
    80  				found := false
    81  				for _, c := range crd.Status.Conditions {
    82  					if c.Type == apiextensions.Established && c.Status == apiextensions.ConditionTrue {
    83  						found = true
    84  					}
    85  				}
    86  				if !found {
    87  					return fmt.Errorf("crd %v not ready: %+v", crd.Name, crd.Status)
    88  				}
    89  			}
    90  		}
    91  		return nil
    92  	})
    93  }