github.com/fafucoder/cilium@v1.6.11/cilium/cmd/endpoint_list.go (about)

     1  // Copyright 2017-2019 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  	"sort"
    21  	"text/tabwriter"
    22  
    23  	"github.com/cilium/cilium/api/v1/models"
    24  	"github.com/cilium/cilium/pkg/command"
    25  
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  // PolicyEnabled and PolicyDisabled represent the endpoint policy status
    30  const (
    31  	PolicyEnabled  = "Enabled"
    32  	PolicyDisabled = "Disabled"
    33  	UnknownState   = "Unknown"
    34  )
    35  
    36  var noHeaders bool
    37  
    38  // endpointListCmd represents the endpoint_list command
    39  var endpointListCmd = &cobra.Command{
    40  	Use:     "list",
    41  	Aliases: []string{"ls"},
    42  	Short:   "List all endpoints",
    43  	Run: func(cmd *cobra.Command, args []string) {
    44  		listEndpoints()
    45  	},
    46  }
    47  
    48  func init() {
    49  	endpointCmd.AddCommand(endpointListCmd)
    50  	endpointListCmd.Flags().BoolVar(&noHeaders, "no-headers", false, "Do not print headers")
    51  	command.AddJSONOutput(endpointListCmd)
    52  }
    53  
    54  func endpointPolicyMode(ep *models.Endpoint) (string, string) {
    55  	if ep.Status == nil || ep.Status.Policy == nil || ep.Status.Policy.Realized == nil {
    56  		return UnknownState, UnknownState
    57  	}
    58  
    59  	switch ep.Status.Policy.Realized.PolicyEnabled {
    60  	case models.EndpointPolicyEnabledNone:
    61  		return PolicyDisabled, PolicyDisabled
    62  	case models.EndpointPolicyEnabledBoth:
    63  		return PolicyEnabled, PolicyEnabled
    64  	case models.EndpointPolicyEnabledIngress:
    65  		return PolicyEnabled, PolicyDisabled
    66  	case models.EndpointPolicyEnabledEgress:
    67  		return PolicyDisabled, PolicyEnabled
    68  	}
    69  
    70  	return UnknownState, UnknownState
    71  }
    72  
    73  func endpointAddressPair(ep *models.Endpoint) (string, string) {
    74  	if ep.Status == nil || ep.Status.Networking == nil {
    75  		return UnknownState, UnknownState
    76  	}
    77  
    78  	if len(ep.Status.Networking.Addressing) < 1 {
    79  		return "No address", "No address"
    80  	}
    81  
    82  	return ep.Status.Networking.Addressing[0].IPV6, ep.Status.Networking.Addressing[0].IPV4
    83  }
    84  
    85  func endpointState(ep *models.Endpoint) string {
    86  	if ep.Status == nil {
    87  		return UnknownState
    88  	}
    89  
    90  	return string(ep.Status.State)
    91  }
    92  
    93  func endpointLabels(ep *models.Endpoint) []string {
    94  	if ep.Status == nil || ep.Status.Labels == nil ||
    95  		len(ep.Status.Labels.SecurityRelevant) == 0 {
    96  		return []string{"no labels"}
    97  	}
    98  
    99  	lbls := ep.Status.Labels.SecurityRelevant
   100  	sort.Strings(lbls)
   101  	return lbls
   102  }
   103  
   104  func endpointID(ep *models.Endpoint) string {
   105  	id := "<no label id>"
   106  	if ep.Status != nil && ep.Status.Identity != nil {
   107  		id = fmt.Sprintf("%d", ep.Status.Identity.ID)
   108  	}
   109  	return id
   110  }
   111  
   112  func listEndpoint(w *tabwriter.Writer, ep *models.Endpoint, id string, label string) {
   113  	policyIngress, policyEgress := endpointPolicyMode(ep)
   114  	ipv6, ipv4 := endpointAddressPair(ep)
   115  
   116  	fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n", ep.ID,
   117  		policyIngress, policyEgress, id, label, ipv6, ipv4, endpointState(ep))
   118  }
   119  
   120  func listEndpoints() {
   121  	eps, err := client.EndpointList()
   122  	if err != nil {
   123  		Fatalf("cannot get endpoint list: %s\n", err)
   124  	}
   125  	w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)
   126  	printEndpointList(w, eps)
   127  }
   128  
   129  func printEndpointList(w *tabwriter.Writer, eps []*models.Endpoint) {
   130  	sort.Slice(eps, func(i, j int) bool { return eps[i].ID < eps[j].ID })
   131  
   132  	const (
   133  		labelsIDTitle      = "IDENTITY"
   134  		labelsDesTitle     = "LABELS (source:key[=value])"
   135  		ipv6Title          = "IPv6"
   136  		ipv4Title          = "IPv4"
   137  		endpointTitle      = "ENDPOINT"
   138  		statusTitle        = "STATUS"
   139  		policyIngressTitle = "POLICY (ingress)"
   140  		policyEgressTitle  = "POLICY (egress)"
   141  		enforcementTitle   = "ENFORCEMENT"
   142  	)
   143  
   144  	if !noHeaders {
   145  		fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
   146  			endpointTitle, policyIngressTitle, policyEgressTitle, labelsIDTitle, labelsDesTitle, ipv6Title, ipv4Title, statusTitle)
   147  		fmt.Fprintf(w, "\t%s\t%s\t\t\t\t\t\n", enforcementTitle, enforcementTitle)
   148  	}
   149  
   150  	if command.OutputJSON() {
   151  		if err := command.PrintOutput(eps); err != nil {
   152  			os.Exit(1)
   153  		}
   154  		return
   155  	}
   156  
   157  	for _, ep := range eps {
   158  		for i, lbl := range endpointLabels(ep) {
   159  			if i == 0 {
   160  				listEndpoint(w, ep, endpointID(ep), lbl)
   161  			} else {
   162  				fmt.Fprintf(w, "\t\t\t\t%s\t\t\t\t\n", lbl)
   163  			}
   164  		}
   165  	}
   166  	w.Flush()
   167  }