/
role.go
64 lines (53 loc) · 1.48 KB
/
role.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
package unused
import (
"github.com/open-kingfisher/king-inspect/check"
)
func init() {
check.Register(&roleCheck{})
}
type roleCheck struct{}
// Name 返回此检查的唯一名称
func (c *roleCheck) Name() string {
return "unused-role"
}
// Groups 返回此检查应属于的组名列表
func (c *roleCheck) Groups() []string {
return []string{"unused"}
}
// Description 返回此检查的描述信息
func (c *roleCheck) Description() string {
return "检查集群中没用使用的Role"
}
// Run 运行这个检查
func (c *roleCheck) Run(objects *check.Objects) ([]check.Diagnostic, check.Summary, error) {
var diagnostics []check.Diagnostic
used, err := check.CRBRoleReferences(objects, "Role")
if err != nil {
return nil, check.Summary{}, err
}
rb, err := check.RBRoleReferences(objects, "Role")
if err != nil {
return nil, check.Summary{}, err
}
for k, v := range rb {
used[k] = v
}
var summary check.Summary
summary.Total = len(objects.Roles.Items)
for _, r := range objects.Roles.Items {
if _, ok := used[check.Identifier{Name: r.GetName(), Namespace: ""}]; !ok && check.IsEnabled(c.Name(), &r.ObjectMeta) {
r := r
d := check.Diagnostic{
Severity: check.Warning,
Message: check.Message[210],
Kind: check.Role,
Object: &r.ObjectMeta,
Owners: r.ObjectMeta.GetOwnerReferences(),
}
diagnostics = append(diagnostics, d)
}
}
summary.Issue = len(diagnostics)
summary.Warning = summary.Issue
return diagnostics, summary, nil
}