github.com/noironetworks/cilium-net@v1.6.12/cilium-health/cmd/status.go (about)

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