istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/environment/kube/kube.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 kube
    16  
    17  import (
    18  	"istio.io/istio/pkg/test/framework/components/cluster"
    19  	"istio.io/istio/pkg/test/framework/components/cluster/clusterboot"
    20  	"istio.io/istio/pkg/test/framework/resource"
    21  	"istio.io/istio/pkg/test/scopes"
    22  )
    23  
    24  // Environment is the implementation of a kubernetes environment. It implements environment.Environment,
    25  // and also hosts publicly accessible methods that are specific to cluster environment.
    26  type Environment struct {
    27  	id       resource.ID
    28  	ctx      resource.Context
    29  	clusters []cluster.Cluster
    30  	s        *Settings
    31  }
    32  
    33  var _ resource.Environment = &Environment{}
    34  
    35  // New returns a new Kubernetes environment
    36  func New(ctx resource.Context, s *Settings) (env resource.Environment, err error) {
    37  	defer func() {
    38  		if err != nil && !ctx.Settings().CIMode {
    39  			scopes.Framework.Infof(`
    40  There was an error while setting up the Kubernetes test environment.
    41  Check the test framework wiki for details on running the tests locally:
    42      https://github.com/istio/istio/wiki/Istio-Test-Framework
    43  `)
    44  		}
    45  	}()
    46  	scopes.Framework.Infof("Test Framework Kubernetes environment Settings:\n%s", s)
    47  	e := &Environment{
    48  		ctx: ctx,
    49  		s:   s,
    50  	}
    51  	e.id = ctx.TrackResource(e)
    52  
    53  	configs, err := s.clusterConfigs()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	clusters, err := clusterboot.NewFactory().With(configs...).Build()
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	e.clusters = clusters
    62  
    63  	return e, nil
    64  }
    65  
    66  func (e *Environment) EnvironmentName() string {
    67  	return "Kube"
    68  }
    69  
    70  func (e *Environment) IsMultiCluster() bool {
    71  	return len(e.clusters) > 1
    72  }
    73  
    74  // IsMultinetwork returns true if there is more than one network name in networkTopology.
    75  func (e *Environment) IsMultiNetwork() bool {
    76  	return len(e.ClustersByNetwork()) > 1
    77  }
    78  
    79  func (e *Environment) AllClusters() cluster.Clusters {
    80  	out := make([]cluster.Cluster, 0, len(e.clusters))
    81  	out = append(out, e.clusters...)
    82  	return out
    83  }
    84  
    85  func (e *Environment) Clusters() cluster.Clusters {
    86  	return e.AllClusters().MeshClusters()
    87  }
    88  
    89  // ClustersByNetwork returns an inverse mapping of the network topolgoy to a slice of clusters in a given network.
    90  func (e *Environment) ClustersByNetwork() map[string][]cluster.Cluster {
    91  	out := make(map[string][]cluster.Cluster)
    92  	for _, c := range e.Clusters() {
    93  		out[c.NetworkName()] = append(out[c.NetworkName()], c)
    94  	}
    95  	return out
    96  }
    97  
    98  // ID implements resource.Instance
    99  func (e *Environment) ID() resource.ID {
   100  	return e.id
   101  }
   102  
   103  func (e *Environment) Settings() *Settings {
   104  	return e.s.clone()
   105  }