github.com/fafucoder/cilium@v1.6.11/cilium/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  	"fmt"
    19  	"os"
    20  	"text/tabwriter"
    21  
    22  	"github.com/cilium/cilium/api/v1/client/daemon"
    23  	"github.com/cilium/cilium/api/v1/models"
    24  	pkg "github.com/cilium/cilium/pkg/client"
    25  	"github.com/cilium/cilium/pkg/command"
    26  	healthPkg "github.com/cilium/cilium/pkg/health/client"
    27  
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  // statusCmd represents the daemon_status command
    32  var statusCmd = &cobra.Command{
    33  	Use:   "status",
    34  	Short: "Display status of daemon",
    35  	Run: func(cmd *cobra.Command, args []string) {
    36  		statusDaemon()
    37  	},
    38  }
    39  var (
    40  	allAddresses   bool
    41  	allControllers bool
    42  	allHealth      bool
    43  	allNodes       bool
    44  	allRedirects   bool
    45  	brief          bool
    46  	healthLines    = 10
    47  )
    48  
    49  func init() {
    50  	rootCmd.AddCommand(statusCmd)
    51  	statusCmd.Flags().BoolVar(&allAddresses, "all-addresses", false, "Show all allocated addresses, not just count")
    52  	statusCmd.Flags().BoolVar(&allControllers, "all-controllers", false, "Show all controllers, not just failing")
    53  	statusCmd.Flags().BoolVar(&allHealth, "all-health", false, "Show all health status, not just failing")
    54  	statusCmd.Flags().BoolVar(&allNodes, "all-nodes", false, "Show all nodes, not just localhost")
    55  	statusCmd.Flags().BoolVar(&allRedirects, "all-redirects", false, "Show all redirects")
    56  	statusCmd.Flags().BoolVar(&brief, "brief", false, "Only print a one-line status message")
    57  	statusCmd.Flags().BoolVar(&verbose, "verbose", false, "Equivalent to --all-addresses --all-controllers --all-nodes --all-health")
    58  	command.AddJSONOutput(statusCmd)
    59  }
    60  
    61  func statusDaemon() {
    62  	isUnhealthy := func(sr *models.StatusResponse) bool {
    63  		if sr.Cilium != nil {
    64  			state := sr.Cilium.State
    65  			return state != models.StatusStateOk && state != models.StatusStateDisabled
    66  		}
    67  
    68  		return false
    69  	}
    70  
    71  	if verbose {
    72  		allAddresses = true
    73  		allControllers = true
    74  		allHealth = true
    75  		allNodes = true
    76  		allRedirects = true
    77  	}
    78  	if allHealth {
    79  		healthLines = 0
    80  	}
    81  	params := daemon.NewGetHealthzParams()
    82  	params.SetBrief(&brief)
    83  	if resp, err := client.Daemon.GetHealthz(params); err != nil {
    84  		if brief {
    85  			fmt.Fprintf(os.Stderr, "%s\n", "cilium: daemon unreachable")
    86  		} else {
    87  			fmt.Fprintf(os.Stderr, "%s\n", pkg.Hint(err))
    88  		}
    89  		os.Exit(1)
    90  	} else if command.OutputJSON() {
    91  		if err := command.PrintOutput(resp.Payload); err != nil {
    92  			os.Exit(1)
    93  		}
    94  	} else if brief {
    95  		sr := resp.Payload
    96  		pkg.FormatStatusResponseBrief(os.Stdout, sr)
    97  		if isUnhealthy(sr) {
    98  			os.Exit(1)
    99  		}
   100  	} else {
   101  		sr := resp.Payload
   102  		w := tabwriter.NewWriter(os.Stdout, 2, 0, 3, ' ', 0)
   103  		pkg.FormatStatusResponse(w, sr, allAddresses, allControllers, allNodes, allRedirects)
   104  		w.Flush()
   105  
   106  		if isUnhealthy(sr) {
   107  			os.Exit(1)
   108  		}
   109  
   110  		healthPkg.GetAndFormatHealthStatus(w, true, allHealth, healthLines)
   111  		w.Flush()
   112  	}
   113  }