istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/match/matchers.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 match
    16  
    17  import (
    18  	"istio.io/api/annotation"
    19  	"istio.io/istio/pkg/test/framework/components/cluster"
    20  	"istio.io/istio/pkg/test/framework/components/echo"
    21  	"istio.io/istio/pkg/test/framework/components/namespace"
    22  )
    23  
    24  // Any doesn't filter out any echos.
    25  var Any Matcher = func(_ echo.Instance) bool {
    26  	return true
    27  }
    28  
    29  // And is an aggregate Matcher that requires all matches return true.
    30  func And(ms ...Matcher) Matcher {
    31  	return func(i echo.Instance) bool {
    32  		for _, m := range ms {
    33  			if m != nil && !m(i) {
    34  				return false
    35  			}
    36  		}
    37  		return true
    38  	}
    39  }
    40  
    41  // Or is an aggregate Matcher that requires at least one matches return true.
    42  func Or(ms ...Matcher) Matcher {
    43  	return func(i echo.Instance) bool {
    44  		for _, m := range ms {
    45  			if m != nil && m(i) {
    46  				return true
    47  			}
    48  		}
    49  		return false
    50  	}
    51  }
    52  
    53  // Not negates the given matcher. Example:
    54  //
    55  //	Not(Naked())
    56  func Not(m Matcher) Matcher {
    57  	return func(i echo.Instance) bool {
    58  		return !m(i)
    59  	}
    60  }
    61  
    62  // ServiceName matches instances with the given namespace and service name.
    63  func ServiceName(n echo.NamespacedName) Matcher {
    64  	return func(i echo.Instance) bool {
    65  		return n == i.NamespacedName()
    66  	}
    67  }
    68  
    69  // AnyServiceName matches instances if they have the same Service and Namespace as any of the provided instances.
    70  func AnyServiceName(expected echo.NamespacedNames) Matcher {
    71  	return func(instance echo.Instance) bool {
    72  		serviceName := instance.NamespacedName()
    73  		for _, expectedName := range expected {
    74  			if serviceName == expectedName {
    75  				return true
    76  			}
    77  		}
    78  		return false
    79  	}
    80  }
    81  
    82  // Namespace matches instances within the given namespace name.
    83  func Namespace(n namespace.Instance) Matcher {
    84  	return NamespaceName(n.Name())
    85  }
    86  
    87  // NamespaceName matches instances within the given namespace name.
    88  func NamespaceName(ns string) Matcher {
    89  	return func(i echo.Instance) bool {
    90  		return i.Config().Namespace.Name() == ns
    91  	}
    92  }
    93  
    94  // Cluster matches instances deployed on the given cluster.
    95  func Cluster(c cluster.Cluster) Matcher {
    96  	return func(i echo.Instance) bool {
    97  		return c.Name() == i.Config().Cluster.Name()
    98  	}
    99  }
   100  
   101  // Network matches instances deployed in the given network.
   102  func Network(n string) Matcher {
   103  	return func(i echo.Instance) bool {
   104  		return i.Config().Cluster.NetworkName() == n
   105  	}
   106  }
   107  
   108  // VM matches instances with DeployAsVM
   109  var VM Matcher = func(i echo.Instance) bool {
   110  	return i.Config().IsVM()
   111  }
   112  
   113  // NotVM is matches against instances that are NOT VMs.
   114  var NotVM = Not(VM)
   115  
   116  // External matches instances that have a custom DefaultHostHeader defined
   117  var External Matcher = func(i echo.Instance) bool {
   118  	return i.Config().IsExternal()
   119  }
   120  
   121  // NotExternal is equivalent to Not(External)
   122  var NotExternal = Not(External)
   123  
   124  // Naked matches instances with any subset marked with SidecarInject equal to false.
   125  var Naked Matcher = func(i echo.Instance) bool {
   126  	return i.Config().IsNaked()
   127  }
   128  
   129  // AllNaked matches instances where every subset has SidecarInject set to false.
   130  var AllNaked Matcher = func(i echo.Instance) bool {
   131  	return i.Config().IsAllNaked()
   132  }
   133  
   134  // NotNaked is equivalent to Not(Naked)
   135  var NotNaked = Not(Naked)
   136  
   137  // Headless matches instances that are backed by headless services.
   138  var Headless Matcher = func(i echo.Instance) bool {
   139  	return i.Config().Headless
   140  }
   141  
   142  // NotHeadless is equivalent to Not(Headless)
   143  var NotHeadless = Not(Headless)
   144  
   145  // ProxylessGRPC matches instances that are Pods with a SidecarInjectTemplate annotation equal to grpc.
   146  var ProxylessGRPC Matcher = func(i echo.Instance) bool {
   147  	return i.Config().IsProxylessGRPC()
   148  }
   149  
   150  // NotProxylessGRPC is equivalent to Not(ProxylessGRPC)
   151  var NotProxylessGRPC = Not(ProxylessGRPC)
   152  
   153  var TProxy Matcher = func(i echo.Instance) bool {
   154  	return i.Config().IsTProxy()
   155  }
   156  
   157  var NotTProxy = Not(TProxy)
   158  
   159  var ServiceAddressedWaypoint Matcher = func(i echo.Instance) bool {
   160  	return i.Config().HasServiceAddressedWaypointProxy()
   161  }
   162  
   163  var WorkloadAddressedWaypoint Matcher = func(i echo.Instance) bool {
   164  	return i.Config().HasWorkloadAddressedWaypointProxy()
   165  }
   166  
   167  var NotWaypoint = And(Not(ServiceAddressedWaypoint), Not(WorkloadAddressedWaypoint))
   168  
   169  // add a "waypointed service" matcher
   170  func WaypointService() Matcher {
   171  	return func(i echo.Instance) bool {
   172  		return ServiceAddressedWaypoint(i)
   173  	}
   174  }
   175  
   176  func AmbientCaptured() Matcher {
   177  	return func(i echo.Instance) bool {
   178  		return i.Config().ZTunnelCaptured() && !i.Config().IsUncaptured()
   179  	}
   180  }
   181  
   182  // RegularPod matches echos that don't meet any of the following criteria:
   183  // - VM
   184  // - Naked
   185  // - Headless
   186  // - TProxy
   187  // - Multi-Subset
   188  var RegularPod Matcher = func(instance echo.Instance) bool {
   189  	return instance.Config().IsRegularPod()
   190  }
   191  
   192  var NotRegularPod = Not(RegularPod)
   193  
   194  // MultiVersion matches echos that have Multi-version specific setup.
   195  var MultiVersion Matcher = func(i echo.Instance) bool {
   196  	if len(i.Config().Subsets) != 2 {
   197  		return false
   198  	}
   199  	var matchIstio, matchLegacy bool
   200  	for _, s := range i.Config().Subsets {
   201  		if s.Version == "vistio" {
   202  			matchIstio = true
   203  		} else if s.Version == "vlegacy" && s.Annotations[annotation.SidecarInject.Name] == "false" {
   204  			matchLegacy = true
   205  		}
   206  	}
   207  	return matchIstio && matchLegacy
   208  }
   209  
   210  var NotMultiVersion = Not(MultiVersion)