istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/istio/eastwest.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 istio
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os"
    21  	"os/exec"
    22  	"path"
    23  
    24  	corev1 "k8s.io/api/core/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/components/cluster"
    29  	"istio.io/istio/pkg/test/scopes"
    30  	"istio.io/istio/pkg/test/util/retry"
    31  	"istio.io/istio/pkg/test/util/tmpl"
    32  )
    33  
    34  const (
    35  	eastWestIngressIstioNameLabel = "eastwestgateway"
    36  	eastWestIngressIstioLabel     = "istio=" + eastWestIngressIstioNameLabel
    37  	eastWestIngressServiceName    = "istio-" + eastWestIngressIstioNameLabel
    38  )
    39  
    40  var (
    41  	mcSamples              = path.Join(env.IstioSrc, "samples", "multicluster")
    42  	exposeIstiodGateway    = path.Join(mcSamples, "expose-istiod.yaml")
    43  	exposeIstiodGatewayRev = path.Join(mcSamples, "expose-istiod-rev.yaml.tmpl")
    44  	exposeServicesGateway  = path.Join(mcSamples, "expose-services.yaml")
    45  	genGatewayScript       = path.Join(mcSamples, "gen-eastwest-gateway.sh")
    46  )
    47  
    48  // deployEastWestGateway will create a separate gateway deployment for cross-cluster discovery or cross-network services.
    49  func (i *istioImpl) deployEastWestGateway(cluster cluster.Cluster, revision string, customSettings string) error {
    50  	// generate istio operator yaml
    51  	args := []string{
    52  		"--cluster", cluster.Name(),
    53  		"--network", cluster.NetworkName(),
    54  		"--revision", revision,
    55  		"--mesh", meshID,
    56  	}
    57  	if !i.env.IsMultiCluster() {
    58  		args = []string{"--single-cluster"}
    59  	}
    60  	cmd := exec.Command(genGatewayScript, args...)
    61  	gwIOP, err := cmd.CombinedOutput()
    62  	if err != nil {
    63  		return fmt.Errorf("failed generating eastwestgateway operator yaml: %v: %v", err, string(gwIOP))
    64  	}
    65  	iopFile := path.Join(i.workDir, fmt.Sprintf("eastwest-%s.yaml", cluster.Name()))
    66  	if err := os.WriteFile(iopFile, gwIOP, os.ModePerm); err != nil {
    67  		return err
    68  	}
    69  
    70  	// Install the gateway
    71  	s := i.ctx.Settings()
    72  	var inFileNames []string
    73  	inFileNames = append(inFileNames, iopFile)
    74  	if customSettings != "" {
    75  		inFileNames = append(inFileNames, customSettings)
    76  	}
    77  
    78  	setArgs := []string{
    79  		"hub=" + s.Image.Hub,
    80  		"tag=" + s.Image.Tag,
    81  		"values.global.imagePullPolicy=" + s.Image.PullPolicy,
    82  		"values.gateways.istio-ingressgateway.autoscaleEnabled=false",
    83  	}
    84  
    85  	if i.cfg.SystemNamespace != "istio-system" {
    86  		setArgs = append(setArgs, "values.global.istioNamespace="+i.cfg.SystemNamespace)
    87  	}
    88  
    89  	// Include all user-specified values.
    90  	for k, v := range i.cfg.Values {
    91  		setArgs = append(setArgs, fmt.Sprintf("values.%s=%s", k, v))
    92  	}
    93  
    94  	for k, v := range i.cfg.OperatorOptions {
    95  		setArgs = append(setArgs, fmt.Sprintf("%s=%s", k, v))
    96  	}
    97  
    98  	if err := i.installer.Install(cluster, installArgs{
    99  		ComponentName: "eastwestgateway",
   100  		Revision:      revision,
   101  		Files:         inFileNames,
   102  		Set:           setArgs,
   103  	}); err != nil {
   104  		return err
   105  	}
   106  
   107  	// wait for a ready pod
   108  	if err := retry.UntilSuccess(func() error {
   109  		pods, err := cluster.Kube().CoreV1().Pods(i.cfg.SystemNamespace).List(context.TODO(), metav1.ListOptions{
   110  			LabelSelector: eastWestIngressIstioLabel,
   111  		})
   112  		if err != nil {
   113  			return err
   114  		}
   115  		for _, p := range pods.Items {
   116  			if p.Status.Phase == corev1.PodRunning {
   117  				return nil
   118  			}
   119  		}
   120  		return fmt.Errorf("no ready pods for " + eastWestIngressIstioLabel)
   121  	}, componentDeployTimeout, componentDeployDelay); err != nil {
   122  		return fmt.Errorf("failed waiting for %s to become ready: %v", eastWestIngressServiceName, err)
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func (i *istioImpl) exposeUserServices(cluster cluster.Cluster) error {
   129  	scopes.Framework.Infof("Exposing services via eastwestgateway in %v", cluster.Name())
   130  	return cluster.ApplyYAMLFiles(i.cfg.SystemNamespace, exposeServicesGateway)
   131  }
   132  
   133  func (i *istioImpl) applyIstiodGateway(cluster cluster.Cluster, revision string) error {
   134  	scopes.Framework.Infof("Exposing istiod via eastwestgateway in %v", cluster.Name())
   135  	if revision == "" {
   136  		return cluster.ApplyYAMLFiles(i.cfg.SystemNamespace, exposeIstiodGateway)
   137  	}
   138  	gwTmpl, err := os.ReadFile(exposeIstiodGatewayRev)
   139  	if err != nil {
   140  		return fmt.Errorf("failed loading template %s: %v", exposeIstiodGatewayRev, err)
   141  	}
   142  	out, err := tmpl.Evaluate(string(gwTmpl), map[string]string{"Revision": revision})
   143  	if err != nil {
   144  		return fmt.Errorf("failed running template %s: %v", exposeIstiodGatewayRev, err)
   145  	}
   146  	return i.ctx.ConfigKube(cluster).YAML(i.cfg.SystemNamespace, out).Apply()
   147  }