istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/util/configdump/cluster.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  	cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
    22  
    23  	v3 "istio.io/istio/pilot/pkg/xds/v3"
    24  )
    25  
    26  // GetDynamicClusterDump retrieves a cluster dump with just dynamic active clusters in it
    27  func (w *Wrapper) GetDynamicClusterDump(stripVersions bool) (*admin.ClustersConfigDump, error) {
    28  	clusterDump, err := w.GetClusterConfigDump()
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	dac := clusterDump.GetDynamicActiveClusters()
    33  	// Allow sorting to work even if we don't have the exact same type
    34  	for i := range dac {
    35  		dac[i].Cluster.TypeUrl = v3.ClusterType
    36  	}
    37  	sort.Slice(dac, func(i, j int) bool {
    38  		cluster := &cluster.Cluster{}
    39  		err = dac[i].Cluster.UnmarshalTo(cluster)
    40  		if err != nil {
    41  			return false
    42  		}
    43  		name := cluster.Name
    44  		err = dac[j].Cluster.UnmarshalTo(cluster)
    45  		if err != nil {
    46  			return false
    47  		}
    48  		return name < cluster.Name
    49  	})
    50  	if stripVersions {
    51  		for i := range dac {
    52  			dac[i].VersionInfo = ""
    53  			dac[i].LastUpdated = nil
    54  		}
    55  	}
    56  	return &admin.ClustersConfigDump{DynamicActiveClusters: dac}, nil
    57  }
    58  
    59  // GetClusterConfigDump retrieves the cluster config dump from the ConfigDump
    60  func (w *Wrapper) GetClusterConfigDump() (*admin.ClustersConfigDump, error) {
    61  	clusterDumpAny, err := w.getSection(clusters)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	clusterDump := &admin.ClustersConfigDump{}
    66  	err = clusterDumpAny.UnmarshalTo(clusterDump)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return clusterDump, nil
    71  }