github.com/fafucoder/cilium@v1.6.11/cilium/cmd/identity_list.go (about) 1 // Copyright 2017-2018 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 identityApi "github.com/cilium/cilium/api/v1/client/policy" 24 "github.com/cilium/cilium/api/v1/models" 25 "github.com/cilium/cilium/pkg/api" 26 pkg "github.com/cilium/cilium/pkg/client" 27 "github.com/cilium/cilium/pkg/command" 28 "github.com/cilium/cilium/pkg/identity/cache" 29 "github.com/cilium/cilium/pkg/identity/identitymanager" 30 "github.com/cilium/cilium/pkg/labels" 31 "github.com/spf13/viper" 32 33 "github.com/spf13/cobra" 34 ) 35 36 // identityListCmd represents the identity_list command 37 var identityListCmd = &cobra.Command{ 38 Use: "list [LABELS]", 39 Aliases: []string{"ls"}, 40 Short: "List identities", 41 Run: func(cmd *cobra.Command, args []string) { 42 listIdentities(args) 43 }, 44 } 45 46 func init() { 47 identityCmd.AddCommand(identityListCmd) 48 command.AddJSONOutput(identityListCmd) 49 flags := identityListCmd.Flags() 50 flags.Bool("endpoints", false, "list identities of locally managed endpoints") 51 viper.BindPFlags(flags) 52 } 53 54 func listIdentities(args []string) { 55 switch { 56 case viper.GetBool("endpoints"): 57 params := identityApi.NewGetIdentityEndpointsParams().WithTimeout(api.ClientTimeout) 58 identities, err := client.Policy.GetIdentityEndpoints(params) 59 if err != nil { 60 Fatalf("Cannot get identities. err: %s", pkg.Hint(err)) 61 } 62 // sort identities by ID 63 im := identitymanager.IdentitiesModel(identities.Payload) 64 sort.Slice(im, im.Less) 65 printIdentitesEndpoints(identities.Payload) 66 default: 67 params := identityApi.NewGetIdentityParams().WithTimeout(api.ClientTimeout) 68 if len(args) != 0 { 69 params = params.WithLabels(args) 70 } 71 identities, err := client.Policy.GetIdentity(params) 72 if err != nil { 73 if params != nil { 74 Fatalf("Cannot get identities for given labels %v. err: %s\n", params.Labels, err.Error()) 75 } else { 76 Fatalf("Cannot get identities. err: %s", pkg.Hint(err)) 77 } 78 } 79 // sort identities by ID 80 im := cache.IdentitiesModel(identities.Payload) 81 sort.Slice(im, im.Less) 82 printIdentities(identities.Payload) 83 } 84 } 85 86 func printIdentitesEndpoints(identities []*models.IdentityEndpoints) { 87 if command.OutputJSON() { 88 if err := command.PrintOutput(identities); err != nil { 89 Fatalf("Unable to provide JSON output: %s", err) 90 } 91 } else { 92 w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0) 93 94 fmt.Fprintf(w, "ID\tLABELS\tREFCOUNT\n") 95 for _, identity := range identities { 96 lbls := labels.NewLabelsFromModel(identity.Identity.Labels) 97 first := true 98 for _, lbl := range lbls.GetPrintableModel() { 99 if first { 100 fmt.Fprintf(w, "%d\t%s\t%d\t\n", identity.Identity.ID, lbl, identity.RefCount) 101 first = false 102 } else { 103 fmt.Fprintf(w, "\t%s\t\n", lbl) 104 } 105 } 106 } 107 108 w.Flush() 109 } 110 }