istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/util/configdump/listener.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  
    20  	admin "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
    21  	listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
    22  
    23  	v3 "istio.io/istio/pilot/pkg/xds/v3"
    24  )
    25  
    26  // GetDynamicListenerDump retrieves a listener dump with just dynamic active listeners in it
    27  func (w *Wrapper) GetDynamicListenerDump(stripVersions bool) (*admin.ListenersConfigDump, error) {
    28  	listenerDump, err := w.GetListenerConfigDump()
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	dal := make([]*admin.ListenersConfigDump_DynamicListener, 0)
    34  	for _, l := range listenerDump.DynamicListeners {
    35  		// If a listener was reloaded, it would contain both the active and draining state
    36  		// delete the draining state for proper comparison
    37  		l.DrainingState = nil
    38  		if l.ActiveState != nil {
    39  			dal = append(dal, l)
    40  		}
    41  	}
    42  
    43  	// Support v2 or v3 in config dump. See ads.go:RequestedTypes for more info.
    44  	for i := range dal {
    45  		dal[i].ActiveState.Listener.TypeUrl = v3.ListenerType
    46  	}
    47  	sort.Slice(dal, func(i, j int) bool {
    48  		l := &listener.Listener{}
    49  		err = dal[i].ActiveState.Listener.UnmarshalTo(l)
    50  		if err != nil {
    51  			return false
    52  		}
    53  		name := l.Name
    54  		err = dal[j].ActiveState.Listener.UnmarshalTo(l)
    55  		if err != nil {
    56  			return false
    57  		}
    58  		return name < l.Name
    59  	})
    60  	if stripVersions {
    61  		for i := range dal {
    62  			dal[i].ActiveState.VersionInfo = ""
    63  			dal[i].ActiveState.LastUpdated = nil
    64  			dal[i].Name = "" // In Istio 1.5, Envoy creates this; suppress it
    65  		}
    66  	}
    67  	return &admin.ListenersConfigDump{DynamicListeners: dal}, nil
    68  }
    69  
    70  // GetListenerConfigDump retrieves the listener config dump from the ConfigDump
    71  func (w *Wrapper) GetListenerConfigDump() (*admin.ListenersConfigDump, error) {
    72  	listenerDumpAny, err := w.getSection(listeners)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	listenerDump := &admin.ListenersConfigDump{}
    77  	err = listenerDumpAny.UnmarshalTo(listenerDump)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return listenerDump, nil
    82  }