github.com/cilium/cilium@v1.16.2/cilium-health/cmd/status.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package cmd
     5  
     6  import (
     7  	"os"
     8  	"text/tabwriter"
     9  
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/cilium/cilium/api/v1/health/models"
    13  	"github.com/cilium/cilium/pkg/command"
    14  	clientPkg "github.com/cilium/cilium/pkg/health/client"
    15  )
    16  
    17  var (
    18  	probe    bool
    19  	succinct bool
    20  	verbose  bool
    21  )
    22  
    23  // statusGetCmd represents the status command
    24  var statusGetCmd = &cobra.Command{
    25  	Use:     "status",
    26  	Aliases: []string{"connectivity"},
    27  	Short:   "Display cilium connectivity to other nodes",
    28  	Run: func(cmd *cobra.Command, args []string) {
    29  		var sr *models.HealthStatusResponse
    30  
    31  		if client == nil {
    32  			Fatalf("Invalid combination of arguments")
    33  		}
    34  
    35  		if probe {
    36  			result, err := client.Connectivity.PutStatusProbe(nil)
    37  			if err != nil {
    38  				Fatalf("Cannot get status/probe: %s\n", err)
    39  			}
    40  			sr = result.Payload
    41  		} else {
    42  			result, err := client.Connectivity.GetStatus(nil)
    43  			if err != nil {
    44  				Fatalf("Cannot get status: %s\n", err)
    45  			}
    46  			sr = result.Payload
    47  		}
    48  
    49  		if command.OutputOption() {
    50  			if err := command.PrintOutput(sr); err != nil {
    51  				os.Exit(1)
    52  			}
    53  		} else {
    54  			w := tabwriter.NewWriter(os.Stdout, 2, 0, 3, ' ', 0)
    55  			clientPkg.FormatHealthStatusResponse(w, sr, true, succinct, verbose, 0)
    56  			w.Flush()
    57  		}
    58  	},
    59  }
    60  
    61  func init() {
    62  	rootCmd.AddCommand(statusGetCmd)
    63  	statusGetCmd.Flags().BoolVarP(&probe, "probe", "", false,
    64  		"Synchronously probe connectivity status")
    65  	statusGetCmd.Flags().BoolVarP(&succinct, "succinct", "", false,
    66  		"Print the result succinctly (one node per line)")
    67  	statusGetCmd.Flags().BoolVarP(&verbose, "verbose", "", false,
    68  		"Print more information in results")
    69  	command.AddOutputOption(statusGetCmd)
    70  }