-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbp.go
133 lines (124 loc) · 3.36 KB
/
bp.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"errors"
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/spf13/pflag"
v1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
"strings"
)
var (
namespace string
help bool
)
func init() {
pflag.StringVarP(&namespace, "namespace", "n", "", "indicate namespace")
pflag.BoolVarP(&help, "help", "h", false, "help info")
pflag.Usage = func() {
fmt.Println(`Usage: bp [-n NAMESPACE ]
Describe:
Expand ClusterRoleBinding or RoleBinding detail info!
Example:
expand clusterrolebinding if If you don't specify -n params:
]# bp
expand rolebinding if If you specify -n params (default: ""):
]# bp -n NAMESPACE
`)
}
pflag.Parse()
if help {
pflag.Usage()
os.Exit(1)
}
}
func getClusterRoleBindingOrRoleBinding() (interface{}, error) {
dir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
kubeConfigPath := filepath.Join(dir, ".kube", "config")
if !fileExists(kubeConfigPath) {
panic(errors.New("kubeConfig file not exists"))
}
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
//test local
//restConfig, err := clientcmd.BuildConfigFromFlags("", "../config")
if err != nil {
panic(err)
}
restClient, err := kubernetes.NewForConfig(restConfig)
if strings.TrimSpace(namespace) == "" {
return restClient.RbacV1().ClusterRoleBindings().List(context.Background(), metav1.ListOptions{})
}
return restClient.RbacV1().RoleBindings(namespace).List(context.Background(), metav1.ListOptions{})
}
func handleData() (data [][]string) {
items, err := getClusterRoleBindingOrRoleBinding()
if err != nil {
panic(err)
}
data = make([][]string, 0)
switch items.(type) {
case *v1.ClusterRoleBindingList:
for _, item := range items.(*v1.ClusterRoleBindingList).Items {
column := make([]string, 0)
if len(item.Subjects) == 0 {
item.Subjects = append(item.Subjects, v1.Subject{
Name: "none",
Kind: "none",
})
}
column = append(column, item.Name, item.RoleRef.Name, item.Subjects[0].Kind, item.Subjects[0].Name)
data = append(data, column)
}
case *v1.RoleBindingList:
for _, item := range items.(*v1.RoleBindingList).Items {
column := make([]string, 0)
if len(item.Subjects) == 0 {
item.Subjects = append(item.Subjects, v1.Subject{
Name: "none",
Kind: "none",
})
}
column = append(column, item.Name, item.RoleRef.Name, item.Subjects[0].Kind, item.Subjects[0].Name)
data = append(data, column)
}
}
return data
}
func main() {
// Change table lines
printColumns(handleData())
}
func printColumns(data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetNoWhiteSpace(true)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetBorder(false)
table.SetHeaderLine(false)
table.SetRowLine(false)
table.SetTablePadding("\t")
table.SetFooterAlignment(tablewriter.ALIGN_LEFT)
table.SetHeader([]string{"BindingName", "RoleName", "SujKind", "SujName"})
table.AppendBulk(data)
table.Render()
}
func fileExists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if os.IsNotExist(err) {
return false
}
return true
}