istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/envoy/configdump/ecds.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  	"fmt"
    19  	"sort"
    20  	"strings"
    21  	"text/tabwriter"
    22  
    23  	core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
    24  	"sigs.k8s.io/yaml"
    25  
    26  	"istio.io/istio/pkg/util/protomarshal"
    27  )
    28  
    29  const typeURLPrefix = "type.googleapis.com/"
    30  
    31  func (c *ConfigWriter) PrintEcds(outputFormat string) error {
    32  	if c.configDump == nil {
    33  		return fmt.Errorf("config writer has not been primed")
    34  	}
    35  
    36  	dump, err := c.configDump.GetEcdsConfigDump()
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	out, err := protomarshal.MarshalIndentWithGlobalTypesResolver(dump, "    ")
    42  	if err != nil {
    43  		return err
    44  	}
    45  	if outputFormat == "yaml" {
    46  		if out, err = yaml.JSONToYAML(out); err != nil {
    47  			return err
    48  		}
    49  	}
    50  	_, _ = fmt.Fprintln(c.Stdout, string(out))
    51  	return nil
    52  }
    53  
    54  func (c *ConfigWriter) PrintEcdsSummary() error {
    55  	w := new(tabwriter.Writer).Init(c.Stdout, 0, 8, 5, ' ', 0)
    56  
    57  	fmt.Fprintln(w, "ECDS NAME\tTYPE")
    58  	dump, err := c.retrieveSortedEcds()
    59  	if err != nil {
    60  		return err
    61  	}
    62  	for _, ecds := range dump {
    63  		fmt.Fprintf(w, "%v\t%v\n",
    64  			ecds.Name,
    65  			strings.TrimPrefix(ecds.GetTypedConfig().GetTypeUrl(), typeURLPrefix),
    66  		)
    67  	}
    68  
    69  	return w.Flush()
    70  }
    71  
    72  func (c *ConfigWriter) retrieveSortedEcds() ([]*core.TypedExtensionConfig, error) {
    73  	if c.configDump == nil {
    74  		return nil, fmt.Errorf("config writer has not been primed")
    75  	}
    76  
    77  	dump, err := c.configDump.GetEcdsConfigDump()
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	ecds := make([]*core.TypedExtensionConfig, 0, len(dump.EcdsFilters))
    83  	for _, config := range dump.GetEcdsFilters() {
    84  		c := &core.TypedExtensionConfig{}
    85  		err := config.GetEcdsFilter().UnmarshalTo(c)
    86  		if err != nil {
    87  			return nil, fmt.Errorf("failed to retrieve TypedExtensionConfig: %v", err)
    88  		}
    89  
    90  		ecds = append(ecds, c)
    91  	}
    92  
    93  	sort.Slice(ecds, func(i, j int) bool {
    94  		if ecds[i].GetTypedConfig().GetTypeUrl() == ecds[j].GetTypedConfig().GetTypeUrl() {
    95  			return ecds[i].GetName() < ecds[j].GetName()
    96  		}
    97  
    98  		return ecds[i].GetTypedConfig().GetTypeUrl() < ecds[j].GetTypedConfig().GetTypeUrl()
    99  	})
   100  
   101  	return ecds, nil
   102  }