istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/workload.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 echo
    16  
    17  import (
    18  	"context"
    19  	"sort"
    20  	"strings"
    21  
    22  	"istio.io/istio/pkg/test"
    23  	"istio.io/istio/pkg/test/echo"
    24  	"istio.io/istio/pkg/test/echo/proto"
    25  	"istio.io/istio/pkg/test/framework/components/cluster"
    26  )
    27  
    28  // WorkloadContainer is container for a number of Workload objects.
    29  type WorkloadContainer interface {
    30  	// Workloads retrieves the list of all deployed workloads for this Echo service.
    31  	// Guarantees at least one workload, if error == nil.
    32  	Workloads() (Workloads, error)
    33  	WorkloadsOrFail(t test.Failer) Workloads
    34  	MustWorkloads() Workloads
    35  
    36  	// Clusters where the workloads are deployed.
    37  	Clusters() cluster.Clusters
    38  }
    39  
    40  // Workload provides an interface for a single deployed echo server.
    41  type Workload interface {
    42  	// PodName gets the original pod name for the workload.
    43  	PodName() string
    44  
    45  	// Address returns the network address of the endpoint.
    46  	Address() string
    47  
    48  	// Addresses returns the network addresses of the endpoint.
    49  	Addresses() []string
    50  
    51  	// Sidecar if one was specified.
    52  	Sidecar() Sidecar
    53  
    54  	// Cluster where this Workload resides.
    55  	Cluster() cluster.Cluster
    56  
    57  	// ForwardEcho executes specific call from this workload.
    58  	// TODO(nmittler): Instead of this, we should just make Workload implement Caller.
    59  	ForwardEcho(context.Context, *proto.ForwardEchoRequest) (echo.Responses, error)
    60  
    61  	// Logs returns the logs for the app container
    62  	Logs() (string, error)
    63  	LogsOrFail(t test.Failer) string
    64  }
    65  
    66  type Workloads []Workload
    67  
    68  func (ws Workloads) Len() int {
    69  	return len(ws)
    70  }
    71  
    72  // Addresses returns the list of addresses for all workloads.
    73  func (ws Workloads) Addresses() []string {
    74  	out := make([]string, 0, len(ws))
    75  	for _, w := range ws {
    76  		out = append(out, w.Addresses()...)
    77  	}
    78  	return out
    79  }
    80  
    81  func (ws Workloads) Clusters() cluster.Clusters {
    82  	clusters := make(map[string]cluster.Cluster)
    83  	for _, w := range ws {
    84  		if c := w.Cluster(); c != nil {
    85  			clusters[c.Name()] = c
    86  		}
    87  	}
    88  	out := make(cluster.Clusters, 0, len(clusters))
    89  	for _, c := range clusters {
    90  		out = append(out, c)
    91  	}
    92  
    93  	// Sort the clusters by name.
    94  	sort.SliceStable(out, func(i, j int) bool {
    95  		return strings.Compare(out[i].Name(), out[j].Name()) < 0
    96  	})
    97  
    98  	return out
    99  }