github.com/fafucoder/cilium@v1.6.11/cilium/cmd/map_list.go (about) 1 // Copyright 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 "path" 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 // mapListCmd represents the map_list command 30 var mapListCmd = &cobra.Command{ 31 Use: "list", 32 Short: "List all open BPF maps", 33 Example: "cilium map list", 34 Run: func(cmd *cobra.Command, args []string) { 35 resp, err := client.Daemon.GetMap(nil) 36 if err != nil { 37 Fatalf("%s", err) 38 } 39 40 mapList := resp.Payload 41 if mapList == nil { 42 return 43 } 44 45 if command.OutputJSON() { 46 if err := command.PrintOutput(mapList); err != nil { 47 os.Exit(1) 48 } 49 } else if mapList.Maps != nil { 50 if verbose { 51 printMapListVerbose(mapList) 52 } else { 53 printMapList(mapList) 54 } 55 } 56 }, 57 } 58 59 func printMapListVerbose(mapList *models.BPFMapList) { 60 for _, m := range mapList.Maps { 61 fmt.Printf("## Map: %s\n", path.Base(m.Path)) 62 printMapEntries(m) 63 fmt.Printf("\n") 64 } 65 } 66 67 func printMapList(mapList *models.BPFMapList) { 68 w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0) 69 fmt.Fprintf(w, "Name\tNum entries\tNum errors\tCache enabled\n") 70 for _, m := range mapList.Maps { 71 entries, errors := 0, 0 72 cacheEnabled := m.Cache != nil 73 74 for _, e := range m.Cache { 75 if e != nil { 76 if e.LastError != "" { 77 errors++ 78 } 79 entries++ 80 } 81 } 82 fmt.Fprintf(w, "%s\t%d\t%d\t%t\n", 83 path.Base(m.Path), entries, errors, cacheEnabled) 84 } 85 w.Flush() 86 } 87 88 func init() { 89 mapCmd.AddCommand(mapListCmd) 90 command.AddJSONOutput(mapListCmd) 91 mapListCmd.Flags().BoolVar(&verbose, "verbose", false, "Print cache contents of all maps") 92 }