istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/analysis/analyzers/virtualservice/util.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 virtualservice
    16  
    17  import (
    18  	"istio.io/api/networking/v1alpha3"
    19  )
    20  
    21  // AnnotatedDestination holds metadata about a Destination object that is used for analyzing
    22  type AnnotatedDestination struct {
    23  	RouteRule        string
    24  	ServiceIndex     int
    25  	DestinationIndex int
    26  	Destination      *v1alpha3.Destination
    27  }
    28  
    29  func getRouteDestinations(vs *v1alpha3.VirtualService) []*AnnotatedDestination {
    30  	destinations := make([]*AnnotatedDestination, 0)
    31  	for i, r := range vs.GetTcp() {
    32  		for j, rd := range r.GetRoute() {
    33  			destinations = append(destinations, &AnnotatedDestination{
    34  				RouteRule:        "tcp",
    35  				ServiceIndex:     i,
    36  				DestinationIndex: j,
    37  				Destination:      rd.GetDestination(),
    38  			})
    39  		}
    40  	}
    41  	for i, r := range vs.GetTls() {
    42  		for j, rd := range r.GetRoute() {
    43  			destinations = append(destinations, &AnnotatedDestination{
    44  				RouteRule:        "tls",
    45  				ServiceIndex:     i,
    46  				DestinationIndex: j,
    47  				Destination:      rd.GetDestination(),
    48  			})
    49  		}
    50  	}
    51  	for i, r := range vs.GetHttp() {
    52  		for j, rd := range r.GetRoute() {
    53  			destinations = append(destinations, &AnnotatedDestination{
    54  				RouteRule:        "http",
    55  				ServiceIndex:     i,
    56  				DestinationIndex: j,
    57  				Destination:      rd.GetDestination(),
    58  			})
    59  		}
    60  	}
    61  
    62  	return destinations
    63  }
    64  
    65  func getHTTPMirrorDestinations(vs *v1alpha3.VirtualService) []*AnnotatedDestination {
    66  	var destinations []*AnnotatedDestination
    67  
    68  	for i, r := range vs.GetHttp() {
    69  		if m := r.GetMirror(); m != nil {
    70  			destinations = append(destinations, &AnnotatedDestination{
    71  				RouteRule:    "http.mirror",
    72  				ServiceIndex: i,
    73  				Destination:  m,
    74  			})
    75  		}
    76  
    77  		for j, m := range r.GetMirrors() {
    78  			destinations = append(destinations, &AnnotatedDestination{
    79  				RouteRule:        "http.mirrors",
    80  				ServiceIndex:     i,
    81  				DestinationIndex: j,
    82  				Destination:      m.GetDestination(),
    83  			})
    84  		}
    85  	}
    86  
    87  	return destinations
    88  }