/
clusterrole.go
209 lines (164 loc) · 5.84 KB
/
clusterrole.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) 2019 Kubenext, Inc. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"context"
"github.com/kubenext/kubeon/pkg/view/component"
"sort"
"github.com/pkg/errors"
rbacv1 "k8s.io/api/rbac/v1"
)
// ClusterRoleListHandler is a printFunc that prints cluster roles
func ClusterRoleListHandler(_ context.Context, list *rbacv1.ClusterRoleList, options Options) (component.Component, error) {
if list == nil {
return nil, errors.New("cluster role list is nil")
}
cols := component.NewTableCols("Name", "Age")
tbl := component.NewTable("Cluster Roles", "We couldn't find any cluster roles!", cols)
for _, clusterRole := range list.Items {
row := component.TableRow{}
nameLink, err := options.Link.ForObject(&clusterRole, clusterRole.Name)
if err != nil {
return nil, err
}
row["Name"] = nameLink
ts := clusterRole.CreationTimestamp.Time
row["Age"] = component.NewTimestamp(ts)
tbl.Add(row)
}
return tbl, nil
}
// ClusterRoleHandler is a printFunc that prints a cluster role
func ClusterRoleHandler(ctx context.Context, clusterRole *rbacv1.ClusterRole, options Options) (component.Component, error) {
o := NewObject(clusterRole)
ch, err := newClusterRoleHandler(clusterRole, o)
if err != nil {
return nil, err
}
if err := ch.Config(options); err != nil {
return nil, errors.Wrap(err, "print clusterrole configuration")
}
if err := ch.PolicyRules(options); err != nil {
return nil, errors.Wrap(err, "print clusterrole policy rules")
}
return o.ToComponent(ctx, options)
}
// ClusterRoleConfiguration generates a clusterrole configuration
type ClusterRoleConfiguration struct {
clusterRole *rbacv1.ClusterRole
}
// NewClusterRoleConfiguration creates an instance of ClusterRoleConfiguration
func NewClusterRoleConfiguration(clusterRole *rbacv1.ClusterRole) *ClusterRoleConfiguration {
return &ClusterRoleConfiguration{
clusterRole: clusterRole,
}
}
// Create creates a clusterrole configuration summary
func (c *ClusterRoleConfiguration) Create(options Options) (*component.Summary, error) {
if c == nil || c.clusterRole == nil {
return nil, errors.New("clusterrole is nil")
}
var sections component.SummarySections
if clusterRoleAggregation := c.clusterRole.AggregationRule; clusterRoleAggregation != nil {
if clusterRoleSelectors := clusterRoleAggregation.ClusterRoleSelectors; clusterRoleSelectors != nil {
var selectors []component.Selector
for _, selector := range clusterRoleSelectors {
for k, v := range selector.MatchLabels {
ls := component.NewLabelSelector(k, v)
selectors = append(selectors, ls)
}
sections = append(sections, component.SummarySection{
Header: "Selectors",
Content: component.NewSelectors(selectors),
})
}
}
}
sections.AddText("Name", c.clusterRole.Name)
summary := component.NewSummary("Configuration", sections...)
return summary, nil
}
func createClusterRolePolicyRulesView(clusterRole *rbacv1.ClusterRole) (*component.Table, error) {
if clusterRole == nil {
return nil, errors.New("cluster role is nil")
}
return printPolicyRules(clusterRole.Rules)
}
func printPolicyRules(rules []rbacv1.PolicyRule) (*component.Table, error) {
breakdownRules := []rbacv1.PolicyRule{}
for _, rule := range rules {
breakdownRules = append(breakdownRules, BreakdownRule(rule)...)
}
rules, err := compactRules(breakdownRules)
if err != nil {
return nil, errors.New("cannot compact rules")
}
sort.SliceStable(rules, func(i, j int) bool {
return rules[i].String() < rules[j].String()
})
cols := component.NewTableCols("Resources", "Non-Resource URLs", "Resource Names", "Verbs")
tbl := component.NewTable("Policy Rules", "There are no policy rules!", cols)
for _, r := range rules {
row := component.TableRow{}
row["Resources"] = component.NewText(CombineResourceGroup(r.Resources, r.APIGroups))
row["Non-Resource URLs"] = component.NewText(printSlice(r.NonResourceURLs))
row["Resource Names"] = component.NewText(printSlice(r.ResourceNames))
row["Verbs"] = component.NewText(printSlice(r.Verbs))
tbl.Add(row)
}
return tbl, nil
}
type clusterRoleObject interface {
Config(options Options) error
PolicyRules(options Options) error
}
type clusterRoleHandler struct {
clusterRole *rbacv1.ClusterRole
configFunc func(*rbacv1.ClusterRole, Options) (*component.Summary, error)
policyRulesFunc func(*rbacv1.ClusterRole, Options) (*component.Table, error)
object *Object
}
var _ clusterRoleObject = (*clusterRoleHandler)(nil)
func newClusterRoleHandler(clusterRole *rbacv1.ClusterRole, object *Object) (*clusterRoleHandler, error) {
if clusterRole == nil {
return nil, errors.New("can't print a nil clusterrole")
}
if object == nil {
return nil, errors.New("can't print a clusterrole using an nil object printer")
}
ch := &clusterRoleHandler{
clusterRole: clusterRole,
configFunc: defaultClusterRoleConfig,
policyRulesFunc: defaultClusterRolePolicyRules,
object: object,
}
return ch, nil
}
func (c *clusterRoleHandler) Config(options Options) error {
out, err := c.configFunc(c.clusterRole, options)
if err != nil {
return err
}
c.object.RegisterConfig(out)
return nil
}
func defaultClusterRoleConfig(clusterRole *rbacv1.ClusterRole, options Options) (*component.Summary, error) {
return NewClusterRoleConfiguration(clusterRole).Create(options)
}
func (c *clusterRoleHandler) PolicyRules(options Options) error {
if c.clusterRole == nil {
return errors.New("can't display policy rules for nil clusterrole")
}
c.object.RegisterItems(ItemDescriptor{
Width: component.WidthFull,
Func: func() (component.Component, error) {
return c.policyRulesFunc(c.clusterRole, options)
},
})
return nil
}
func defaultClusterRolePolicyRules(clusterRole *rbacv1.ClusterRole, options Options) (*component.Table, error) {
return createClusterRolePolicyRulesView(clusterRole)
}