github.com/cilium/cilium@v1.16.2/operator/cmd/status.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package cmd 5 6 import ( 7 "context" 8 "fmt" 9 "io" 10 "os" 11 "os/signal" 12 13 "github.com/go-openapi/strfmt" 14 "github.com/spf13/cobra" 15 16 "github.com/cilium/cilium/api/v1/operator/client" 17 "github.com/cilium/cilium/api/v1/operator/client/cluster" 18 "github.com/cilium/cilium/operator/api" 19 ciliumdbg "github.com/cilium/cilium/pkg/client" 20 "github.com/cilium/cilium/pkg/command" 21 ) 22 23 // StatusCmd represents the status command for the operator. 24 var StatusCmd = &cobra.Command{ 25 Use: "status", 26 Short: "Display status of operator", 27 } 28 29 var StatusClusterMesh = func() *cobra.Command { 30 var host string 31 var verbose bool 32 33 cmd := &cobra.Command{ 34 Use: "clustermesh", 35 Short: "Display status of remote clusters", 36 Run: func(cmd *cobra.Command, args []string) { status(cmd.Context(), host, cmd.OutOrStdout(), verbose) }, 37 38 PersistentPreRun: func(cmd *cobra.Command, args []string) { 39 ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt) 40 cmd.SetContext(ctx) 41 }, 42 } 43 44 StatusCmd.AddCommand(cmd) 45 command.AddOutputOption(cmd) 46 47 cmd.Flags().StringVarP(&host, "server-address", "s", api.OperatorAPIServeAddrDefault, "Address of the operator API server") 48 cmd.Flags().BoolVar(&verbose, "verbose", false, "Output verbose status information for ready clusters as well") 49 50 return cmd 51 }() 52 53 func status(ctx context.Context, host string, writer io.Writer, verbose bool) { 54 cfg := client.DefaultTransportConfig().WithHost(host) 55 cl := client.NewHTTPClientWithConfig(strfmt.Default, cfg) 56 57 params := cluster.NewGetClusterParams().WithContext(ctx) 58 resp, err := cl.Cluster.GetCluster(params) 59 if err != nil { 60 fmt.Printf("Failed to retrieve status information: %s\n", err) 61 os.Exit(1) 62 } 63 64 if command.OutputOption() { 65 if err := command.PrintOutput(resp.Payload); err != nil { 66 fmt.Printf("Failed to output status information: %s\n", err) 67 os.Exit(1) 68 } 69 return 70 } 71 72 clusters := resp.GetPayload() 73 74 verbosity := ciliumdbg.RemoteClustersStatusBrief 75 if verbose { 76 verbosity = ciliumdbg.RemoteClustersStatusVerbose 77 } 78 79 fmt.Fprintf(writer, "ClusterMesh:\t%d/%d clusters ready\n", 80 ciliumdbg.NumReadyClusters(clusters), len(clusters)) 81 ciliumdbg.FormatStatusResponseRemoteClusters(writer, clusters, verbosity) 82 }