istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/writer/ztunnel/configdump/configdump.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  	"encoding/json"
    19  	"fmt"
    20  	"io"
    21  	"text/tabwriter"
    22  
    23  	"sigs.k8s.io/yaml"
    24  )
    25  
    26  // ConfigWriter is a writer for processing responses from the Ztunnel Admin config_dump endpoint
    27  type ConfigWriter struct {
    28  	Stdout      io.Writer
    29  	ztunnelDump *ZtunnelDump
    30  	FullDump    []byte
    31  }
    32  
    33  // Prime loads the config dump into the writer ready for printing
    34  func (c *ConfigWriter) Prime(b []byte) error {
    35  	cd := ZtunnelDump{}
    36  	// TODO(fisherxu): migrate this to jsonpb when issue fixed in golang
    37  	// Issue to track -> https://github.com/golang/protobuf/issues/632
    38  	err := json.Unmarshal(b, &cd)
    39  	if err != nil {
    40  		return fmt.Errorf("error unmarshalling config dump response from ztunnel: %v", err)
    41  	}
    42  	c.ztunnelDump = &cd
    43  	return nil
    44  }
    45  
    46  // PrintBootstrapDump prints just the bootstrap config dump to the ConfigWriter stdout
    47  func (c *ConfigWriter) PrintBootstrapDump(outputFormat string) error {
    48  	// TODO
    49  	return nil
    50  }
    51  
    52  func (c *ConfigWriter) PrintFullSummary() error {
    53  	_, _ = c.Stdout.Write([]byte("\n"))
    54  	if err := c.PrintWorkloadSummary(WorkloadFilter{Verbose: true}); err != nil {
    55  		return err
    56  	}
    57  	_, _ = c.Stdout.Write([]byte("\n"))
    58  	if err := c.PrintServiceSummary(ServiceFilter{}); err != nil {
    59  		return err
    60  	}
    61  	_, _ = c.Stdout.Write([]byte("\n"))
    62  	if err := c.PrintPolicySummary(PolicyFilter{}); err != nil {
    63  		return err
    64  	}
    65  	_, _ = c.Stdout.Write([]byte("\n"))
    66  	if err := c.PrintSecretSummary(); err != nil {
    67  		return err
    68  	}
    69  	return nil
    70  }
    71  
    72  func (c *ConfigWriter) PrintFullDump(outputFormat string) error {
    73  	out := c.FullDump
    74  	if outputFormat == "yaml" {
    75  		var err error
    76  		out, err = yaml.JSONToYAML(out)
    77  		if err != nil {
    78  			return err
    79  		}
    80  	}
    81  	fmt.Fprintln(c.Stdout, string(out))
    82  	return nil
    83  }
    84  
    85  // PrintVersionSummary prints version information for Istio and Ztunnel from the config dump
    86  func (c *ConfigWriter) PrintVersionSummary() error {
    87  	// TODO
    88  	return nil
    89  }
    90  
    91  // PrintPodRootCAFromDynamicSecretDump prints just pod's root ca from dynamic secret config dump to the ConfigWriter stdout
    92  func (c *ConfigWriter) PrintPodRootCAFromDynamicSecretDump() (string, error) {
    93  	// TODO
    94  	return "", nil
    95  }
    96  
    97  func (c *ConfigWriter) tabwriter() *tabwriter.Writer {
    98  	w := new(tabwriter.Writer).Init(c.Stdout, 0, 8, 1, ' ', 0)
    99  	return w
   100  }