istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/proxyconfig/clusters.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 proxyconfig 16 17 import ( 18 "context" 19 "encoding/json" 20 "fmt" 21 "io" 22 "text/tabwriter" 23 24 "github.com/spf13/cobra" 25 26 "istio.io/istio/istioctl/pkg/cli" 27 "istio.io/istio/istioctl/pkg/clioptions" 28 "istio.io/istio/pkg/cluster" 29 ) 30 31 // TODO move to multicluster package; requires exposing some private funcs/vars in this package 32 func ClustersCommand(ctx cli.Context) *cobra.Command { 33 var opts clioptions.ControlPlaneOptions 34 cmd := &cobra.Command{ 35 Use: "remote-clusters", 36 Short: "Lists the remote clusters each istiod instance is connected to.", 37 RunE: func(cmd *cobra.Command, args []string) error { 38 kubeClient, err := ctx.CLIClientWithRevision(opts.Revision) 39 if err != nil { 40 return err 41 } 42 res, err := kubeClient.AllDiscoveryDo(context.Background(), ctx.IstioNamespace(), "debug/clusterz") 43 if err != nil { 44 return err 45 } 46 return writeMulticlusterStatus(cmd.OutOrStdout(), res) 47 }, 48 } 49 opts.AttachControlPlaneFlags(cmd) 50 return cmd 51 } 52 53 func writeMulticlusterStatus(out io.Writer, input map[string][]byte) error { 54 statuses, err := parseClusterStatuses(input) 55 if err != nil { 56 return err 57 } 58 w := new(tabwriter.Writer).Init(out, 0, 8, 5, ' ', 0) 59 _, _ = fmt.Fprintln(w, "NAME\tSECRET\tSTATUS\tISTIOD") 60 for istiod, clusters := range statuses { 61 for _, c := range clusters { 62 _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", c.ID, c.SecretName, c.SyncStatus, istiod) 63 } 64 } 65 _ = w.Flush() 66 return nil 67 } 68 69 func parseClusterStatuses(input map[string][]byte) (map[string][]cluster.DebugInfo, error) { 70 statuses := make(map[string][]cluster.DebugInfo, len(input)) 71 for istiodKey, bytes := range input { 72 var parsed []cluster.DebugInfo 73 if err := json.Unmarshal(bytes, &parsed); err != nil { 74 return nil, err 75 } 76 statuses[istiodKey] = parsed 77 } 78 return statuses, nil 79 }