istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/util/configdump/route.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 configdump
    16  
    17  import (
    18  	"sort"
    19  	"time"
    20  
    21  	admin "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
    22  	route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
    23  
    24  	"istio.io/istio/pilot/pkg/util/protoconv"
    25  	v3 "istio.io/istio/pilot/pkg/xds/v3"
    26  )
    27  
    28  // GetLastUpdatedDynamicRouteTime retrieves the LastUpdated timestamp of the
    29  // most recently updated DynamicRouteConfig
    30  func (w *Wrapper) GetLastUpdatedDynamicRouteTime() (*time.Time, error) {
    31  	routeDump, err := w.GetRouteConfigDump()
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	drc := routeDump.GetDynamicRouteConfigs()
    36  
    37  	lastUpdated := time.Unix(0, 0) // get the oldest possible timestamp
    38  	for i := range drc {
    39  		if drc[i].LastUpdated != nil {
    40  			drLastUpdated := drc[i].LastUpdated.AsTime()
    41  			if drLastUpdated.After(lastUpdated) {
    42  				lastUpdated = drLastUpdated
    43  			}
    44  		}
    45  	}
    46  	if lastUpdated.After(time.Unix(0, 0)) { // if a timestamp was obtained from a drc
    47  		return &lastUpdated, nil
    48  	}
    49  	return nil, nil
    50  }
    51  
    52  // GetDynamicRouteDump retrieves a route dump with just dynamic active routes in it
    53  func (w *Wrapper) GetDynamicRouteDump(stripVersions bool) (*admin.RoutesConfigDump, error) {
    54  	routeDump, err := w.GetRouteConfigDump()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	drc := routeDump.GetDynamicRouteConfigs()
    59  	// Support v2 or v3 in config dump. See ads.go:RequestedTypes for more info.
    60  	for i := range drc {
    61  		drc[i].RouteConfig.TypeUrl = v3.RouteType
    62  	}
    63  	sort.Slice(drc, func(i, j int) bool {
    64  		r := &route.RouteConfiguration{}
    65  		err = drc[i].RouteConfig.UnmarshalTo(r)
    66  		if err != nil {
    67  			return false
    68  		}
    69  		name := r.Name
    70  		err = drc[j].RouteConfig.UnmarshalTo(r)
    71  		if err != nil {
    72  			return false
    73  		}
    74  		return name < r.Name
    75  	})
    76  
    77  	// In Istio 1.5, it is not enough just to sort the routes.  The virtual hosts
    78  	// within a route might have a different order.  Sort those too.
    79  	for i := range drc {
    80  		route := &route.RouteConfiguration{}
    81  		err = drc[i].RouteConfig.UnmarshalTo(route)
    82  		if err != nil {
    83  			return nil, err
    84  		}
    85  		sort.Slice(route.VirtualHosts, func(i, j int) bool {
    86  			return route.VirtualHosts[i].Name < route.VirtualHosts[j].Name
    87  		})
    88  		drc[i].RouteConfig = protoconv.MessageToAny(route)
    89  	}
    90  
    91  	if stripVersions {
    92  		for i := range drc {
    93  			drc[i].VersionInfo = ""
    94  			drc[i].LastUpdated = nil
    95  		}
    96  	}
    97  	return &admin.RoutesConfigDump{DynamicRouteConfigs: drc}, nil
    98  }
    99  
   100  // GetRouteConfigDump retrieves the route config dump from the ConfigDump
   101  func (w *Wrapper) GetRouteConfigDump() (*admin.RoutesConfigDump, error) {
   102  	routeDumpAny, err := w.getSection(routes)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	routeDump := &admin.RoutesConfigDump{}
   107  	err = routeDumpAny.UnmarshalTo(routeDump)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	return routeDump, nil
   112  }