github.com/fafucoder/cilium@v1.6.11/cilium/cmd/endpoint_get.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  	"bytes"
    19  	"encoding/json"
    20  	"fmt"
    21  	"os"
    22  
    23  	endpointApi "github.com/cilium/cilium/api/v1/client/endpoint"
    24  	"github.com/cilium/cilium/api/v1/models"
    25  	"github.com/cilium/cilium/pkg/api"
    26  	"github.com/cilium/cilium/pkg/command"
    27  
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  var lbls []string
    32  
    33  // endpointGetCmd represents the endpoint_get command
    34  var endpointGetCmd = &cobra.Command{
    35  	Use:     "get ( <endpoint identifier> | -l <endpoint labels> ) ",
    36  	Aliases: []string{"inspect, show"},
    37  	Short:   "Display endpoint information",
    38  	Example: "cilium endpoint get 4598, cilium endpoint get pod-name:default:foobar, cilium endpoint get -l id.baz",
    39  	Run: func(cmd *cobra.Command, args []string) {
    40  
    41  		if len(lbls) > 0 && len(args) > 0 {
    42  			Usagef(cmd, "Cannot provide both endpoint ID and labels arguments concurrently")
    43  		}
    44  		var endpointInst []*models.Endpoint
    45  
    46  		if len(lbls) > 0 {
    47  			params := endpointApi.NewGetEndpointParams().WithLabels(lbls).WithTimeout(api.ClientTimeout)
    48  			result, err := client.Endpoint.GetEndpoint(params)
    49  			if err != nil {
    50  				Fatalf("Cannot get endpoints for given list of labels %s: %s\n", lbls, err)
    51  			}
    52  			endpointInst = result.Payload
    53  		} else {
    54  			requireEndpointID(cmd, args)
    55  			eID := args[0]
    56  			result, err := client.EndpointGet(eID)
    57  			if err != nil {
    58  				Fatalf("Cannot get endpoint %s: %s\n", eID, err)
    59  			}
    60  			endpointInst = append(endpointInst, result)
    61  		}
    62  
    63  		if command.OutputJSON() {
    64  			if err := command.PrintOutput(endpointInst); err != nil {
    65  				os.Exit(1)
    66  			}
    67  			return
    68  		} else {
    69  			result := bytes.Buffer{}
    70  			enc := json.NewEncoder(&result)
    71  			enc.SetEscapeHTML(false)
    72  			enc.SetIndent("", "  ")
    73  			if err := enc.Encode(endpointInst); err != nil {
    74  				Fatalf("Cannot marshal endpoints %s", err.Error())
    75  			}
    76  
    77  			expandedResult, err := expandNestedJSON(result)
    78  			if err != nil {
    79  				Fatalf(err.Error())
    80  			}
    81  			fmt.Println(string(expandedResult.Bytes()))
    82  		}
    83  	},
    84  }
    85  
    86  func init() {
    87  	endpointCmd.AddCommand(endpointGetCmd)
    88  	endpointGetCmd.Flags().StringSliceVarP(&lbls, "labels", "l", []string{}, "list of labels")
    89  	command.AddJSONOutput(endpointGetCmd)
    90  }