istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/integration/pilot/mcs/common/common.go (about)

     1  //go:build integ
     2  // +build integ
     3  
     4  // Copyright Istio Authors
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package common
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"os"
    24  
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  
    27  	"istio.io/istio/pkg/test/framework/components/cluster"
    28  	"istio.io/istio/pkg/test/framework/components/echo"
    29  	"istio.io/istio/pkg/test/framework/components/echo/common/ports"
    30  	"istio.io/istio/pkg/test/framework/components/echo/deployment"
    31  	"istio.io/istio/pkg/test/framework/components/environment/kube"
    32  	"istio.io/istio/pkg/test/framework/components/namespace"
    33  	"istio.io/istio/pkg/test/framework/resource"
    34  	"istio.io/istio/pkg/test/framework/resource/config/apply"
    35  	"istio.io/istio/pkg/test/util/tmpl"
    36  )
    37  
    38  const (
    39  	ServiceA = "svc-a"
    40  	ServiceB = "svc-b"
    41  )
    42  
    43  func IsMCSControllerEnabled(t resource.Context) bool {
    44  	return KubeSettings(t).MCSControllerEnabled
    45  }
    46  
    47  func KubeSettings(t resource.Context) *kube.Settings {
    48  	return t.Environment().(*kube.Environment).Settings()
    49  }
    50  
    51  func InstallMCSCRDs(t resource.Context) error {
    52  	params := struct {
    53  		Group   string
    54  		Version string
    55  	}{
    56  		Group:   KubeSettings(t).MCSAPIGroup,
    57  		Version: KubeSettings(t).MCSAPIVersion,
    58  	}
    59  
    60  	for _, kind := range []string{"serviceexport", "serviceimport"} {
    61  		// Generate the CRD YAML
    62  		fileName := fmt.Sprintf("mcs-%s-crd.yaml", kind)
    63  		crdTemplate, err := os.ReadFile("../../testdata/" + fileName)
    64  		if err != nil {
    65  			return err
    66  		}
    67  		crdYAML, err := tmpl.Evaluate(string(crdTemplate), params)
    68  		if err != nil {
    69  			return err
    70  		}
    71  
    72  		// Make sure the CRD exists in each cluster.
    73  		for _, c := range t.Clusters() {
    74  			crdName := fmt.Sprintf("%ss.%s", kind, params.Group)
    75  			if isCRDInstalled(c, crdName, params.Version) {
    76  				// It's already installed on this cluster - nothing to do.
    77  				continue
    78  			}
    79  
    80  			// Add/Update the CRD in this cluster...
    81  			if t.Settings().NoCleanup {
    82  				if err := t.ConfigKube(c).YAML("", crdYAML).Apply(apply.NoCleanup); err != nil {
    83  					return err
    84  				}
    85  			} else {
    86  				if err := t.ConfigKube(c).YAML("", crdYAML).Apply(); err != nil {
    87  					return err
    88  				}
    89  			}
    90  		}
    91  	}
    92  	return nil
    93  }
    94  
    95  func isCRDInstalled(c cluster.Cluster, crdName string, version string) bool {
    96  	crd, err := c.Ext().ApiextensionsV1().CustomResourceDefinitions().Get(context.TODO(), crdName, metav1.GetOptions{})
    97  	if err == nil {
    98  		// Found the CRD, now check against the version.
    99  		for _, v := range crd.Spec.Versions {
   100  			if v.Name == version {
   101  				// The CRD is already installed on this cluster.
   102  				return true
   103  			}
   104  		}
   105  	}
   106  	return false
   107  }
   108  
   109  type EchoDeployment struct {
   110  	Namespace namespace.Instance
   111  	echo.Instances
   112  }
   113  
   114  func DeployEchosFunc(nsPrefix string, d *EchoDeployment) func(t resource.Context) error {
   115  	return func(t resource.Context) error {
   116  		// Create a new namespace in each cluster.
   117  		ns, err := namespace.New(t, namespace.Config{
   118  			Prefix: nsPrefix,
   119  			Inject: true,
   120  		})
   121  		if err != nil {
   122  			return err
   123  		}
   124  		d.Namespace = ns
   125  
   126  		// Create echo instances in each cluster.
   127  		d.Instances, err = deployment.New(t).
   128  			WithClusters(t.Clusters()...).
   129  			WithConfig(echo.Config{
   130  				Service:   ServiceA,
   131  				Namespace: ns,
   132  				Ports:     ports.All(),
   133  			}).
   134  			WithConfig(echo.Config{
   135  				Service:   ServiceB,
   136  				Namespace: ns,
   137  				Ports:     ports.All(),
   138  			}).Build()
   139  		return err
   140  	}
   141  }