/
lrp_list.go
68 lines (57 loc) · 1.64 KB
/
lrp_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cmd
import (
"fmt"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/command"
)
// lrpListCmd represents the lrp_list command
var lrpListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List local redirect policies",
Run: func(cmd *cobra.Command, args []string) {
listLRPs(cmd, args)
},
}
func init() {
lrpCmd.AddCommand(lrpListCmd)
command.AddOutputOption(lrpListCmd)
}
func listLRPs(cmd *cobra.Command, args []string) {
list, err := client.GetLRPs()
if err != nil {
Fatalf("Cannot get lrp list: %s", err)
}
if command.OutputOption() {
if err := command.PrintOutput(list); err != nil {
os.Exit(1)
}
return
}
w := tabwriter.NewWriter(os.Stdout, 5, 0, 3, ' ', 0)
printLRPList(w, list)
}
func getPrintableMapping(feM *models.FrontendMapping) string {
var ret string
ret = ret + fmt.Sprintf("%s:%d/%s -> ", feM.FrontendAddress.IP, feM.FrontendAddress.Port, feM.FrontendAddress.Protocol)
for _, be := range feM.Backends {
ret = ret + fmt.Sprintf("%s:%d(%s), ", *(be.BackendAddress.IP), be.BackendAddress.Port, be.PodID)
}
return ret
}
func printLRPList(w *tabwriter.Writer, list []*models.LRPSpec) {
fmt.Fprintln(w, "LRP namespace\tLRP name\tFrontendType\tMatching Service")
for _, lrp := range list {
entry := fmt.Sprintf("%s\t%s\t%s\t%s", lrp.Namespace, lrp.Name, lrp.FrontendType, lrp.ServiceID)
fmt.Fprintln(w, entry)
for _, feM := range lrp.FrontendMappings {
fmt.Fprintln(w, fmt.Sprintf("\t|\t%s", getPrintableMapping(feM)))
}
}
w.Flush()
}